当前位置:   article > 正文

springcloud-eureka集群-整合Rabbit消息中间件_eureka rabbit

eureka rabbit

配置好rabbitmq服务器

编写消息发送者

添加依赖pom.xml

  1. <!-- 消息驱动 -->
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
  5. </dependency>

修改配置application.yml

  1. server:
  2. port: 8088
  3. spring:
  4. application:
  5. name: tms-send
  6. rabbitmq:
  7. host: 192.168.1.109
  8. port: 5672
  9. username: rabbit-username
  10. password: rabbit-password

启动类 TmsSendApplication.java

  1. @SpringBootApplication
  2. @EnableEurekaClient
  3. @EnableBinding(RabbitSendService.class)
  4. public class TmsSendApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(TmsSendApplication.class, args);
  7. }
  8. }

编写发送接口

  1. public interface RabbitSendService {
  2. @Output(value = "myInput")
  3. SubscribableChannel sendMsg() throws Exception;
  4. }

编写controller

  1. @RestController
  2. public class TestController {
  3. @Autowired
  4. private RabbitSendService rabbitService;
  5. @GetMapping(value = "/sendMsg")
  6. public String sendMsg() throws Exception {
  7. Message message = MessageBuilder.withPayload("Hello World".getBytes()).build();
  8. rabbitService.sendMsg().send(message);
  9. return "success";
  10. }
  11. }

三、编写消息接受者

添加依赖pom.xml

  1. <!-- 消息驱动 -->
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
  5. </dependency>

修改配置application.yml

  1. server:
  2. port: 8088
  3. spring:
  4. application:
  5. name: tms-send
  6. rabbitmq:
  7. host: 192.168.1.109
  8. port: 5672
  9. username: rabbit-username
  10. password: rabbit-password

启动类 TmsSendApplication.java

  1. @SpringBootApplication
  2. @EnableEurekaClient
  3. @EnableBinding(RabbitReceiveService.class)
  4. public class TmsReceiveApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(TmsReceiveApplication.class, args);
  7. }
  8. }

编写消息监听

  1. @Component
  2. public class RabbitReceiveListener {
  3. @StreamListener(value = "myInput")
  4. public void onReceive(byte[] msg){
  5. System.out.println(new String(msg));
  6. }
  7. }

编写消息接口

  1. public interface RabbitReceiveService {
  2. @Input(value = "myInput")
  3. SubscribableChannel inputMsg() throws Exception;
  4. }

四、启动eureka注册中心,启动消息发送者与消息接收者

访问接口 http://127.0.0.1:8088/sendMsg

消息接收者的的监听器获取到的消息

Hello World

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/963336
推荐阅读
相关标签
  

闽ICP备14008679号