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

一、配置好rabbitmq服务器



二、编写消息发送者

添加依赖pom.xml

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

修改配置application.yml

server:
  port: 8088

spring:
  application:
    name: tms-send
  rabbitmq:
    host: 192.168.1.109
    port: 5672
    username: rabbit-username
    password: rabbit-password

启动类 TmsSendApplication.java

@SpringBootApplication
@EnableEurekaClient
@EnableBinding(RabbitSendService.class)
public class TmsSendApplication {
   public static void main(String[] args) {
      SpringApplication.run(TmsSendApplication.class, args);
   }
}

编写发送接口

public interface RabbitSendService {
    @Output(value = "myInput")
    SubscribableChannel sendMsg() throws Exception;
}

编写controller

@RestController
public class TestController {

    @Autowired
    private RabbitSendService rabbitService;

    @GetMapping(value = "/sendMsg")
    public String sendMsg() throws Exception {
        Message message = MessageBuilder.withPayload("Hello World".getBytes()).build();
        rabbitService.sendMsg().send(message);
        return "success";
    }
}

三、编写消息接受者

添加依赖pom.xml

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

修改配置application.yml

server:
  port: 8088

spring:
  application:
    name: tms-send
  rabbitmq:
    host: 192.168.1.109
    port: 5672
    username: rabbit-username
    password: rabbit-password

启动类 TmsSendApplication.java

@SpringBootApplication
@EnableEurekaClient
@EnableBinding(RabbitReceiveService.class)
public class TmsReceiveApplication {
   public static void main(String[] args) {
      SpringApplication.run(TmsReceiveApplication.class, args);
   }
}

编写消息监听

@Component
public class RabbitReceiveListener {
    @StreamListener(value = "myInput")
    public void onReceive(byte[] msg){
        System.out.println(new String(msg));
    }
}

编写消息接口

public interface RabbitReceiveService {
    @Input(value = "myInput")
    SubscribableChannel inputMsg() throws Exception;
}


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

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


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

Hello World





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值