SpringCloud-SpringCloud Stream消息驱动之消息消费者(Day10)

SpringCloud-SpringCloud Stream消息驱动之消息消费者(Day10),第1张

SpringCloud-SpringCloud Stream消息驱动之消息消费者(Day10) 创建消息消费模块cloud-stream-rabbitmq-consumer8802



更改消费模块的pom


    
        cloud2022
        org.gcl.learn
        1.0-SNAPSHOT
    
    4.0.0

    cloud-stream-rabbitmq-consumer8802

    
        
            org.springframework.cloud
            spring-cloud-starter-stream-rabbit
        

        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-actuator
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        

        
            org.projectlombok
            lombok
            true
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            junit
            junit
            test
        
    


更改消费模块的yml
server:
  port: 8802

spring:
  application:
    name: cloud-stream-rabbitmq-consumer
  rabbitmq:
    host: xxx.xx.xx.xx
    port: 5672
    username: guest
    password: guest
  cloud:
    stream:
      binders:
        defaultRabbit:
          type: rabbit
      bindings:
        input:
          destination: studyExchange
          content-type: application/json
          binder: defaultRabbit
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:7001/eureka
    register-with-eureka: true
    fetch-registry: true
更改消费模块的主启动类
package com.gcl.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@EnableEurekaClient
@SpringBootApplication
public class StreamRabbitMQMain8802 {

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

}
更改消费模块的业务类
package com.gcl.springcloud.service;

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 StreamRabbitMQService {

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

    @StreamListener(Sink.INPUT)
    public void input(Message message){
        System.out.println("***************接收到的消息:" + message.getPayload() + " server.port : " + serverPort );
    }

}
运行




欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5677501.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存