一、发送方式
本地可以通过配置发邮件,但阿里云服务器中25端口被禁用,自带邮件功能无法发送,需要开放端口;
也可以自定义其他通知方式:微信或者短信通知
二、创建boot项目
pom.xml
<properties>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<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>
<dependencies>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.0.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
application.yml配置
server:
port: 6095
#给谁发送短信
smsmsg:
# 经理 xx xx xx xx 多个逗号隔开
# phones: xxx,xxx,xxxx,xxxx,xxxx
phones: 1555555xxxx
# 访问安全配置
security:
user:
name: admin
password: 123456
basic:
enabled: false
spring:
application:
name: boot-admin
# 注册到注册中心
eureka:
instance:
lease-renewal-interval-in-seconds: 10
lease-expiration-duration-in-seconds: 30
hostname: 127.0.0.1
instance-id: ${spring.application.name}:${eureka.instance.hostname}:${server.port}
prefer-ip-address: true
ip-address: ${eureka.instance.hostname}
client:
register-with-eureka: true
fetch-registry: true
serviceUrl:
defaultZone: http://admin:123456@${eureka.instance.hostname}:6001/eureka/
三、配置类
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// Page with login form is served as /login.html and does a POST on /login
http.formLogin().loginPage("/login.html").loginProcessingUrl("/login").permitAll();
// The UI does a POST on /logout on logout
http.logout().logoutUrl("/logout");
// The ui currently doesn't support csrf
http.csrf().disable();
// Requests for the login page and the static assets are allowed
http.authorizeRequests()
.antMatchers("/login.html", "/**/*.css", "/img/**", "/third-party/**")
.permitAll();
// ... and any other request needs to be authorized
http.authorizeRequests().antMatchers("/**").authenticated();
// Enable so that the clients can authenticate via HTTP basic for registering
http.httpBasic();
}
}
四、自定义通知类
@Service
public class CustomNotifier extends AbstractStatusChangeNotifier {
private final Logger log = LoggerFactory.getLogger(this.getClass());
@Value("#{'${smsmsg.phones}'.split(',')}")
private List<String> phones;
@Resource
private SendMsgUtil sendMsgUtil;
@Override
protected void doNotify(ClientApplicationEvent event) throws Exception {
if (event instanceof ClientApplicationStatusChangedEvent) {
ClientApplicationStatusChangedEvent statusChange = (ClientApplicationStatusChangedEvent) event;
// down表示服务里部分中间件有问题
// offline表示服务无法http访问了
//if ((statusChange.getTo().isOffline() || statusChange.getTo().isDown()) && !statusChange.getFrom().isUnknown()) {
if (statusChange.getTo().isOffline() && !statusChange.getFrom().isUnknown()) {
DateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Application application = event.getApplication();
// 服务名称
String appName = application.getName();
// 实例名称(在eureka中注册的唯一id)
String instance = application.getId();
// 事件发生的时间
String time = sf.format(new Date(event.getTimestamp()));
// 服务器的ip
URL url = new URL(statusChange.getApplication().getServiceUrl());
String server = url.getHost();
// 封装成自定义事件对象
JsonObject json = new JsonObject();
json.addProperty("appName", appName.concat(":").concat(instance));
json.addProperty("server", server);
json.addProperty("from", statusChange.getFrom().getStatus());
json.addProperty("to", statusChange.getTo().getStatus());
json.addProperty("time", time);
// 事件的全部信息
json.addProperty("details", new Gson().toJson(event));
log.warn("-----------------------------warn event !!!!,content is:{}", json.toString());
//发送消息
String msg = "自定义模板";
log.info(environment);
for(String phone : phones){
// 自定义消息发送
sendMsgUtil.sendSms(phone,msg);
}
}
}
}
@Override
protected boolean shouldNotify(ClientApplicationEvent event) {
return super.shouldNotify(event);
}
}