stream消息驱动之生产者

在实际的工作环境中可能要用到不同类型的消息中间件,而springcloud的消息驱动组件stream可以屏蔽不同消息中间件底层的差异,为实际开发提供统一的接口
尽管目前stream只支持rabbitmq和Kafka
下面介绍stream组件关于消息生产者的配置,这里消息中间件选用rabbitmq

一 环境配置

1 pom的核心依赖

由于使用该工程的项目仍然是一个微服务,依旧需要将服务注册到注册中心

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
        </dependency>
2 properties配置文件

在这里主要需要配置

  • rabbitmq相关信息的配置

  • 服务的整合处理,包括

    • 交换机名称
    • 消息的类型
    • 要绑定的消息的具体服务设置
server.port=8801

spring.application.name=cloud-stream-provider-8801

eureka.client.service-url.defaultZone=http://localhost:7001/eureka

#设置消息组件类型
spring.cloud.stream.binders.defaultRabbit.type=rabbit
#进行mq相关配置
spring.cloud.stream.binders.defaultRabbit.environment.host=localhost
spring.cloud.stream.binders.defaultRabbit.environment.port=5672
spring.cloud.stream.binders.defaultRabbit.environment.username=root
spring.cloud.stream.binders.defaultRabbit.environment.password=root
#服务的整合处理
#交换机名称
spring.cloud.stream.bindings.output.destination=studyExchange
#消息的类型
spring.cloud.stream.bindings.output.content-type=application/json
#要绑定的消息的具体服务设置
spring.cloud.stream.bindings.output.binder=defaultRabbit


3 主启动类
@SpringBootApplication
@EnableEurekaClient
public class CloudStreamMqProvider8801Application {

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

}

二 核心代码的编写

1 service接口

这里简单定义一个用于发送消息的接口

public interface MyService {
    String send();
}
2 实现业务逻辑

通常的service层要使用的注解是@Service,由于这里要将消息发送到交换机,所以使用的注解是@EnableBinding,注解中的value指定为Source.class表示这是消息的提供者
最后在接口的实现类中使用MessageChannel统一的接口对消息中间件进行操作

import com.chen.service.MyService;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;

import javax.annotation.Resource;
import java.util.UUID;

@EnableBinding(Source.class)    //定义消息的推送管道,Source表示消息源
public class MyServiceImpl implements MyService {
    @Resource
    private MessageChannel output;  //消息发送通道
    @Override
    public String send() {
        String serial = UUID.randomUUID().toString();
        output.send(MessageBuilder.withPayload(serial).build());
        return "ok";
    }
}
3 controller层

在这一层就是调用上一步的接口即可

@RestController
public class MyController {
    @Resource
    private MyService myService;
    @RequestMapping("/send")
    public String sendMessage(){
        return myService.send();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值