Spring cloud:Stream配置

简介:

Spring Cloud Stream is a framework for building highly scalable event-driven microservices connected with shared messaging systems.

The framework provides a flexible programming model built on already established and familiar Spring idioms and best practices, including support for persistent pub/sub semantics, consumer groups, and stateful partitions.

Spring Cloud Stream是一个框架,用于构建连接到共享消息传递系统的高度可伸缩的事件驱动微服务。

该框架提供了一个灵活的编程模型,它建立在已经建立和熟悉的Spring习惯用法和最佳实践之上,包括对持久发布/子语义、消费者组和有状态分区的支持。

环境准备:Consul注册中心和RabbitMQ

POM

<dependency>
    <groupId>org.springframework.cloudgroupId>
    <artifactId>spring-cloud-starter-consul-discoveryartifactId>
dependency>
<dependency>
    <groupId>org.springframework.cloudgroupId>
    <artifactId>spring-cloud-starter-stream-rabbitartifactId>
dependency>
<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-actuatorartifactId>
dependency>

YAML(服务提供者)

stream部分,在spring.cloud层级下

output:提供者

input:消费者

destination:就是RabbitMQ的Exchange交换机

content-type:就是消息内容的格式

binder:就是bindings和binders的绑定关系

 stream:
      binders:
        defaultRabbit:
          type: rabbit
      bindings:
        output:
          destination: cloudStreamExchange
          content-type: application/json
          binder: defaultRabbit
server:
  port: 8801

spring:
  application:
    name: cloud-stream-provider

  rabbitmq:
    host: 123.57.x.x
    username: 123
    password: 123

  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        prefer-ip-address: true
        service-name: ${spring.application.name}
        instance-id: provider8801

    stream:
      binders:
        defaultRabbit:
          type: rabbit
      bindings:
        output:
          destination: cloudStreamExchange
          content-type: application/json
          binder: defaultRabbit

YAMl(消费者)

持久化:指定group组名,在同一个组内,消息只被消费一次,且指定group后会开启持久化(也就是提供者发布消息后,消费者还没有上线,等消费者上线后还是可以获取消息);如果不指定,stream会随机分配一个ID,所以消息会被重复消费,且不会被持久化
server:
  port: 8803

spring:
  application:
    name: cloud-stream-consumer

  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        prefer-ip-address: true
        service-name: ${spring.application.name}
        instance-id: comsumer8803

    stream:
      binders:
        defaultRabbit:
          type: rabbit
      bindings:
        input:
          destination: cloudStreamExchange
          content-type: application/json
          binder: defaultRabbit
          group: liveA

  rabbitmq:
    host: 123.57.x.x
    username: 123
    password: 123

主要代码

Service(服务提供者)

@EnableBinding(Source.class)

不需要加@Service注解,只需@EnableBinding(Source.class)

然后用MessageChannel来发送消息

import com.live.service.SendMessageService;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;

import javax.annotation.Resource;
import java.util.UUID;

@EnableBinding(Source.class)
public class SendMessageServiceImpl implements SendMessageService {

    @Resource
    private MessageChannel output;

    @Override
    public String send() {
        String serial = UUID.randomUUID().toString();
        output.send(MessageBuilder.withPayload(serial).build());
        System.out.println(serial);
        return serial;
    }

}

Component(消费者)

@EnableBinding(Sink.class):匹配binding关系

@StreamListener(Sink.INPUT):监听消息

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

@Component
@EnableBinding(Sink.class)
public class ReceiveComponent {

    @Value("${server.port}")
    private String serverPort;

    @StreamListener(Sink.INPUT)
    public void receive(Message<String> message) {
        System.out.println(serverPort + ": \t" + message.getPayload());
    }

}

更多具体、详细的配置查阅官网文档

你可能感兴趣的:(spring-boot,java,rabbitmq,cloud)