修改readme.md
This commit is contained in:
parent
5cb814a58c
commit
4e2d231a7a
12
README.md
12
README.md
@ -68,13 +68,13 @@ spring-cloud:Finchley.SR2
|
||||
|
||||
| samples | 描述 | 官方文档 |
|
||||
| ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ |
|
||||
| [spring-cloud-Eureka](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-cloud/spring-cloud-eureka) | eureka 服务的注册和发现 | [Service Discovery: Eureka Server](https://cloud.spring.io/spring-cloud-static/Finchley.SR2/multi/multi_spring-cloud-eureka-server.html) |
|
||||
| [spring-cloud-Eureka-cluster](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-cloud/spring-cloud-eureka-cluster) | eureka 高可用集群搭建 | [Service Discovery: Eureka Server](https://cloud.spring.io/spring-cloud-static/Finchley.SR2/multi/multi_spring-cloud-eureka-server.html) |
|
||||
| [spring-cloud-Ribbon](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-cloud/spring-cloud-ribbon) | ribbon客户端负载均衡 | [Client Side Load Balancer: Ribbon](https://cloud.spring.io/spring-cloud-static/Finchley.SR2/multi/multi_spring-cloud-ribbon.html) |
|
||||
| [spring-cloud-OpenFeign](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-cloud/spring-cloud-feign) | openfeign 声明式服务调用 | [Declarative REST Client: Feign](https://cloud.spring.io/spring-cloud-static/Finchley.SR2/multi/multi_spring-cloud-feign.html) |
|
||||
| [spring-cloud-Hystrix](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-cloud/spring-cloud-hystrix) | hystix 服务容错保护<br/>hystrix dashboard 断路器监控<br>turbine 断路器聚合监控 | [Circuit Breaker: Hystrix Clients](https://cloud.spring.io/spring-cloud-static/Finchley.SR2/multi/multi__circuit_breaker_hystrix_clients.html)<br/>[Hystrix metrics aggregation with Turbine ](https://cloud.spring.io/spring-cloud-static/Finchley.SR2/multi/multi_spring-cloud-consul-turbine.html) |
|
||||
| [spring-cloud-eureka](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-cloud/spring-cloud-eureka) | eureka 服务的注册和发现 | [Service Discovery: Eureka Server](https://cloud.spring.io/spring-cloud-static/Finchley.SR2/multi/multi_spring-cloud-eureka-server.html) |
|
||||
| [spring-cloud-eureka-cluster](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-cloud/spring-cloud-eureka-cluster) | eureka 高可用集群搭建 | [Service Discovery: Eureka Server](https://cloud.spring.io/spring-cloud-static/Finchley.SR2/multi/multi_spring-cloud-eureka-server.html) |
|
||||
| [spring-cloud-ribbon](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-cloud/spring-cloud-ribbon) | ribbon客户端负载均衡 | [Client Side Load Balancer: Ribbon](https://cloud.spring.io/spring-cloud-static/Finchley.SR2/multi/multi_spring-cloud-ribbon.html) |
|
||||
| [spring-cloud-openFeign](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-cloud/spring-cloud-feign) | openfeign 声明式服务调用 | [Declarative REST Client: Feign](https://cloud.spring.io/spring-cloud-static/Finchley.SR2/multi/multi_spring-cloud-feign.html) |
|
||||
| [spring-cloud-hystrix](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-cloud/spring-cloud-hystrix) | hystix 服务容错保护<br/>hystrix dashboard 断路器监控<br>turbine 断路器聚合监控 | [Circuit Breaker: Hystrix Clients](https://cloud.spring.io/spring-cloud-static/Finchley.SR2/multi/multi__circuit_breaker_hystrix_clients.html)<br/>[Hystrix metrics aggregation with Turbine ](https://cloud.spring.io/spring-cloud-static/Finchley.SR2/multi/multi_spring-cloud-consul-turbine.html) |
|
||||
| [spring-cloud-zuul](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-cloud/spring-cloud-zuul) | zuul 网关服务 | [Router and Filter: Zuul](https://cloud.spring.io/spring-cloud-static/Finchley.SR2/multi/multi__router_and_filter_zuul.html) |
|
||||
| [spring-cloud-sleuth-Zipkin](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-cloud/spring-cloud-sleuth-zipkin) | sleuth 分布式系统服务追踪 | [Spring Cloud Sleuth](https://cloud.spring.io/spring-cloud-static/Finchley.SR2/multi/multi__introduction.html#sleuth-adding-project) |
|
||||
| [spring-cloud-sleuth-zipkin](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-cloud/spring-cloud-sleuth-zipkin) | sleuth 分布式系统服务追踪 | [Spring Cloud Sleuth](https://cloud.spring.io/spring-cloud-static/Finchley.SR2/multi/multi__introduction.html#sleuth-adding-project) |
|
||||
| [spring-cloud-config-bus](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-cloud/spring-cloud-config) | config 分布式配置中心 <br>bus消息总线 实现配置热更新 | [Spring Cloud Config Client](https://cloud.spring.io/spring-cloud-static/Finchley.SR2/multi/multi__spring_cloud_config_client.html) |
|
||||
| [spring-cloud-stream](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-cloud/spring-cloud-stream) | stream 对消息服务的抽象整合 | [spring-cloud-stream](http://cloud.spring.io/spring-cloud-stream/single/spring-cloud-stream.html) |
|
||||
|
||||
|
BIN
pictures/SCSt-with-binder.png
Normal file
BIN
pictures/SCSt-with-binder.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 18 KiB |
BIN
pictures/spring-cloud-stream.png
Normal file
BIN
pictures/spring-cloud-stream.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 13 KiB |
@ -1,72 +0,0 @@
|
||||
<?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>
|
@ -1,14 +0,0 @@
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,22 +0,0 @@
|
||||
package com.heibaiying.stream.bean;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
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;
|
||||
}
|
@ -1,93 +0,0 @@
|
||||
package com.heibaiying.stream.controller;
|
||||
|
||||
import com.heibaiying.stream.bean.Programmer;
|
||||
import com.heibaiying.stream.stream.CustomStream;
|
||||
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 CustomStream customStream;
|
||||
|
||||
/***
|
||||
* 1、发送简单消息
|
||||
*/
|
||||
@RequestMapping("sendSimpleMessage")
|
||||
public void sendSimpleMessage() {
|
||||
customStream.input().send(MessageBuilder.withPayload("hello spring cloud stream").build());
|
||||
}
|
||||
|
||||
|
||||
/***
|
||||
* 2、发送消息体为对象的消息
|
||||
*/
|
||||
@RequestMapping("sendObject")
|
||||
public void sendObject() {
|
||||
Programmer programmer=new Programmer("pro",12,212.2f,new Date());
|
||||
customStream.input().send(MessageBuilder.withPayload(programmer).build());
|
||||
}
|
||||
|
||||
/**
|
||||
* 3、发送带有消息头的消息
|
||||
*/
|
||||
@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);
|
||||
customStream.input().send(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 4、条件消息 可以看做是消息路由键的一种实现
|
||||
*/
|
||||
@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);
|
||||
customStream.input().send(message);
|
||||
|
||||
// 创建消息头key 为 02 的消息
|
||||
programmer.setName("key02");
|
||||
map.put("key","02");
|
||||
MessageHeaders messageHeaders02=new MessageHeaders(map);
|
||||
Message<Programmer> message02= MessageBuilder.createMessage(programmer,messageHeaders02);
|
||||
customStream.input().send(message02);
|
||||
}
|
||||
|
||||
/**
|
||||
* 5、消息转发
|
||||
*/
|
||||
@RequestMapping("forward")
|
||||
public void forward(){
|
||||
customStream.input().send(MessageBuilder.withPayload("hello spring cloud stream").build());
|
||||
}
|
||||
|
||||
/**
|
||||
* 5、直接往output发消息
|
||||
*/
|
||||
@RequestMapping("toOutPut")
|
||||
public void toOutPut(){
|
||||
customStream.output().send(MessageBuilder.withPayload("direct to output channel").build());
|
||||
}
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
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 CustomStream {
|
||||
|
||||
String INPUT = "customInput";
|
||||
String OUTPUT = "customOutput";
|
||||
|
||||
@Input(CustomStream.INPUT)
|
||||
SubscribableChannel input();
|
||||
|
||||
@Output(CustomStream.OUTPUT)
|
||||
MessageChannel output();
|
||||
|
||||
}
|
@ -1,81 +0,0 @@
|
||||
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.messaging.handler.annotation.SendTo;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :消息的监听
|
||||
* 注意: 测试这个类的时候需要注释掉不必要的监听,因为对同一个通道存在多个监听,任何一个通道都不能存在返回值(如果有返回值应该指定出站目标) 我们下面的forward消息转发的方法是有返回值的
|
||||
* 否则会抛出异常: IllegalArgumentException: If multiple @StreamListener methods are listening to the same binding target, none of them may return a value
|
||||
*/
|
||||
|
||||
@Component
|
||||
@EnableBinding(CustomStream.class)
|
||||
@Slf4j
|
||||
public class StreamReceived {
|
||||
|
||||
@StreamListener(value = CustomStream.INPUT)
|
||||
public void simple(Object payload) {
|
||||
log.info("收到简单消息: {}", payload);
|
||||
}
|
||||
|
||||
@StreamListener(value = CustomStream.INPUT)
|
||||
public void object(Programmer programmer) {
|
||||
log.info("收到对象消息: {}", programmer);
|
||||
}
|
||||
|
||||
/*
|
||||
* 用 @Header 监听时候需要注意,指定名称的属性必须在消息头中存在 不然就会抛出异常 MessageHandlingException: Missing header 'XXXX' for method parameter type [class java.lang.String]
|
||||
*/
|
||||
@StreamListener(value = CustomStream.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 = CustomStream.INPUT, condition = "headers['key']=='01'")
|
||||
public void key01(@Payload Programmer programmer) {
|
||||
log.info("key01 监听器接收到消息: {}", programmer.getName());
|
||||
}
|
||||
|
||||
/*
|
||||
* 监听消息头key = 02 的消息
|
||||
*/
|
||||
@StreamListener(target = CustomStream.INPUT, condition = "headers['key']=='01'")
|
||||
public void key02(@Payload Programmer programmer) {
|
||||
log.info("key02 监听器接收到消息: {}", programmer.getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* 消息转发
|
||||
*/
|
||||
@StreamListener(target = CustomStream.INPUT)
|
||||
@SendTo(CustomStream.OUTPUT)
|
||||
public String forward(String payload){
|
||||
log.info("input forward: {}",payload);
|
||||
return "forward "+payload;
|
||||
}
|
||||
|
||||
@StreamListener(target = CustomStream.OUTPUT)
|
||||
public void outSimpleListen(String payload){
|
||||
log.info("output 收到简单消息: {}", payload);
|
||||
}
|
||||
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
spring:
|
||||
rabbitmq:
|
||||
host: localhost
|
||||
port: 5672
|
||||
username: guest
|
||||
password: guest
|
||||
# 指定消息的分组 默认交换机的类型是topic,发送消息是广播的方式,
|
||||
# 所有客户端都会受到消息,指定分组后只有一个客户端可以接收到消息
|
||||
cloud:
|
||||
stream:
|
||||
bindings:
|
||||
# 和我们在接口中声明消息名称的保持一致
|
||||
customInput:
|
||||
# 分组名称任意填写即可
|
||||
group: gro
|
@ -1,17 +0,0 @@
|
||||
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() {
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user