物联网架构成长之路(30)-Spring Boot Admin微服务WebUI监控
Posted 无脑仔的小明
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了物联网架构成长之路(30)-Spring Boot Admin微服务WebUI监控相关的知识,希望对你有一定的参考价值。
0. 前言
一个完整的微服务解决方案包含了许多微服务,基于我们需要观察各个微服务的运行状态,因此Spring Boot 生态提供了Spring Boot Admin 这个组件来实现微服务管理WEB UI。但是整体的注册中心还是基于Eureka,只是WebUI是用这个Spring Boot Admin 来显示而已。具体的结构如下所示
1. Eureka服务
这个没什么好说的,按照创建一个微服务的流程,通过 Spring Starter 工具,自动生成一个Eureka微服务。主要就是配置Application.java application.yml pom.xml 这三个文件。
pom.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 6 <groupId>com.wunaozai.eureka</groupId> 7 <artifactId>global-service-eureka</artifactId> 8 <version>0.0.1</version> 9 <packaging>jar</packaging> 10 11 <name>global-service-eureka</name> 12 <description>服务注册中心</description> 13 14 <parent> 15 <groupId>org.springframework.boot</groupId> 16 <artifactId>spring-boot-starter-parent</artifactId> 17 <version>2.1.0.RELEASE</version> 18 <relativePath/> <!-- lookup parent from repository --> 19 </parent> 20 21 <properties> 22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 23 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 24 <java.version>1.8</java.version> 25 <spring-cloud.version>Greenwich.M2</spring-cloud.version> 26 </properties> 27 28 <dependencies> 29 <dependency> 30 <groupId>org.springframework.cloud</groupId> 31 <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> 32 </dependency> 33 34 <dependency> 35 <groupId>org.springframework.boot</groupId> 36 <artifactId>spring-boot-starter-test</artifactId> 37 <scope>test</scope> 38 </dependency> 39 </dependencies> 40 41 <dependencyManagement> 42 <dependencies> 43 <dependency> 44 <groupId>org.springframework.cloud</groupId> 45 <artifactId>spring-cloud-dependencies</artifactId> 46 <version>${spring-cloud.version}</version> 47 <type>pom</type> 48 <scope>import</scope> 49 </dependency> 50 </dependencies> 51 </dependencyManagement> 52 53 <build> 54 <plugins> 55 <plugin> 56 <groupId>org.springframework.boot</groupId> 57 <artifactId>spring-boot-maven-plugin</artifactId> 58 </plugin> 59 </plugins> 60 </build> 61 62 <repositories> 63 <repository> 64 <id>spring-milestones</id> 65 <name>Spring Milestones</name> 66 <url>https://repo.spring.io/milestone</url> 67 <snapshots> 68 <enabled>false</enabled> 69 </snapshots> 70 </repository> 71 </repositories> 72 73 </project>
application.yml 这里我配置两份,一份EUREKA1:8761 一份EUREKA:8762
1 server: 2 port: 8761 3 4 spring: 5 application: 6 name: EUREKA服务注册中心 7 8 management: 9 endpoints: 10 web: 11 exposure: 12 include: 13 - "*" 14 endpoint: 15 health: 16 show-details: ALWAYS 17 18 eureka: 19 client: 20 # register-with-eureka: false 21 # fetch-registry: false 22 service-url: 23 defaultZone: http://EUREKA1:8761/eureka/,http://EUREKA2:8762/eureka/ 24 instance: 25 hostname: EUREKA1
Application.java
1 package com.wunaozai.eureka; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; 6 7 @EnableEurekaServer 8 @SpringBootApplication 9 public class GlobalServiceEurekaApplication { 10 11 public static void main(String[] args) { 12 SpringApplication.run(GlobalServiceEurekaApplication.class, args); 13 } 14 }
2. Spring Boot Admin 服务
默认是不需要帐号密码登录的,我这里配置一下spring-boot-starter-security 增加帐号密码登录
pom.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-parent</artifactId> 8 <version>2.1.2.RELEASE</version> 9 <relativePath/> <!-- lookup parent from repository --> 10 </parent> 11 <groupId>com.wunaozai.admin.demo</groupId> 12 <artifactId>spring-cloud-admin-demo</artifactId> 13 <version>0.0.1</version> 14 <name>spring-cloud-admin-demo</name> 15 <description>Demo project for Spring Boot</description> 16 17 <properties> 18 <java.version>1.8</java.version> 19 <spring-boot-admin.version>2.1.1</spring-boot-admin.version> 20 <spring-cloud.version>Finchley.RELEASE</spring-cloud.version> 21 </properties> 22 23 <dependencies> 24 <dependency> 25 <groupId>de.codecentric</groupId> 26 <artifactId>spring-boot-admin-starter-server</artifactId> 27 </dependency> 28 <dependency> 29 <groupId>de.codecentric</groupId> 30 <artifactId>spring-boot-admin-starter-client</artifactId> 31 </dependency> 32 <dependency> 33 <groupId>org.springframework.cloud</groupId> 34 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 35 </dependency> 36 <dependency> 37 <groupId>org.springframework.cloud</groupId> 38 <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> 39 </dependency> 40 41 <dependency> 42 <groupId>org.springframework.boot</groupId> 43 <artifactId>spring-boot-starter-security</artifactId> 44 </dependency> 45 46 47 <dependency> 48 <groupId>org.springframework.boot</groupId> 49 <artifactId>spring-boot-devtools</artifactId> 50 <scope>runtime</scope> 51 </dependency> 52 <dependency> 53 <groupId>org.springframework.boot</groupId> 54 <artifactId>spring-boot-starter-test</artifactId> 55 <scope>test</scope> 56 </dependency> 57 </dependencies> 58 59 <dependencyManagement> 60 <dependencies> 61 <dependency> 62 <groupId>de.codecentric</groupId> 63 <artifactId>spring-boot-admin-dependencies</artifactId> 64 <version>${spring-boot-admin.version}</version> 65 <type>pom</type> 66 <scope>import</scope> 67 </dependency> 68 <dependency> 69 <groupId>org.springframework.cloud</groupId> 70 <artifactId>spring-cloud-dependencies</artifactId> 71 <version>${spring-cloud.version}</version> 72 <type>pom</type> 73 <scope>import</scope> 74 </dependency> 75 </dependencies> 76 </dependencyManagement> 77 78 <build> 79 <plugins> 80 <plugin> 81 <groupId>org.springframework.boot</groupId> 82 <artifactId>spring-boot-maven-plugin</artifactId> 83 </plugin> 84 </plugins> 85 </build> 86 87 </project>
applicatoin.yml
1 server: 2 port: 8788 3 4 spring: 5 boot: 6 admin: 7 client: 8 url: 9 - "http://ADMIN:8788" #这里配置Spring Boot Admin 服务地址,配置这里表示把自己注册到Admin上,如果使用Eureka服务发现的,这部分可以省略 10 username: \'user\' 11 password: \'password\' 12 application: 13 name: WEB服务监控中心 14 security: 15 user: 16 name: \'user\' 17 password: \'password\' 18 19 20 management: 21 endpoints: 22 web: 23 exposure: 24 include: 25 - "*" 26 endpoint: 27 health: 28 show-details: ALWAYS 29 30 info: 31 version: ${spring.application.name}:${server.port} 32 33 34 eureka: 35 instance: 36 lease-renewal-interval-in-seconds: 10 37 health-check-url-path: /actuator/health 38 # 注册给eureka的时候告诉eureka自己的密码 39 metadata-map: 40 "user.name": ${spring.security.user.name} 41 "user.password": ${spring.security.user.password} 42 prefer-ip-address: true 43 client: 44 registry-fetch-interval-seconds: 5 45 # fetch-registry: false 46 # register-with-eureka: false 47 service-url: 48 defaultZone: ${EUREKA_SERVICE_URL:http://EUREKA1:8761}/eureka/,${EUREKA_SERVICE_URL:http://EUREKA2:8762}/eureka/
Application.java
1 package com.wunaozai.admin.demo; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 6 import org.springframework.context.annotation.Configuration; 7 import org.springframework.security.config.annotation.web.builders.HttpSecurity; 8 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 9 import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler; 10 11 import de.codecentric.boot.admin.server.config.AdminServerProperties; 12 import de.codecentric.boot.admin.server.config.EnableAdminServer; 13 14 @Configuration 15 @EnableAdminServer 16 @EnableEurekaClient 17 @SpringBootApplication 18 public class SpringCloudAdminDemoApplication { 19 20 public static void main(String[] args) { 21 SpringApplication.run(SpringCloudAdminDemoApplication.class, args); 22 } 23 24 @Configuration 25 public static class SecuritySecureConfig extends WebSecurityConfigurerAdapter { 26 private final String adminContextPath; 27 28 public SecuritySecureConfig(AdminServerProperties prop) { 29 this.adminContextPath = prop.getContextPath(); 30 } 31 32 @Override 33 protected void configure(HttpSecurity http) throws Exception { 34 SavedRequestAwareAuthenticationSuccessHandler handler = new SavedRequestAwareAuthenticationSuccessHandler(); 35 handler.setTargetUrlParameter("redirectTo"); 36 37 http.authorizeRequests() 38 .antMatchers(adminContextPath + "/assets/**").permitAll() 39 .antMatchers(adminContextPath + "/login").permitAll() 40 .antMatchers(adminContextPath + "/actuator/**").permitAll() 41 .anyRequest().authenticated() 42 .and() 43 .formLogin().loginPage(adminContextPath + "/login").successHandler(handler) 44 .and() 45 .logout().logoutUrl(adminContextPath + "/logout").and() 46 .httpBasic().and().csrf().disable(); 物联网架构成长之路(12)-物联网架构小结1物联网架构成长之路(35)-利用Netty解析物联网自定义协议