目录
Spring Boot Admin监控Spring Cloud服务结合Eureka注册中心
Spring Boot Admin是一个用于管理和监控Spring Boot应用程序的工具。它通过Spring Boot Admin Client通过HTTP注册,或者使用Spring Cloud如Eureka进行服务发现。其用户界面是一个基于AngularJs的Web应用,构建在Spring Boot Actuator端点之上,提供了一个简洁的可视化界面来管理Spring Boot应用程序,对Actuator暴露的管理端点进行格式化和展示。
AdminServer
创建工程AdminServer,添加依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
修改配置文件:
server:
port: 7013
spring:
application:
name: AdminServer
启动类添加@EnableAdminServer注解,启动就行了
AdminClient
创建工程AdminClient,添加依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
修改配置文件:
server:
port: 7014
spring:
application:
name: AdminClient
boot:
admin:
client:
url: http://127.0.0.1:7013
instance:
prefer-ip: true
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always
启动项目,访问http://127.0.0.1:7013/applications,出现如下界面:
添加认证
给AdminServer添加认证,添加依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
修改配置文件:
server:
port: 7013
spring:
application:
name: AdminServer
security:
user:
name: admin
password: public
修改启动类:
import de.codecentric.boot.admin.server.config.AdminServerProperties;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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;
@EnableAdminServer
@SpringBootApplication
public class AdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(AdminServerApplication.class, args);
}
@Configuration
public static class SecurityConfig extends WebSecurityConfigurerAdapter {
private final String adminContextPath;
public SecurityConfig(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 + "/");
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/**"
);
// @formatter:on
}
}
}
修改AdminClient:
server:
port: 7014
spring:
application:
name: AdminClient
boot:
admin:
client:
url: http://127.0.0.1:7013
instance:
prefer-ip: true
username: admin
password: public
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always
这是监控界面需要认证才能进入:
Spring Boot Admin监控Spring Cloud服务结合Eureka注册中心
当我们监控微服务的时候,服务数量众多,我们肯定想统一管理微服务,我可以将服务全部注册到注册中心上,admin会自己拉取Eureka上注册的应用信息,主动去注册。这也是唯一区别之前手动注册(SBA连接方式)的地方,就是client端不需要admin-client的依赖,也不需要配置admin地址了,一切全部由admin-server自己实现。这样的设计对环境变化很友好,不用改了admin-server后去改所有应用的配置了。
给AdminServer和AdminClient添加依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
启动类加@EnableEurekaClient注解
AdminClient配置文件添加:
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:7001/eureka
instance:
lease-renewal-interval-in-seconds: 1 #间隔1s,向服务端发送一次心跳,证明自己依然“存活”
lease-expiration-duration-in-seconds: 2 # 告诉服务端,如果我2s之内没有给你发心跳,就代表我“死”了,请将我踢掉
AdminClient配置文件添加:
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:7001/eureka
instance:
lease-renewal-interval-in-seconds: 1 #间隔1s,向服务端发送一次心跳,证明自己依然“存活”
lease-expiration-duration-in-seconds: 2 # 告诉服务端,如果我2s之内没有给你发心跳,就代表我“死”了,请将我踢掉
metadata-map:
user.name: ${spring.security.user.name}
user.password: ${spring.security.user.password}
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always
启动三个工程,可以看到监控界面可以监控AdminServer本身,还能选择监控的项目:
标签:Admin,Spring,boot,springframework,admin,Boot,spring,org From: https://blog.csdn.net/weixin_55987175/article/details/145227786