gateway动态路由nacos配置中心路由配置spring-cloud-alibaba 解决nacos config配置不生效问题
解决nacos版本问题
使用最新版本的 sping cloud alibaba 2.2.1和spirng cloud Hoxton.SR4发现版本不兼容,nacos 修改配置文件是可以收到修改消息但不将修改后信息注入spirng, 也就是说修改不生效,其实启动的时候就不能注入。将 sping cloud alibaba版本将至2.1.1.RELEASE就正常了。
以下是我的相关的jar 包信息
<spring-cloud.version>Hoxton.SR4</spring-cloud.version> <spring-cloud-alibaba.version>2.1.1.RELEASE</spring-cloud-alibaba.version> <dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${spring-cloud-alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> #nacos注册中心和配置中心jar包 <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> # gateway网管jar包 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> #这边主要又来测试动态配置是否生效 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> <version>${spring.boot.version}</version> </dependency>
配置动态的路由
在项目的resources目录创建bootstrap.yml或bootstrap.properties文件,记住必须是bootstrap文件名,不要问我为什么,问了我只能说我被坑的很惨,因为之前discovery配置信息放入application配置文件中就没有问题,到了config就死活不成。
以下为bootstrap.yml文件
spring: cloud: nacos: config: server-addr: 127.0.0.1:8848 namespace: dev discovery: server-addr: 127.0.0.1:8848
application.yml
server: port: 8103 spring: application: name: gateway cloud: gateway: discovery: locator: enabled: true #配置这一项可以监控gateway各个节点的信息 management: endpoints: web: exposure: include: gateway
在不设置不设置dataid情况下 nacos系统的具体dataid的设置网上很多,我这里就不说了
到这里所有的代码端的配置基本结束了,nacos的安装我这里也不说了,网上很丰富,我这里用docker简单部署了一个
下面开始测试:
spring.cloud.gateway.routes[0].id=service-user spring.cloud.gateway.routes[0].uri=lb://service-user spring.cloud.gateway.routes[0].predicates[0]=Path=/user/** spring.cloud.gateway.routes[1].id=service-goods spring.cloud.gateway.routes[1].uri=lb://service-goods spring.cloud.gateway.routes[1].predicates[0]=Path=/goods/**
我自己写了两个节点做测试,spring.cloud.gateway.routes[0].id=service-user这一项是代表注册到nacos里的服务名称如下图
spring.cloud.gateway.routes[0].uri=lb://service-user 这一项固定写法 lb://服务名 ,当然你愿意也可以写固定连接,predicates是断言没什么好说,还有一项order我没添,默认是0
启动类代码很简单没什么说的
@EnableDiscoveryClient @SpringBootApplication public class KflGatewayApplication { public static void main(String[] args) { SpringApplication.run(KflGatewayApplication.class, args); } }
访问http://127.0.0.1:8103/actuator/gateway/routes
这里看出动态路由已经生效。
访问我的两个测试节点
- Spring Cloud Alibaba Nacos Config配置中心实现
- Spring Cloud - - - 通过消息总线更新配置中心,导致client-config服务实例在注册中心全部丢失的问题
- Spring Cloud Gateway 结合配置中心限流
- 记录使用spring-cloud-starter-alibaba-nacos-config 注册到 nacos 时配置问题。
- 走进Spring Cloud之十 高可用配置中心动态刷新(Greenwich版本)
- 问题记录:SpringCloud 灰度环境路由 Gateway
- SpringCloud Alibaba Nacos作为配置中心(六)(1)--------自定义扩展的 Data Id 配置
- spring cloud Bus + RabbitMQ实现配置中心动态加载
- Spring Cloud Alibaba基础教程:使用Nacos作为配置中心
- Shell脚本实现动态配置IP与路由:解决嵌入式Android/Linux有线和无线网卡双网共存问题
- 关于在Spring Cloud Feign工程中使用Hystrix配置不生效的问题
- SpringCloud Alibaba Nacos作为配置中心(三)----------yml格式配置文件
- Spring Cloud Alibaba从入门到放弃-Nacos构建配置中心
- Spring Cloud Zuul 在版本Edgware.RELEASE断路超时配置无法生效问题
- Spring Alibaba Cloud 使用 Seata 实现分布式事务,Nacos 作为 配置中心(一)
- Spring Cloud Alibaba基础教程第三篇: Nacos作为配置中心
- SpringCloud Alibaba Nacos作为配置中心(十)--------自定义登录用户名和密码
- Idea-SpringCloud-Nacos配置中心配置文件无法访问的问题
- Spring Cloud Config的配置中心获取不到最新配置信息的问题
- 【SpringCloud】搭建高可用分布式配置中心(Spring Cloud Config)(二)全过程详解(自动刷新)解决webhooks 400错误