前言
在之前的工作中,微服務框架使用的是springcloud,訊息中介軟體使用的rocketmq,這段時間看到阿里出了spring cloud alibaba整合了rocketmq,出於好奇,寫了個demo
一些概念
- 官方對 Spring Cloud Stream 的一段介紹:Spring Cloud Stream 是一個用於構建基於訊息的微服務應用框架。基於 SpringBoot 建立具有生產級別的單機 Spring 應用,並且使用 Spring Integration 與 Broker 進行連線。
- Binder :Components responsible to provide integration with the external messaging systems.【與外部訊息中介軟體進行整合】
- Binding:Bridge between the external messaging systems and application provided Producers and Consumers of messages (created by the Destination Binders).【在訊息中介軟體與應用程式提供的 Provider 和 Consumer 之間提供了一個橋樑,開發者只需使用應用程式的 生產者或消費者生產或消費資料即可,遮蔽了開發者與底層訊息中介軟體的接觸。】
- Message: The canonical data structure used by producers and consumers to communicate with Destination Binders (and thus other applications via external messaging systems).【生產者和消費者用於與目標繫結器通訊的規範資料結構。】
快速在本地啟動rocketmq
第一步:下載:www.apache.org/dyn/closer.… 第二步:解壓 第三步:修改三個配置檔案:runbroker.sh,runserver.sh,tools.sh,將其中JAVA_HOME改成自己電腦的環境配置,修改完如下
[ ! -e "$JAVA_HOME/bin/java" ] && JAVA_HOME=自己的地址
#[ ! -e "$JAVA_HOME/bin/java" ] && JAVA_HOME=/usr/java
[ ! -e "$JAVA_HOME/bin/java" ] && error_exit "Please set the JAVA_HOME variable in your environment, We need java(x64)!"
複製程式碼
第四步:依次執行命令
./mqnamesrv
./mqbroker -n localhost:9876
./mqadmin updateTopic -n localhost:9876 -c DefaultCluster -t test-topic
複製程式碼
如果啟動成功,沒有報錯,代表啟動成功哈,下面就可以開發了
開發demo
第一步:匯入相關的pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.2.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
複製程式碼
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rocketmq</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 為了Endpoint 資訊檢視 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-core</artifactId>
<version>3.2.6</version>
</dependency>
複製程式碼
第二步:建一個springboot專案,啟動類如下:
@SpringBootApplication
@EnableBinding({ Source.class, Sink.class })
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
複製程式碼
第三步:建立provider
@Service
public class RocketmqProducer {
public void send(String message) throws MQClientException, RemotingException, InterruptedException, MQBrokerException {
DefaultMQProducer producer = new DefaultMQProducer("test_producer_group");
producer.setNamesrvAddr("127.0.0.1:9876");
producer.start();
Message msg = new Message("test-topic", "test-tag", message.getBytes());
producer.send(msg);
}
}
複製程式碼
第四步:建立consumer
@Service
public class ReceiveService {
/**
* 預設是input,在Sink類中指定,如果想要多個input,需要寫一個實現Sink的類
* @param receiveMsg
*/
@StreamListener("input")
public void receiveInput1(String receiveMsg) {
System.out.println("input receive: " + receiveMsg);
}
}
複製程式碼
第五步:加入配置檔案:
server.port=8087
spring.application.name=spring-cloud-alibaba-rocketmq-demo
# 配置rocketmq的nameserver地址
spring.cloud.stream.rocketmq.binder.namesrv-addr=127.0.0.1:9876
# 定義name為output的binding
spring.cloud.stream.bindings.output.destination=test-topic
spring.cloud.stream.bindings.output.content-type=application/json
#定義name為input的binding
spring.cloud.stream.bindings.input.destination=test-topic
spring.cloud.stream.bindings.input.content-type=application/json
spring.cloud.stream.bindings.input.group=test-group
management.endpoint.health.show-details=always
複製程式碼
第六步:寫一個controller,啟動專案,訪問介面
@RestController
@RequestMapping(value = "/api/demo/test")
public class TestController {
@Autowired
RocketmqProducer rocketmqProducer;
@RequestMapping(value = "/send", method = RequestMethod.GET)
public String send() throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
rocketmqProducer.send("test rocketmq message");
return "success";
}
}
複製程式碼
會看到控制檯輸出:input receive: test rocketmq message
Endpoint 資訊檢視
瀏覽器輸入:http://127.0.0.1:8087/actuator/rocketmq-binder
結語
這一篇文章只是將spring cloud stream 和 rocketmq跑通了,其實對於spring cloud stream和rocketmq還是學習的階段,只能感嘆spring cloud博大精深
更多網站可以訪問www.zplxjj.com或關注公眾號: