spring-cloud-stream使用

一、创建spring-cloud-stream的生产者服务


1.创建module,cloud-stream-rabbitmq-provider8801
2.引入以pom下依赖
<dependencies>
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-webartifactId>
    dependency>
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-actuatorartifactId>
    dependency>
    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
    dependency>
    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-stream-rabbitartifactId>
    dependency>
dependencies>
3.配置application.yml
server:
  port: 8801
spring:
  application:
    name: cloub-stream-provider
  cloud:
    stream:
      binders: 
        defaultRabbit:
          type: rabbit
          environment:
            spring:
              rabbitmq:
                host: localhost
                port: 5672
                username: guest
                password: guest
      bindings:
        output:
          destination: studyExchange
          content-type: application/json
          binder: defaultRabbit
eureka:
  client:
    #      抓取eureka中已注册的服务,集群服务必须开启,ribbon才能实现负载均衡
    fetch-registry: true
    #    是否向eureka注册自己
    register-with-eureka: true
    service-url:
      defaultZone: http://localhost:7001/eureka/
  instance:
    instance-id: cloud-stream-provider8801
    prefer-ip-address: true
4.主启动类
@SpringBootApplication
public class StreamMQMain8801 {

    public static void main(String[] args) {
        SpringApplication.run(StreamMQMain8801.class,args);
    }
}
5.业务类
业务类定义完成以后,写一个controller类,然后再controller类进行调用MessageProviderImpl
import cn.hehe.provider.service.IMessageProvider;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;

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

/**
 * @program: springCloud2020
 * @description:
 * @author: Mr.si
 * @create: 2021-12-08 21:50
 **/

@EnableBinding(Source.class) //定义消息推送管道
public class MessageProviderImpl implements IMessageProvider {

    @Resource
    private MessageChannel output; //消息发送管道

    public String send() {
        String uuid = UUID.randomUUID().toString();
        System.out.println("=====> "+uuid);
        Message<String> message = MessageBuilder.withPayload(uuid).build();
        output.send(message);
        return null;
    }
}

二、创建spring-cloud-stream的消费者服务


1.创建module,cloud-stream-rabbitmq-consumer8802
2.引入以pom下依赖
<dependencies>
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-webartifactId>
    dependency>
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-actuatorartifactId>
    dependency>
    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
    dependency>
    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-stream-rabbitartifactId>
    dependency>
dependencies>
3.配置application.yml
server:
  port: 8802
spring:
  application:
    name: cloub-stream-consumer
  cloud:
    stream:
      binders: 
        defaultRabbit:
          type: rabbit
          environment:
            spring:
              rabbitmq:
                host: localhost
                port: 5672
                username: guest
                password: guest
      bindings:
        input:
          destination: studyExchange
          content-type: application/json
          binder: defaultRabbit
eureka:
  client:
    #      抓取eureka中已注册的服务,集群服务必须开启,ribbon才能实现负载均衡
    fetch-registry: true
    #    是否向eureka注册自己
    register-with-eureka: true
    service-url:
      defaultZone: http://localhost:7001/eureka/
  instance:
    instance-id: cloud-stream-consumer8802
    prefer-ip-address: true
4.启动类
@SpringBootApplication
public class StreamMQMain8802 {

    public static void main(String[] args) {
        SpringApplication.run(StreamMQMain8802.class,args);
    }
}
5.业务类
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;

/**
 * @program: springCloud2020
 * @description:
 * @author: Mr.si
 * @create: 2021-12-08 22:28
 **/


@Component
@EnableBinding({Sink.class})
public class ConsumerListenerController {

    @StreamListener(Sink.INPUT)
    public void input(Message<String> message){
        String payload = message.getPayload();
        System.out.println("=====> "+payload);
    }
}

重复消费问题,

默认配置,会创建两个不同的组队列,两个队列同时接受一个topic主题,两个会同时收到消息,造成重复消费
解决

将队列配置成同一个组,组内就会有竞争关系;

消费侧配置更改

server:
  port: 8802
spring:
  application:
    name: cloub-stream-consumer
  cloud:
    stream:
      binders: 
        defaultRabbit:
          type: rabbit
          environment:
            spring:
              rabbitmq:
                host: localhost
                port: 5672
                username: guest
                password: guest
      bindings:
        input:
          destination: studyExchange
          content-type: application/json
          binder: defaultRabbit
          group: groupA  #指定组名,解决重复消费问题
eureka:
  client:
    #      抓取eureka中已注册的服务,集群服务必须开启,ribbon才能实现负载均衡
    fetch-registry: true
    #    是否向eureka注册自己
    register-with-eureka: true
    service-url:
      defaultZone: http://localhost:7001/eureka/
  instance:
    instance-id: cloud-stream-consumer8802
    prefer-ip-address: true

你可能感兴趣的:(Spring,Cloud,spring,cloud,spring)