六、搭建springCloudAlibaba2021.1版本分布式微服务-admin监控中心

发布时间:2022-07-23 17:41:04 作者:yexindonglai@163.com 阅读(708)

前言

  Spring Boot Actuator 是 spring-boot 自带监控功能 ,可以帮助实现对程序内部运行情况监控,比如监控状况、Bean 加载情况、环境变量、日志信息、线程信息等。

Spring Boot Admin是一个针对 spring-boot 的 actuator 接口进行 UI 美化封装的监控工具。他可以:

  • 在列表中浏览所有被监控 spring-boot 项目的基本信息,
  • 详细的Health信息、内存信息、JVM 信息、垃圾回收信息、各种配置信息(比如数据源、缓存列表和命中率)等,
  • 还可以直接修改 logger 的 level。

搭建

创建子module spring-cloud-alibaba-2021-admin,pom.xml 内容如下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>spring-cloud-alibaba-2021</artifactId>
  7. <groupId>org.example</groupId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>spring-cloud-alibaba-2021-admin</artifactId>
  12. <packaging>pom</packaging>
  13. <modules>
  14. <module>admin-client</module>
  15. <module>admin-server</module>
  16. </modules>
  17. <properties>
  18. <admin.version>2.5.3</admin.version>
  19. </properties>
  20. <dependencyManagement>
  21. <dependencies>
  22. <!-- SpringBoot Admin 服务端 -->
  23. <dependency>
  24. <groupId>de.codecentric</groupId>
  25. <artifactId>spring-boot-admin-starter-server</artifactId>
  26. <version>${admin.version}</version>
  27. </dependency>
  28. <!-- SpringBoot Admin 客户端 -->
  29. <dependency>
  30. <groupId>de.codecentric</groupId>
  31. <artifactId>spring-boot-admin-starter-client</artifactId>
  32. <version>${admin.version}</version>
  33. </dependency>
  34. </dependencies>
  35. </dependencyManagement>
  36. <dependencies>
  37. <!-- web支持 -->
  38. <dependency>
  39. <groupId>org.springframework.boot</groupId>
  40. <artifactId>spring-boot-starter-web</artifactId>
  41. </dependency>
  42. </dependencies>
  43. </project>

创建二级子module

创建 监控中心服务端server

spring-cloud-alibaba-2021-admin 工程下在创建子module admin-server, pom.xml 内容如下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>spring-cloud-alibaba-2021-admin</artifactId>
  7. <groupId>org.example</groupId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>admin-server</artifactId>
  12. <dependencies>
  13. <!-- SpringBoot Admin 服务端依赖 -->
  14. <dependency>
  15. <groupId>de.codecentric</groupId>
  16. <artifactId>spring-boot-admin-starter-server</artifactId>
  17. </dependency>
  18. <!-- spring security -->
  19. <dependency>
  20. <groupId>org.springframework.boot</groupId>
  21. <artifactId>spring-boot-starter-security</artifactId>
  22. </dependency>
  23. <!-- nacos 服务注册发现(客户端)依赖 -->
  24. <dependency>
  25. <groupId>com.alibaba.cloud</groupId>
  26. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  27. </dependency>
  28. <!-- nacos-config 配置中心依赖 -->
  29. <dependency>
  30. <groupId>com.alibaba.cloud</groupId>
  31. <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
  32. </dependency>
  33. <!--spring-cloud-dependencies 2020.0.0 版本不在默认加载bootstrap.yml 文件,如果需要加载bootstrap 文件需要手动添加依赖-->
  34. <dependency>
  35. <groupId>org.springframework.cloud</groupId>
  36. <artifactId>spring-cloud-starter-bootstrap</artifactId>
  37. </dependency>
  38. </dependencies>
  39. </project>

application.yml 配置文件如下

  1. server:
  2. port: 7001
  3. spring:
  4. # 后面的bean会覆盖前面相同名称的bean
  5. main:
  6. allow-bean-definition-overriding: true
  7. # 安全认证
  8. security:
  9. user:
  10. # 账号
  11. name: admin
  12. # 密码
  13. password: 123456
  14. boot:
  15. admin:
  16. ui:
  17. # 网页标题
  18. title: spring-cloud-alibaba-2021微服务监控

bootstrap.yml配置文件内容如下

  1. spring:
  2. application:
  3. name: admin-server-demo
  4. profiles:
  5. active: yexindong_active
  6. nacos-server-addr: http://chn520.cn:8848
  7. cloud:
  8. nacos:
  9. discovery:
  10. server-addr: ${spring.profiles.nacos-server-addr} # 服务注册中心地址
  11. namespace: public # 注册到nacos的名称空间,默认为public
  12. config:
  13. # namespace: public # 如果是默认的public,则不需要指定名称空间,否则控制台会一直打印日志
  14. file-extension: yaml # 指定yaml格式的配置, 必须要放到bootstrao.yml 才会生效,放到application下不会生效
  15. server-addr: ${spring.profiles.nacos-server-addr} #配置中心地址
  16. group: DEFAULT_GROUP
  17. # 指定配置文件,可指定多个
  18. shared-configs[0]:
  19. data-id: spring-cloud-alibaba-config.yaml
  20. refresh: true # 是否自动刷新

