Spring Boot Admin + 服务注册中心(Eureka)+ 监控所有微服务

Spring Boot Admin + 服务注册中心(Eureka)+ 监控所有微服务

  • 创建Spring Boot Admin服务端
    • pom.xml配置
    • yml配置
    • Application.java
    • 添加权限
    • 创建默认用户
    • 启动Spring Boot Admin服务端
  • 配置Spring Boot Admin客户端
    • pom.xml增加spring-boot-admin-starter-client依赖
    • yml文件增加Spring Boot Admin服务端地址和授权
    • 重启服务注册中心

创建Spring Boot Admin服务端

在 Spring Boot 项目中,Spring Boot Admin 作为 Server 端,其他的要被监控的应用作为 Client 端

pom.xml配置


<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-clound-learningartifactId>
        <groupId>com.jigroupId>
        <version>1.0.0version>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>admin-serverartifactId>
    <name>服务监控中心name>

    <dependencies>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-securityartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-actuatorartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
        dependency>
        
        <dependency>
            <groupId>de.codecentricgroupId>
            <artifactId>spring-boot-admin-starter-serverartifactId>
        dependency>
        <dependency>
            <groupId>de.codecentricgroupId>
            <artifactId>spring-boot-admin-server-uiartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webfluxartifactId>
        dependency>
        <dependency>
            <groupId>org.jolokiagroupId>
            <artifactId>jolokia-coreartifactId>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>build-infogoal>
                        goals>
                    execution>
                executions>
            plugin>
        plugins>
    build>
project>

yml配置

server:
  port: 8888
info:
  groupId: @project.groupId@
  artifactId: @project.artifactId@
  version: @project.version@
spring:
  security:
    # 登陆所需的账号密码
    user:
      name: admin
      password: admin
  application:
    name: spring-boot-admin-server
  boot:
    admin:
      ui:
        title: 服务监控中心
eureka:
  instance:
    hostname: localhost
    prefer-ip-address: true
    lease-renewal-interval-in-seconds: 10
    metadata-map:
      user:
        name: admin
        password: admin
  client:
    registry-fetch-interval-seconds: 5
    registerWithEureka: true
    fetchRegistry: true
    serviceUrl:
      defaultZone: http://localhost:1111/eureka/
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: always

Application.java

@SpringBootApplication
@EnableEurekaClient
@EnableAdminServer
public class Application {

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

添加权限

package com.ji;

import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.context.annotation.Configuration;
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;

@Configuration
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
    private final String adminContextPath;
 
    public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
        this.adminContextPath = adminServerProperties.getContextPath();
    }
 
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // @formatter:off
        SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
        successHandler.setTargetUrlParameter("redirectTo");
        successHandler.setDefaultTargetUrl(adminContextPath + "/monitor");
 
        http.authorizeRequests()
                .antMatchers(adminContextPath + "/assets/**").permitAll()
                .antMatchers(adminContextPath + "/login").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
                .logout().logoutUrl(adminContextPath + "/logout").and()
                .httpBasic().and()
                .csrf()
                .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                .ignoringAntMatchers(
                        adminContextPath + "/instances",
                        adminContextPath + "/actuator/**"
                );
    }
}

创建默认用户

@EnableWebSecurity
public class WebSecurityConfig implements WebMvcConfigurer {
 
    @Bean
    public UserDetailsService userDetailsService() throws Exception {
        InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
        PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
        manager.createUser(User.withUsername("admin").password(encoder.encode("admin")).roles("administrator").build());
        return manager;
    }
}

启动Spring Boot Admin服务端

打开浏览器输入http://localhost:8888/,首先进入的是登陆界面,输入用户/密码 admin/admin,即可进入监控页面
Spring Boot Admin + 服务注册中心(Eureka)+ 监控所有微服务_第1张图片

配置Spring Boot Admin客户端

这里我没有在实际监控的应用端配置Spring Boot Admin客户端,我只在服务注册中心配置Spring Boot Admin客户端,其它应用端注册到服务注册中心,也能在Spring Boot Admin监控中心看到。
所以我在 Spring Cloud 基于Spring Boot 2.0.6的服务注册与发现(Eureka)创建的服务注册中心修改pom文件和yml文件后,重启了下服务注册中心,修改结果如下

pom.xml增加spring-boot-admin-starter-client依赖

<dependency>
	<groupId>de.codecentricgroupId>
	<artifactId>spring-boot-admin-starter-clientartifactId>
dependency>

yml文件增加Spring Boot Admin服务端地址和授权

spring:
  boot:
    admin:
      client:
        url: http://localhost:8888
        username: admin
        password: admin

重启服务注册中心

刷新Spring Boot Admin服务端管理页面即可监控到所有注册到服务中心的所以应用端程序
Spring Boot Admin + 服务注册中心(Eureka)+ 监控所有微服务_第2张图片

你可能感兴趣的:(SpringCloud)