spring cloud zuul
Mave 导入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
开启 zuul
- 增加
@EnableZuulProxy
整合 ribbon
配置路由规则
server.port = 7070
# zuul.routes.${app-name} = /${app-url-prefix}/**
zuul.routes.person-service = /person-service/**
# 取消ribbon eureka的整合
ribbon.eureka.enable = false
# 配置person-service的负载均衡
person-service.ribbon.listOfServers = http://localhost:8083 //服务提供地址
启动项目
eureka server
provider
zuul
访问测试
localhost:7070/person-service/demo/getHost -> 返回 true,表示成功
/person-service 是服务的名称
/demo/getHost 是
localhost:8083
的服务调用链路:zuul --> person-service
整合 eureka
zuul 项目
Maven 导入
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
激活服务注册和发现客户端
@EnableDiscoveryClient
配置
spring.application.name = zuul-service
server.port = 7070
# zuul.routes.${app-name} = /${app-url-prefix}/**
zuul.routes.person-service = /person-service/**
# 取消ribbon eureka的整合
ribbon.eureka.enable = false
# 配置person-service的负载均衡
person-service.ribbon.listOfServers = http://localhost:8083 //服务提供地址
# 服务注册和发现客户端地址
eureka.client.service-url.defaultZone = http://localhost:9090/eureka
整合 Hystrix
provider 项目
Maven 导入
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
开启 Hystrix
@EnableHystrix
配置规则
@RestController
public class PersonController {
public Random random = new Random();
@PostMapping("/person/save")
@HystrixCommand(fallbackMethod="fallback",
commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",
value = "100")
}
)
boolean save(@RequestBody Person person) throws InterruptedException {
System.out.println("remote request ok !");
int i = random.nextInt(200);
Thread.sleep(i);
System.out.println("随机 : "+i);
return new HashMap<>().put(person.getId(),person)==null;
}
public List fallback(){
return new ArrayList();
}
}
整合 Feign
服务消费方 : person-client
配置
server.port = 8082
spring.application.name = person-consumer
eureka.client.service-url.defaultZone = http://localhost:9090/eureka
management.endpoints.web.exposure.include=*
服务网关 :zuul
增加路由应用到 client
zuul.routes.person-consumer = /person-consumer/**
访问测试
localhost:7070/person-consumer/demo/getHost -> 返回 true,表示成功
调用链路:zuul --> person-consumer --> person-provider
整合 config server
config server 配置
server.port = 9091
spring.application.name = ws
### 拉取远程git中配置
spring.cloud.config.server.git.uri = file:///${user.dir}/src/main/resources/configs
### 关闭actuator验证
management.endpoints.enabled-by-default=true
zuul 增加配置文件
三个 profile 配置文件
- zuul.properties
- zuul-test.properties
- zuul-prod.properties
zuul.properties
zuul.routes.person-service = /person-service/**
zuul-test.properties
zuul.routes.person-consumer = /person-consumer/**
zuul-prod.properties
zuul.routes.person-service = /person-service/**
zuul.routes.person-consumer = /person-consumer/**
file:///${user.dir}/src/main/resources/configs
目录下初始化 git,并提交
增加 eureka 客户端依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
注册到 eureka 服务器
# 注到eureka服务器
eureka.client.service-url.defaultZone = http://localhost:9090/eureka
激活 Eureka
@EnableDiscoveryClient
测试配置
http://localhost:9091/zuul/default
zuul 配置
增加 config client 依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config</artifactId>
</dependency>
创建 bootstrap.properties 配置项
# 远程地址properties 前缀 [ws.properties]
spring.cloud.config.name = zuul
# 远程地址properties -后缀 [ws-dev.properties]
spring.cloud.config.profile = default
# git仓局分支
spring.cloud.config.label = master
# 采用Discovery client连接方式
spring.cloud.config.discovery.enabled = true
# 通过eureka注册中心选择config服务
spring.cloud.config.discovery.serviceId = config-server
测试访问
访问 : localhost:7070/person-service/person/save
调用路径 : zuul --> person-service --> person-provider
访问 : localhost:7070/person-service/person/save
调用路径 : zuul --> person-provider
所以 config 服务器配置生效
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于