gpt4 book ai didi

java - Spring Cloud : Can not get Feign client to work with consul

转载 作者:太空宇宙 更新时间:2023-11-04 12:29:11 28 4
gpt4 key购买 nike

我正在尝试设置一个简单的 Spring Cloud Consul 应用程序。

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

我有一个“获取”服务,正在尝试使用 feign 调用“分发”服务。

这是我的主课

@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”网址时,出现以下异常:

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$1.call(LoadBalancerCommand.java:184) ~[ribbon-loadbalancer-2.2.0.jar:2.2.0] at com.netflix.loadbalancer.reactive.LoadBalancerCommand$1.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 添加到依赖项即可解决问题。

关于java - Spring Cloud : Can not get Feign client to work with consul,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38043287/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com