博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring-Cloud-Finchley | 路由网关 Zuul
阅读量:4104 次
发布时间:2019-05-25

本文共 4370 字,大约阅读时间需要 14 分钟。

简介

Zuul 是Netflix 旗下实现路由网关的一个组件,客户端在进行请求时,首先请求会到达网关,然后网关将请求分发到不同的服务,网关可以根据 URI 区别用户请求的具体服务,然后进行转发,返回响应的资源给用户。

实例

1、创建 maven 工程

创建 maven 工程,添加核心的父 maven 依赖,所有子 pom 文件继承这个父 pom 文件。

UTF-8
1.8
1.8
Finchley.RELEASE
org.springframework.boot
spring-boot-starter-parent
2.0.5.RELEASE
junit
junit
4.11
test
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
2、搭建服务注册中心 service-server

添加 maven 依赖

com.hly
05-spring-cloud-zuul
1.0-SNAPSHOT
UTF-8
UTF-8
1.8
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-starter-web

application.yml 配置文件

server:  port: 8761eureka:  instance:    hostname: localhost  client:    registerWithEureka: false    fetchRegistry: false    serviceUrl:      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/spring:  application:    name: service-server

SpringBoot 启动类

@SpringBootApplication@EnableEurekaServerpublic class ServiceServerApplication {	public static void main(String[] args) {		SpringApplication.run(ServiceServerApplication.class, args);	}}
3、搭建客户端 service-client-hello 和 service-client-he

两个客户端的配置代码基本一致,这里以一个为例

添加 maven 依赖

com.hly
05-spring-cloud-zuul
1.0-SNAPSHOT
UTF-8
UTF-8
1.8
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-web

application.yml 配置文件

server:  port: 8762spring:  application:    name: service-client-helloeureka:  client:    serviceUrl:      defaultZone: http://localhost:8761/eureka/

SpringBoot 启动类

@SpringBootApplication@RestController@EnableEurekaClientpublic class ServiceClientHelloApplication {	public static void main(String[] args) {		SpringApplication.run(ServiceClientHelloApplication.class, args);	}	@Value("${server.port}")	String port;	@RequestMapping("/hello")	public String home(@RequestParam(value = "name",defaultValue = "hly") String name){		return "hello "+name+",I am from port: " + port;	}}
3、搭建路由网关 service-zuul

添加 maven 依赖

com.hly
05-spring-cloud-zuul
1.0-SNAPSHOT
UTF-8
UTF-8
1.8
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-zuul

application.yml 配置文件

eureka:  client:    serviceUrl:      defaultZone: http://localhost:8761/eureka/server:  port: 8769spring:  application:    name: service-zuulzuul:  routes:    api-hello:      path: /api-hello/**      serviceId: service-client-hi    api-hi:      path: /api-hi/**      serviceId: service-client-hello

SpringBoot 启动类

@SpringBootApplication@EnableEurekaServerpublic class ServiceServerApplication {	public static void main(String[] args) {		SpringApplication.run(ServiceServerApplication.class, args);	}}

演示

1、首先启动注册中心

2、然后启动两个客户端
3、启动路由网关
4、访问 http://localhost:8769/api-hello/hello 路由到 hello 客户端
5、访问 http://localhost:8769/api-hi/hi 路由到 hi 客户端

代码下载

05-spring-cloud-zuul:

关于

我的 Github:

CSDN:
个人网站:
E-mail: 1136513099@qq.com

推荐阅读

转载地址:http://tufsi.baihongyu.com/

你可能感兴趣的文章
C 语言 学习---复选框及列表框的使用
查看>>
第四章 - 程序计数器
查看>>
第七章 - 本地方法栈
查看>>
第十一章 - 直接内存
查看>>
JDBC核心技术 - 上篇
查看>>
JDBC核心技术 - 下篇
查看>>
一篇搞懂Java反射机制
查看>>
一篇彻底搞懂Java注解与枚举类
查看>>
【2021-MOOC-浙江大学-陈越、何钦铭-数据结构】树
查看>>
【2021-MOOC-浙江大学-陈越、何钦铭-数据结构】树-中
查看>>
【2021-MOOC-浙江大学-陈越、何钦铭-数据结构】线性结构
查看>>
【2021-MOOC-浙江大学-陈越、何钦铭-数据结构】图
查看>>
JSP中文验证码
查看>>
JavaScript学习笔记1:鼠标划过选中
查看>>
JavaScript学习笔记2之特殊公告栏效果
查看>>
AJAX视频教程下载
查看>>
android 开发资源
查看>>
九度1001 A+B for Matrices
查看>>
九度1002 Grading
查看>>
九度1003 A+B
查看>>