SpringCloud統合コンポーネントリボンがエラーを報告します:java.lang.IllegalStateException:ローカルホストで使用可能なインスタンスがありません

1.エラーメッセージ

java.lang.IllegalStateException:ローカルホストで使用可能なインスタンスがありません

2.問題を再現します

ここに画像の説明を挿入
サービスプロバイダーはインターフェイスを外部に公開し、アクセスできます。ただし、リボンがサービスプロバイダーのインターフェイスを呼び出すと、エラーが報告されます。

@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.問題の原因

これは、リボンを使用してサービスプロバイダーのサービスを呼び出すと、インターフェイスが単純なサービスプロバイダーインターフェイスではなく、サービスプロバイダーのアプリケーションサービス名になるためです。

4.解決策

リボンのWebレイヤーはサービスプロバイダーを呼び出しますlocalhost:8010/student/findAllが、provider/student/findAll必要です。この名前の構成は、それぞれのサービスの構成ファイルにあります。

#服务提供者的配置
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

変更されたコード:

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

再訪してください!
ここに画像の説明を挿入

私のブログをありがとう:https://blog.csdn.net/Echouuu/article/details/104292845/

おすすめ

転載: blog.csdn.net/JAYU_37/article/details/109268015