forked from yudaocode/SpringBoot-Labs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
YunaiV
committed
Mar 4, 2020
1 parent
ab4a541
commit 8f17803
Showing
26 changed files
with
707 additions
and
1 deletion.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
labx-10/labx-10-sc-stream-rabbitmq-consumer-broadcasting/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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-10</artifactId> | ||
<groupId>cn.iocoder.springboot.labs</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>labx-10-sc-stream-rabbitmq-consumer-broadcasting</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> | ||
|
||
<!-- 引入 Spring Cloud Stream RabbitMQ 相关依赖,将 RabbitMQ 作为消息队列,并实现对其的自动配置 --> | ||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-starter-stream-rabbit</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
16 changes: 16 additions & 0 deletions
16
...ain/java/cn/iocoder/springcloud/labx10/rabbitmqdemo/consumerdemo/ConsumerApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package cn.iocoder.springcloud.labx10.rabbitmqdemo.consumerdemo; | ||
|
||
import cn.iocoder.springcloud.labx10.rabbitmqdemo.consumerdemo.listener.MySink; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.cloud.stream.annotation.EnableBinding; | ||
|
||
@SpringBootApplication | ||
@EnableBinding(MySink.class) | ||
public class ConsumerApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(ConsumerApplication.class, args); | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
...java/cn/iocoder/springcloud/labx10/rabbitmqdemo/consumerdemo/listener/Demo01Consumer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package cn.iocoder.springcloud.labx10.rabbitmqdemo.consumerdemo.listener; | ||
|
||
import cn.iocoder.springcloud.labx10.rabbitmqdemo.consumerdemo.message.Demo01Message; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.cloud.stream.annotation.StreamListener; | ||
import org.springframework.messaging.handler.annotation.Payload; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class Demo01Consumer { | ||
|
||
private Logger logger = LoggerFactory.getLogger(getClass()); | ||
|
||
@StreamListener(MySink.DEMO01_INPUT) | ||
public void onMessage(@Payload Demo01Message message) { | ||
logger.info("[onMessage][线程编号:{} 消息内容:{}]", Thread.currentThread().getId(), message); | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
...rc/main/java/cn/iocoder/springcloud/labx10/rabbitmqdemo/consumerdemo/listener/MySink.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package cn.iocoder.springcloud.labx10.rabbitmqdemo.consumerdemo.listener; | ||
|
||
import org.springframework.cloud.stream.annotation.Input; | ||
import org.springframework.messaging.SubscribableChannel; | ||
|
||
public interface MySink { | ||
|
||
String DEMO01_INPUT = "demo01-input"; | ||
|
||
@Input(DEMO01_INPUT) | ||
SubscribableChannel demo01Input(); | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
...n/java/cn/iocoder/springcloud/labx10/rabbitmqdemo/consumerdemo/message/Demo01Message.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package cn.iocoder.springcloud.labx10.rabbitmqdemo.consumerdemo.message; | ||
|
||
/** | ||
* 示例 01 的 Message 消息 | ||
*/ | ||
public class Demo01Message { | ||
|
||
/** | ||
* 编号 | ||
*/ | ||
private Integer id; | ||
|
||
public Demo01Message setId(Integer id) { | ||
this.id = id; | ||
return this; | ||
} | ||
|
||
public Integer getId() { | ||
return id; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Demo01Message{" + | ||
"id=" + id + | ||
'}'; | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
labx-10/labx-10-sc-stream-rabbitmq-consumer-broadcasting/src/main/resources/application.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
spring: | ||
application: | ||
name: demo-consumer-application | ||
cloud: | ||
# Spring Cloud Stream 配置项,对应 BindingServiceProperties 类 | ||
stream: | ||
# Binder 配置项,对应 BinderProperties Map | ||
binders: | ||
rabbit001: | ||
type: rabbit # 设置 Binder 的类型 | ||
environment: # 设置 Binder 的环境配置 | ||
# 如果是 RabbitMQ 类型的时候,则对应的是 RabbitProperties 类 | ||
spring: | ||
rabbitmq: | ||
host: 127.0.0.1 # RabbitMQ 服务的地址 | ||
port: 5672 # RabbitMQ 服务的端口 | ||
username: guest # RabbitMQ 服务的账号 | ||
password: guest # RabbitMQ 服务的密码 | ||
# Binding 配置项,对应 BindingProperties Map | ||
bindings: | ||
demo01-input: | ||
destination: DEMO-TOPIC-01 # 目的地。这里使用 RabbitMQ Exchange | ||
content-type: application/json # 内容格式。这里使用 JSON | ||
# group: demo01-consumer-group-DEMO-TOPIC-01 # 消费者分组 | ||
binder: rabbit001 # 设置使用的 Binder 名字 | ||
|
||
server: | ||
port: ${random.int[10000,19999]} # 随机端口,方便启动多个消费者 |
58 changes: 58 additions & 0 deletions
58
labx-10/labx-10-sc-stream-rabbitmq-consumer-concurrency/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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-10</artifactId> | ||
<groupId>cn.iocoder.springboot.labs</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>labx-10-sc-stream-rabbitmq-consumer-concurrency</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> | ||
|
||
<!-- 引入 Spring Cloud Stream RabbitMQ 相关依赖,将 RabbitMQ 作为消息队列,并实现对其的自动配置 --> | ||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-starter-stream-rabbit</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
16 changes: 16 additions & 0 deletions
16
...ain/java/cn/iocoder/springcloud/labx10/rabbitmqdemo/consumerdemo/ConsumerApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package cn.iocoder.springcloud.labx10.rabbitmqdemo.consumerdemo; | ||
|
||
import cn.iocoder.springcloud.labx10.rabbitmqdemo.consumerdemo.listener.MySink; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.cloud.stream.annotation.EnableBinding; | ||
|
||
@SpringBootApplication | ||
@EnableBinding(MySink.class) | ||
public class ConsumerApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(ConsumerApplication.class, args); | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
...java/cn/iocoder/springcloud/labx10/rabbitmqdemo/consumerdemo/listener/Demo01Consumer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package cn.iocoder.springcloud.labx10.rabbitmqdemo.consumerdemo.listener; | ||
|
||
import cn.iocoder.springcloud.labx10.rabbitmqdemo.consumerdemo.message.Demo01Message; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.cloud.stream.annotation.StreamListener; | ||
import org.springframework.messaging.handler.annotation.Payload; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class Demo01Consumer { | ||
|
||
private Logger logger = LoggerFactory.getLogger(getClass()); | ||
|
||
@StreamListener(MySink.DEMO01_INPUT) | ||
public void onMessage(@Payload Demo01Message message) { | ||
logger.info("[onMessage][线程编号:{} 消息内容:{}]", Thread.currentThread().getId(), message); | ||
// | ||
// try { | ||
// Thread.sleep(10 * 1000L); | ||
// } catch (InterruptedException ignored) { | ||
// } | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
...rc/main/java/cn/iocoder/springcloud/labx10/rabbitmqdemo/consumerdemo/listener/MySink.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package cn.iocoder.springcloud.labx10.rabbitmqdemo.consumerdemo.listener; | ||
|
||
import org.springframework.cloud.stream.annotation.Input; | ||
import org.springframework.messaging.SubscribableChannel; | ||
|
||
public interface MySink { | ||
|
||
String DEMO01_INPUT = "demo01-input"; | ||
|
||
@Input(DEMO01_INPUT) | ||
SubscribableChannel demo01Input(); | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
...n/java/cn/iocoder/springcloud/labx10/rabbitmqdemo/consumerdemo/message/Demo01Message.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package cn.iocoder.springcloud.labx10.rabbitmqdemo.consumerdemo.message; | ||
|
||
/** | ||
* 示例 01 的 Message 消息 | ||
*/ | ||
public class Demo01Message { | ||
|
||
/** | ||
* 编号 | ||
*/ | ||
private Integer id; | ||
|
||
public Demo01Message setId(Integer id) { | ||
this.id = id; | ||
return this; | ||
} | ||
|
||
public Integer getId() { | ||
return id; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Demo01Message{" + | ||
"id=" + id + | ||
'}'; | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
labx-10/labx-10-sc-stream-rabbitmq-consumer-concurrency/src/main/resources/application.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
spring: | ||
application: | ||
name: demo-consumer-application | ||
cloud: | ||
# Spring Cloud Stream 配置项,对应 BindingServiceProperties 类 | ||
stream: | ||
# Binder 配置项,对应 BinderProperties Map | ||
binders: | ||
rabbit001: | ||
type: rabbit # 设置 Binder 的类型 | ||
environment: # 设置 Binder 的环境配置 | ||
# 如果是 RabbitMQ 类型的时候,则对应的是 RabbitProperties 类 | ||
spring: | ||
rabbitmq: | ||
host: 127.0.0.1 # RabbitMQ 服务的地址 | ||
port: 5672 # RabbitMQ 服务的端口 | ||
username: guest # RabbitMQ 服务的账号 | ||
password: guest # RabbitMQ 服务的密码 | ||
# Binding 配置项,对应 BindingProperties Map | ||
bindings: | ||
demo01-input: | ||
destination: DEMO-TOPIC-01 # 目的地。这里使用 RabbitMQ Exchange | ||
content-type: application/json # 内容格式。这里使用 JSON | ||
group: demo01-consumer-group-DEMO-TOPIC-01 # 消费者分组 | ||
binder: rabbit001 # 设置使用的 Binder 名字 | ||
# Consumer 配置项,对应 ConsumerProperties 类 | ||
consumer: | ||
concurrency: 2 # 每个 Consumer 消费线程数的初始大小,默认为 1 | ||
# RabbitMQ 自定义 Binding 配置项,对应 RabbitBindingProperties Map | ||
rabbit: | ||
bindings: | ||
demo01-input: | ||
# RabbitMQ Consumer 配置项,对应 RabbitConsumerProperties 类 | ||
consumer: | ||
max-concurrency: 10 # 每个 Consumer 消费线程数的最大大小,默认为 1 | ||
|
||
server: | ||
port: ${random.int[10000,19999]} # 随机端口,方便启动多个消费者 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.