SpringCloud发现服务代码(EurekaClient,DiscoveryClient)
1.说明
本文介绍SpringCloud发现服务代码的开发,
通过使用EurekaClient,DiscoveryClient来发现注册中心的服务等,
从而可以自定义客户端对注册中心的高级用法。
2.使用EurekaClient发现服务
通过Spring自动注入的EurekaClient,
不仅能发现服务实例,
还能查看客户端连接等各种配置。
2.1.代码
package com.yuwen.spring.config.client.controller;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.eureka.EurekaClientConfigBean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.netflix.discovery.EurekaClientConfig;
import com.netflix.discovery.shared.Application;
import com.netflix.discovery.shared.Applications;
@RestController
public class EurekaClientController {
private static Logger logger = LoggerFactory.getLogger(DiscoveryClientController.class);
@Autowired
private EurekaClient eurekaClient;
@GetMapping("/eureka")
public String getEurekaClient() {
EurekaClientConfig config = eurekaClient.getEurekaClientConfig();
if (!(config instanceof EurekaClientConfigBean)) {
return eurekaClient.toString();
}
EurekaClientConfigBean eurekaConfig = (EurekaClientConfigBean) config;
logger.info("serviceUrl=" + eurekaConfig.getServiceUrl());
Applications applications = eurekaClient.getApplications();
List<Application> registeredApplications = applications.getRegisteredApplications();
for (Application application : registeredApplications) {
logger.info("Application Name=" + application.getName());
List<InstanceInfo> instances = application.getInstances();
for (InstanceInfo instance : instances) {
StringBuilder sb = new StringBuilder();
sb.append(instance.getAppName()).append("\t").append(instance.getHostName()).append("\t")
.append(instance.getPort()).append("\t").append(instance.getVIPAddress());
logger.info("InstanceInfo=" + sb);
}
}
return eurekaClient.toString();
}
}
2.2.测试
使用浏览器访问:
http://localhost:8005/eureka
页面返回:
org.springframework.cloud.netflix.eureka.CloudEurekaClient@6715470c
控制台打印:
serviceUrl={fetch-registry=true, defaultZone=http://localhost:7007/eureka, register-with-eureka=true}
Application Name=CONFIG-CLIENT
InstanceInfo=CONFIG-CLIENT 192.168.171.1 8005 config-client
3.使用DiscoveryClient发现服务
使用DiscoveryClient可以发现注册中心上的服务,
注册中心可以是Eureka,也可以是Zookeeper等,
因为不使用原始Netflix EurekaClient,
所以在某种包装器后面的DiscoveryClient更易于使用。
它也能提供通过服务名称查询对应的所有服务实例等功能。
3.1.代码
package com.yuwen.spring.config.client.controller;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DiscoveryClientController {
private static Logger logger = LoggerFactory.getLogger(DiscoveryClientController.class);
@Autowired
private DiscoveryClient discoveryClient;
@GetMapping("/discovery")
public Object getDiscoveryClient() {
List<String> services = discoveryClient.getServices();
for (String service : services) {
logger.info("service=" + service);
}
String serviceId = "CONFIG-CLIENT";
List<ServiceInstance> instances = discoveryClient.getInstances(serviceId);
for (ServiceInstance instance : instances) {
StringBuilder sb = new StringBuilder();
sb.append(instance.getServiceId()).append("\t").append(instance.getHost()).append("\t")
.append(instance.getPort()).append("\t").append(instance.getUri());
logger.info("instance=" + sb);
}
return discoveryClient;
}
}
3.1.测试
使用浏览器访问:
http://localhost:8005/discovery
页面返回:
config-client00config-client0
控制台打印:
service=config-client
instance=CONFIG-CLIENT 192.168.171.1 8005 http://192.168.171.1:8005
可以看到Eureka上面只有一个服务CONFIG-CLIENT,
这个服务只有一个实例http://192.168.171.1:8005,
其实就是提供http://localhost:8005/discovery这个服务的实例。
SpringCloud发现服务代码(EurekaClient,DiscoveryClient)的更多相关文章
- SpringCloud——Eureka服务注册和发现
一.SpringCloud和Dubbo SpringCloud整合了一套较为完整的微服务解决方案框架,而Dubbo只是解决了微服务的几个方面的问题. content Dubbo SpringCloud ...
- SpringCloud+Consul 服务注册与服务发现
SpringCloud+Consul 服务注册与服务发现 1. 服务注册: 在Spring.factories有一段: # Discovery Client Configuration org.spr ...
- springcloud之服务注册与发现
本次分享的是关于springcloud服务注册与发现的内容,将通过分别搭建服务中心,服务注册,服务发现来说明:现在北京这边很多创业公司都开始往springcloud靠了,可能是由于文档和组件比较丰富的 ...
- SpringCloud - 2. 服务注册 和 发现
SpringCloud 的服务注册和发现是由Eureka来完成. 1.eureka server 1.1 依赖 <dependency> <groupId>org.spring ...
- SpringCloud微服务(01):Eureka组件,管理服务注册与发现
本文源码:GitHub·点这里 || GitEE·点这里 一.Eureka基本架构 1.Eureka简介 Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,SpringCl ...
- 将SpringCloud Eureka 服务注册与发现部署到docker
一.前言 最近在学习docker,顺便把之前学习的spring cloud 部署到Docker 中.至于什么是SpringCloud的服务注册与发现,什么是docker,我这里就不作赘述了.可以先去学 ...
- SpringCloud微服务治理技术入门(SCN)
1.集群.分布式.微服务 首先先理解三个感念 什么是集群?: 同一个业务,部署在多个服务器上,目的是实现高可用,保证节点可用! 什么是分布式?: 一个业务分拆成多个子业务,部署在不同的服务器上,每个子 ...
- springCloud 微服务框架搭建入门(很简单的一个案例不喜勿扰)
Spring cloud 实现服务注册及发现 服务注册与发现对于微服务系统来说非常重要.有了服务发现与注册,你就不需要整天改服务调用的配置文件了,你只需要使用服务的标识符,就可以访问到服务. clou ...
- springcloud微服务架构搭建
SpringCloud微服务框架搭建 一.微服务架构 1.1什么是分布式 不同模块部署在不同服务器上 作用:分布式解决网站高并发带来问题 1.2什么是集群 多台服务器部署相同应用构成一个集群 作用:通 ...
随机推荐
- ViewStub应用
在开发应用程序的时候,会遇到这样的情况,在运行时动态的根据条件来决定显示哪个View或哪个布局,可以把可能用到的View都写在上面,先把他们的可见性设置为View.GONE,然后在代码中动态的更改它的 ...
- my41_主从延迟大排查
半同步复制 主库执行 INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so'; SET GLOBAL rpl_semi_sync ...
- 【Service】【Web】【Middleware】Tomcat
1. 概念 1.1. 官方网站:tomcat.apache.org 1.2. tomcat的组件 <Server> <Service> <Connector/> & ...
- JS 的三种定义变量 var let const
Let 只在 let 命令所在的代码块内有效,在外就会报错 Let 是块级作用域,函数内部使用let定义后,对函数外部无影响 Let/const 不存在变量提升,使用前一定要声明后,在使用,否则会报错 ...
- Spring boot 数据源配置。
配置文件 : spring boot 配置文件 有两种形式 ,一种是properties文件.一种是yml文件.案列使用properties文件. 数据源的默认配置 : spring boot 约定 ...
- C语言实现鼠标绘图
使用C语言+EGE图形库(Easy Graphics Engine).思路是通过不断绘制直线来实现鼠标绘图的功能,前一个时刻鼠标的坐标作为直线的起点,现在时刻的坐标作为终点(严格意义是线段而不是直线) ...
- cmd窗口连接mongodb服务端
1----->配置环境变量,将mongodb\bin目录配置到path 2----->打开cmd窗口,进入到bin目录,测试mongodb服务端是否在运行:net start mongod ...
- 推荐2个Mac OS X上的JSON工具
原文:http://www.giser.net/?p=887 1 visual JSON 能够将JSON串以列表的方式展示,方便对JSON数据的解析. 2 JSONModeler 可以解析JSON串生 ...
- QT QApplication干了啥?
------------恢复内容开始------------ QCoreApplicationPrivate 会取得current thread; 在windows平台创建TLS变量,记录线程信息,并 ...
- [BUUCTF]REVERSE——[ACTF新生赛2020]easyre
[ACTF新生赛2020]easyre 附件 步骤 查壳,32位程序,upx壳儿 脱完壳儿,扔进ida 分析 一开始给我们定义了一个数组, v4=[42,70,39,34,78,44,34,40,73 ...