当前位置:网站首页>Eureka console accesses the info endpoint exposed by the microservice

Eureka console accesses the info endpoint exposed by the microservice

2022-06-21 21:54:00 crysw

1. brief introduction

I'm learning Eureka In the process , Find out Eureka In the list of microservice instances registered on the console , The microservice address can be accessed by clicking info Endpoint , But it will jump to SpringBoot The default exception information interface , This is because microservices are not exposed info Endpoint . Next is Eureka Instance naming and info End point information access gives a case .

2. Build microservices

2.1 Microservice design

Here, the process of setting up a micro service is omitted , I created three microservices :
eureka Micro service registration center : microservicecloud-eureka
Microservice providers : microservicecloud-provider
Micro service consumers : microservicecloud-consumer

2.2 Micro service configuration information

2.2.1 eureka Registry Center To configure

Stand alone configuration

server:
  port: 7001
eureka:
  instance:
    hostname: localhost # Eureka The instance name of the server 
  client:
     #  Whether to register to Eureka Service center 
    register-with-eureka: false 
    #  Whether from Eureka The service center obtains the registered service instance 
    fetch-registry: false 
    service-url:
      defaultZone: http://${
    eureka.instance.hostname}:${
    server.port}/eureka 
  server:
    enable-self-preservation: true #  Self-protection mechanism 

2.2.2 Microservice providers To configure

server:
  port: 8002
spring:
  application:
    name: microservicecloud-provider
# eureka client 
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka # Eureka Address of Registration Center 

2.2.3 Micro service consumers To configure

server:
  port: 8080
spring:
  application:
    name: microservicecloud-consumer

# Eureka Client configuration 
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

2.2.4 Start microservices

Start the above three microservices , visit http://localhost:7001/, see Eureka Microservice registration list of the console .
 Insert picture description here
From the picture above , We can see the name of the microservice instance , Address , Port and other information , But the host name is shown wang_qz, If there are many servers in the production environment , Host names are not identifiable , If you want to show specific ip Address , Configure... As follows .
stay Eureka client Microservice configuration ( service provider or Service consumer )

# Eureka Client configuration 
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka
  instance:
    instance-id: microservicecloud-consumer-01 #  Configure the name of the microservice registration 
    prefer-ip-address: true #  priority of use ip register , Access display ip

start-up Microservice providers , Revisit http://localhost:7001/, see Eureka Console Registration list :
 Insert picture description here

3. visit info Endpoint

3.1 reform Service consumer

Click on microservicecloud-consumer-01, visit http://192.168.43.191:8080/actuator/info, 192.168.43.191 It's my host address , The result is an error message interface .
 Insert picture description here
This is because there is no exposure info Endpoint Give external access to , You need to add startup dependencies that expose endpoints :

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

Configure exposed info Endpoint Information

#  Exposed end point info
info:
  app.name: microservicecloud-consumer
  company.name: www.atguigu.com
  build.artifactId: com.atguigu.springcloud
  build.version: 1.0-SNAPSHOT

restart Microservice providers , Revisit http://localhost:7001/, see Eureka Console For the registration list, click microservicecloud-consumer-01 visit http://192.168.43.191:8080/actuator/info, It is found that... Has been accessed normally info Endpoint information .
 Insert picture description here

3.2 reform service provider

above Service consumer Of info The endpoint has passed Eureka Click the link to access the registration center successfully , however info Endpoint The information is written in the configuration file , If the project name or version changes , The configuration file also needs to be modified synchronously , have access to ${} Placeholder to get project information .
First , Add the same actuator Start dependency

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

Add exposed info Endpoint Information

# eureka client 
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka # Eureka Address of Registration Center 
  instance:
    instance-id: microservicecloud-provider-02 #  Custom microservice name information 
    prefer-ip-address: true #  priority of use id register , The access path can be displayed IP Address 
#  exposed info Endpoint information 
info:
  app.name: ${
    spring.application.name}
  company.name: www.atguigu.com
  build.artifactId: ${
    project.artifactId}

restart service provider , visit http://localhost:7001/, see Eureka Console List of Registry Services .
 Insert picture description here
Click on microservicecloud-provider-02 , visit http://192.168.43.191:8002/actuator/info, I found the wrong report , Why ?
 Insert picture description here
Let's check the error log , The discovery cannot be resolved ${} Placeholder to get the attribute value .
 Insert picture description here
resolvent , Building pom Add build to build , Specify resolution $ Symbol .

<!--  structure , Packaging plug-in  -->
    <build>
        <finalName>microservicecloud</finalName>
        <resources>
            <resource>
	            <!-- Specify the resource file location -->
                <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>
            <!-- Able to parse  src/main/resources/ In the configuration file $ Placeholder to get property value ; -->
            <!--Eureka Click on the service list to access the exposed endpoint information of the specified microservice -->
                            <delimit>$</delimit>
                        </delimiters>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

restart service provider , visit http://localhost:7001/, see Eureka Console List of Registry Services .
Click on microservicecloud-provider-02 , visit http://192.168.43.191:8002/actuator/info, info The normal parsing of information returns .
 Insert picture description here

4. summary

Access to microservices info Endpoint Information , Here are some points :

<1> add to actuator Start dependency , Exposed end point .
<2> Profile add info Endpoint information .
<3> If info Endpoint information uses ${} Placeholder to get property value , Need to build pom Specify the resolution in the file $ Symbol configuration .

Personal blog

Welcome to my personal blog : https://www.crystalblog.xyz/

Alternate address : https://wang-qz.gitee.io/crystal-blog/

原网站

版权声明
本文为[crysw]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211954217001.html