增加 spring cloud 用例
This commit is contained in:
72
spring-cloud/spring-cloud-stream/pom.xml
Normal file
72
spring-cloud/spring-cloud-stream/pom.xml
Normal file
@ -0,0 +1,72 @@
|
||||
<?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.0.8.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>com.heibaiying.stream</groupId>
|
||||
<artifactId>spring-cloud-stream</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>spring-cloud-stream</name>
|
||||
<description>spring cloud stream project for Spring Boot</description>
|
||||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-amqp</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<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>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
@ -0,0 +1,14 @@
|
||||
package com.heibaiying.stream;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringCloudStreamApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringCloudStreamApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,23 @@
|
||||
package com.heibaiying.stream.bean;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class Programmer implements Serializable {
|
||||
|
||||
private String name;
|
||||
|
||||
private int age;
|
||||
|
||||
private float salary;
|
||||
|
||||
private Date birthday;
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
package com.heibaiying.stream.controller;
|
||||
|
||||
import com.heibaiying.stream.bean.Programmer;
|
||||
import com.heibaiying.stream.stream.Custom;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageHeaders;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : 发送测试消息
|
||||
*/
|
||||
@RestController
|
||||
public class MessageController {
|
||||
|
||||
@Autowired
|
||||
private Custom custom;
|
||||
|
||||
/***
|
||||
* 发送简单消息
|
||||
*/
|
||||
@RequestMapping("sendSimpleMessage")
|
||||
public void sendSimpleMessage() {
|
||||
custom.input().send(MessageBuilder.withPayload("hell spring cloud stream").build());
|
||||
}
|
||||
|
||||
|
||||
/***
|
||||
* 发送消息体为对象的消息
|
||||
*
|
||||
*/
|
||||
@RequestMapping("sendObject")
|
||||
public void sendObject() {
|
||||
Programmer programmer=new Programmer("pro",12,212.2f,new Date());
|
||||
custom.input().send(MessageBuilder.withPayload(programmer).build());
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送带有消息头的消息
|
||||
*/
|
||||
@RequestMapping("sendWithHeads")
|
||||
public void sendWithHeads() {
|
||||
Programmer programmer=new Programmer("pro",12,212.2f,new Date());
|
||||
Map<String,Object> map=new HashMap<>();
|
||||
map.put("code","868686");
|
||||
MessageHeaders messageHeaders=new MessageHeaders(map);
|
||||
Message<Programmer> message= MessageBuilder.createMessage(programmer,messageHeaders);
|
||||
custom.input().send(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 条件消息 可以看做是消息路由键的一种实现
|
||||
*/
|
||||
@RequestMapping("sendWithKey")
|
||||
public void sendWithKey() {
|
||||
// 创建消息头key 为 01 的消息
|
||||
Programmer programmer=new Programmer("key01",12,212.2f,new Date());
|
||||
Map<String,Object> map=new HashMap<>();
|
||||
map.put("key","01");
|
||||
MessageHeaders messageHeaders=new MessageHeaders(map);
|
||||
Message<Programmer> message= MessageBuilder.createMessage(programmer,messageHeaders);
|
||||
custom.input().send(message);
|
||||
|
||||
// 创建消息头key 为 02 的消息
|
||||
programmer.setName("key02");
|
||||
map.put("key","02");
|
||||
MessageHeaders messageHeaders02=new MessageHeaders(map);
|
||||
Message<Programmer> message02= MessageBuilder.createMessage(programmer,messageHeaders02);
|
||||
custom.input().send(message02);
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.heibaiying.stream.stream;
|
||||
|
||||
import org.springframework.cloud.stream.annotation.Input;
|
||||
import org.springframework.cloud.stream.annotation.Output;
|
||||
import org.springframework.messaging.MessageChannel;
|
||||
import org.springframework.messaging.SubscribableChannel;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
*/
|
||||
public interface Custom {
|
||||
|
||||
String INPUT = "input";
|
||||
String OUTPUT = "output";
|
||||
|
||||
@Input(Custom.INPUT)
|
||||
SubscribableChannel input();
|
||||
|
||||
@Output(Custom.OUTPUT)
|
||||
MessageChannel output();
|
||||
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
package com.heibaiying.stream.stream;
|
||||
|
||||
import com.heibaiying.stream.bean.Programmer;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.cloud.stream.annotation.EnableBinding;
|
||||
import org.springframework.cloud.stream.annotation.StreamListener;
|
||||
import org.springframework.messaging.handler.annotation.Header;
|
||||
import org.springframework.messaging.handler.annotation.Headers;
|
||||
import org.springframework.messaging.handler.annotation.Payload;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :消息的监听
|
||||
*/
|
||||
|
||||
@Component
|
||||
@EnableBinding(Custom.class)
|
||||
@Slf4j
|
||||
public class StreamReceived {
|
||||
|
||||
@StreamListener(value = Custom.INPUT)
|
||||
public void simple(Object payload) {
|
||||
log.info("收到简单消息: {}", payload);
|
||||
}
|
||||
|
||||
@StreamListener(value = Custom.INPUT)
|
||||
public void object(Programmer programmer) {
|
||||
log.info("收到对象消息: {}", programmer);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用 @Header 监听时候需要注意,指定名称的属性必须在消息头中存在 不然就会抛出异常 MessageHandlingException: Missing header 'XXXX' for method parameter type [class java.lang.String]
|
||||
*/
|
||||
@StreamListener(value = Custom.INPUT)
|
||||
public void heads(@Payload Programmer programmer, @Headers Map<String, Object> map, @Header(name = "code") String code) {
|
||||
log.info("收到对象消息: {}", programmer);
|
||||
map.forEach((key, value) -> {
|
||||
log.info("消息头{}的值为{}", key, value);
|
||||
});
|
||||
log.info("绑定指定消息头: code = {}", code);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 监听消息头key = 01 的消息
|
||||
*/
|
||||
@StreamListener(target = Custom.INPUT, condition = "headers['key']=='01'")
|
||||
public void key01(@Payload Programmer programmer) {
|
||||
log.info("key01 监听器接收到消息: {}", programmer.getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* 监听消息头key = 02 的消息
|
||||
*/
|
||||
@StreamListener(target = Custom.INPUT, condition = "headers['key']=='01'")
|
||||
public void key02(@Payload Programmer programmer) {
|
||||
log.info("key02 监听器接收到消息: {}", programmer.getName());
|
||||
}
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
spring:
|
||||
rabbitmq:
|
||||
host: localhost
|
||||
port: 5672
|
||||
username: guest
|
||||
password: guest
|
@ -0,0 +1,17 @@
|
||||
package com.heibaiying.stream;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class SpringCloudStreamApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user