Spring Cloud Admin
添加配置中心服务
创建一个标准的 Spring Boot 工程,命名为:admin-woqu
- POM
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>common-server-woqu</artifactId>
<groupId>com.orrin</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>admin-woqu</artifactId>
<packaging>jar</packaging>
<name>admin-woqu</name>
<dependencies>
<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>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<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>
<dependency>
<groupId>com.orrin</groupId>
<artifactId>woqu-cloud-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
- 启动类
AdminWoquApplication.java启动类
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@ComponentScan("com.woqu")
@Configuration
@EnableAutoConfiguration
@EnableDiscoveryClient
@EnableAdminServer
public class AdminWoquApplication {
public static void main(String[] args) {
SpringApplication.run(AdminWoquApplication.class, args);
}
}
CostomSecuritySecureConfig.java Security配置文件
这个配置的作用主要针对登录页面及监控数据的获取
package com.woqu.common.admin.config;
import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
/**
* @author orrin
*/
@Configuration
public class CostomSecuritySecureConfig extends WebSecurityConfigurerAdapter {
private final String adminContextPath;
public CostomSecuritySecureConfig(AdminServerProperties adminServerProperties) {
this.adminContextPath = adminServerProperties.getContextPath();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter("redirectTo");
successHandler.setDefaultTargetUrl(adminContextPath + "/");
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()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.ignoringAntMatchers(adminContextPath + "/instances", adminContextPath + "/actuator/**");
// @formatter:on
}
}
- 配置文件 在application.yml中配置信息如下:
server:
port: 7000
spring:
application:
name: admin-woqu
cloud:
consul:
host: woqu.consul
port: 8500
discovery:
#instance-id: ${spring.application.name}:${server.port}
instance-group: ${spring.application.name}
register: true
service-name: ${spring.application.name}
tags: management.context-path=/foo, health.path=/ping, user.name=user, user.password=password
boot:
admin:
notify:
mail:
enabled: false
to: 接收人
from: 发送人
# ignore-changes: UNKNOWN:UP
ignore-changes:
-
"*:UP" #从任何状态到up状态都不要发邮件通知
routes:
endpoints: env,metrics,dump,jolokia,info,configprops,trace,logfile,refresh,flyway,liquibase,heapdump,loggers,auditevents,hystrix.stream
turbine:
clusters: default
#,unieap-eureka-server-single
location: turbine
discovery:
ignored-services: consul,${spring.application.name}
mail:
host: smtp.163.com
username: o_rrin
password: ******
properties:
mail.smtp.auth : true
mail.smtp.timeout : 2000
mail.smtp.starttls.enable : true
mail.smtp.starttls.required : true
security:
user:
name: "user"
password: "123456"
management:
server:
servlet:
context-path: /foo
endpoints:
web:
exposure:
include: "*"
path-mapping:
health: /ping
endpoint:
health:
show-details: ALWAYS
添加客户端模块
以server-businessa-woqu为例,添加配置中心模块
- POM 在pom.xml中加入以下依赖:
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
- 配置文件
在application.yml中加入以下配置:
spring:
boot:
admin:
client:
url: http://woqu.admin:7000
- 测试
分别启动:
- consul-server
- gateway-woqu
- hystrix-dashboard-woqu
- turbine-woqu
- server-businessa-woqu
- server-businessb-woqu
- admin-woqu
在浏览器地址栏输入http://localhost:7000/login,填写用户名密码。