SpringCloud integration component Ribbon reports an error: java.lang.IllegalStateException: No instances available for localhost

1. Error message

java.lang.IllegalStateException: No instances available for localhost

2. Reproduce the problem

Insert picture description here
The service provider exposes the interface to the outside, which we can access. But when ribbon calls the service provider's interface, an error is reported! !

@RestController
@RequestMapping("ribbon")
public class RibbonController {
    
    

    @Autowired
    private RestTemplate restTemplate;

    //查询所有
    @GetMapping("findAll")
    public Collection<Student> findAll(){
    
    
        //url指向服务提供者,这里的restTemplate类似于一个服务消费者
        return restTemplate.getForEntity("http://localhost:8010/student/findAll",Collection.class).getBody();
}

3. The cause of the problem

This is because when we use ribbon to call the service of the service provider, the interface is no longer a simple service provider interface, but the application service name of the service provider! !

4. Solution

localhost:8010/student/findAllInstead provider/student/findAll, the web layer of ribbon calls the service provider. The configuration of this name is in the configuration file of the respective service!

#服务提供者的配置
server:
  port: 8010
spring:
  application:
      name: provider
eureka:
  instance:
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
#ribbon的配置
server:
  port: 8040
spring:
  application:
    name: ribbon
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true

The modified code:

    @GetMapping("findAll")
    public Collection<Student> findAll(){
    
    
        //url指向服务提供者,这里的restTemplate类似于一个服务消费者
        return restTemplate.getForEntity("http://provider/student/findAll",Collection.class).getBody();
    }

Just revisit!
Insert picture description here

Thanks for my blog: https://blog.csdn.net/Echouuu/article/details/104292845/

Guess you like

Origin blog.csdn.net/JAYU_37/article/details/109268015