ws配置 zuul_spring cloud zuul 服务网关

Spring cloud zuul

Mave 导入

org.springframework.boot

spring-boot-starter-actuator

org.springframework.boot

spring-boot-starter-web

org.springframework.cloud

spring-cloud-starter-netflix-zuul

开启 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 导入

org.springframework.cloud

spring-cloud-starter-netflix-eureka-client

激活服务注册和发现客户端

@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 导入

org.springframework.cloud

spring-cloud-starter-netflix-hystrix

开启 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 客户端依赖

org.springframework.cloud

spring-cloud-starter-netflix-eureka-client

注册到 eureka 服务器

# 注到eureka服务器

eureka.client.service-url.defaultZone = http://localhost:9090/eureka

激活 Eureka

@EnableDiscoveryClient

测试配置

http://localhost:9091/zuul/default

zuul 配置

增加 config client 依赖

org.springframework.cloud

spring-cloud-config

创建 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 服务器配置生效

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值