【发布时间】:2020-03-31 16:29:29
【问题描述】:
我正在使用 Spring Cloud Kubernetes,我试图让 feign 能够根据 kubernetes 中存在的服务名称发送请求,但是我不能,当我尝试发出请求时,会出现以下错误:
"timestamp": "2019-12-06T15:37:50.285+0000",
"status": 500,
"error": "Internal Server Error",
"message": "com.netflix.client.ClientException: Load balancer does not have available server for client: poc-saldo",
"trace": "java.lang.RuntimeException: com.netflix.client.ClientException: Load balancer does not have available server for client: poc-saldo\n\tat org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient.execute....
我尝试调用集群内的其他服务,但问题都是一样的,我通过进入 poc-deposit pod 并执行 poc-balance curl 进行了测试,它工作正常,所以问题不使用 poc-deposit 服务。显然是平衡或与 Kubernetes 的服务发现。
该项目的公开资料位于:
https://gitlab.com/viniciusxyz/spring-kubernetes-feign
对于那些想要更直接信息的人:
我的主要课程如下:
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ServiceDiscoveryApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceDiscoveryApplication.class, args);
}
}
我与feign的界面如下:
@FeignClient("poc-saldo")
public interface ProxyGenerico {
@RequestMapping(method = RequestMethod.GET)
String getHttpResponse();
}
我可以在应用程序中列出 Kubernetes 中可用的服务,如下所示:
@RestController
public class RestTest {
@Autowired
private DiscoveryClient discoveryClient;
@Autowired
private ProxyGenerico proxyGenerico;
@GetMapping("/services")
public ResponseEntity<?> services() {
return new ResponseEntity<Object>(discoveryClient.getServices(), HttpStatus.OK);
}
@GetMapping("/pocsaldo")
public ResponseEntity<?> gitlab() {
return new ResponseEntity<Object>(proxyGenerico.getHttpResponse(), HttpStatus.OK);
}
}
在这个列表中,我有几个服务,其中我要访问的服务称为 poc-balance,返回的 json 如下所示:
[
"poc-deposito",
"poc-saldo",
"sonarqube",
"sql-server-sonar",
"zookeeper",
"gitlab"
]
要补充列表,请遵循我的依赖项:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-kubernetes</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-kubernetes-ribbon</artifactId>
</dependency>
discoveryClient.getInstances ("poc-saldo") 命令返回:
[
{
"instanceId": "32a4db0d-0549-11ea-8850-e0d55ef66cf8",
"serviceId": "poc-saldo",
"secure": false,
"metadata": {
"helm.sh/chart": "spring-boot-app-0.1.23",
"port.http": "8080",
"app.kubernetes.io/managed-by": "Tiller",
"app.kubernetes.io/name": "poc-saldo",
"app.kubernetes.io/instance": "banco-digital-poc-saldo",
"app.kubernetes.io/version": "1.0"
},
"port": 8080,
"host": "10.42.0.60",
"scheme": "http://",
"uri": "http://10.42.0.60:8080"
}
]
你能想到问题可能出在哪里吗?
【问题讨论】:
-
您是否使用
eureka作为您的服务发现?如果不是,它怎么知道这些服务在哪里。在这种情况下,您应该将它们的 url 配置为<app>.ribbon.listOfServers=http://<app-host>/... -
我使用的是使用 Kubernetes 服务发现的 Spring Cloud Kubernetes,eureka 只是支持的服务发现提供者之一。
-
在你的情况下,你能看到 kubernetes 中列出的那些服务吗?
-
总而言之,我使用 feign 作为 kubernetes 服务的 url 的参数,例如:@FeignClient("http://kubernetes-service"),我只是举这个例子如果您有同样的问题并需要快速解决方案,晚上发布完整的答案。
-
@ViniciusVieira 好的,所以无法像在 Eureka 中那样使用 FeignClient。我发现,您可以使用 @FeignClient(url="service-name") 而不是 @FeignClient("service-name") 并且它可以在不传递完整地址
http://service-name:port的情况下工作
标签: java spring-boot kubernetes spring-cloud spring-cloud-feign