电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

詳解用Spring Boot Admin來監控我們的微服務

瀏覽:61日期:2023-08-21 13:34:26

1.概述

Spring Boot Admin是一個Web應用程序,用于管理和監視Spring Boot應用程序。每個應用程序都被視為客戶端,并注冊到管理服務器。底層能力是由Spring Boot Actuator端點提供的。

在本文中,我們將介紹配置Spring Boot Admin服務器的步驟以及應用程序如何集成客戶端。

2.管理服務器配置

由于Spring Boot Admin Server可以作為servlet或webflux應用程序運行,根據需要,選擇一種并添加相應的Spring Boot Starter。在此示例中,我們使用Servlet Web Starter。

首先,創建一個簡單的Spring Boot Web應用程序,并添加以下Maven依賴項:

<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> <version>2.2.3</version></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency>

之后,@ EnableAdminServer將可用,因此我們將其添加到主類中,如下例所示:

@EnableAdminServer@SpringBootApplicationpublic class SpringBootAdminServerApplication { public static void main(String[] args) { SpringApplication.run(SpringBootAdminServerApplication.class, args); }}

至此,服務端就配置完了。

3.設置客戶端

要在Spring Boot Admin Server服務器上注冊應用程序,可以包括Spring Boot Admin客戶端或使用Spring Cloud Discovery(例如Eureka,Consul等)。

下面的例子使用Spring Boot Admin客戶端進行注冊,為了保護端點,還需要添加spring-boot-starter-security,添加以下Maven依賴項:

<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>2.2.3</version></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId></dependency>

接下來,我們需要配置客戶端說明管理服務器的URL。為此,只需添加以下屬性:

spring.boot.admin.client.url=http://localhost:8080

從Spring Boot 2開始,默認情況下不公開運行狀況和信息以外的端點,對于生產環境,應該仔細選擇要公開的端點。

management.endpoints.web.exposure.include=*management.endpoint.health.show-details=always

使執行器端點可訪問:

@Configurationpublic static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().anyRequest().permitAll() .and().csrf().disable(); }}

為了簡潔起見,暫時禁用安全性。

如果項目中已經使用了Spring Cloud Discovery,則不需要Spring Boot Admin客戶端。只需將DiscoveryClient添加到Spring Boot Admin Server,其余的自動配置完成。

下面使用Eureka做例子,但也支持其他Spring Cloud Discovery方案。

將spring-cloud-starter-eureka添加到依賴中:

<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency>

通過添加@EnableDiscoveryClient到配置中來啟用發現

@Configuration@EnableAutoConfiguration@EnableDiscoveryClient@EnableAdminServerpublic class SpringBootAdminApplication { public static void main(String[] args) { SpringApplication.run(SpringBootAdminApplication.class, args); } @Configuration public static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().anyRequest().permitAll() .and().csrf().disable(); } }}

配置Eureka客戶端:

eureka: instance: leaseRenewalIntervalInSeconds: 10 health-check-url-path: /actuator/health metadata-map: startup: ${random.int} #需要在重啟后觸發信息和端點更新 client: registryFetchIntervalSeconds: 5 serviceUrl: defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/management: endpoints: web: exposure: include: '*' endpoint: health: show-details: ALWAYS

4.安全配置

Spring Boot Admin服務器可以訪問應用程序的敏感端點,因此建議為admin 服務和客戶端應用程序添加一些安全配置。 由于有多種方法可以解決分布式Web應用程序中的身份驗證和授權,因此Spring Boot Admin不會提供默認方法。默認情況下spring-boot-admin-server-ui提供登錄頁面和注銷按鈕。

服務器的Spring Security配置如下所示:

