springboot整合actuator、admin对应用程序进行监控

目录

一、Spring Boot Actuator

1、整合Spring Boot Actuator

2、返回端口详情

3、暴露所有端点

二、整合Spring Boot Admin

1、搭建Spring Boot Admin Server

2、搭建Spring Boot Admin Client

3、整合Spring Security

登录失败问题

客户端注册失败问题

动态配置用户名/密码

4、服务下线邮箱提醒

5、整合nacos注册中心


一、Spring Boot Actuator

Spring Boot Actuator 是 Spring Boot 的一个子项目,可以对 Spring Boot 应用程序进行监控和管理,并对外提供了大量的端点,可以选择使用 HTTP 端点或 JMX 来管理和监控应用程序。

1、整合Spring Boot Actuator

通过IntelliJ IDEA创建一个名为springboot-actuator的springboot项目

修改pom.xml文件,将spring boot的版本修改为2.5.9。

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.5.9</version>
    <relativePath />
</parent>

在我们的springboot应用中整合actuator非常简单,只需要一个maven依赖:

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

完整的pom.xml

<?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.5.9</version>
        <relativePath />
    </parent>

    <groupId>cn.edu.sgu.www</groupId>
    <artifactId>springboot-actuator</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

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

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

最后设置一下启动端口,修改application.yml配置文件

server:
  port: 8077

访问localhost:端口号/actuator,可以看到返回了很多个http链接地址,这些就是actuator默认提供给我们获取应用信息的端点。

http://localhost:8077/actuator/info用于获取应用的信息,这个端点默认返回空数据,这个是需要我们自己配置的。

http://localhost:8077/actuator/health用于获取服务的健康状态,如果服务依赖的所有中间件都是正常状态,这里会返回一个UP,表示在线状态。

2、返回端口详情

health端点默认只返回了一个状态,可以通过以下配置,让端点返回详细的信息。

management:
  endpoint:
    health:
      show-details: always

重启项目,再次访问health端点:

3、暴露所有端点

上一步中,我们访问actuator只返回了两个端点:health和info,其实actuator提供的端点远不止这些,官网提供的jmx环境和web环境下各个端点的开放情况。

IDJMXWeb

auditevents

Yes

No

beans

Yes

No

caches

Yes

No

conditions

Yes

No

configprops

Yes

No

env

Yes

No

flyway

Yes

No

health

Yes

Yes

heapdump

N/A

No

httptrace

Yes

No

info

Yes

Yes

integrationgraph

Yes

No

jolokia

N/A

No

logfile

N/A

No

loggers

Yes

No

liquibase

Yes

No

metrics

Yes

No

mappings

Yes

No

prometheus

N/A

No

scheduledtasks

Yes

No

sessions

Yes

No

shutdown

Yes

No

threaddump

Yes

No

那么,其他端点怎么暴露出来呢,让我们访问http://localhost:8077/actuator就能得到所有可用端点。

修改application.yml,添加暴露所有端点的配置

server:
  port: 8077

management:
  endpoint:
    health:
      show-details: always # 显示健康状态详情
  endpoints:
    web:
      exposure:
        include: "*" # 暴露所有端点
      base-path: /actuator

这里的端点endpoint可以理解为Controller接口,通过端点可以获取应用的详细信息,包括实例健康状态、配置信息、映射信息、缓存信息等等。

关于具体每个端点的功能,就不一一介绍了,可以通过页面进行查看。

二、整合Spring Boot Admin

那么,actuator提供给我们那么多端点,不可能每次都通过这些端点来获取应用信息吧,太麻烦了,有没有一种更好的方式可以方便又快捷的查看应用程序的状态信息呢。

这个时候就需要引入Spring Boot Admin了

Spring Boot Admin是一个社区项目,用于管理和监控Spring Boot应用程序。

应用程序在我们的Spring Boot Admin Client(通过 HTTP)上注册,或使用Spring Cloud(如 Eureka、Consul)发现。

github可能访问缓慢或者无法访问,这里提供了gitee的仓库地址

Spring Boot Adminicon-default.png?t=N7T8https://gitee.com/pujiaolin/spring-boot-admin

1、搭建Spring Boot Admin Server

接下来开始整合Spring Boot Admin,根据官方文档的介绍,要先设置admin的服务器端,这是一个整合了spring-boot-admin-starter-server单独的服务。

通过idea创建一个Springboot项目,取名为springboot-admin-server

