🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] ## 服务监控 微服务中,我们需要对我们对服务进行监控管理,这里使用Spring Boot Admin ## 配置xml 主要就是加入security和服务发现和SpringBoot Admin ~~~ <dependencies> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> </dependency> <dependency> <groupId>cn.xiejx</groupId> <artifactId>jfun-common-springboot-starter</artifactId> <exclusions> <exclusion> <groupId>cn.xiejx</groupId> <artifactId>jfun-db-springboot-starter</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.jolokia</groupId> <artifactId>jolokia-core</artifactId> <version>1.5.0</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-security</artifactId> </dependency> <!--nacos 配置中心https://www.jianshu.com/p/3750b7be331f--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <!--nacos 服务发现--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> </dependencies> ~~~ ## 配置security ~~~ @Configuration @EnableConfigurationProperties(PermitUrlProperties.class) public class SecuritySecureConfig extends WebSecurityConfigurerAdapter { @Autowired private PermitUrlProperties permitUrlProperties; @Override public void configure(WebSecurity web) throws Exception { web.ignoring().antMatchers(permitUrlProperties.getIgnored()); } @Override protected void configure(HttpSecurity http) throws Exception { // @formatter:off SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler(); successHandler.setTargetUrlParameter("redirectTo"); successHandler.setDefaultTargetUrl("/"); http.authorizeRequests() .antMatchers("/assets/**").permitAll()//Grants public access to all static assets and the login page. .antMatchers("/login").permitAll() .anyRequest().authenticated()// Every other request must be authenticated. .and() .formLogin().loginPage("/login").successHandler(successHandler).and()//Configures login and logout. .logout().logoutUrl("/logout").and() .httpBasic().and()//Enables HTTP-Basic support. This is needed for the Spring Boot Admin Client to register. .csrf() .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())// Enables CSRF-Protection using Cookies .ignoringAntMatchers( "/instances",// Disables CRSF-Protection the endpoint the Spring Boot Admin Client uses to register. "/actuator/**"//Disables CRSF-Protection for the actuator endpoints. ); } } ~~~ ## 启动类 ~~~ @EnableAdminServer @EnableDiscoveryClient @SpringBootApplication public class MonitorAdminApp { public static void main(String[] args) { //https://www.vojtechruzicka.com/spring-boot-admin/ SpringApplication.run(MonitorAdminApp.class,args); } } ~~~ ## nacos服务设置 ``` spring: boot: admin: notify: mail: from: "Spring Boot Admin <787824374@qq.com>" to: 787824374@qq.com,254191036@qq.com routes: endpoints: env,metrics,dump,jolokia,info,configprops,trace,logfile,refresh,flyway,liquibase,heapdump,loggers,auditevents,hystrix.stream turbine: clusters: default location: turbine mail: host: smtp.qq.com username: 787824374@qq.com password:     properties:       mail.debug: false       mail.smtp.auth: true       mail.smtp.port: 465            mail.smtp.ssl.enable: true       mail.smtp.ssl.socketFactory: sf management: endpoints: web: exposure: include: "*" endpoint: health: show-details: always ``` ## bootstrap.yml配置 ~~~ spring: security: user: password: jfun name: jfun profiles: active: dev application: name: MONITOR-ADMIN cloud: nacos: config: server-addr: ${jfun.nacos-addr} file-extension: yml prefix: monitor-admin discovery: server-addr: ${jfun.nacos-addr} server: port: 9007 jfun: nacos-addr: 127.0.0.1:8848 ~~~ ## 效果 使用用户名:jfun 密码:jfun登陆 ![](https://box.kancloud.cn/f13e1e34d7c33bf8f978d7b7730688e5_2634x1344.jpg) ![](https://box.kancloud.cn/a8866f705554c9a8ec6623d429952aef_2878x1426.jpg) ![](https://box.kancloud.cn/4572f8c70a1db65e9677de3872f82288_2880x1486.jpg) ## 遗留问题 邮件通知发送失败