继续Springcloud 之 eureka注册中心加feign调用Windows 安装 rabbitMQ,整合RabbitMQ。

1.在eureka-producer、eureka-consumer两个项目的pom.xml中加入

<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
		</dependency>

2.在eureka-producer项目中创建输出通道接口MessageService

public interface MessageService {
//自定义通道名为messge
	@Output(value = "message")
	SubscribableChannel getOutput();
}

3.在eureka-producer项目中创建消息发送接口

@RestController
@RequestMapping("/message")
public class MessageController {

	@Autowired
	private MessageService sendMessageService;

	@RequestMapping("/send")
	public String sendMessage(String messageInfo) {
		sendMessageService.getOutput()
				.send(MessageBuilder.withPayload(messageInfo).build());
		return "success";
	}
}

4.在eureka-producer项目启动类中绑定输出通道

@EnableBinding(MessageService.class)

5.在eureka-consumer项目中创建输入通道接口

public interface ReceiveMessageService {
   //与输出通道名相一致
	@Input(value = "message")
	SubscribableChannel getInput();
}

6.在eureka-consumer项目中创建消息接收接口

@RestController
public class ReceiveController {
	@StreamListener("message")
	public void onReceive(String messageInfo) {
		System.out.println("接受到的消息:" + messageInfo);
	}
}

7.同样,要在eureka-consumer项目中绑定输入通道

@EnableBinding(ReceiveMessageService.class)

8.测试

按顺序启动springcloud-eureka、eureka-producer、eureka-consumer三个项目,结果如下:

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