修改pom.xml,添加spring-boot-admin-starter-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</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath />
    </parent>

    <groupId>cn.edu.sgu.www</groupId>
    <artifactId>springboot-admin-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
        <admin.version>2.3.1</admin.version>
    </properties>

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

        <!--admin server-->
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
            <version>${admin.version}</version>
        </dependency>
    </dependencies>

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

在启动类上添加@EnableAdminServer注解

package cn.edu.sgu.www.admin;

import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@EnableAdminServer
@SpringBootApplication
public class AdminServer {

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

}

这样,admin的服务端就搭建好了,然后访问localhost:8080,看到的就是admin的界面。

2、搭建Spring Boot Admin Client

创建完服务器,需要往服务器注册客户端应用程序。

同样的,通过idea创建一个Springboot应用,取名为springboot-admin-client

修改pom.xml,添加admin客户端的依赖

<?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.4.RELEASE</version>
        <relativePath />
    </parent>

    <groupId>cn.edu.sgu.www</groupId>
    <artifactId>springboot-admin-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
        <admin.version>2.3.1</admin.version>
    </properties>

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

        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
            <version>${admin.version}</version>
        </dependency>
    </dependencies>

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

然后修改配置文件application.yml,配置admin服务器的URL,并把第一章节的actuator配置复制过来。

admin-server没有配置端口,默认是8080

server:
  port: 8081

spring:
  application:
    name: admin-client
  boot:
    admin:
      client:
        url: http://localhost:8080

management:
  endpoints:
    web:
      exposure:
        include: "*"
      base-path: /actuator
  endpoint:
    health:
      show-details: always

启动客户端服务springboot-admin-client,再次访问localhost:8080,可以看到,客户端已经注册到了服务器

3、整合Spring Security

通过上一步,我们已经搭建好了spring boot admin的全部服务了,但是存在很严重的安全问题,任何人都可以访问程序的运行状态和信息,这个章节通过整合Spring Security来解决这个问题,配置只有登录才能访问admin-client客户端程序的各种端点信息。

点击Spring Boot Admin官网左边的目录链接,直接跳转到Security

修改springboot-admin-server服务服务的pom.xml文件,添加spring security的依赖

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

pom.xml

<?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.4.RELEASE</version>
        <relativePath />
    </parent>

    <groupId>cn.edu.sgu.www</groupId>
    <artifactId>springboot-admin-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
        <admin.version>2.3.1</admin.version>
    </properties>

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

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

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

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

在admin-server端添加一个配置类,根据自己的喜好来修改第二个configure方法里的用户名和密码

package cn.edu.sgu.www.admin.config;

import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import java.util.UUID;

/**
 * @author heyunlin
 * @version 1.0
 */
@Configuration(proxyBeanMethods = false)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    private final AdminServerProperties adminServer;

    public SecurityConfig(AdminServerProperties adminServer) {
        this.adminServer = adminServer;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
        successHandler.setTargetUrlParameter("redirectTo");
        successHandler.setDefaultTargetUrl(this.adminServer.path("/"));

        http.authorizeRequests(
                (authorizeRequests) -> authorizeRequests.antMatchers(this.adminServer.path("/assets/**")).permitAll()
                        .antMatchers(this.adminServer.path("/login")).permitAll().anyRequest().authenticated()
        ).formLogin(
                (formLogin) -> formLogin.loginPage(this.adminServer.path("/login")).successHandler(successHandler).and()
        ).logout((logout) -> logout.logoutUrl(this.adminServer.path("/logout"))).httpBasic(Customizer.withDefaults())
                .csrf((csrf) -> csrf.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                        .ignoringRequestMatchers(
                                new AntPathRequestMatcher(this.adminServer.path("/instances"),
                                        HttpMethod.POST.toString()),
                                new AntPathRequestMatcher(this.adminServer.path("/instances/*"),
                                        HttpMethod.DELETE.toString()),
                                new AntPathRequestMatcher(this.adminServer.path("/actuator/**"))
                        ))
                .rememberMe((rememberMe) -> rememberMe.key(UUID.randomUUID().toString()).tokenValiditySeconds(1209600));
    }

    // Required to provide UserDetailsService for "remember functionality"
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("user").password("12345").roles("USER");
    }

}

登录失败问题

然后重启springboot-admin-server服务,再次访问localhost:8080的时候,发现要登录

