gpt4 book ai didi

java - Spring Cloud kubernetes 使用 feign 的正确方法是什么?

转载 作者:行者123 更新时间:2023-12-01 12:11:15 24 4
gpt4 key购买 nike

我正在使用 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 存款服务。平衡或显然与 kubernetes 的服务发现。

该项目的公开资料位于:
https://gitlab.com/viniciusxyz/spring-kubernetes-feign

对于那些想要更直接信息的人:

我的主要类(class)如下:
@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"
}
]

你能想到问题可能出在哪里吗?

最佳答案

如果您使用 Ribbon 进行负载平衡,我会在您的错误 org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient 中看到它

你需要说 Ribbon 关于你所有微服务的真实地址和
用于此操作发现客户端。

  • 在你的主类之上,添加一些注释
    @EnableDiscoveryClient
    @AutoConfigureAfter(RibbonAutoConfiguration.class)
    @RibbonClients(defaultConfiguration = RibbonConfiguration.class)
    public class MyApp
  • 添加实现您的动态服务器列表
    public class MyServerList extends AbstractServerList<Server> {
    private final DiscoveryClient discoveryClient;
    private IClientConfig clientConfig;

    public MyServerList(DiscoveryClient discoveryClient) {
    this.discoveryClient = discoveryClient;
    }

    @Override
    public List<Server> getInitialListOfServers() {
    return getUpdatedListOfServers();
    }

    @Override
    public List<Server> getUpdatedListOfServers() {
    Server[] servers = discoveryClient.getInstances(clientConfig.getClientName()).stream()
    .map(i -> new Server(i.getHost(), i.getPort()))
    .toArray(Server[]::new);
    return Arrays.asList(servers);
    }

    @Override
    public void initWithNiwsConfig(IClientConfig clientConfig) {
    this.clientConfig = clientConfig;
    }
    }
  • 添加 RibbonConfiguration.class
    public class RibbonConfiguration {

    @Autowired
    private DiscoveryClient discoveryClient;

    @Bean
    @ConditionalOnMissingBean
    public ServerList<?> ribbonServerList(IClientConfig config) {
    MyServerList myserverLis = new MyServerList(discoveryClient);
    myserverLis.initWithNiwsConfig(config);
    return myserverLis;
    }
    }

  • 你可以在你的应用程序属性刷新时间中配置我的意思是一段时间调用 getUpdatedListOfServers

    关于java - Spring Cloud kubernetes 使用 feign 的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59217208/

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