在做eureka 服务集群是,使用Ribbon做客户端负载均衡,服务提供者使用服务名,客户端报错, java.net.UnknownHostException

 

查看日志:

坑二:Spring Cloud Ribbon使用服务名调用服务提供者异常 java.net.UnknownHostException

就是不识别FUTURECLOUD-USER,

查看Spring Cloud 官网,Ribbon的用法,发现使用Ribbon的时候,必须要在RestTemplate bean配置中添加负载均衡注解@LoadBalanced

于是将Ribbon项目主类中的RestTemplate 添加注解 @LoadBalanced

@SpringBootApplication
@EnableEurekaClient
@RibbonClient("FUTURECLOUD-USER") //启动ribbon ,并对FUTURECLOUD-USER进行负载均衡
public class FuturecloudRibbonApplication
{
    @Bean  //相当于xml中的bean标签,主要是用于调用当前方法获取到指定对象
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
    public static void main( String[] args )
    {

        SpringApplication.run(FuturecloudRibbonApplication.class,args);
    }
}

 

再次启动Ribbon项目,成功!

相关文章:

  • 2021-09-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-07
  • 2021-12-13
  • 2021-06-28
  • 2022-12-23
猜你喜欢
  • 2021-09-08
  • 2021-09-08
  • 2021-09-18
  • 2021-10-29
  • 2021-11-21
  • 2021-09-05
相关资源
相似解决方案