关于Springcloud的RestTemplate使用服务名报异常java.net.UnknownHostException

本文探讨了在SpringCloud中使用RestTemplate时遇到的两大常见问题:使用localhost时出现的java.lang.IllegalStateException异常,以及使用服务名时出现的java.net.UnknownHostException异常。文章详细解释了如何通过正确配置@LoadBalanced注解来解决这些问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关于Springcloud的RestTemplate使用服务名报异常java.net.UnknownHostException和使用localhost:8081报异常java.lang.IllegalStateException: No instances available for localhost问题

1.关于java.lang.IllegalStateException: No instances available for localhost问题

在Springcloud使用RestTemplate访问其他模块的时候,可能会出现java.lang.IllegalStateException: No instances available for localhost问题。也有时候回出现空指针的问题。对于这类Bug解决的方法可能是由于你的restTemplate配置的问题。这些问题发生在访问的模块,被访问的模块这里就不用写出来了。

首先是启动类和restTemplate配置。这里需要注意的是,如果restTemplate对象是用纯地址访问的话,就不需要@LoadBalanced这个标签,如果

@SpringBootApplication
@EnableEurekaClient
public class RibbonApplication {

    @Bean
    //@LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
    public static void main(String[] args) {
        SpringApplication.run(RibbonApplication.class,args);
    }
}

然后就是controller层通过restTemplate对象访问其他模块接口的方法

	@RestController
	public class UserController {
	
    @Autowired
    private RestTemplate restTemplate;
    /***
     *      通过ribbon和template对象访问 user里面的数据
     */
    @GetMapping("/movie/{id}")
    public User findById(@PathVariable Long id) {
        System.err.println("consumer---------------------"+id);
        return this.restTemplate.getForObject("http://localhost:7900/simple/"+ id, User.class);
    }
}

值得注意的是,如果你不注释 //@LoadBalanced这个标签的话,就会报出一下错误:java.lang.IllegalStateException: No instances available for localhost。所以解决这种异常的方法是注释掉@LoadBalanced这个标签

2.关于Springcloud的RestTemplate不能使用服务名java.net.UnknownHostException: micorservice-provider-user

如果restTemplate对象需要使用服务名的话会报异常:java.net.UnknownHostException: micorservice-provider-user。 micorservice-provider-user是其他模块的服务名
服务名:在这里插入图片描述

启动类代码

@SpringBootApplication
@EnableEurekaClient
public class RibbonApplication {

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

    public static void main(String[] args) {

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

访问其他模块的接口的代码

@RestController
public class UserController {

    @Autowired
    private RestTemplate restTemplate;
    
    /***
     *      通过ribbon和template对象访问 user里面的数据
     */
    @GetMapping("/movie/{id}")
    public User findById(@PathVariable Long id) {
        System.err.println("consumer---------------------"+id);
        
        //这里使用的micorservice-provider-user是其他模块的服务名
        
        return this.restTemplate.getForObject("http://micorservice-provider-user/simple/"+ id, User.class);
    }
}

解决这类异常的方法是在resttemplate配置 @LoadBalanced这个注解

3.总结

如果需要使用ip和端口,就不需要加 @LoadBalanced注解,如果需要使用应用名访问,那就需要在restTemplate配置加上 @LoadBalanced注解 。因为@LoadBalanced这个注解是负载均衡的注解,而负载均衡就是通过访问服务名而实现的。如果你加上这个注解之后使用restTemplate,那么他就默认你的localhost一个模块的服务名称(ip+端口),而不是本机的ip。总而言之,如果需要使用ip和端口,就不需要加 @LoadBalanced注解,如果需要使用应用名访问,那就需要在restTemplate配置加上 @LoadBalanced注解

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值