当前位置:网站首页>Eureka控制台访问微服务暴露的info端点
Eureka控制台访问微服务暴露的info端点
2022-06-21 19:55:00 【crysw】
1. 简介
在学习Eureka过程中, 发现Eureka控制台注册的微服务实例列表中, 微服务地址可以点击访问微服务的info端点, 但是会跳转到SpringBoot默认的异常信息界面, 这是因为微服务没有暴露info端点. 下面就Eureka实例命名和info端点信息访问给出案例.
2. 搭建微服务
2.1 微服务设计
这里搭建微服务的过程就省略了, 我创建了三个微服务:
eureka微服务注册中心: microservicecloud-eureka
微服务提供方: microservicecloud-provider
微服务消费方: microservicecloud-consumer
2.2 微服务配置信息
2.2.1 eureka注册中心配置
单机版配置
server:
port: 7001
eureka:
instance:
hostname: localhost # Eureka服务端的实例名称
client:
# 是否注册到Eureka服务中心
register-with-eureka: false
# 是否从Eureka服务中心获取注册的服务实例
fetch-registry: false
service-url:
defaultZone: http://${
eureka.instance.hostname}:${
server.port}/eureka
server:
enable-self-preservation: true # 自我保护机制
2.2.2 微服务提供方配置
server:
port: 8002
spring:
application:
name: microservicecloud-provider
# eureka客户端
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka # Eureka注册中心地址
2.2.3 微服务消费方配置
server:
port: 8080
spring:
application:
name: microservicecloud-consumer
# Eureka客户端配置
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
2.2.4 启动微服务
启动上面的三个微服务, 访问http://localhost:7001/, 查看Eureka控制台的微服务注册列表.
从上面图片中, 我们可以看到微服务实例名称, 地址, 端口等信息, 但是展示的是主机名wang_qz, 如果在生产环境服务器比较多, 主机名不具有标识性, 如果要展示具体的ip地址, 按照下面方式配置.
在Eureka客户端微服务配置(服务提供方或服务消费方)
# Eureka客户端配置
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
instance:
instance-id: microservicecloud-consumer-01 # 配置微服务注册的名称
prefer-ip-address: true # 优先使用ip注册,访问显示ip
启动微服务提供方, 再次访问http://localhost:7001/, 查看Eureka控制台注册列表:
3. 访问info端点
3.1 改造服务消费方
点击microservicecloud-consumer-01, 访问http://192.168.43.191:8080/actuator/info, 192.168.43.191是我的主机地址, 结果是报错的异常信息界面.
这是因为没有暴露info端点给外部访问, 需要添加暴露端点的启动依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
配置暴露的info端点信息
# 暴露端点info
info:
app.name: microservicecloud-consumer
company.name: www.atguigu.com
build.artifactId: com.atguigu.springcloud
build.version: 1.0-SNAPSHOT
重启微服务提供方, 再次访问http://localhost:7001/, 查看Eureka控制台注册列表点击microservicecloud-consumer-01访问http://192.168.43.191:8080/actuator/info, 发现已经正常访问到info端点信息.
3.2 改造服务提供方
上面服务消费方的info端点已经通过Eureka注册中心点击链接访问成功, 但是info端点信息都是写死在配置文件中的, 如果项目名称或版本更改, 配置文件也需要同步修改, 可以使用${}占位符获取项目信息.
首先, 一样的添加actuator启动依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
添加暴露的info端点信息
# eureka客户端
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka # Eureka注册中心地址
instance:
instance-id: microservicecloud-provider-02 # 自定义微服务名称信息
prefer-ip-address: true # 优先使用id注册,访问路径可以显示IP地址
# 暴露的info端点信息
info:
app.name: ${
spring.application.name}
company.name: www.atguigu.com
build.artifactId: ${
project.artifactId}
重启服务提供方, 访问http://localhost:7001/, 查看Eureka控制台注册中心服务列表.
点击microservicecloud-provider-02 , 访问http://192.168.43.191:8002/actuator/info, 发现报错了, 是为什么呢?
我们来查看报错日志, 发现是无法解析${}占位符的方式获取属性值.
解决方法, 在构建的pom中添加构建构建, 指定解析$符号.
<!-- 构建,打包插件 -->
<build>
<finalName>microservicecloud</finalName>
<resources>
<resource>
<!--指定资源文件位置-->
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<delimiters>
<!--能够解析 src/main/resources/配置文件中的$占位符获取属性值; -->
<!--Eureka服务列表点击访问指定微服务的暴露端点信息使用-->
<delimit>$</delimit>
</delimiters>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
重启服务提供方, 访问http://localhost:7001/, 查看Eureka控制台注册中心服务列表.
点击microservicecloud-provider-02 , 访问http://192.168.43.191:8002/actuator/info, info信息正常解析返回.
4. 总结
访问微服务info端点信息, 有如下几点:
<1> 添加
actuator启动依赖, 暴露端点.
<2> 配置文件添加info端点信息.
<3> 如果info端点信息使用了${}占位符获取属性值, 需要在构建pom文件中指定解析$符号的配置.
个人博客
欢迎各位访问我的个人博客: https://www.crystalblog.xyz/
边栏推荐
- 杰理之获取当前音频文件(长)文件名和当前(长)文件夹名【篇】
- J - Count the string HDU - 3336 (KMP)
- Time modification method for search device of Jerry's Bluetooth transmitter [chapter]
- Constructor in JS (emphasis)
- ARP protocol and ARP attack
- Go单元测试对数据库CRUD进行Mock测试
- 微信小程序js把数字转化成字母
- 拖延患者自救指南|“我有不拖延的100种借口,却不愿意跨出一步”
- 30 groups of outdoor travel vlog record LUTS color matching preset moody travel LUTS
- 2022 National latest fire facility operator (intermediate fire facility operator) simulation question bank and answers
猜你喜欢

英文论文要怎么查重?

ACM. HJ35 蛇形矩阵 ●

ACM. HJ61 放苹果 ●

Jinghe integration has passed the registration: it plans to raise 9.5 billion yuan. Hefei Construction Investment and Midea are shareholders

拿什么来拯救你?我的注意力

matplotlib plt.subplots()详解

Caricature scientifique | Vous pouvez apprendre l'EEG en regardant les images. Voulez - vous essayer?

怎样有效率地进行外文文献检索?

matplotlib plt. Details of subplots()

英文论文如何进行润色?
随机推荐
Xshell7+Xftp7免费版下载
科研漫畫 | 看圖可以學腦電,來試試?
PowerPoint tutorial, how to organize slides into groups in PowerPoint?
JS里的数据类型(基础)
Xcode plug-in management tool Alcatraz
Wechat applet JS converts numbers into letters
Xmind8 latest cracking tutorial (useful for personal testing)
可以在网上炒股开户吗?是安全的吗
怎样有效率地进行外文文献检索?
Why does defer not execute after the program exits
Dedecms dream weaving background system adds its own column menu
Tx9118 Synchronous Boost IC
Definition of unused processing methods when compiling C51 with keil 5
2022 National latest fire facility operator (intermediate fire facility operator) simulation question bank and answers
Dedecms dream weaving background mailbox verification sending mail configuration tutorial
Product innovation - an innovative social app that returns to real life
Data visualization tool software
Improve four performance! D-wave releases next generation quantum annealing prototype
When Jerry made Bluetooth transmission, when he modified stereo to mono differential output, there was a jam sound at the receiving end [chapter]
ACM. HJ51 输出单向链表中倒数第k个结点 ●