在学习Spring Cloud时通过注入DiscoveryClient对象,打印服务的相关内容时,使用的getLocalServiceInstance()被剔除
- 解决方法:使用Registration代替DiscoveryClient
- 代码如下:
-
@RestController public class HelloController { private final Logger logger = Logger.getLogger(String.valueOf(getClass())); @Resource private Registration registration; // 服务注册 @RequestMapping(value = "/hello", method = RequestMethod.GET) public String test() throws InterruptedException { long start = System.currentTimeMillis(); String host = registration.getHost(); String serviceId = registration.getServiceId(); //让处理线程等待几秒 int sleepTime = new Random().nextInt(3000); logger.info("sleepTime:"+sleepTime); Thread.sleep(sleepTime); logger.info("//hello,host" + host + ",service_id:" + serviceId); long end = System.currentTimeMillis(); logger.info("spend time :"+(end-start)); return "spring-boot-hello-test"; }
}