本文將以Spring Boot Admin 2.0 與 Eureka整合展示Spring Boot Admin 2.0 的使用。
下面我介紹一下使用到的相關(guān)應(yīng)用:
服務(wù) 端口 服務(wù)說(shuō)明
ljx-eureka 1025 服務(wù)注冊(cè)與發(fā)現(xiàn)中心
ljx-user-service 8083 用戶微服務(wù)(監(jiān)控客戶端)
ljx-springcloud-sbaserver 8085 spring boot admin監(jiān)控服務(wù)(監(jiān)控服務(wù)端)
這里我主要講解spring boot admin服務(wù)端與客戶端的搭建,服務(wù)注冊(cè)發(fā)現(xiàn)的搭建不再做贅述。
服務(wù)端admin搭建
pom.xml
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.0.4</version>
</dependency>
<!--JMX-bean 管理-->
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui</artifactId>
<version>2.0.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!--排除tomcat依賴 -->
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<!--undertow容器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
啟動(dòng)類
@SpringBootApplication
@EnableAdminServer
@EnableEurekaClient
public class SbaServerApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(SbaServerApplication.class);
}
public static void main( String[] args )
{
SpringApplication.run(SbaServerApplication.class,args);
}
}
安全控制
這里我們給springBootAdmin增加一個(gè)安全控制悲龟,增加登錄頁(yè)面
/**
* 安全控制
*/
@Profile("secure")
@Configuration
public static class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
private final String adminContextPath;
public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
this.adminContextPath = adminServerProperties.getContextPath();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter("redirectTo");
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().disable();
// @formatter:on
}
}
bootstrap.yml配置
server:
port: 8085
servlet:
context-path: /sbaserver #給服務(wù)增加上下文 前綴
spring:
application:
name: ljx-springcloud-sbaserver
profiles:
active:
- secure
# 注冊(cè)中心配置
eureka:
instance:
leaseRenewalIntervalInSeconds: 10 #表示eureka client發(fā)送心跳給server端的頻率,默認(rèn)為30秒
prefer-ip-address: true
instance-id: ljx-springcloud-sbaserver8085
health-check-url-path: /sbaserver/actuator/health #健康檢查的地址(依賴spring-boot-starter-actuator)
client:
registryFetchIntervalSeconds: 5 #表示eureka client間隔多久去拉取服務(wù)注冊(cè)信息树碱,默認(rèn)為30秒
service-url:
defaultZone: http://jiaxing:jiaxing@localhost:1025/eureka/
# 暴露監(jiān)控端點(diǎn) springBootAdmin2 只暴露了兩個(gè)
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: ALWAYS #health endpoint是否必須顯示全部細(xì)節(jié)记罚。默認(rèn)情況下, /actuator/health 是公開(kāi)的,并且不顯示細(xì)節(jié)
---
spring:
profiles: insecure
---
#配置登錄名望迎,密碼和安全策略
spring:
profiles: secure
security:
user:
name: 'jiaxing'
password: 'jiaxing'
eureka:
instance:
metadata-map:
user.name: ${spring.security.user.name} #These two are needed so that the server
user.password: ${spring.security.user.password} #can access the protected client endpoints
啟動(dòng)
image.png
由上圖可看出服務(wù)均已注冊(cè)到eureka障癌,我們直接點(diǎn)開(kāi)(localhost:8085/sbaserver)
image.png
看到上圖說(shuō)明,服務(wù)端已配置成功辩尊。
客戶端搭建ljx-user-server
pom.xml
<!--監(jiān)控客戶端-->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.0.4</version>
</dependency>
<!--監(jiān)控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
bootstrap.yml配置
server:
port: 8082
spring:
application:
name: ljx-user-service
# 注冊(cè)中心配置
eureka:
instance:
metadata-map:
management:
context-path: /actuator
health-check-url: http://localhost:${server.port}/actuator/health
status-page-url: http://localhost:${server.port}/actuator/info
home-page-url: http://localhost:${server.port}
prefer-ip-address: true
instance-id: ljx-user-service8082
client:
service-url:
defaultZone: http://jiaxing:jiaxing@ljx-eureka:1025/eureka/
info:
version: 1.0.0
# 暴露監(jiān)控端點(diǎn)
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: ALWAYS
完成上面一系列配置涛浙,我們登錄后可以看到下面的頁(yè)面可以說(shuō)是配置部署成功了
在這里可以看到每個(gè)連接的時(shí)長(zhǎng),以及版本等
image.png
image.png
image.png
image.png