- Triển khai dịch vụ quản trị:
Tạo dự án Spring Boot phiên bản 2.3.7.RELEASE với các phụ thuộc sau:
<!-- Module quản trị -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>
<!-- Framework bảo mật -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- Gửi thông báo qua email -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
Khai báo annotation khởi tạo:
@EnableAdminServer
@SpringBootApplication
public class AdminServiceApplication {
public static void main(String[] args) {
SpringApplication.run(AdminServiceApplication.class, args);
}
}
Cấu hình YAML:
# Cổng kết nối dịch vụ<br></br><br></br>server.port: 9999
spring:
application:
name: admin-service
# ############## Cấu hình quản trị ##############
boot:
admin:
notify:
mail:
to: admin@example.com
monitor:
info-interval: 5s
info-lifetime: 5s
status-interval: 5s
status-lifetime: 5s
# ############## Cấu hình bảo mật ###############
security:
user:
name: manager
password: secure@2023
# ############## Cấu hình email ##############
mail:
host: smtp.gmail.com
username: notification@gmail.com
password:
Lớp cấu hình bảo mật:
@Configuration(proxyBeanMethods = false)
class SecuritySetup extends WebSecurityConfigurerAdapter {
private final AdminServerProperties adminProps;
private final SecurityProperties securityProps;
public SecuritySetup(AdminServerProperties adminProps, SecurityProperties securityProps) {
this.adminProps = adminProps;
this.securityProps = securityProps;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
SavedRequestAwareAuthenticationSuccessHandler handler = new SavedRequestAwareAuthenticationSuccessHandler();
handler.setTargetUrlParameter("redirect");
handler.setDefaultTargetUrl(adminProps.path("/"));
http.authorizeRequests(auth -> auth
.antMatchers(adminProps.path("/assets/**")).permitAll()
.antMatchers(adminProps.path("/actuator/health")).permitAll()
.antMatchers(adminProps.path("/login")).permitAll()
.anyRequest().authenticated())
.formLogin(form -> form
.loginPage(adminProps.path("/login")).successHandler(handler))
.logout(logout -> logout.logoutUrl(adminProps.path("/logout")))
.httpBasic(Customizer.withDefaults())
.csrf(csrf -> csrf
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.ignoringRequestMatchers(
new AntPathRequestMatcher(adminProps.path("/instances"), HttpMethod.POST.toString()),
new AntPathRequestMatcher(adminProps.path("/instances/*"), HttpMethod.DELETE.toString()),
new AntPathRequestMatcher(adminProps.path("/actuator/**"))
))
.rememberMe(remember -> remember
.key(UUID.randomUUID().toString())
.tokenValiditySeconds(1209600));
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser(securityProps.getUser().getName())
.password("{noop}" + securityProps.getUser().getPassword()).roles("ADMIN");
}
}
- Cấu hình ứng dụng được giám sát
Thêm phụ thuộc vào ứng dụng cần theo dõi:
<!-- Module giám sát -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- Client quản trị -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
Cấu hình YAML:
spring:<br></br># ############# Cấu hình quản trị #############<br></br> boot:<br></br> admin:<br></br> client:<br></br># ############ Địa chỉ và thông tin đăng nhập ############<br></br> url: http://localhost:9999<br></br> username: manager<br></br> password: secure@2023<br></br> instance:<br></br> prefer-ip: true
# Cấu hình endpoint giám sát
management:
endpoints:
web:
exposure:
# ############## Mở tất cả endpoint ##############
include: '*'
endpoint:
health:
# ############## Hiển thị chi tiết tình trạng ##############
show-details: always
-
Triển khai qua trung tâm đăng ký dịch vụ: Admin server tự động lấy thông tin từ trung tâm đăng ký
-
Cài đặt trung tâm đăng ký (ví dụ sử dụng Eureka):
Thêm phụ thuộc:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
Cấu hình YAML:
spring:
application:
name: service-registry
server:
port: 8761
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
register-with-eureka: false
fetch-registry: false
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
- Cấu hình admin-server
Thêm phụ thuộc:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
Cập nhật YAML:
eureka:
client:
registryFetchIntervalSeconds: 5
service-url:
defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
instance:
leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health<br></br> metadata-map: <br></br> user.name: ${spring.security.user.name} <br></br> user.password: ${spring.security.user.password}
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
Thêm annotation vào lớp khởi tạo:
@EnableDiscoveryClient
@EnableAdminServer
@SpringBootApplication
public class AdminServiceApplication {
public static void main(String[] args) {
SpringApplication.run(AdminServiceApplication.class, args);
}
}