Spring 云:无法让 Feign 客户端与领事一起工作

Spring cloud : Can not get Feign client to work with consul

我正在尝试设置一个简单的 spring 云领事应用程序。

我有一个 "distribution" 服务已启动并在 consul 中注册(spring.application.name 属性 设置为 "distribution")

我有一个 "acquisition" 服务正在尝试使用伪装调用 "distribution" 服务。

这是我的主要class

@Configuration
@EnableAutoConfiguration
@EnableDiscoveryClient
@RestController
@EnableFeignClients
@EnableHystrix
public class Acquisition {


    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private DistributionClient distributionClient;


    @RequestMapping("/use-feign")
    public String sendData() {
        distributionClient.sendData(new Data("Hello World"));
        return "sent";
    }

    @RequestMapping("/disco")
    public String disco() {
         List<ServiceInstance> list = discoveryClient.getInstances("distribution");
        if (list != null && list.size() > 0) {
            return list.get(0).getUri().toString();
        }
        return null;
    }

    public static void main(String[] args) {
        SpringApplication.run(Acquisition.class, args);
    }

}

这是我的假客户

@FeignClient(value = "distribution")
interface DistributionClient {

    @RequestMapping(method = RequestMethod.POST, value = "/data", consumes = "application/json")
    void sendData(Data data);
}

这是我的 pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-consul-all</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-feign</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-hystrix</artifactId>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-consul-dependencies</artifactId>
            <version>1.0.1.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

当我请求“/disco”Url 时,"distribution" 服务的 url 被正确检索,这意味着整个发现工作按预期工作。

但是,当我请求“/use-feign”时 url,我得到以下异常:

com.netflix.client.ClientException: Load balancer does not have available server for client: distribution at com.netflix.loadbalancer.LoadBalancerContext.getServerFromLoadBalancer(LoadBalancerContext.java:468) ~[ribbon-loadbalancer-2.2.0.jar:2.2.0] at com.netflix.loadbalancer.reactive.LoadBalancerCommand.call(LoadBalancerCommand.java:184) ~[ribbon-loadbalancer-2.2.0.jar:2.2.0] at com.netflix.loadbalancer.reactive.LoadBalancerCommand.call(LoadBalancerCommand.java:180) ~[ribbon-loadbalancer-2.2.0.jar:2.2.0] at rx.Observable.unsafeSubscribe(Observable.java:8460) ~[rxjava-1.1.5.jar:1.1.5] at rx.internal.operators.OnSubscribeConcatMap.call(OnSubscribeConcatMap.java:94) ~[rxjava-1.1.5.jar:1.1.5] at rx.internal.operators.OnSubscribeConcatMap.call(OnSubscribeConcatMap.java:42) ~[rxjava-1.1.5.jar:1.1.5]

我是否缺少任何配置?

感谢您的帮助。

spencergibb 指出问题:未部署健康检查端点。 只需将 spring-boot-actuator 添加到依赖项即可解决问题。

在我的例子中,我有:spring-boot (2.4.2)spring-cloud 2020.0.1,我刚刚添加了:

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