mirror of
https://gitee.com/yudaocode/SpringBoot-Labs.git
synced 2026-09-03 05:53:54 +08:00
增加 Consul 入门
This commit is contained in:
64
labx-29/labx-29-sc-bus-consul-demo-listener-actuator/pom.xml
Normal file
64
labx-29/labx-29-sc-bus-consul-demo-listener-actuator/pom.xml
Normal file
@@ -0,0 +1,64 @@
|
||||
<?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">
|
||||
<parent>
|
||||
<artifactId>labx-29</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>labx-29-sc-bus-consul-demo-listener-actuator</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
|
||||
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
|
||||
</properties>
|
||||
|
||||
<!--
|
||||
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
|
||||
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
|
||||
-->
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<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>
|
||||
<!-- 引入 SpringMVC 相关依赖,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 引入基于 Consul 的 Spring Cloud Bus 的实现的依赖,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-consul-bus</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 实现对 Actuator 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,15 @@
|
||||
package cn.iocoder.springcloud.labx29.listenerdemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.bus.jackson.RemoteApplicationEventScan;
|
||||
|
||||
@SpringBootApplication
|
||||
@RemoteApplicationEventScan
|
||||
public class ListenerDemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ListenerDemoApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package cn.iocoder.springcloud.labx29.listenerdemo.event;
|
||||
|
||||
import org.springframework.cloud.bus.event.RemoteApplicationEvent;
|
||||
|
||||
/**
|
||||
* 用户注册事件
|
||||
*/
|
||||
public class UserRegisterEvent extends RemoteApplicationEvent {
|
||||
|
||||
/**
|
||||
* 用户名
|
||||
*/
|
||||
private String username;
|
||||
|
||||
public UserRegisterEvent() { // 序列化
|
||||
}
|
||||
|
||||
public UserRegisterEvent(Object source, String originService, String destinationService, String username) {
|
||||
super(source, originService);
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package cn.iocoder.springcloud.labx29.listenerdemo.listener;
|
||||
|
||||
import cn.iocoder.springcloud.labx29.listenerdemo.event.UserRegisterEvent;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 用户注册事件的监听器
|
||||
*/
|
||||
@Component
|
||||
public class UserRegisterListener implements ApplicationListener<UserRegisterEvent> {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(UserRegisterEvent event) {
|
||||
logger.info("[onApplicationEvent][监听到用户({}) 注册]", event.getUsername());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
spring:
|
||||
application:
|
||||
name: listener-demo
|
||||
|
||||
cloud:
|
||||
# Consul
|
||||
consul:
|
||||
host: 127.0.0.1
|
||||
port: 8500
|
||||
|
||||
server:
|
||||
port: 18080 # 随机端口,方便启动多个消费者
|
||||
|
||||
management:
|
||||
endpoints:
|
||||
# Actuator HTTP 配置项,对应 WebEndpointProperties 配置类
|
||||
web:
|
||||
exposure:
|
||||
include: '*' # 需要开放的端点。默认值只打开 health 和 info 两个端点。通过设置 * ,可以开放所有端点。
|
||||
@@ -14,6 +14,7 @@
|
||||
<modules>
|
||||
<module>labx-29-sc-bus-consul-demo-publisher</module>
|
||||
<module>labx-29-sc-bus-consul-demo-listener</module>
|
||||
<module>labx-29-sc-bus-consul-demo-listener-actuator</module>
|
||||
</modules>
|
||||
|
||||
|
||||
|
||||
4
pom.xml
4
pom.xml
@@ -41,7 +41,7 @@
|
||||
<!-- <module>lab-28</module>-->
|
||||
<!-- <module>lab-29</module>-->
|
||||
<!-- <module>lab-30</module>-->
|
||||
<module>lab-31</module>
|
||||
<!-- <module>lab-31</module>-->
|
||||
<!-- <module>lab-32</module>-->
|
||||
<!-- <module>lab-33</module>-->
|
||||
<!-- <module>lab-34</module>-->
|
||||
@@ -104,7 +104,7 @@
|
||||
<!-- <module>labx-26</module>-->
|
||||
<!-- <module>labx-27</module>-->
|
||||
<!-- <module>labx-28</module>-->
|
||||
<module>labx-28</module>
|
||||
<!-- <module>labx-28</module>-->
|
||||
<module>labx-29</module>
|
||||
</modules>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user