在spring cloud kubernetes中使用feign的正确方法是什么?

问题描述 投票:5回答:1

我正在使用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"
    }
]

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

java spring-boot kubernetes spring-cloud spring-cloud-feign
1个回答
0
投票

如果你使用Ribbon来进行负载均衡,我在你的错误中看到了这一点 org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient

你需要说Ribbon关于真实地址的所有微服务,并为这个动作发现客户端使用。

  1. 在你的主类上面,添加一些注解。

    @EnableDiscoveryClient
    @AutoConfigureAfter(RibbonAutoConfiguration.class)
    @RibbonClients(defaultConfiguration = RibbonConfiguration.class) 
    public class MyApp
    
  2. 添加实现你的动态服务器列表

    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;
        } 
    } 
    
  3. 添加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。

© www.soinside.com 2019 - 2024. All rights reserved.