输入刚刚设置的用户名/密码:user/12345,点击登录按钮,这时候发现页面只是刷新了一下,并没有跳转到首页,这里也是个坑。

查看服务端报错信息

点第一行或者第二行的matches()方法进去看看

很显然,就是因为这里主动抛出的异常导致的登录失败,往鼠标上面滚动,这是一个PasswordEncoder的实现类

由此可见,这是默认调用的这个实现类的matches()方法,为了解决这个异常问题,我们需要在admin-server自己创建一个org.springframework.security.crypto.password.PasswordEncoder的实现类,并声明为bean,然后重写matches()方法,直接通过equals()比较即可。

package cn.edu.sgu.www.admin.encoder;

import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * @author heyunlin
 * @version 1.0
 */
@Component
public class MyPasswordEncoder implements PasswordEncoder {

    @Override
    public String encode(CharSequence rawPassword) {
        return null;
    }

    @Override
    public boolean matches(CharSequence rawPassword, String encodedPassword) {
        return rawPassword.equals(encodedPassword);
    }

}

客户端注册失败问题

然后再次重启springboot-admin-server服务,这一次输入user/12345点击登录,成功进到了首页,但是,客户端怎么没了,居然没有注册进来。

接着看文档,后面还有很关键的说明,需要在客户端添加下面这个配置,用户名密码就是刚刚设置的用户名user和密码12345。

修改springboot-admin-client服务的application.yml,添加用户名和密码的配置,重启一下。

server:
  port: 8081

spring:
  application:
    name: admin-client
  boot:
    admin:
      client:
        username: user
        password: 12345
        url: http://localhost:8080

management:
  endpoints:
    web:
      exposure:
        include: "*"
      base-path: /actuator
  endpoint:
    health:
      show-details: always

刷新admin-server的页面,这时候发现admin-client终于成功注册进来了

动态配置用户名/密码

最后,因为springboot-admin-server的用户名、密码都是写死在代码里的,一般希望能过通过配置文件动态修改。

修改springboot-admin-server服务的pom.xml文件,在配置文件中配置用户名和密码

server:
  port: 8080

spring:
  application:
    name: admin-server
  security:
    user:
      name: user
      password: 12345

修改一下配置类,从配置中读取用户名和密码

package cn.edu.sgu.www.admin.config;

import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import java.util.UUID;

/**
 * @author heyunlin
 * @version 1.0
 */
@Configuration(proxyBeanMethods = false)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    private final SecurityProperties securityProperties;
    private final AdminServerProperties adminServerProperties;


    public SecurityConfig(SecurityProperties securityProperties, AdminServerProperties adminServerProperties) {
        this.securityProperties = securityProperties;
        this.adminServerProperties = adminServerProperties;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
        successHandler.setTargetUrlParameter("redirectTo");
        successHandler.setDefaultTargetUrl(this.adminServerProperties.path("/"));

        http.authorizeRequests(
                (authorizeRequests) -> authorizeRequests.antMatchers(this.adminServerProperties.path("/assets/**")).permitAll()
                        .antMatchers(this.adminServerProperties.path("/login")).permitAll().anyRequest().authenticated()
        ).formLogin(
                (formLogin) -> formLogin.loginPage(this.adminServerProperties.path("/login")).successHandler(successHandler).and()
        ).logout((logout) -> logout.logoutUrl(this.adminServerProperties.path("/logout"))).httpBasic(Customizer.withDefaults())
                .csrf((csrf) -> csrf.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                        .ignoringRequestMatchers(
                                new AntPathRequestMatcher(this.adminServerProperties.path("/instances"),
                                        HttpMethod.POST.toString()),
                                new AntPathRequestMatcher(this.adminServerProperties.path("/instances/*"),
                                        HttpMethod.DELETE.toString()),
                                new AntPathRequestMatcher(this.adminServerProperties.path("/actuator/**"))
                        )
                ).rememberMe((rememberMe) -> rememberMe.key(UUID.randomUUID().toString()).tokenValiditySeconds(1209600));
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser(securityProperties.getUser().getName())
            .password(securityProperties.getUser().getPassword()).roles("USER");
    }

}

4、服务下线邮箱提醒

作为应用监控的工具,spring boot admin提供了在服务由于异常原因而掉线/宕机触发邮件提醒功能。

第一步:在admin-server端添加邮件发送工具的依赖

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

pom.xml

