搭建过程网上很多, 主要是各个依赖的版本, 导致的各种 jar 包问题, 此处记录下我的 pom 和 yml 文件
目录
1. SpringAdmin server 配置
(1) pom文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.vpark</groupId>
<artifactId>spring-admin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-admin</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
<version>2.2.8.RELEASE</version>
</dependency>
</dependencies>
(2) yml 文件
server:
port: 2010
spring:
application:
name: admin
cloud:
config:
enabled: true #是否启用配置中心
discovery:
enabled: true #是否启用配置中心
serviceId: configserver
label: master
profile: dev
security:
profiles: secure
user:
name: "user"
password: "password"
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: ALWAYS
eureka:
instance:
prefer-ip-address: true
client:
serviceUrl:
defaultZone: http://user:password@ip:port/eureka/
(3) 启动类
import de.codecentric.boot.admin.server.config.AdminServerProperties;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.autoconfigure.security.reactive.EndpointRequest;
import org.springframework.boot.actuate.health.HealthEndpoint;
import org.springframework.boot.actuate.info.InfoEndpoint;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.web.server.SecurityWebFilterChain;
import org.springframework.security.web.server.authentication.RedirectServerAuthenticationSuccessHandler;
import org.springframework.security.web.server.csrf.CookieServerCsrfTokenRepository;
import org.springframework.security.web.server.csrf.CsrfWebFilter;
import org.springframework.security.web.server.util.matcher.AndServerWebExchangeMatcher;
import org.springframework.security.web.server.util.matcher.NegatedServerWebExchangeMatcher;
import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatchers;
@EnableAdminServer
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
@EnableEurekaClient
public class SpringAdminApplication {
public static void main(String[] args) {
SpringApplication.run(SpringAdminApplication.class, args);
}
@Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http,
AdminServerProperties adminServer) {
http.authorizeExchange(exchanges -> exchanges
.matchers(EndpointRequest.to(HealthEndpoint.class, InfoEndpoint.class)).permitAll()
.pathMatchers(adminServer.path("/assets/**")).permitAll()
.pathMatchers(adminServer.path("/login")).permitAll()
.anyExchange().authenticated()
);
http.formLogin()
.loginPage(adminServer.path("/login"))
.authenticationSuccessHandler(new RedirectServerAuthenticationSuccessHandler(adminServer.path("/")));
http.logout().logoutUrl(adminServer.path("/logout"));
http.httpBasic(Customizer.withDefaults());
http.csrf().csrfTokenRepository(CookieServerCsrfTokenRepository.withHttpOnlyFalse())
.requireCsrfProtectionMatcher(new AndServerWebExchangeMatcher(
CsrfWebFilter.DEFAULT_CSRF_MATCHER,
new NegatedServerWebExchangeMatcher(EndpointRequest.toAnyEndpoint()),
new NegatedServerWebExchangeMatcher(ServerWebExchangeMatchers.pathMatchers(HttpMethod.POST, adminServer.path("/instances"))),
new NegatedServerWebExchangeMatcher(ServerWebExchangeMatchers.pathMatchers(HttpMethod.DELETE, adminServer.path("/instances/*"))),
new NegatedServerWebExchangeMatcher(ServerWebExchangeMatchers.pathMatchers(adminServer.path("/actuator/*")))
));
http.csrf().disable();
return http.build();
}
}
2. 业务服务 ( SpringAdmin client ) 配置
(1) yml 文件
其它正常配置就行, 主要是要有下面两段
logging:
file: /usr/local/bd.log
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: ALWAYS
加上以上配置, 就可以监控服务, 查看实时日志了