前面的文章我們講了Spring Boot的Actuator。但是Spring Boot Actuator只是提供了一個個的接口,需要我們自行集成到監控程序中。今天我們將會講解一個優秀的監控工具Spring Boot Admin。 它採用圖形化的界面,讓我們的Spring Boot管理更加簡單。
先上圖給大家看一下Spring Boot Admin的界面:
從界面上面我們可以看到Spring Boot Admin提供了眾多強大的監控功能。那麼開始我們的學習吧。
配置Admin Server
既然是管理程序,肯定有一個server,配置server很簡單,我們添加這個依賴即可:
<code><
dependency
><
groupId
>de.codecentricgroupId
><
artifactId
>spring-boot-admin-starter-serverartifactId
><
version
>2.2.2version
>dependency
> /<code>
同時我們需要在main程序中添加@EnableAdminServer來啟動admin server。
<code>@EnableAdminServer
@SpringBootApplication
public class SpringBootAdminServerApplication {public
static
void
main
(String[] args) {SpringApplication
.run
(SpringBootAdminServerApplication.class, args); } } /<code>
配置admin client
有了server,我們接下來配置需要監控的client應用程序,在本文中,我們自己監控自己,添加client依賴如下:
<code><
dependency
><
groupId
>de.codecentricgroupId
><
artifactId
> spring-boot-admin-starter-clientartifactId
><
version
>2.2.2version
>dependency
> /<code>
我們需要為client指定要註冊到的admin server:
<code>spring.boot.admin.client.url
=http://localhost:8080
/<code>
因為Spring Boot Admin依賴於 Spring Boot Actuator, 從Spring Boot2 之後,我們需要主動開啟暴露的主鍵,如下:
<code>management.endpoints.web.exposure.include
=*management.endpoint.health.show-details
=always /<code>
配置安全主鍵
通常來說,我們需要一個登陸界面,以防止未經授權的人訪問。spring boot admin提供了一個UI供我們使用,同時我們添加Spring Security依賴:
<code><
dependency
><
groupId
>de.codecentricgroupId
><
artifactId
>spring-boot-admin-server-ui-loginartifactId
><
version
>1.5.7version
>dependency
><
dependency
><
groupId
>org.springframework.bootgroupId
><
artifactId
>spring-boot-starter-securityartifactId
>dependency
> /<code>
添加了Spring Security,我們需要自定義一些配置:
<code>public
class
WebSecurityConfig
extends
WebSecurityConfigurerAdapter
{private
final
AdminServerProperties adminServer;public
WebSecurityConfig
(AdminServerProperties adminServer)
{this
.adminServer = adminServer; }protected
void
configure
(HttpSecurity http)
throws
Exception { SavedRequestAwareAuthenticationSuccessHandler successHandler =new
SavedRequestAwareAuthenticationSuccessHandler(); successHandler.setTargetUrlParameter("redirectTo"
); successHandler.setDefaultTargetUrl(this
.adminServer.getContextPath() +"/"
); http .authorizeRequests() .antMatchers(this
.adminServer.getContextPath() +"/assets/**"
).permitAll() .antMatchers(this
.adminServer.getContextPath() +"/login"
).permitAll() .anyRequest().authenticated() .and() .formLogin() .loginPage(this
.adminServer.getContextPath() +"/login"
) .successHandler(successHandler) .and() .logout() .logoutUrl(this
.adminServer.getContextPath() +"/logout"
) .and() .httpBasic() .and() .csrf() .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) .ignoringRequestMatchers(new
AntPathRequestMatcher(this
.adminServer.getContextPath() +"/instances"
, HttpMethod.POST.toString()),new
AntPathRequestMatcher(this
.adminServer.getContextPath() +"/instances/*"
, HttpMethod.DELETE.toString()),new
AntPathRequestMatcher(this
.adminServer.getContextPath() +"/actuator/**"
)) .and() .rememberMe() .key(UUID.randomUUID().toString()) .tokenValiditySeconds(1209600
); } } /<code>
接下來,我們在配置文件中指定服務器的用戶名和密碼:
<code>spring.boot.admin.client.username
=adminspring.boot.admin.client.password
=admin /<code>
作為一個客戶端,連接服務器的時候,我們也需要提供相應的認證信息如下:
<code>spring.boot.admin.client.instance.metadata.user.name
=adminspring.boot.admin.client.instance.metadata.user.password
=adminspring.boot.admin.client.username
=adminspring.boot.admin.client.password
=admin /<code>
好了,登錄頁面和權限認證也完成了。
Hazelcast集群
Spring Boot Admin 支持Hazelcast的集群,我們先添加依賴如下:
<code><
dependency
><
groupId
>com.hazelcastgroupId
><
artifactId
>hazelcastartifactId
><
version
>3.12.2version
>dependency
> /<code>
然後添加Hazelcast的配置:
<code>public
class
HazelcastConfig
{public
Confighazelcast
()
{ MapConfig eventStoreMap =new
MapConfig("spring-boot-admin-event-store"
) .setInMemoryFormat(InMemoryFormat.OBJECT) .setBackupCount(1
) .setEvictionPolicy(EvictionPolicy.NONE) .setMergePolicyConfig(new
MergePolicyConfig(PutIfAbsentMapMergePolicy.
class
.getName
(), 100)); MapConfig sentNotificationsMap =new
MapConfig("spring-boot-admin-application-store"
) .setInMemoryFormat(InMemoryFormat.OBJECT) .setBackupCount(1
) .setEvictionPolicy(EvictionPolicy.LRU) .setMergePolicyConfig(new
MergePolicyConfig(PutIfAbsentMapMergePolicy.
class
.getName
(), 100)); Config config =new
Config(); config.addMapConfig(eventStoreMap); config.addMapConfig(sentNotificationsMap); config.setProperty("hazelcast.jmx"
,"true"
); config.getNetworkConfig() .getJoin() .getMulticastConfig() .setEnabled(false
); TcpIpConfig tcpIpConfig = config.getNetworkConfig() .getJoin() .getTcpIpConfig(); tcpIpConfig.setEnabled(true
); tcpIpConfig.setMembers(Collections.singletonList("127.0.0.1"
));return
config; } } /<code>
歡迎關注我的公眾號:程序那些事,更多精彩等著您!
更多內容請訪問:flydean的博客 flydean.com
關鍵字: client artifactId adminServer