@Configuration(proxyBeanMethods = false)public class SecuritySecureConfig extends WebSecurityConfigurerAdapter { private final AdminServerProperties adminServer; public SecuritySecureConfig(AdminServerProperties adminServer) { this.adminServer = adminServer; } @Override protected void configure(HttpSecurity http) throws Exception { SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler(); successHandler.setTargetUrlParameter('redirectTo'); successHandler.setDefaultTargetUrl(this.adminServer.path('/')); http.authorizeRequests( (authorizeRequests) -> authorizeRequests.antMatchers(this.adminServer.path('/assets/**')).permitAll() // 授予對所有靜態資產和登錄頁面的公共訪問權限 .antMatchers(this.adminServer.path('/login')).permitAll().anyRequest().authenticated() //其他所有請求都必須經過驗證 ).formLogin( (formLogin) -> formLogin.loginPage(this.adminServer.path('/login')).successHandler(successHandler).and() //配置登錄和注銷 ).logout((logout) -> logout.logoutUrl(this.adminServer.path('/logout'))).httpBasic(Customizer.withDefaults()) //啟用HTTP基本支持,這是Spring Boot Admin Client注冊所必需的 .csrf((csrf) -> csrf.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) //使用Cookies啟用CSRF保護 .ignoringRequestMatchers(new AntPathRequestMatcher(this.adminServer.path('/instances'), HttpMethod.POST.toString()), new AntPathRequestMatcher(this.adminServer.path('/instances/*'), HttpMethod.DELETE.toString()), //禁用Spring Boot Admin Client用于(注銷)注冊的端點的CSRF-Protectionnew AntPathRequestMatcher(this.adminServer.path('/actuator/**')) )) //對執行器端點禁用CSRF-Protection。 .rememberMe((rememberMe) -> rememberMe.key(UUID.randomUUID().toString()).tokenValiditySeconds(1209600)); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication().withUser('user').password('{noop}password').roles('USER'); }}

添加之后,客戶端無法再向服務器注冊。為了向服務器注冊客戶端,必須在客戶端的屬性文件中添加更多配置:

spring.boot.admin.client.username=adminspring.boot.admin.client.password=admin

當使用HTTP Basic身份驗證保護執行器端點時,Spring Boot Admin Server需要憑據才能訪問它們。可以在注冊應用程序時在元數據中提交憑據。在BasicAuthHttpHeaderProvider隨后使用該元數據添加Authorization頭信息來訪問應用程序的執行端點。也可以提供自己的屬性HttpHeadersProvider來更改行為(例如添加一些解密)或添加額外的請求頭信息。

使用Spring Boot Admin客戶端提交憑據:

spring.boot.admin.client: url: http://localhost:8080 instance: metadata: user.name: ${spring.security.user.name} user.password: ${spring.security.user.password}

使用Eureka提交憑據:

eureka: instance: metadata-map: user.name: ${spring.security.user.name} user.password: ${spring.security.user.password}

5.日志文件查看器

默認情況下,日志文件無法通過執行器端點訪問,因此在Spring Boot Admin中不可見。為了啟用日志文件執行器端點,需要通過設置logging.file.path或將Spring Boot配置為寫入日志文件 logging.file.name。

Spring Boot Admin將檢測所有看起來像URL的內容,并將其呈現為超鏈接。 還支持ANSI顏色轉義。因為Spring Boot的默認格式不使用顏色,可以設置一個自定義日志格式支持顏色。

logging.file.name=/var/log/sample-boot-application.log logging.pattern.file=%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID}){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wEx

6. 通知事項

郵件通知

郵件通知將作為使用Thymeleaf模板呈現的HTML電子郵件進行傳遞。要啟用郵件通知,請配置JavaMailSender使用spring-boot-starter-mail并設置收件人。

將spring-boot-starter-mail添加到依賴項中:

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId></dependency>

配置一個JavaMailSender

spring.mail.username=smtp_userspring.mail.password=smtp_passwordspring.boot.admin.notify.mail.to=admin@example.com

無論何時注冊客戶端將其狀態從“ UP”更改為“ OFFLINE”,都會將電子郵件發送到上面配置的地址。

自定義通知程序

可以通過添加實現Notifier接口的Spring Bean來添加自己的通知程序,最好通過擴展 AbstractEventNotifier或AbstractStatusChangeNotifier來實現。

public class CustomNotifier extends AbstractEventNotifier { private static final Logger LOGGER = LoggerFactory.getLogger(LoggingNotifier.class); public CustomNotifier(InstanceRepository repository) { super(repository); } @Override protected Mono<Void> doNotify(InstanceEvent event, Instance instance) { return Mono.fromRunnable(() -> { if (event instanceof InstanceStatusChangedEvent) { LOGGER.info('Instance {} ({}) is {}', instance.getRegistration().getName(), event.getInstance(), ((InstanceStatusChangedEvent) event).getStatusInfo().getStatus()); } else { LOGGER.info('Instance {} ({}) {}', instance.getRegistration().getName(), event.getInstance(), event.getType()); } }); }}

其他的一些配置參數和屬性可以通過官方文檔來了解。

到此這篇關于詳解用Spring Boot Admin來監控我們的微服務的文章就介紹到這了,更多相關Spring Boot Admin監控微服務內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: PCB接线端子_栅板式端子_线路板连接器_端子排生产厂家-置恒电气 喷码机,激光喷码打码机,鸡蛋打码机,手持打码机,自动喷码机,一物一码防伪溯源-恒欣瑞达有限公司 假肢-假肢价格-假肢厂家-河南假肢-郑州市力康假肢矫形器有限公司 | 纯化水设备-纯水设备-超纯水设备-[大鹏水处理]纯水设备一站式服务商-东莞市大鹏水处理科技有限公司 | 成都中天自动化控制技术有限公司| 升降机-高空作业车租赁-蜘蛛车-曲臂式伸缩臂剪叉式液压升降平台-脚手架-【普雷斯特公司厂家】 | 丁基胶边来料加工,医用活塞边角料加工,异戊二烯橡胶边来料加工-河北盛唐橡胶制品有限公司 | 重庆私家花园设计-别墅花园-庭院-景观设计-重庆彩木园林建设有限公司 | 生态板-实木生态板-生态板厂家-源木原作生态板品牌-深圳市方舟木业有限公司 | 股票入门基础知识_股票知识_股票投资大师_格雷厄姆网 | 专注氟塑料泵_衬氟泵_磁力泵_卧龙泵阀_化工泵专业品牌 - 梭川泵阀 | 上海小程序开发-上海小程序制作公司-上海网站建设-公众号开发运营-软件外包公司-咏熠科技 | 厌氧工作站-通用型厌氧工作站-上海胜秋科学仪器有限公司 | 镀锌钢格栅_热镀锌格栅板_钢格栅板_热镀锌钢格板-安平县昊泽丝网制品有限公司 | 超声波破碎仪-均质乳化机(供应杭州,上海,北京,广州,深圳,成都等地)-上海沪析实业有限公司 | 品牌设计_VI设计_电影海报设计_包装设计_LOGO设计-Bacross新越品牌顾问 | 小型数控车床-数控车床厂家-双头数控车床 | 储能预警-储能消防系统-电池舱自动灭火装置-四川千页科技股份有限公司官网 | 拖鞋定制厂家-品牌拖鞋代加工厂-振扬实业中国高端拖鞋大型制造商 | 金属检测机_金属分离器_检针验针机_食品药品金属检探测仪器-广东善安科技 | 扫地车厂家-山西洗地机-太原电动扫地车「大同朔州吕梁晋中忻州长治晋城洗地机」山西锦力环保科技有限公司 | 非标压力容器_碳钢储罐_不锈钢_搪玻璃反应釜厂家-山东首丰智能环保装备有限公司 | 奶茶加盟,奶茶加盟店连锁品牌-甜啦啦官网 | 英国雷迪地下管线探测仪-雷迪RD8100管线仪-多功能数字听漏仪-北京迪瑞进创科技有限公司 | 井式炉-台车式回火炉-丹阳市电炉厂有限公司 | 真空干燥烘箱_鼓风干燥箱 _高低温恒温恒湿试验箱_光照二氧化碳恒温培养箱-上海航佩仪器 | 连续油炸机,全自动油炸机,花生米油炸机-烟台茂源食品机械制造有限公司 | PE拉伸缠绕膜,拉伸缠绕膜厂家,纳米缠绕膜-山东凯祥包装 | 实战IT培训机构_IT培训班选大学生IT技术培训中心_中公优就业 | 亚克隆,RNAi干扰检测,miRNA定量检测-上海基屹生物科技有限公司 | 深圳APP开发_手机软件APP定制外包_小程序开发公司-来科信 | 三氯异氰尿酸-二氯-三氯-二氯异氰尿酸钠-优氯净-强氯精-消毒片-济南中北_优氯净厂家 | 亿立分板机_曲线_锯片式_走刀_在线式全自动_铣刀_在线V槽分板机-杭州亿协智能装备有限公司 | 阿尔法-MDR2000无转子硫化仪-STM566 SATRA拉力试验机-青岛阿尔法仪器有限公司 | 衡阳耐适防护科技有限公司——威仕盾焊接防护用品官网/焊工手套/焊接防护服/皮革防护手套 | elisa试剂盒-PCR试剂盒「上海谷研实业有限公司」 | 发光字|标识设计|标牌制作|精神堡垒 - 江苏苏通广告有限公司 | 钢格板|热镀锌钢格板|钢格栅板|钢格栅|格栅板-安平县昊泽丝网制品有限公司 | 油漆辅料厂家_阴阳脚线_艺术漆厂家_内外墙涂料施工_乳胶漆专用防霉腻子粉_轻质粉刷石膏-魔法涂涂 | 成都思迪机电技术研究所-四川成都思迪编码器 | 江苏远邦专注皮带秤,高精度皮带秤,电子皮带秤研发生产 | 杭州厂房降温,车间降温设备,车间通风降温,厂房降温方案,杭州嘉友实业爽风品牌 | 点胶机_点胶阀_自动点胶机_智能点胶机_喷胶机_点胶机厂家【欧力克斯】 |