在上一遍中我们学习了,SpringBootAdmin的自定义属性配置,以及前面所说的各种配置和整合,但是都是单机的,这一篇我们来说说 微服务开发 中从Eureka 注册中心上面去作为客户端监控。
不熟悉Spring Cloud 请看官方文档 Spring Cloud
首先需要一个 Eureka Service 做服务注册与发现模块
创建一个项目模块: SpringCloudEurekaservice
pom.xml:
加入 spring-cloud-starter-netflix-eureka-server
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>eurekaservice</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eurekaservice</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<!-- spring-cloud 版本 如果 出现 与 springboot 版本不一致的情况 请查看 https://spring.io/projects/spring-cloud#overview-->
<spring-cloud.version>Hoxton.SR8</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
yml配置:
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
启动类:
启动类加入 @EnableEurekaServer
/**
* EnableEurekaServer 服务端做注册中心服务: springbooadmin 从注册中心中获取客户端进行监控
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaserviceApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaserviceApplication.class, args);
}
}
启动Eureka service 访问 http://localhost:8761/
出现如下界面,说明成功了。
整合SpringBootAdmin
新建立一个新的模块项目这个项目中使用 spring.boot.admin.api-path: instances 特性来做,客户端和服务端配置。
pom.xml 依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>spring-boot-admin-sample-eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-admin-sample-eureka</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<!-- spring-cloud 版本 如果 出现 与 springboot 版本不一致的情况 请查看 https://spring.io/projects/spring-cloud#overview-->
<spring-cloud.version>Hoxton.SR8</spring-cloud.version>
</properties>
<dependencies>
<!-- spring-boot-admin 服务端-->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<exclusions>
<exclusion>
<groupId>io.projectreactor.netty</groupId>
<artifactId>reactor-netty</artifactId>
</exclusion>
</exclusions>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>io.projectreactor.netty</groupId>
<artifactId>reactor-netty</artifactId>
<version>0.9.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- tag::dependency-eureka[] -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- end::dependency-eureka[] -->
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
yml配置:
spring:
application:
name: spring-boot-admin-sample-eureka
profiles:
active:
- secure
# tag::configuration-eureka[]
eureka: #<1>
instance:
leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health
metadata-map:
startup: ${random.int} #needed to trigger info and endpoint update after restart
client:
registryFetchIntervalSeconds: 5
serviceUrl:
defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
management:
endpoints:
web:
exposure:
include: "*" #<2>
endpoint:
health:
show-details: ALWAYS
# end::configuration-eureka[]
logging:
file:
name: admin-eureka-client.log
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'
# end::logging
---
spring:
profiles: insecure
---
spring:
profiles: secure
security:
user:
name: "admin"
password: "admin"
eureka:
instance:
metadata-map:
user.name: "admin" #These two are needed so that the server
user.password: "admin" #can access the protected client endpoints
server:
port: 8080
这里需要注意的就是 spring.profiles 来做配置隔离 比如 insecure 是客户端的配置 secure是服务端的配置,其他配置玩法参考前面的模块实现。
启动类
启动类中加入配置属性,注意区分 @Profile(“insecure”) 配置。
/**
* https://spring.io/projects/spring-cloud#overview
* spring-cloud 与 spring boot 版本兼容:
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableAdminServer
public class SpringBootAdminSampleEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootAdminSampleEurekaApplication.class, args);
}
/**
* 客户端配置
*/
@Profile("insecure")
@Configuration(proxyBeanMethods = false)
public static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
private final String adminContextPath;
public SecurityPermitAllConfig(AdminServerProperties adminServerProperties) {
this.adminContextPath = adminServerProperties.getContextPath();
}
/**
* 释放断点
* @param http
* @throws Exception
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests((authorizeRequests) -> authorizeRequests.anyRequest().permitAll())
.csrf((csrf) -> csrf.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.ignoringRequestMatchers(
new AntPathRequestMatcher(this.adminContextPath + "/instances",
HttpMethod.POST.toString()),
new AntPathRequestMatcher(this.adminContextPath + "/instances/*",
HttpMethod.DELETE.toString()),
new AntPathRequestMatcher(this.adminContextPath + "/actuator/**")));
}
}
/**
* 服务端登录配置:
*/
@Profile("secure")
@Configuration(proxyBeanMethods = false)
public static class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
private final String adminContextPath;
private final SecurityProperties security;
public SecuritySecureConfig(AdminServerProperties adminServerProperties,SecurityProperties security) {
this.adminContextPath = adminServerProperties.getContextPath();
this.security = security;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter("redirectTo");
successHandler.setDefaultTargetUrl(this.adminContextPath + "/");
http.authorizeRequests((authorizeRequests) -> authorizeRequests
.antMatchers(this.adminContextPath + "/assets/**").permitAll()
.antMatchers(this.adminContextPath + "/login").permitAll().anyRequest().authenticated())
.formLogin((formLogin) -> formLogin.loginPage(this.adminContextPath + "/login")
.successHandler(successHandler))
.logout((logout) -> logout.logoutUrl(this.adminContextPath + "/logout"))
.httpBasic(Customizer.withDefaults())
.csrf((csrf) -> csrf.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.ignoringRequestMatchers(
new AntPathRequestMatcher(this.adminContextPath + "/instances",
HttpMethod.POST.toString()),
new AntPathRequestMatcher(this.adminContextPath + "/instances/*",
HttpMethod.DELETE.toString()),
new AntPathRequestMatcher(this.adminContextPath + "/actuator/**")));
}
/**
* 记住我功能配置,当点击记住我时候,必须有以下配置,否则无法登录成功。
* @param auth
* @throws Exception
*/
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser(security.getUser().getName())
.password("{noop}" + security.getUser().getPassword()).roles("USER");
}
}
}
配置完成之后,启动测试,首先启动 Eureka 服务端,然后启动客户端,查看是否注册中心注册成功之后,
在客户端的日志中心中查看到拉取中心配置为true的时候就是成功了。
2021-04-13 11:01:23.346 INFO 16944 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient : Getting all instance registry info from the eureka server
2021-04-13 11:01:23.367 INFO 16944 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient : The response status is 200
2021-04-13 11:01:28.375 INFO 16944 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient : Disable delta property : false
2021-04-13 11:01:28.375 INFO 16944 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient : Single vip registry refresh property : null
2021-04-13 11:01:28.375 INFO 16944 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient : Force full registry fetch : false
2021-04-13 11:01:28.375 INFO 16944 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient : Application is null : false
2021-04-13 11:01:28.375 INFO 16944 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient : Registered Applications size is zero : true
2021-04-13 11:01:28.375 INFO 16944 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient : Application version is -1: false
2021-04-13 11:01:28.375 INFO 16944 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient : Getting all instance registry info from the eureka server
2021-04-13 11:01:28.379 INFO 16944 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient : The response status is 200
2021-04-13 11:01:33.394 INFO 16944 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient : Disable delta property : false
2021-04-13 11:01:33.394 INFO 16944 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient : Single vip registry refresh property : null
2021-04-13 11:01:33.394 INFO 16944 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient : Force full registry fetch : false
2021-04-13 11:01:33.394 INFO 16944 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient : Application is null : false
2021-04-13 11:01:33.395 INFO 16944 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient : Registered Applications size is zero : true
2021-04-13 11:01:33.395 INFO 16944 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient : Application version is -1: false
2021-04-13 11:01:33.395 INFO 16944 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient : Getting all instance registry info from the eureka server
2021-04-13 11:01:33.438 INFO 16944 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient : The response status is 200
登录进admin service 的时候,刚开始是没有的不要着急,一直看日志 出现 true的时候就能看到 service 中的注册的客户端了。
说明整合成功了。
完整实例代码,GitHub: SpringBootAmdinDemo
有问题下方讨论,一起学习。