目的:使用springboot-admin作为监控端监控springboot节点 将springboot-admin-server注册到consul 客户端自动发现 自动注册

创建springboot-admin-server服务

pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0<&#47;modelVersion>
<parent>
<groupId>org.springframework.boot<&#47;groupId>
<artifactId>spring-boot-starter-parent<&#47;artifactId>
<version>2.2.2.RELEASE<&#47;version>
<relativePath/> <!-- lookup parent from repository -->
<&#47;parent>
<groupId>com.guochen<&#47;groupId>
<artifactId>adminserver<&#47;artifactId>
<version>0.0.1-SNAPSHOT<&#47;version>
<name>adminserver<&#47;name>
<description>Demo project for Spring Boot<&#47;description>

<properties>
<java.version>1.8<&#47;java.version>
<&#47;properties>

<dependencies>
<dependency>
<groupId>de.codecentric<&#47;groupId>
<artifactId>spring-boot-admin-starter-server<&#47;artifactId>
<version>2.2.2<&#47;version>
<&#47;dependency>
<dependency>
<groupId>org.springframework.boot<&#47;groupId>
<artifactId>spring-boot-starter-web<&#47;artifactId>
<&#47;dependency>
<dependency>
<groupId>org.springframework.cloud<&#47;groupId>
<artifactId>spring-cloud-starter-consul-all<&#47;artifactId>
<version>2.2.1.RELEASE<&#47;version>
<&#47;dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot<&#47;groupId>-->
<!-- <artifactId>spring-boot-starter-security<&#47;artifactId>-->
<!-- <&#47;dependency>-->
<&#47;dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot<&#47;groupId>
<artifactId>spring-boot-maven-plugin<&#47;artifactId>
<&#47;plugin>
<&#47;plugins>
<&#47;build>

<&#47;project>

启动类

@SpringBootApplication
@EnableAdminServer
@EnableDiscoveryClient
public class AdminserverApplication {

public static void main(String[] args) {
SpringApplication.run(AdminserverApplication.class, args);
}

}

application.yml配置

server:
port: 8777
spring:
application:
name: admin-server
cloud:
config:
enabled: false
consul:
host: localhost
port: 8500
discovery:
# 启用服务发现
enabled: true
# 启用服务注册
register: true
# 服务停止时取消注册
deregister: true
# 表示注册时使用IP而不是hostname
prefer-ip-address: true
# 执行监控检查的频率
health-check-interval: 30s
# 设置健康检查失败多长时间后,取消注册
health-check-critical-timeout: 30s
# 健康检查的路径
health-check-path: /actuator/info
# 服务注册标识,格式为:应用名称+服务器IP+端口
instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
# tags: management.context-path=/foo, health.path=/ping, user.name=admin, user.password=123



#监控监控
management:
health:
redis:
enabled: false
consul:
enabled: true
binders:
enabled: false
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: always

# security:
# user:
# name: "admin"
# password: "123"

创建springboot-admin-client服务

pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0<&#47;modelVersion>
<parent>
<groupId>org.springframework.boot<&#47;groupId>
<artifactId>spring-boot-starter-parent<&#47;artifactId>
<version>2.2.2.RELEASE<&#47;version>
<relativePath/> <!-- lookup parent from repository -->
<&#47;parent>
<groupId>com.guochen<&#47;groupId>
<artifactId>consuldemo<&#47;artifactId>
<version>0.0.1-SNAPSHOT<&#47;version>
<name>consuldemo<&#47;name>
<description>Demo project for Spring Boot<&#47;description>

<properties>
<java.version>1.8<&#47;java.version>
<spring-cloud.version>Hoxton.SR1<&#47;spring-cloud.version>
<&#47;properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot<&#47;groupId>
<artifactId>spring-boot-starter-web<&#47;artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot<&#47;groupId>
<artifactId>spring-boot-starter-tomcat<&#47;artifactId>
<&#47;exclusion>
<&#47;exclusions>
<&#47;dependency>
<dependency>
<groupId>org.springframework.cloud<&#47;groupId>
<artifactId>spring-cloud-starter-consul-config<&#47;artifactId>
<&#47;dependency>
<dependency>
<groupId>org.springframework.cloud<&#47;groupId>
<artifactId>spring-cloud-starter-consul-discovery<&#47;artifactId>
<&#47;dependency>
<dependency>
<groupId>org.springframework.cloud<&#47;groupId>
<artifactId>spring-cloud-starter-netflix-hystrix<&#47;artifactId>
<&#47;dependency>
<dependency>
<groupId>org.springframework.boot<&#47;groupId>
<artifactId>spring-boot-starter-actuator<&#47;artifactId>
<&#47;dependency>
<dependency>
<groupId>de.codecentric<&#47;groupId>
<artifactId>spring-boot-admin-starter-client<&#47;artifactId>
<version>2.2.2<&#47;version>
<&#47;dependency>
<dependency>
<groupId>org.springframework.boot<&#47;groupId>
<artifactId>spring-boot-starter-jetty<&#47;artifactId>
<&#47;dependency>
<&#47;dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud<&#47;groupId>
<artifactId>spring-cloud-dependencies<&#47;artifactId>
<version>${spring-cloud.version}<&#47;version>
<type>pom<&#47;type>
<scope>import<&#47;scope>
<&#47;dependency>
<&#47;dependencies>
<&#47;dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot<&#47;groupId>
<artifactId>spring-boot-maven-plugin<&#47;artifactId>
<&#47;plugin>
<&#47;plugins>
<&#47;build>

<&#47;project>

启动类

@SpringBootApplication
@EnableDiscoveryClient

重点 bootstrap.yml

spring:
cloud:
consul:
host: localhost
port: 8500
#enabled将此值设置为“false”禁用Consul配置
config:
enabled: true #默认是true --
format: YAML # 表示consul上面文件的格式 有四种 YAML PROPERTIES KEY-VALUE FILES
data-key: data #表示consul上面的KEY值(或者说文件的名字) 默认是data
# watch选项为配置监视功能,主要监视配置的改变
watch:
enabled: true
delay: 10000
wait-time: 30

consul 配置中心配置

spring: 
boot:
admin:
client:
url: admin-server
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: ALWAYS

consul截图

运行成功截图

a
a
a

客户端信息

a