监控中心服务端配置类WebSecurityConfig.java

  1. package com.alibaba.cloud.config;
  2. import de.codecentric.boot.admin.server.config.AdminServerProperties;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  5. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  6. import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
  7. /**
  8. * 监控权限配置
  9. *
  10. */
  11. @Configuration
  12. public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  13. /**
  14. * 上下文路径
  15. */
  16. private final String contextPath;
  17. /**
  18. * 构造函数
  19. * @param adminServerProperties
  20. */
  21. public WebSecurityConfig(AdminServerProperties adminServerProperties) {
  22. this.contextPath = adminServerProperties.getContextPath();
  23. }
  24. @Override
  25. protected void configure(HttpSecurity http) throws Exception {
  26. SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
  27. successHandler.setTargetUrlParameter("redirectTo");
  28. successHandler.setDefaultTargetUrl(contextPath + "/");
  29. http.headers().frameOptions().disable()
  30. .and().authorizeRequests()
  31. .antMatchers(contextPath + "/assets/**"
  32. , contextPath + "/login"
  33. , contextPath + "/actuator/**"
  34. , contextPath + "/instances/**"
  35. ).permitAll()
  36. .anyRequest().authenticated()
  37. .and()
  38. .formLogin().loginPage(contextPath + "/login")
  39. .successHandler(successHandler).and()
  40. .logout().logoutUrl(contextPath + "/logout")
  41. .and()
  42. .httpBasic().and()
  43. .csrf()
  44. .disable();
  45. }
  46. }

启动类 AdminServerApp.java

  1. package com.alibaba.cloud;
  2. import de.codecentric.boot.admin.server.config.EnableAdminServer;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. @SpringBootApplication
  6. @EnableAdminServer
  7. public class AdminServerApp {
  8. /**
  9. * 启动后访问地址: http://localhost:7001 即可查看监控信息
  10. * @param args
  11. */
  12. public static void main(String[] args) {
  13. SpringApplication.run(AdminServerApp.class, args);
  14. }
  15. }

启动main方法,在浏览器输入http://localhost:7001, 因为已经将当前服务注册到了nacos,而admin监控中心会自动去nacos取所有的服务地址,所以可以看到监控中心有自己的服务;

创建监控中心客户端 client

spring-cloud-alibaba-2021-admin 工程下在创建子module admin-client, pom.xml 内容如下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>spring-cloud-alibaba-2021-admin</artifactId>
  7. <groupId>org.example</groupId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>admin-client</artifactId>
  12. <dependencies>
  13. <!-- SpringBoot Admin 客户端 -->
  14. <dependency>
  15. <groupId>de.codecentric</groupId>
  16. <artifactId>spring-boot-admin-starter-client</artifactId>
  17. </dependency>
  18. <!-- SpringBoot Actuator 依赖,admin就是对Actuator封装了一层,增加了一些功能 -->
  19. <dependency>
  20. <groupId>org.springframework.boot</groupId>
  21. <artifactId>spring-boot-starter-actuator</artifactId>
  22. </dependency>
  23. <!-- nacos 服务注册发现(客户端)依赖 -->
  24. <dependency>
  25. <groupId>com.alibaba.cloud</groupId>
  26. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  27. </dependency>
  28. <!-- nacos-config 配置中心依赖 -->
  29. <dependency>
  30. <groupId>com.alibaba.cloud</groupId>
  31. <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
  32. </dependency>
  33. <!--spring-cloud-dependencies 2020.0.0 版本不在默认加载bootstrap.yml 文件,如果需要加载bootstrap 文件需要手动添加依赖-->
  34. <dependency>
  35. <groupId>org.springframework.cloud</groupId>
  36. <artifactId>spring-cloud-starter-bootstrap</artifactId>
  37. </dependency>
  38. </dependencies>
  39. </project>

application.yml内容如下

  1. server:
  2. port: 9001
  3. spring:
  4. boot:
  5. # admin: # 若已将服务注册到nacos,则这里不需要指定,监控中心会自动去nacos取服务列表
  6. # client:
  7. # # 监控中心地址
  8. # url:
  9. # - http://localhost:7001
  10. # 暴露我们的所有监控信息
  11. management:
  12. endpoints:
  13. web:
  14. exposure:
  15. include: '*'

bootstrap.yml 内容如下

  1. spring:
  2. application:
  3. name: admin-client-demo # 取pom文件中 artifactId 标签的值
  4. profiles:
  5. active: yexindong_active
  6. nacos-server-addr: http://chn520.cn:8848
  7. cloud:
  8. nacos:
  9. discovery:
  10. server-addr: ${spring.profiles.nacos-server-addr} # 服务注册中心地址
  11. namespace: public # 注册到nacos的名称空间,默认为public
  12. config:
  13. # namespace: public # 如果是默认的public,则不需要指定名称空间,否则控制台会一直打印日志
  14. file-extension: yaml # 指定yaml格式的配置, 必须要放到bootstrao.yml 才会生效,放到application下不会生效
  15. server-addr: ${spring.profiles.nacos-server-addr} #配置中心地址
  16. group: DEFAULT_GROUP
  17. # 指定配置文件,可指定多个
  18. shared-configs[0]:
  19. data-id: spring-cloud-alibaba-config.yaml
  20. refresh: true # 是否自动刷新

启动类 AdminClientApp.java

  1. package com.libaba.cloud;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class AdminClientApp {
  6. public static void main(String[] args) {
  7. SpringApplication.run(AdminClientApp.class, args);
  8. }
  9. }

启动 main 方法,然后再次访问 http://localhost:7001 ,监控中心显示如下,可以看到,已经对客户端进行监控了

接着我们点进去,就可以看到该服务的所有监控信息

项目结构图

整体项目结构如下

在来张展开后的结构图

关键字SpringCloud