<?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.4.RELEASE</version>
        <relativePath />
    </parent>

    <groupId>cn.edu.sgu.www</groupId>
    <artifactId>springboot-admin-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
        <admin.version>2.3.1</admin.version>
    </properties>

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

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

        <!--邮件发送服务-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

        <!--spring security-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
    </dependencies>

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

第二步:配置邮箱

这里的发送邮箱需要开启smtp服务,具体操作可以参考博主的文章java实现一个简单的账号登录时的邮件通知功能icon-default.png?t=N7T8https://blog.csdn.net/heyl163_/article/details/132962539springboot-admin-server服务的application.yml

server:
  port: 8080

spring:
  application:
    name: admin-server
  security:
    user:
      name: user
      password: 12345
  boot:
    admin:
      notify:
        mail:
          from: xxxxx@xxx.com # 发送邮箱
          to: xxxxx@xxx.com # 接收邮箱
  mail:
    port: 25
    host: smtp.163.com # 邮箱对应的发送服务器,如@163.com邮箱的发送服务器为smtp.163.com
    default-encoding: UTF-8
    username: xxxxx@xxx.com # 开启smtp服务的邮箱
    password: xxxxxxxxxxxxx # 授权码
    properties:
      mail:
        debug: false
        smtp:
          auth: true
          ssl:
            trust: smtp.163.com
          starttls:
            enable: true
            required: true
          socketFactory:
            port: 465
            class: javax.net.ssl.SSLSocketFactory

至此,邮箱提醒设置完成~(下图为博主尝试主动关闭admin-client触发的邮箱提醒功能)

5、整合nacos注册中心

以springboot-admin-server为例,将其注册到nacos。

添加nacos相关依赖,注册中心和配置中心的依赖都加进来,统一管理配置信息。

<?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.4.RELEASE</version>
        <relativePath />
    </parent>

    <groupId>cn.edu.sgu.www</groupId>
    <artifactId>springboot-admin-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
        <admin.version>2.3.1</admin.version>
        <nacos.version>2.2.0.RELEASE</nacos.version>
    </properties>

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

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

        <!--邮件发送服务-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

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

        <!--nacos注册中心-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>${nacos.version}</version>
        </dependency>

        <!--nacos配置中心-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            <version>${nacos.version}</version>
        </dependency>
    </dependencies>

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

然后创建bootstrap.yml,添加nacos注册中心和配置中心配置

nacos:
  server: localhost:8848
  namespace: 0df4345c-cf1e-4af4-9501-d4be92ca6fda

spring:
  cloud:
    nacos:
      discovery:
        register-enabled: true
        server-addr: ${nacos.server}
        namespace: ${nacos.namespace}
      config:
        file-extension: yaml
        server-addr: ${nacos.server}
        namespace: ${nacos.namespace}

注意:需要配置spring.application.name的值,否则服务不会注册到nacos

启动nacos服务,然后再启动springboot-admin-server,在nacos控制台的服务列表,成功看到了注册进来的的springboot-admin-server服务

接着,通过同样的步骤把admin-client注册到nacos。

admin-server和admin-client注册到nacos注册中心之后,会自动拉取添加了spring-boot-starter-admin-client依赖的服务,所以需要删除客户端配置,否则将会有两个同一服务的实例注册到admin服务器。

修改springboot-admin-client服务的配置文件application.yml,删除之前添加的admin客户端配置。

server:
  port: 8081

spring:
  application:
    name: admin-client
#  boot:
#    admin:
#      client:
#        username: user
#        password: 12345
#        url: http://localhost:8080

management:
  endpoints:
    web:
      exposure:
        include: "*"
      base-path: /actuator
  endpoint:
    health:
      show-details: always

好了,以上就是本篇文章要分享的全部内容了,看完不要忘了点赞+收藏哦~

文章中涉及的项目均已上传到gitee,需要的可以下载到本地:

springboot整合actuator案例项目icon-default.png?t=N7T8https://gitee.com/he-yunlin/springboot-actuator.git

Spring Boot Admin Client案例项目icon-default.png?t=N7T8https://gitee.com/he-yunlin/springboot-admin-client.gitspring boot整合admin实现对应用监控服务器项目icon-default.png?t=N7T8https://gitee.com/he-yunlin/springboot-admin-server.git

 关于nacos如何使用,可以参考博主的另一篇文章:

nacos作为注册中心和配置中心icon-default.png?t=N7T8https://blog.csdn.net/heyl163_/article/details/128536799

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值