版本清单:
spring-boot-admin-starter-server 2.5.0
spring-boot-admin-starter-client 2.5.0
spring-boot-starter-actuator 2.5.2
springboot 2.5.4springcloud 2020.0.5
springcloud alibaba 2.2.6.RELEASE
sentinel 1.8.1
gateway 3.0.6
nacos 1.4.2
一、整合spring-boot-admin
1、创建admin-server服务
2、admin-server端引入依赖
<!--admin-->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.5.0</version>
</dependency>
<!--nacos-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
3、配置admin服务端信息
去nacos找注册的服务,本身不注册
server:
port: 8000
spring:
application:
name: amdin
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 #nacos地址
register-enabled: false
4、admin服务端启动类
启动当前服务前,先启动nacos
@SpringBootApplication
@EnableAdminServer //启动admin服务端
public class AdminApplication {
public static void main(String[] args) {
SpringApplication.run(AdminApplication.class,args);
}
}
二、整合spring-security配置一个admin服务端登陆页面
1、引入spring-security依赖
<!--security-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2、创建spring-security配置类
@Configuration
public class AdminSecurityConfig extends WebSecurityConfigurerAdapter {
private String adminServerPropertiesContextPath;
public AdminSecurityConfig( AdminServerProperties adminServerProperties){
this.adminServerPropertiesContextPath = adminServerProperties.getContextPath();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
SavedRequestAwareAuthenticationSuccessHandler handler = new SavedRequestAwareAuthenticationSuccessHandler();
handler.setTargetUrlParameter("redirectTo");
http.authorizeRequests()
.antMatchers(this.adminServerPropertiesContextPath + "/assets/**").permitAll()
.antMatchers(this.adminServerPropertiesContextPath + "/login").permitAll()
.anyRequest().authenticated()
.and().formLogin().loginPage(this.adminServerPropertiesContextPath + "/login")
.successHandler(handler)
.and().logout().logoutUrl(this.adminServerPropertiesContextPath + "/logout")
.and().httpBasic().and().csrf().disable();
}
}
3、配置spring-security登录账号密码
server:
port: 8000
spring:
application:
name: amdin
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
register-enabled: false
#添加security配置
security:
user:
name: admin #登录账号
password: Eshore@189cn #登录密码
4、启动访问并登录admin服务
三、配置微服务的admin-client端
1、微服务引入依赖
<!--actuator-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--admin-->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.5.0</version>
</dependency>
2、微服务配置admin-client配置信息
spring:
boot:
admin:
client:
username: admin #server端配置的账号
password: Eshore@189cn #server端配置的密码
url: http://127.0.0.1:8000 #admin-server服务信息
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 #微服务需要注册到nacos中,才能被admin检测到
management:
endpoints:
web:
exposure:
include: '*' #暴露所有路由规则
base-path: /actuator
3、启动服务
可以看见已在nacos中注册,并且admin已经检测到
nacos:
admin:
四、整合springbootmail实现离线警告
1、admin-server端添加mail依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2、配置mail信息
server:
port: 8000 #server端口
spring:
application:
name: amdin #server服务名
#mail
mail:
host: smtp.yeah.net #邮箱smtp地址
username: xxxxx@yeah.net #邮箱地址
password: XXXXXXXXXXXXXX #邮箱授权码
boot:
admin:
notify:
mail:
to: xxxxxxxxxx@qq.com #告警接收端
from: xxxxxxxxx@yeah.net #告警发送端
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
register-enabled: false
security:
user:
name: admin
password: Eshore@189cn
3、创建邮箱告警通知类NotificationConfiguration
@Configuration
public class NotificationConfiguration {
private InstanceRepository instanceRepository;
private ObjectProvider<List<Notifier>> provider;
public NotificationConfiguration(InstanceRepository instanceRepository,ObjectProvider<List<Notifier>> provider){
this.instanceRepository = instanceRepository;
this.provider = provider;
}
@Bean
public FilteringNotifier getFilterNotifier(){
CompositeNotifier compositeNotifier = new CompositeNotifier(this.provider.getIfAvailable(Collections::emptyList));
return new FilteringNotifier(compositeNotifier,this.instanceRepository);
}
@Bean
@Primary
public RemindingNotifier remindingNotifier(){
RemindingNotifier remindingNotifier = new RemindingNotifier(getFilterNotifier(), this.instanceRepository);
remindingNotifier.setReminderPeriod(Duration.ofMinutes(1)); //每一分钟提醒一次
remindingNotifier.setCheckReminderInverval(Duration.ofSeconds(10)); //每10s检查一次
return remindingNotifier;
}
}