mirror of
https://gitee.com/yudaocode/SpringBoot-Labs.git
synced 2026-09-03 05:53:54 +08:00
增加 Consul 入门
This commit is contained in:
@@ -12,6 +12,7 @@ import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/demo")
|
||||
@RefreshScope
|
||||
public class DemoController {
|
||||
|
||||
@Autowired
|
||||
|
||||
@@ -12,6 +12,8 @@
|
||||
<artifactId>labx-28-sc-consul-config-auto-refresh</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>
|
||||
|
||||
@@ -3,6 +3,7 @@ package cn.iocoder.springcloud.labx28.consuldemo.controller;
|
||||
import cn.iocoder.springcloud.labx28.consuldemo.config.OrderProperties;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
@@ -12,6 +13,7 @@ import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/demo")
|
||||
@RefreshScope
|
||||
public class DemoController {
|
||||
|
||||
@Autowired
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package cn.iocoder.springcloud.labx28.consuldemo.listener;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class DemoEnvironmentChangeListener implements ApplicationListener<EnvironmentChangeEvent> {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
@Autowired
|
||||
private ConfigurableEnvironment environment;
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(EnvironmentChangeEvent event) {
|
||||
for (String key : event.getKeys()) {
|
||||
logger.info("[onApplicationEvent][key({}) 最新 value 为 {}]", key, environment.getProperty(key));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -14,6 +14,7 @@
|
||||
|
||||
<modules>
|
||||
<module>labx-28-sc-consul-config-demo</module>
|
||||
<module>labx-28-sc-consul-config-auto-refresh</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
|
||||
1
labx-28/《芋道 Spring Cloud 配置中心 Consul 入门》.md
Normal file
1
labx-28/《芋道 Spring Cloud 配置中心 Consul 入门》.md
Normal file
@@ -0,0 +1 @@
|
||||
<http://www.iocoder.cn/Spring-Cloud/Consul-Config/?github>
|
||||
58
labx-29/labx-29-sc-bus-consul-demo-listener/pom.xml
Normal file
58
labx-29/labx-29-sc-bus-consul-demo-listener/pom.xml
Normal file
@@ -0,0 +1,58 @@
|
||||
<?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</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>
|
||||
</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,12 @@
|
||||
spring:
|
||||
application:
|
||||
name: listener-demo
|
||||
|
||||
cloud:
|
||||
# Consul
|
||||
consul:
|
||||
host: 127.0.0.1
|
||||
port: 8500
|
||||
|
||||
server:
|
||||
port: ${random.int[10000,19999]} # 随机端口,方便启动多个消费者
|
||||
59
labx-29/labx-29-sc-bus-consul-demo-publisher/pom.xml
Normal file
59
labx-29/labx-29-sc-bus-consul-demo-publisher/pom.xml
Normal file
@@ -0,0 +1,59 @@
|
||||
<?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-publisher</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>
|
||||
<spring.cloud.alibaba.version>2.2.0.RELEASE</spring.cloud.alibaba.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>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,14 @@
|
||||
package cn.iocoder.springcloud.labx29.publisherdemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
//@RemoteApplicationEventScan
|
||||
public class PublisherDemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(PublisherDemoApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package cn.iocoder.springcloud.labx29.publisherdemo.controller;
|
||||
|
||||
import cn.iocoder.springcloud.labx29.publisherdemo.event.UserRegisterEvent;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.bus.ServiceMatcher;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/demo")
|
||||
public class DemoController {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
@Autowired
|
||||
private ApplicationEventPublisher applicationEventPublisher;
|
||||
|
||||
@Autowired
|
||||
private ServiceMatcher busServiceMatcher;
|
||||
|
||||
@GetMapping("/register")
|
||||
public String register(String username) {
|
||||
// ... 执行注册逻辑
|
||||
logger.info("[register][执行用户({}) 的注册逻辑]", username);
|
||||
|
||||
// ... 发布
|
||||
applicationEventPublisher.publishEvent(new UserRegisterEvent(this, busServiceMatcher.getServiceId(),
|
||||
null, username));
|
||||
return "success";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package cn.iocoder.springcloud.labx29.publisherdemo.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,46 @@
|
||||
package org.springframework.cloud.consul.binder;
|
||||
|
||||
import com.ecwid.consul.v1.ConsulClient;
|
||||
import com.ecwid.consul.v1.QueryParams;
|
||||
import com.ecwid.consul.v1.Response;
|
||||
import com.ecwid.consul.v1.event.model.Event;
|
||||
import com.ecwid.consul.v1.event.model.EventParams;
|
||||
import org.springframework.integration.handler.AbstractMessageHandler;
|
||||
import org.springframework.messaging.Message;
|
||||
|
||||
/**
|
||||
* Adapter that converts and sends Messages as Consul events.
|
||||
*
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
public class ConsulSendingHandler extends AbstractMessageHandler {
|
||||
|
||||
private final ConsulClient consul;
|
||||
|
||||
private final String eventName;
|
||||
|
||||
public ConsulSendingHandler(ConsulClient consul, String eventName) {
|
||||
this.consul = consul;
|
||||
this.eventName = eventName;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handleMessageInternal(Message<?> message) {
|
||||
if (this.logger.isTraceEnabled()) {
|
||||
this.logger.trace("Publishing message" + message);
|
||||
}
|
||||
|
||||
// 转换成 String
|
||||
Object payload = message.getPayload();
|
||||
if (payload instanceof byte[]) {
|
||||
payload = new String((byte[]) payload);
|
||||
}
|
||||
|
||||
// TODO: support headers
|
||||
// TODO: support consul event filters: NodeFilter, ServiceFilter, TagFilter
|
||||
Response<Event> event = this.consul.eventFire(this.eventName, (String) payload,
|
||||
new EventParams(), QueryParams.DEFAULT);
|
||||
// TODO: return event?
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
server:
|
||||
port: 8081
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: publisher-demo
|
||||
|
||||
cloud:
|
||||
# Consul
|
||||
consul:
|
||||
host: 127.0.0.1
|
||||
port: 8500
|
||||
# Bus 相关配置项,对应 BusProperties
|
||||
bus:
|
||||
enabled: true # 是否开启,默认为 true
|
||||
destination: springCloudBus # 目标消息队列,默认为 springCloudBus
|
||||
20
labx-29/pom.xml
Normal file
20
labx-29/pom.xml
Normal file
@@ -0,0 +1,20 @@
|
||||
<?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>labs-parent</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>labx-29</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>labx-29-sc-bus-consul-demo-publisher</module>
|
||||
<module>labx-29-sc-bus-consul-demo-listener</module>
|
||||
</modules>
|
||||
|
||||
|
||||
</project>
|
||||
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>-->
|
||||
@@ -103,7 +103,9 @@
|
||||
<!-- <module>labx-25</module>-->
|
||||
<!-- <module>labx-26</module>-->
|
||||
<!-- <module>labx-27</module>-->
|
||||
<!-- <module>labx-28</module>-->
|
||||
<module>labx-28</module>
|
||||
<module>labx-29</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
|
||||
Reference in New Issue
Block a user