springcloud2020.0.0,gateway,nacos,loadbalancer

springcloud2020.0.0使用gateway的一个小问题


创建gateway服务时只引入了naocs和gateway的依赖,启动后通过lb://方式转发总是报503
Spring Cloud 2020.0.0版本之前会自动引入Netflix Ribbon依赖,Netflix Ribbon功能跟loadbalancer一样,因Netflix公司停止维护Ribbon后, 在Spring Cloud 2020.0.0版本之后Spring使用loadbalancer替代了Ribbon, 但是loadbalancer依赖需要手动引入. 所以如果你使用的Spring Cloud版本是2020.0.0或更高的版本, 添加loadbalancer依赖即可解决503的问题

所以引入loadbalancer的依赖即可解决问题

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>
Spring Cloud 2021.0.5.0 是 Spring Cloud 的一个版本,它可以和 Sentinel-datasource-nacos 模块一起使用。下面是使用 Spring Cloud 2021.0.5.0 和 Sentinel-datasource-nacos 的步骤: 1. 在 pom.xml 文件中引入以下依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> <version>2021.0.5.0</version> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-nacos-discovery</artifactId> <version>2.2.5.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-nacos-config</artifactId> <version>2.2.5.RELEASE</version> </dependency> ``` 2. 配置 Nacos 作为 Sentinel 的数据源,例如: ```yaml spring: cloud: sentinel: transport: dashboard: localhost:8080 port: 8719 nacos: server-addr: localhost:8848 groupId: DEFAULT_GROUP dataId: sentinel rule-type: flow ``` 3. 在 Nacos 中创建对应的命名空间和配置文件,例如: ```yaml spring: cloud: sentinel: transport: dashboard: localhost:8080 port: 8719 nacos: server-addr: localhost:8848 groupId: DEFAULT_GROUP dataId: sentinel rule-type: flow data-type: json namespace: 7e1d4f1f-8a31-4f0d-9f8d-0d4a563beba4 ``` 4. 在应用程序中添加 Sentinel 规则,例如: ```java @PostConstruct public void init() throws Exception { String appName = env.getProperty("spring.application.name"); ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new NacosDataSource<>(nacosServerAddr, groupId, appName + "-flow-rule", source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {})); FlowRuleManager.register2Property(flowRuleDataSource.getProperty()); } ``` 以上是 Spring Cloud 2021.0.5.0 使用 Sentinel-datasource-nacos 的基本步骤,具体实现可以根据项目需要进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值