Spring Cloud获取本地服务实例方法getLocalServiceInstance()过时

在使用discoveryClient.getLocalServiceInstance()时,发现该方法已经过时。源码提示使用org.springframework.cloud.client.serviceregistry.Registration,该类可以根据服务名,获取注册了该服务名的所有实例。具体使用如下的testBalance()serviceInstance()方法。

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.netflix.eureka.EurekaDiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AController {

    private final Logger logger = Logger.getLogger(getClass());

    @Autowired
    private Registration registration;  // 服务注册

    @Autowired
    private DiscoveryClient client;  // 服务发现客户端

    @Value("${server.port}")
    private int port;

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public String test(@RequestParam String param) {
        ServiceInstance instance = client.getLocalServiceInstance();
        logger.info("/test, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + param);
        return "From Service-A, Param is " + param;
    }

    @RequestMapping(value = "/testBalance", method = RequestMethod.GET)
    public String testBalance(@RequestParam String param) {
        ServiceInstance instance = serviceInstance();
        String result = "/testBalance, host:port=" + instance.getUri()  + ", "
                + "service_id:" + instance.getServiceId();
        logger.info(result);
        return "From Service-A , " + result;
    }

    public ServiceInstance serviceInstance() {
        List<ServiceInstance> list = client.getInstances(registration.getServiceId());
        if (list != null && list.size() > 0) {
            for (ServiceInstance instance : list) {
                if (instance.getPort() == port)
                    return instance;
            }   
        }
        return null;
    }
}

可以看到注入了RegistrationDiscoveryClient两个对象:

(1)Registration服务注册接口,包含了获取服务 ID 的方法。
(2)DiscoveryClient服务发现客户端,具有以下方法:

  • String description(); 获取描述
  • ServiceInstance getLocalServiceInstance(); @Deprecated 获取本地服务实例,方法被删除,推荐不要使用
  • List getInstances(String serviceId); 通过服务 ID,获取当前服务的服务实例
  • List getServices(); 获取所有服务 ID 列表
上一篇 Spring Cloud Eureka 使用外网IP和端口号进行服务注册
下一篇 MyBatis generator自定义插件或者扩展报Cannot instantiate object of type XXX
目录
文章列表
1 高中数学基础:随机变量及其分布
高中数学基础:随机变量及其分布
2
Python Selenium设置元素等待
Python Selenium设置元素等待
3
Android编译OpenSSL库报错ui_openssl.c:function read_string_inner
Android编译OpenSSL库报错ui_openssl.c:function read_string_inner
4
Taro基础教程之React
Taro基础教程之React
5
Kubernetes部署一个应用程序
Kubernetes部署一个应用程序
最新评论
一位WordPress评论者
一位WordPress评论者
2月12日
您好,这是一条评论。若需要审核、编辑或删除评论,请访问仪表盘的评论界面。评论者头像来自 Gravatar。