spring cloud 基于注册中心 的spring admin监控,并设置密码 nacos为例,spring boot架构

基于单一应用(spring boot架构):

Admin Server端 第一步. 自动配置类

主启动类添加:@SpringBootApplication ,@EnableAdminServer

POM添加:

        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            de.codecentric
            spring-boot-admin-starter-server
            2.3.0
        

        
        
            org.springframework.boot
            spring-boot-starter-security
        

SpringBootAdmin 登录鉴权使用同下面的一样

application.yml 配置

server:
  port: 8777

spring:
  application:
    name: spring-boot-admin

  security:
    basic:
      enabled: true
    user:
      name: admin
      password: root

Admin Client端

主启动类添加:@SpringBootApplication 

POM添加:

        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            org.springframework.boot
            spring-boot-starter-actuator
        

        
        
            de.codecentric
            spring-boot-admin-starter-client
            2.1.6
        

application.yml 配置

server:
  port: 8770

spring:
  application:
    name: web-api

  boot:
    admin:
      client:
        url: http://127.0.0.1:8777
        username: admin
        password: root

## 暴露监控端点 spring boot admin 使用的
management:
  endpoints:
    web:
      base-path: /actuator
      exposure:
        include: '*'

注意:连接的地址和需要密码验证时用户名和密码要相同

基于注册中心(spring cloud微服务架构):

Admin Server端 第一步. 自动配置类

主启动类添加:@SpringCloudApplication ,@EnableAdminServer

spring-boot-admi服务 POM添加:
          
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-config
            2.1.2.RELEASE
        

        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
            2.1.2.RELEASE
        

      
        
            org.springframework.boot
            spring-boot-starter-actuator
        

        
        
            de.codecentric
            spring-boot-admin-starter-server
            2.3.0
        

        
        
            org.springframework.boot
            spring-boot-starter-security
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        

 一、SpringBootAdmin 登录鉴权使用

package com.wang.springbootadmin.config;

import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;

/**
 * SpringBootAdmin 登录鉴权使用
 *
 */
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    private final String contextPath;

    public WebSecurityConfig(AdminServerProperties adminServerProperties) {
        this.contextPath = adminServerProperties.getContextPath();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 跨域设置,SpringBootAdmin客户端通过instances注册,见InstancesController
        http.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                .ignoringAntMatchers(contextPath + "/instances");

        http.authorizeRequests().antMatchers(contextPath + "/assets/**").permitAll(); // 静态资源
        http.authorizeRequests().antMatchers(contextPath + "/actuator/**").permitAll(); // 自身监控
        http.authorizeRequests().anyRequest().authenticated(); // 所有请求必须通过认证

        // 整合spring-boot-admin-server-ui
        http.formLogin().loginPage("/login").permitAll();
        http.logout().logoutUrl("/logout").logoutSuccessUrl("/login");

        // 启用basic认证,SpringBootAdmin客户端使用的是basic认证
        http.httpBasic();
    }
}

 bootstrap.yml配置

server:
  port: 9091

spring:
  application:
    name: spring-boot-admin

  security:
    basic:
      enabled: true
    user:
      name: admin
      password: root

  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #nacos注册地址
        group: ONE_GROUP  #注册组 (服务组管理,不强制)
      config:
        server-addr: localhost:8848 #nacos注册地址
        file-extension: yaml #配置文件后缀支持 txt json yam xml html Properties 
        group: ONE_GROUP # 分组 (配置组管理,不强制)
        prefix: nocas_pubilc


## 暴露监控端点 spring boot admin 使用的
management:
  endpoints:
    web:
      base-path: /actuator
      exposure:
        include: '*'

Admin Client端 第二步. 其它服务

POM添加:

     
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-config
            2.1.2.RELEASE
        

        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
            2.1.2.RELEASE
        

      
        
            org.springframework.boot
            spring-boot-starter-actuator
        

bootstrap.yml配置

server:
  port: 9091

spring:
  application:
    name: spring-boot
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #nacos注册地址
        group: ONE_GROUP  #注册组 (服务组管理,不强制)
      config:
        server-addr: localhost:8848 #nacos注册地址
        file-extension: yaml #配置文件后缀支持 txt json yam xml html Properties 
        group: ONE_GROUP # 分组 (配置组管理,不强制)
        prefix: nocas_pubilc


## 暴露监控端点 spring boot admin 使用的
management:
  endpoints:
    web:
      base-path: /actuator
      exposure:
        include: '*'

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