更改消费模块的ymlcloud2022 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
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 );
}
}
运行



