Spring Cloud Stream实现使用RocketMQ

maven配置 ,对于 spring cloud dependencies 这些依赖,更推荐放到一个通用依赖管理父项目

   <properties>
   
        <spring-cloud.version>Greenwich.SR2spring-cloud.version>
        <spring-cloud-alibaba.version>2.1.0.RELEASEspring-cloud-alibaba.version>
   properties>

 <dependencies>
      <dependency>
         	<groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-dependenciesartifactId>
            <version>${spring-cloud.version}version>
            <type>pomtype>
            <scope>importscope>
     dependency>

     <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-alibaba-dependenciesartifactId>
            <version>${spring-cloud-alibaba.version}version>
            <type>pomtype>
            <scope>importscope>
     dependency>


	 <dependency>
	        <groupId>org.springframework.bootgroupId>
	        <artifactId>spring-boot-starter-webartifactId>
	dependency>

	<dependency>
	       <groupId>org.springframework.bootgroupId>
	       <artifactId>spring-boot-starter-testartifactId>
	       <scope>testscope>
	dependency>

	<dependency>
	      <groupId>com.alibaba.cloudgroupId>
	      <artifactId>spring-cloud-starter-stream-rocketmqartifactId>
	dependency>
 dependencies>

生产者

application.yml内容

spring:
  application:
    name: rocketmq-provider
  cloud:
    stream:
      rocketmq:
        binder:
          # RocketMQ 服务器地址
          name-server: 192.168.18.47:9876
      bindings:
        # 这里是个 Map 类型参数,{} 为 YAML 中 Map 的行内写法
        output: {destination: test-topic, content-type: application/json}

server:
  port: 9093

management:
  endpoints:
    web:
      exposure:
        include: '*'

代码实现

@SpringBootApplication
@EnableBinding({Source.class})
public class SpringCloudRocketmqProviderApplication implements CommandLineRunner {
	@Autowired
    private MessageChannel output;
	

    public static void main(String[] args) {
        SpringApplication.run(SpringCloudRocketmqProviderApplication.class, args);
    }

    /**
     * 实现了 CommandLineRunner 接口,是为了 Spring Boot 启动时执行任务
     * @param args
     * @throws Exception
     */
    @Override
    public void run(String... args) throws Exception {
        output.send(MessageBuilder.withPayload("Hello RocketMQ Consumer").build());
    }

}

消费者

然后我们在创建消费者项目,下面是Maven配置

  <properties>
   
        <spring-cloud.version>Greenwich.SR2spring-cloud.version>
        <spring-cloud-alibaba.version>2.1.0.RELEASEspring-cloud-alibaba.version>
   properties>

 <dependencies>
      <dependency>
         	<groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-dependenciesartifactId>
            <version>${spring-cloud.version}version>
            <type>pomtype>
            <scope>importscope>
     dependency>

     <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-alibaba-dependenciesartifactId>
            <version>${spring-cloud-alibaba.version}version>
            <type>pomtype>
            <scope>importscope>
     dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>

        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-stream-rocketmqartifactId>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.18.8version>
        dependency>

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-jsonartifactId>
        dependency>

    dependencies>

application.yml配置

spring:
  application:
      name: rocketmq-consume-example
  cloud:
    stream:
      rocketmq:
        binder:
          name-server: 192.168.18.47:9876
        bindings:
          input:
            consumer:
              orderly: true
      bindings:
        input:
          destination: test-topic
          content-type: application/json
          group: test-group

server:
  port: 9094

management:
  endpoints:
    web:
      exposure:
        include: '*'

下面我们的接收处理类

@SpringBootApplication
@EnableBinding({Sink.class})
public class RocketmqConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(RocketmqConsumerApplication.class, args);
    }

    @StreamListener("input")
    public void receiveInput(String message) {
        System.out.println("Receive input: " + message);
    }
}

案例测试,下面是消费者的控制台
在这里插入图片描述

你可能感兴趣的:(Spring,Cloud微服务)