官方网站
Spring Cloud Gateway
Spring Cloud Gateway 使用的Webflux中的reactor-netty响应式编程组件,底层使用了Netty通讯框架
Webflux是spring5推出的异步非阻塞框架不同于springMvc
1.1 GateWay功能- 反向代理
- 鉴权
- 流量控制
- 熔断
- 日志监控
- 路由
路由是构建网关的基本模块,它由ID,目标URI,一系列的断言和过滤器组成,如果断言为true则匹配该路由
- 断言
参考的是java8的java.util.function.Predicate开发人员可以匹配HTTP请求中的所有内容(例如请求头或请求参数),如果请求与断言相匹配则进行路由
- 过滤
指的是Spring框架中GatewayFilter的实例,使用过滤器,可以在请求被路由前或者之后对请求进行修改。
1.3工作流程路由转发+执行过滤器链
1.4案例演示新建cloud-gateway-gateway9527模块
依赖
org.springframework.cloud
spring-cloud-starter-gateway
配置文件
server:
port: 9527
spring:
application:
name: cloud-gateway
cloud:
gateway:
routes:
- id: payment_routh2 #id 不唯一即可
uri: http://localhost:8002 #匹配后提供服务的路由地址
predicates:
- Path=/payment/get
@Override
public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) {
log.info("*********come in MyLogGateWayFilter: "+new Date());
String uname = exchange.getRequest().getQueryParams().getFirst("username");
if(StringUtils.isEmpty(uname)){
log.info("*****用户名为Null 非法用户,(┬_┬)");
exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);//给人家一个回应
return exchange.getResponse().setComplete();
}
return chain.filter(exchange);
}
}
这样我们在9527端口访问进行转发的时候必须带有username
必须是username而且不为空
2.config分布式配置中心依赖
org.springframework.cloud spring-cloud-starter-config
随着分布式微服务的业务不断增加,模块划分越来越小,每个服务都有自己的 yml配置文件(properties)。如果每一次产品的更新迭代会需要我们逐步修改这些配置文件非常麻烦。
详细请阅读官网
Spring Cloud Config
2.1介绍ABC 就是我们传统的分布式微服务,我们的Config就为我们提供了这样一种方案我们可以把所有配置统一的部分例如 数据库连接配置 服务注册中心的配置全部交给Configserver进行管理。实现修改一处ConfigServer、ABC处处修改。
这样就实现了再github上进行修改代码 然后由configService进行本地读取,子项目从configservice读取配置文件信息
- 作用
首先我们把yml的配置文件放到github上这里不做过多演示可以参考我的git使用博客进行操作
这里值得注意的是这些配置文件一定要符合我们的spring yml配置格式的规范
新建
cloud-config-center-3344作为我们的configService
cloud-config-client-3355作为我们的ABCservice这里我只演示一个其他集群通过Eureka可以实现
- cloud-config-center-3344
pom文件
配置文件
server:
port: 3344
spring:
application:
name: cloud-config-center
cloud:
config:
server:
git:
uri: #总配置github代码仓库地址
search-paths:
- config #存放的文件目录名称
label: master #分支
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
启动类
@SpringBootApplication
@EnableConfigServer
public class ConfigCenterMain3344 {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterMain3344.class);
}
}
- cloud-config-client-3355
添加pom
配置文件bootstap.yml
server:
port: 3355
spring:
application:
name: config-client
cloud:
config:
label: master #分支名称
name: config #配置文件名称
profile: dev #后缀名
uri: http://localhost:3344 #你的springconfine 服务地址
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
特别说明
测试
我们的3355服务会根据配置文件定义的内容进行获取
- 问题
假如我们的的项目经理迭代了GitHub上的配置信息更新了最新的版本 将版本改为2
我们刷新测试
我们发现3344我们的服务configServic可以很好的进行更新
但是我的3355端口没有进行更新版本信息
这个时候我需要重启3355服务器让他加载配置文件的信息
但是每次重启很麻烦我们可以配置动态刷新
2.3Config客户端之动态刷新添加到客户端
org.springframework.boot spring-boot-starter-actuator
改造3355
在启动类上添加
@RefreshScope
在配置文件中配置 暴露暴露监控端口
management:
endpoint:
web:
exposure:
include: "*"
重启项目3355
修改github的内容
刷新页面
发现3344依旧可以自动更新,但是3355不可以,因为少了一步,我们需要告诉3355端口刷新内容但是不需要重启项目 cmd发送curl 的post命令刷新3355服务
curl -X POST "http://localhost:3355/actuator/refresh"
我们在修改github的内容后
发送命令刷新网页
3.Bus动态刷新全局广播说说上面的springCloudConfig的方式我们得要一个个去子服务刷新让服务器去服务中心更新配置文件有没有一种可能,我们只需要通知服务中心让服务中心下发通知。进行全局的服务更新。
- Spring Cloud Bus配合Spring Cloud Config使用可以实现配置的动态刷新
- Bus支持两种消息代理:RabbitMQ和Kafka
架构图
我们这里用rabbitmq进行演示 需要学习安装rabbitmq的可以翻看我之前的博客
按照3355 新建子服务3366
3.1 案例我们用2台机器演示
融合bus改造
3个机器都引入rabbitmq
org.springframework.cloud spring-cloud-starter-bus-amqp
3344
server:
port: 3344
spring:
rabbitmq:
host: 192.168.1.111
port: 5672
username: root
password: 123456
application:
name: cloud-config-center
cloud:
config:
server:
git:
uri: #总配置github代码仓库地址
search-paths:
- config #存放的文件目录名称
label: master #分支
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
management:
endpoints:
web:
exposure:
include: 'bus-refresh'
因为我们这次是刷新服务分发中心所以我们需要暴露服务中心的端口
3355
server:
port: 3355
spring:
application:
name: config-client
cloud:
config:
label: master
name: config
profile: dev
uri: http://localhost:3344
rabbitmq:
host: 192.168.1.111
port: 5672
username: root
password: 123456
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
management:
endpoints:
web:
exposure:
include: "*"
3366
server:
port: 3366
spring:
rabbitmq:
host: 192.168.1.111
port: 5672
username: root
password: 123456
application:
name: config-client
cloud:
config:
label: master
name: config
profile: dev
uri: http://localhost:3344
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
management:
endpoints:
web:
exposure:
include: 'bus-refresh'
注意rabbitmq的地址是一致的,
测试 先后启动7001 3344 3355 3366
修改gitHub的配置内容为 6.6 !!!!按照config的思路进行测试(原先配置是6)
我们发送通知给3344
测试
一处通知处处更新
3.2单独通知某一个服务进行配置更新我们在githun更新配置为6.7
因为我们在3355 3366都就进行了端口暴露
http://localhost:配置中心的端口号/actuator/bus-refresh/{destination}
现在我们只通知了3355
测试



