这里用最简单的方式配置
一、首先配置Spring Boot Admin Server端:
1.1.pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
1.2 启动程序注解配置和安全设置代码:
@SpringBootApplication
@EnableAdminServer
@EnableAutoConfiguration
public class MonitoringCenterApplication {
public static void main(String[] args) {
SpringApplication.run(MonitoringCenterApplication.class, args);
}
@Configuration
public static class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
private final String adminContextPath;
public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
this.adminContextPath = adminServerProperties.getContextPath();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
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();
}
}
}
1.3 application.properties配置:
#sping mail
spring.mail.host = smtp.qq.com
spring.mail.username=200239@qq.com
spring.mail.password=xxxxxx
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
#管理的实例有启动或关闭时发送邮件
spring.boot.admin.notify.mail.enabled=true
spring.boot.admin.notify.mail.to=200239@qq.com
spring.boot.admin.notify.mail.from=200239@qq.com
#设置监控台用户名密码
spring.security.user.name=admin
spring.security.user.password=admin
管理端配置完成,注意,这个的from使用的邮箱账号一定要开启POP3/SMTP服务,密码不是登录密码,是开启pop3分配的密码
二、注册客户端
2.1 pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2.2 application.properties
server.port=8081
#名称
spring.application.name=message-server
#AdminServer URL路径
spring.boot.admin.client.url=http://127.0.0.1:8080
#如果Admin Server 设置密码这里就要设置对应的密码
spring.boot.admin.client.username=admin
spring.boot.admin.client.password=admin
#使用ip注册,默认是使用主机名
spring.boot.admin.client.instance.prefer-ip=true
#开放所有endpoints
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
#开启Http请求关机
management.endpoint.shutdown.enabled=true
#暴露endpoints的上下文路径
management.endpoints.web.base-path=/message
#暴露endpoints的端口
management.server.port=12345
#ip限制,一般设置内网ip。
management.server.address=127.0.0.1
配置完成,启动二个项目即可
因为这里配置了:management.endpoints.web.base-path=/message 所以如果要用http调用接口关闭客户端服务时:
POST请求、Content-Type application/json格式 ,只允许本机访问接口。路径为: