参考视频
1.什么是Eureka元数据
Eureka元数据有两种,分别是标准元数据和自定元数据。
标准元数据:主机名、IP、端口号、状态也及健康检查等信息。这些信息会被发布在服务注册表中,用于服务之间的调用。
自定义元数据:用户自行定义的元素,远程客户端可访问并且可以根据这些元素进行一定的处理。
2.获取元数据的方法
获取元数据的前提是:有元数据,即已启动 Eureka Server ,且有实例完成了注册。
元数据需要通过 EurekaClient 获取,首先在需要获取元数据的项目中导入 EurekaClient :
<!-- 自行添加坐标 -->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2021.0.3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
在需要获取元数据的类中,使用@Resource获取 EurekaClient 对象:
@Resource
private EurekaClient eurekaClient;
通过这个 EurekaClient 对象获取 InstanceInfoList 实例列表(当应用采用集群部署时,一个应用就会有多个实例)
// 通过eurekaClient获取实例信息:根据应用名(provider_demo_hello)
Application app = eurekaClient.getApplication("provider_demo_hello");
List<InstanceInfo> instanceInfoList = app.getInstances();
根据需求获取指定的 InstanceInfo 对象(我这边由于没有集群部署,因此直接获取第一个),其中便保存着元数据。
InstanceInfo instanceInfo = instanceInfoList.get(0);
3.演示 - 获取Eureka元数据并调用服务提供者
总的来说,分为以下五个步骤:
- 部署 Eureka Server(120.79.xxx.xxx:9000);
- 配置并部署 provider_demo_hello(82.157.xx.xxx:8081),若不是本地部署,需要在配置文件中配置公网IP;
- 编写 consumer_demo_hello(127.0.0.1:8080),借助 EurekaClient 获取所有元数据;
- 从所有元数据中提取公网IP和端口号,拼接url,借助RestTemplate发起Http请求。

-
[部署EurekaServer] 在 Eureka Server 的启动类中添加
@EnableEurekaServer,并部署在 120.79.xxx.xxx ,端口号为9000,配置文件如下:server.port=9000 # 服务名 spring.application.name=eureka_server_demo # 是否将自身注册到注册中心 eureka.client.register-with-eureka=false # 是否从注册中心拉取注册信息 eureka.client.fetch-registry=false
-
[部署provider_demo_hello] 在 provider_demo_hello 的启动类中添加
@EnableEurekaClient,并部署在 82.157.xx.xxx ,端口号为8081,配置文件如下:server.port=8081 # 服务名 spring.application.name=provider_demo_hello # Eureka Server的请求地址 eureka.client.service-url.defaultZone=http://120.79.xxx.xxx:9000/eureka/ # 声明外网ip地址 eureka.instance.ip-address=82.157.xx.xxx若不做其他配置, Eureka Client 只能获取到 提供者 的内网IP。若想获取公网IP,则需要在配置文件中使用
eureka.instance.ip-address声明; -
[编写consumer_demo_hello] 创建程序 consumer_demo_hello ,实现功能:
-
在pom.xml中引入 Eureka Client 的Maven坐标:
<!-- 自行添加坐标 --> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>2021.0.3</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> -
修改配置文件(本机启动,端口号8080):
server.port=8080 # 服务名 spring.application.name=consumer_demo_hello # Eureka Server的请求地址 eureka.client.service-url.defaultZone=http://120.79.xxx.xxx:9000/eureka/ -
注入对象并开启Eureka Client:在SpringBoot启动类中创建一个RestTemplate对象,并注入到Spring容器当中,并在类代码上添加
@EnablueEurekaClient;@EnableEurekaClient @SpringBootApplication public class ConsumerDemoHelloApplication { /** * 将RestTemplate对象放入Spring容器当中成为一个Bean */ @Bean public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(ConsumerDemoHelloApplication.class, args); } } -
定义接口:创建controller包,在其中创建TestController,使用
@Resource获取RestTemplate对象和EurekaClient对象,并实现接口功能:/** * @author xyx-Eshang */ @CrossOrigin @RequestMapping("/test") @RestController public class TestController { @Resource private RestTemplate restTemplate; @Resource private EurekaClient eurekaClient; @GetMapping("/sayHelloByRestTemplate") public String sayHello() { // 通过eurekaClient获取实例信息:根据应用名 Application app = eurekaClient.getApplication("provider_demo_hello"); List<InstanceInfo> instanceInfoList = app.getInstances(); InstanceInfo instanceInfo = instanceInfoList.get(0); // 该实例的ip地址和端口号 String ipAddress = instanceInfo.getIPAddr(); int port = instanceInfo.getPort(); // 借助RestTemplate访问服务提供者 String url = "http://" + ipAddress + ":" + port + "/test/sayHello"; String result = restTemplate.getForObject(url, String.class); return "通过RestTemplate发送http请求以获取响应结果:" + result; } }
-
-
[测试结果] 启动 consumer_demo_hello ,在浏览器中输入
http://localhost:8080/test/sayHelloByRestTemplate测试结果。

1万+

被折叠的 条评论
为什么被折叠?



