基于单一应用(spring boot架构):
Admin Server端 第一步. 自动配置类
主启动类添加:@SpringBootApplication ,@EnableAdminServer
POM添加:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--监控服务包 spring-boot-admin服务端-->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.3.0</version>
</dependency>
<!--需要密码验证包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
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添加:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--spring-boot-admin使用暴露监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- admin-client 客户端-->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.1.6</version>
</dependency>
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添加:
<!-- nacos 配置中心 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<!-- nacos 发现服务 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<!--spring-boot-admin使用服务包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--监控服务包-->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.3.0</version>
</dependency>
<!--需要密码验证包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
一、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添加:
<!-- nacos 配置中心 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<!-- nacos 发现服务 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<!--spring-boot-admin使用服务包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
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: '*'