cloud-alibaba2021.0.5.0——Nacos——java.net.UnknownHostException: userservice

1.前提

在RestTemplate上方加@LoadBalanced注解。

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

2.解决方法

加入loadbalancer依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>

注:SpringCloud2020.0.1.0版本以后不在使用netflix,所以不用ribbon来实现负载均衡。

3.版本

spring-boot:2.7.12   spring-cloud:2021.0.7   spring-cloud-alibaba:2021.0.5.0   nacos:2.2.0

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Spring Cloud 2021.0.5.0Spring 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 的基本步骤,具体实现可以根据项目需要进行调整。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值