SpringBoot-Admin(服务监控,基于nacos实现) 详解

SpringBoot-Admin(服务监控,基于nacos实现) 详解

1.服务端

1.1导pom.xml包

	   <dependency>
            <groupId>de.codecentricgroupId>
            <artifactId>spring-boot-admin-starter-serverartifactId>
        dependency>
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-securityartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

1.2application.yml配置文件

server:
  port: 1010

spring:
  application:
    name: admin-server
  main:
    allow-bean-definition-overriding: true
  cloud:
    nacos:
      discovery:
        server-addr: 1.1.1.1:8848 	#注册中心地址
        metadata:
          user.name: "admin"
          user.password: "123456"
        group: DEFAULT_GROUP
  security:
    user:
      name: "admin"
      password: "123456" #springboot-admin密码
management:
  health:
    redis:
      enabled: false #关闭redis健康检查
    sentinel:
      enabled: false #关闭sentinel健康检查
    ldap:
      enabled: false #关闭ldap健康检查
  endpoints:
    web:
      exposure:
        include: '*'
    health:
      sensitive: false #关闭过滤敏感信息
  endpoint:
    health:
      show-details: ALWAYS  #显示详细信息


1.3启动类

/**
 * @Author: dengx
 * @Date: 2021/5/25
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableAdminServer
public class AdminServerApplication {

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

1.4配置密码(可以不配置,为了安全最好配置下)

package com.es.security;

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;
 
/**
 * @author dengx
 */
@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 {
        // 登录成功处理类
        SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
        successHandler.setTargetUrlParameter("redirectTo");
        successHandler.setDefaultTargetUrl(adminContextPath + "/");
 
        http.authorizeRequests()
                //静态文件允许访问
                .antMatchers(adminContextPath + "/assets/**").permitAll()
                //登录页面允许访问
                .antMatchers(adminContextPath + "/login","/css/**","/js/**","/image/*").permitAll()
                //其他所有请求需要登录
                .anyRequest().authenticated()
                .and()
                //登录页面配置,用于替换security默认页面
                .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
                //登出页面配置,用于替换security默认页面
                .logout().logoutUrl(adminContextPath + "/logout").and()
                .httpBasic().and()
                .csrf()
                .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                .ignoringAntMatchers(
                        "/instances",
                        "/actuator/**"
                );
    }
}

1.5启动如图(服务端配置已完成)

SpringBoot-Admin(服务监控,基于nacos实现) 详解_第1张图片
SpringBoot-Admin(服务监控,基于nacos实现) 详解_第2张图片

2.客服端(其他需要管理的微服务)

2.1导包

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

2.2配置yml

management:
  health:
    redis:
      enabled: false #关闭redis健康检查
    sentinel:
      enabled: false #关闭sentinel健康检查
    ldap:
      enabled: false #关闭ldap健康检查
  endpoints:
    web:
      exposure:
        include: '*'
    health:
      sensitive: false #关闭过滤敏感信息
  endpoint:
    health:
      show-details: ALWAYS  #显示详细信息

你可能感兴趣的:(java,spring,boot)