spring cloud 快速上手系列
系列说明:快速上手,一切从简,搭建一个简单的微服务框架,让新手可以在这个基础框架上做各种学习、研究。
03-消息队列 Stream
035-发送消息
1,说明
现在,我们关闭配置中心,自行发送消息。利用前一章的微服务收获我们发送的消息。
2,StreamProvider
1) 代码目录
2) 代码内容
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.3</version>
<relativePath/>
</parent>
<groupId>com.hui.study.cloud</groupId>
<artifactId>StudyStreamProvider</artifactId>
<version>1.0.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2021.0.4</spring-cloud.version>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
没有新加依赖,就以前的依赖包。
server:
port: 5002
spring:
application:
name: stream-provider
cloud:
stream:
binders:
defaultRabbit:
type: rabbit
environment:
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
bindings:
cloudBus-out-0:
destination: springCloudBus
content-type: application/json
eureka:
client:
register-with-eureka: true
fetchRegistry: true
service-url:
defaultZone: http://localhost:7001/eureka
- SendMessageController.java
package com.hui.study.cloud.stream.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
public class SendMessageController {
@Autowired
private StreamBridge streamBridge;
@PostMapping("/send")
public void send(String msg) {
Map<String, Object> msgContent = new HashMap<>();
msgContent.put("msg", msg);
msgContent.put("timeStamp", System.currentTimeMillis());
streamBridge.send("cloudBus-out-0", msgContent);
}
}
从简原则,就不单独写业务Service了,直接在Controller里面写逻辑了。正常的工程里面,发送消息的逻辑还是要写在Service里的。 没有用@EnableBinding注解,最新版本的spring cloud,这个注解已经是标记了Deprecated 我们用新推荐的StreamBridge。
- CloudStreamProviderApplication.java
package com.hui.study.cloud.stream;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class CloudStreamProviderApplication {
public static void main(String[] args) {
SpringApplication.run(CloudStreamProviderApplication.class, args);
}
}
3) 启动
注册中心正常启动,配置中心不用了。上一章的消费微服务启动。
执行 CloudStreamProviderApplication.java 启动成功后,访问 http://localhost:7001
4)调用
用apifox或postman访问:http://localhost:5002/send
可以看到,我们已经消费到了消息:
3,代码及作业
链接:https://pan.baidu.com/s/1Pia3pkKzyy0-Bvf4uhgIQQ?pwd=pydh 提取码:pydh
链接:https://pan.baidu.com/s/1KwjfIKc3usZ5RSAKmGDwyA?pwd=v1vp 提取码:v1vp
作业: 1、接收消息微服务,启动多个节点来接收消息。
|