admin-server项目
添加 spring-boot-admin-starter-server 的依赖。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/de.codecentric/spring-boot-admin-starter-server -->
<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-actuator</artifactId>
</dependency>
在admin-server项目的springboot启动类添加 @EnableAdminServer 注解。
admin-client-simple项目
添加依赖
<!-- https://mvnrepository.com/artifact/de.codecentric/spring-boot-admin-starter-client -->
<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-actuator</artifactId>
</dependency>
修改对应配置,使其注册到amin-server上
spring.application.name=adminclientsimple
server.port=8768
server.servlet.session.timeout=3600s
spring.boot.admin.client.url=http://localhost:8769
management.endpoints.web.exposure.include=*
management.endpoint.healthshow-details=always
安全配置
在对应pom.xml中添加 spring boot security的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
admin-server端添加安全后需要添加配置
spring.security.user.name=admin
spring.security.user.password=admin
这样访问admin-server就需要密码了。
admin-client-simple同样设置也可以设置密码访问。
同时需要更好的控制安全访问需要编写控制配置
写一个配置类SecuritySecureConfig继承WebSecurityConfigurerAdapter,配置如下 ,根据需要修改。
@Configuration
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
private final String adminContextPath;
public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
this.adminContextPath = adminServerProperties.getContextPath();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter( "redirectTo" );
http.authorizeRequests()
.antMatchers( adminContextPath + "/assets/**" ).permitAll()
.antMatchers( adminContextPath + "/login" ).permitAll()
.anyRequest().authenticated()
.and()
.formLogin().loginPage( adminContextPath + "/login" ).successHandler( successHandler ).and()
.logout().logoutUrl( adminContextPath + "/logout" ).and()
.httpBasic().and()
.csrf().disable();
// @formatter:on
}
}
四、使用注册中心自动注册结合配置
admin-server项目
在admin-server中引入注册中心的client的依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
在admin-server的配置中添加如下配置:
#eureka.instance.hostname=192.168.31.192
#eureka.client.register-with-eureka=false
#eureka.client.fetch-registry=false
#eureka.client.serviceUrl.defaultZone=http://192.168.31.192:8761/eureka/
eureka:
client:
registryFetchIntervalSeconds: 5
service-url:
defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
instance:
leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health
一个是yaml,一个是properties按自己情况修改对应参数添加到配置里。
最后还需要在admin-server项目启动类上添加 @EnableDiscoveryClient 注解。
如果启用了security的还需要添加注册对应的安全信息。
eureka:
instance:
metadata-map:
user.name: ${spring.security.user.name}
user.password: ${spring.security.user.password}
admin-client-simple项目
引入注册中心的client的依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
写入对应配置:
#eureka.instance.hostname=192.168.31.192
#eureka.client.register-with-eureka=false
#eureka.client.fetch-registry=false
#eureka.client.serviceUrl.defaultZone=http://192.168.31.192:8761/eureka/
eureka:
instance:
leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health
client:
registryFetchIntervalSeconds: 5
service-url:
defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
同样根据需求添加和修改对应的配置。
来源:oschina
链接:https://my.oschina.net/lenglingx/blog/4424174