欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring boot admin服务监控

程序员文章站 2022-06-28 17:07:23
Consul:服务注册发现、配置中心zuul:网关,路由spring boot admin: 服务监控...

Consul:服务注册发现、配置中心

zuul:网关,路由

spring boot admin: 服务监控

SpringBootAdmin的Server端:

       只需要pom加入springbootadmin的Server依赖,在启动项加入@EnableAdminServer注解即可。

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</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>zuul-gateway-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>zuul-gateway-test</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <java.version>1.8</java.version>
        <elastic.version>7.6.0</elastic.version>
        <spring-cloud.version>2.2.4.RELEASE</spring-cloud.version>
        <log4j.version>1.2.17</log4j.version>
        <spring-boot-admin.version>2.3.0</spring-boot-admin.version>
        <docker.image.prefix>springboot</docker.image.prefix>
        <docker.maven.plugin.version> 1.2.2</docker.maven.plugin.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions><!-- 去掉springboot默认配置 -->
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency> <!-- 引入log4j2依赖 -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
            <version>${spring-cloud.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-config</artifactId>
            <version>${spring-cloud.version}</version>
        </dependency>
        <!--zipkin-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
            <version>${spring-cloud.version}</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <!--服务监控,admin server-->
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
            <version>${spring-boot-admin.version}</version>
        </dependency>
        <!--服务监控,admin client-->
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
            <version>${spring-boot-admin.version}</version>
        </dependency>
        <!--test依赖部分-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
            <version>${spring-cloud.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

 

ZuulGatewayTestApplication.java
package com.example;

import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@SpringBootApplication
@EnableZuulProxy  /**zuul和consul*/
@EnableAdminServer /**spring cloud admin*/
public class ZuulGatewayTestApplication {

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

}

 bootstrap.yml为Consul配置中心配置

#consul配置中心服务
spring:
  cloud:
    consul:
      host: localhost
      #host: 00.0.100.200
      port: 8500
      #enabled将此值设置为“false”禁用Consul配置
      config:
        enabled: true   #默认是true --
        format: YAML    # 表示consul上面文件的格式 有四种 YAML PROPERTIES KEY-VALUE FILES
        #data-key: configuration    #表示consul上面的KEY值(或者说文件的名字) 默认是data
        data-key: data    #表示consul上面的KEY值(或者说文件的名字) 默认是data
        #prefix设置配置值的基本文件夹
        prefix: spring-cloud
        #defaultContext设置所有应用程序使用的文件夹名称
        #profileSeparator设置用于使用配置文件在属性源中分隔配置文件名称的分隔符的值

 application.yml为服务端口,服务名,id,Consul服务注册发现,zuul网关、admin配置

#***********************************************************************************************************
#                                   一下配置为zuul 按模块路径路由模式
#***********************************************************************************************************
#spring:
#  cloud:
#    consul:
#      host: hdp04  # consul 服务器主机名
#      port: 8500  # consul 服务器端口
#      discovery:
#        #        healthCheckPath: ${management.contextPath}/health # actuator 健康检查的 url 路径
#        healthCheckPath: /health  # 自定义健康检查的 url(适合于不适用 actuator 的场景)
#        healthCheckInterval: 15s  # 健康检查的频率, 默认 15 秒
#        # 直接指定服务的 consul service id(即 instance id).
#        # 默认情况下为 spring.application.name + server.port, 如果在多个服务器上同一个服务, 因为应用名和端口都一致,
#        #会导致service id 会重复, 所以一般情况都需要引入一个随机数避免重复 .
#        # ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}  应用名称+服务器IP+端口
#        instance-id: test-config-service02 #${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}}
#        #        prefer-ip-address: true  # 在注册时使用 consul IP, 而不是 hostname
#        #        ip-address: hdp04 # 使用 consul 服务器 IP, 而不是 hostname, 需要搭配 prefer-ip-address 属性
#        #        acl-token: 4efb1523-76a3-f476-e6d8-452220593089 #设定 consul acl token 值
#        enabled: true #是否启用服务发现
#        # 维护 tags
#        #$ 下面示例的 tag map 是:  foo->bar 和 baz->baz
#        tags: version=1.0,author=PCL.CSRC
#      enabled: true
#  #      ribbon:
#  #        enabled: false
#  application:
#    name: test-config-service02
#
#server:
#  port: 8804
#
#zuul:
#  routes:
#    dns: #自定义
#      #静态路径配置 所有的以/dns 开始的请求,全部转发到test-config服务的url 8801端口服务去执行
#      path: /dns/**   #dns为自定义前缀 匹配规则三种:/?  /* /**
#      #service-id: test-config
#      url: http://hdp01:8801/api/dns  #注意不要写成 hdp01:8801/api/dns  否则报错Load balancer does not have available server for client: hdp01:8801/api/dns
#    handle:
#      path: /handle/**
#      #service-id: test-config
#      url: http://hdp01:8805/api/handle
#    neo4j:
#      path: /neo4j/**
#      #service-id: test-config
#      url: http://hdp01:8801/api/

#  host:
#    connect-timeout-millis: 3000
#    socket-timeout-millis: 60000

# 添加ribbon的超时时间设置
#ribbon:
#  ReadTimeout: 1000
#  ConnectTimeout: 3000
#hystrix:
#  command:
#    default:
#      execution:
#        isolation:
#          thread:
#            timeoutInMilliseconds: 6000



#***********************************************************************************************************
#                                   一下配置为zuul轮询方式
#***********************************************************************************************************

server:
  port: 8804
spring:
  boot:
    admin:
      discovery:
        ignored-services: consul*,config*
      ui:
        title: 微服务监控
  #  boot:  #spring cloud admin 增加
#    admin:
#      client:
#        url: http://hdp01:8804/     #spring boot admin client 的url
  cloud:
    #consul服务注册发现监控检测
    consul:
      host:  localhost # consul 服务器主机名
      port: 8500  # consul 服务器端口
      discovery:
#        healthCheckPath: ${management.contextPath}/health # actuator 健康检查的 url 路径
        #healthCheckPath: /actuator/health  # 自定义健康检查的 url(适合于不适用 actuator 的场景)
        healthCheckInterval: 5s  # 健康检查的频率, 默认 15 秒
        prefer-ip-address: false  #识别服务主机以IP优先
        # 直接指定服务的 consul service id(即 instance id).
        # 默认情况下为 spring.application.name + server.port, 如果在多个服务器上同一个服务, 因为应用名和端口都一致,
        #会导致service id 会重复, 所以一般情况都需要引入一个随机数避免重复 .
        # ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}  应用名称+服务器IP+端口
        instance-id: ${spring.application.name}:${server.port}:${spring.cloud.client.hostname} #${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}}
#        prefer-ip-address: true  # 在注册时使用 consul IP, 而不是 hostname
#        ip-address: hdp04 # 使用 consul 服务器 IP, 而不是 hostname, 需要搭配 prefer-ip-address 属性
#        acl-token: 4efb1523-76a3-f476-e6d8-452220593089 #设定 consul acl token 值
        enabled: true #是否启用服务发现
        # 维护 tags
        #$ 下面示例的 tag map 是:  foo->bar 和 baz->baz
        tags: version=1.0,author=PCL.CSRC
        register: true
        service-name: ${spring.application.name}

#      ribbon:
#        enabled: false
  application:
    name: test-config-service02
    # ES搜索引擎   ES 健康检测启用
#  elasticsearch: #spring cloud admin 增加:
#    rest:
#      #username: 
#      #password:
#     
#    #cluster-nodes: hdp01 #已经过期,失效
#    #cluster-name:   #已经过期,失效
#  data:
#    elasticsearch: #spring cloud admin 增加:
#      client:
#        reactive:
#          use-ssl: false
#          #查看Spring boot admin  Client界面的配置,写的是spring.data.elasticsearch.client.reactive.endpoints[0]	localhost:9200 (在这里修改尝试)
#          endpoints: hdp01:9201,hdp02:9201,hdp03:9201,hdp04:9201
#      repositories:
#        enabled: true


#zuul属性配置  zuul.routes路由转发配置
#Zuul来代理请求转发到内部的服务上去,统一为外部提供服务  动态的路由转发功能
zuul:
  routes:
    #prefix: /zuul
    order:
      serviceId: test-config
      path: /business/**
  ignored-services: test-config

# 添加ribbon的超时时间设置
#ribbon:
#  ReadTimeout: 1000
#  ConnectTimeout: 3000

##
management: #spring cloud admin 增加
  health:
    elasticsearch:  #关闭ES健康检测
      enabled: false
    #defaults:  #禁用HealthIndicator的实现 包括ES状态等等
      #enabled: false
  security:
    enabled: false
  endpoints:
    web:
      exposure:
        include: '*'
      base-path: /actuator       #default monitor root path
  endpoint:
    health:
      show-details: always
    #enabled-by-default: false  #显示全部
    #info:
      #defaults:
        #enabled: false
#logging:
#  level:
#    root: info

监控Web端查看:

 spring boot admin服务监控

spring boot admin服务监控 

SpringBootClient端:

pom如Server端, 

<!--服务监控,admin server-->
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-server</artifactId>
    <version>${spring-boot-admin.version}</version>
</dependency>

application.yml

server: #For spring boot config only
    port: 8888 #也可以通过变量方式${port}传入,如:java -Dport=9999
management:
    health:
        elasticsearch:
            enabled: false #在引入spring-boot-starter-data-elasticsearch后spring启动后默认会对es做检查
    endpoints:
        web:
            exposure:
                include: "*" #Default for only health and info, open all now
            base-path: /actuator       #default monitor root path
    endpoint:
        health:
            show-details: always

spring:
    boot:
        admin:
            client:
                url: http://localhost:7777  #spring boot admin client配置地址指向admin server的运行地址,所有服务都指向这个地址
    application:
        name: spring-service #muti service must be the same name but different instance id
    zipkin:
        enabled: true
        base-url: http://localhost:9411/
        sender:
            type: web
    sleuth:
        sampler:
            probability: 1

 

 

 

本文地址:https://blog.csdn.net/ITwangnengjie/article/details/109309704