美文网首页
获取本地服务实例方法getLocalServiceInstanc

获取本地服务实例方法getLocalServiceInstanc

作者: BscLiao | 来源:发表于2018-06-06 08:53 被阅读0次
在使用discoveryClient.getLocalServiceInstance()时,发现该方法已经过时。源码提示使用org.springframework.cloud.client.serviceregistry.Registration,该类可以根据服务名,获取注册了该服务名的所有实例。具体使用如下的testBalance()和serviceInstance()方法。
package com.liao.web;

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; // 服务发现客户端

    @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 itm : list){
                if(itm.getPort() == 2001)
                    return itm;
            }   
        }
        return null;
    }

}

可以看到注入了 Registration 和 DiscoveryClient 两个对象:

  • Registration 服务注册接口,包含了获取服务 ID 的方法。
  • DiscoveryClient 服务发现客户端,具有以下方法:
    • String description(); 获取描述
    • ServiceInstance getLocalServiceInstance(); @Deprecated 方法被删除,推荐不要使用。获取本地服务实例
    • List getInstances(String serviceId); 通过服务 ID,获取当前服务的服务实例
    • List getServices(); 获取所有服务 ID 列表

相关文章

  • 获取本地服务实例方法getLocalServiceInstanc

    在使用discoveryClient.getLocalServiceInstance()时,发现该方法已经过时。源...

  • 三、ProcessEngine与EngineServer简介

    一、ProcessEngine 流程引擎 EngineServers:该接口定义了获取各种服务类实例对象的方法。 ...

  • 文本内容替换

    获取远程服务图片放本地,html报告中的图片资源地址需要替换为本地服务地址,因此用到了文本替换的方法。

  • RunTime 相关函数使用

    方法交换,一般在分类的load方法使用 获取方法列表 获取实例变量列表 获取实例属性列表 获取协议列表 为类别添加...

  • 如何mock数据?

    方法一:通过本地服务器 1.通过在控制台输入http-server开启本地服务器获取端口号http://local...

  • Python 优雅获取本机 IP 方法

    见过很多获取服务器本地IP的代码,个人觉得都不是很好,例如以下这些 不推荐:靠猜测去获取本地IP方法 这类代码带有...

  • runtime 应用

    1. 交换方法 1.1 获取类方法 1.2 获取实例方法 1.3 交换两个方法 实例: 2. 分类添加属性 下面给...

  • 以太坊与智能合约交互

    与智能合约的交互首先要获取合约实例,然后调用合约方法:本地调用 或 广播. 本文中的合约和方法均见于ERC20。 ...

  • 26、反射(二)

    反射除了获取属性,也可以获取方法,调用方法。 获取实例方法:Method method = clazz.getMe...

  • Redis 服务器

    Redis 服务器 Redis 服务器命令主要是用于管理 redis 服务。 实例 以下实例演示了如何获取 red...

网友评论

      本文标题:获取本地服务实例方法getLocalServiceInstanc

      本文链接:https://www.haomeiwen.com/subject/vnbvsftx.html