整合spring cloud云架构 -消息驱动 Spring Cloud Stream

简介: Spring Cloud Stream

在使用spring cloud云架构的时候,我们不得不使用Spring cloud Stream,因为消息中间件的使用在项目中无处不在,我们公司后面做了娱乐方面的APP,在使用spring cloud做架构的时候,其中消息的异步通知,业务的异步处理都需要使用消息中间件机制。spring cloud的官方给出的集成建议(使用rabbit mq和kafka),我看了一下源码和配置,只要把rabbit mq集成,kafka只是换了一个pom配置jar包而已,闲话少说,我们就直接进入配置实施:

  1. 简介:

Spring cloud Stream 数据流操作开发包,封装了与Redis,Rabbit、Kafka等发送接收消息。

  1. 使用工具:

rabbit,具体的下载和安装细节我这里不做太多讲解,网上的实例太多了

  1. 创建commonservice-mq-producer消息的发送者项目,在pom里面配置stream-rabbit的依赖


<groupId>org.springframework.cloud</groupId>  
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>  
AI 代码解读

  1. 在yml文件里面配置rabbit mq

Java代码 收藏代码
server:
port: 5666
spring:
application:

name: commonservice-mq-producer  
AI 代码解读

profiles:

active: dev  
AI 代码解读

cloud:

config:  
  discovery:   
    enabled: true  
    service-id: commonservice-config-server  
AI 代码解读

# rabbitmq和kafka都有相关配置的默认值,如果修改,可以再次进行配置

stream:  
  bindings:  
    mqScoreOutput:   
      destination: honghu_exchange  
      contentType: application/json  
        
AI 代码解读

rabbitmq:

 host: localhost  
 port: 5672  
 username: honghu  
 password: honghu</span>  
AI 代码解读

eureka:
client:

service-url:  
  defaultZone: http://honghu:123456@localhost:8761/eureka  
AI 代码解读

instance:

prefer-ip-address: true</span>  
AI 代码解读
  1. 定义接口ProducerService

Java代码 收藏代码
package com.honghu.cloud.producer;

import org.springframework.cloud.stream.annotation.Output;
import org.springframework.messaging.SubscribableChannel;

public interface ProducerService {

  
String SCORE_OUPUT = "mqScoreOutput";  
  
@Output(ProducerService.SCORE_OUPUT)  
SubscribableChannel sendMessage();  
AI 代码解读

}

  1. 定义绑定

Java代码 收藏代码
package com.honghu.cloud.producer;

import org.springframework.cloud.stream.annotation.EnableBinding;

@EnableBinding(ProducerService.class)
public class SendServerConfig {

}

  1. 定义发送消息业务ProducerController

Java代码 收藏代码
package com.honghu.cloud.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.honghu.cloud.common.code.ResponseCode;
import com.honghu.cloud.common.code.ResponseVO;
import com.honghu.cloud.entity.User;
import com.honghu.cloud.producer.ProducerService;

import net.sf.json.JSONObject;

@RestController
@RequestMapping(value = "producer")
public class ProducerController {

  
@Autowired  
private ProducerService producerService;  
  
  
/** 
 * 通过get方式发送</span>对象<span style="font-size: 16px;"> 
 * @param name 路径参数 
 * @return 成功|失败 
 */  
@RequestMapping(value = "/sendObj", method = RequestMethod.GET)  
public ResponseVO sendObj() {  
    User user = new User(1, "hello User");  
    <span style="color: #ff0000;">Message<User> msg = MessageBuilder.withPayload(user).build();</span>  
    boolean result = producerService.sendMessage().send(msg);  
    if(result){  
        return ResponseCode.buildEnumResponseVO(ResponseCode.RESPONSE_CODE_SUCCESS, false);  
    }  
    return ResponseCode.buildEnumResponseVO(ResponseCode.RESPONSE_CODE_FAILURE, false);  
}  
  
  
/** 
 * 通过get方式发送字符串消息 
 * @param name 路径参数 
 * @return 成功|失败 
 */  
@RequestMapping(value = "/send/{name}", method = RequestMethod.GET)  
public ResponseVO send(@PathVariable(value = "name", required = true) String name) {  
    Message msg = MessageBuilder.withPayload(name.getBytes()).build();  
    boolean result = producerService.sendMessage().send(msg);  
    if(result){  
        return ResponseCode.buildEnumResponseVO(ResponseCode.RESPONSE_CODE_SUCCESS, false);  
    }  
    return ResponseCode.buildEnumResponseVO(ResponseCode.RESPONSE_CODE_FAILURE, false);  
}  
  
/** 
 * 通过post方式发送</span>json对象<span style="font-size: 16px;"> 
 * @param name 路径参数 
 * @return 成功|失败 
 */  
@RequestMapping(value = "/sendJsonObj", method = RequestMethod.POST)  
public ResponseVO sendJsonObj(@RequestBody JSONObject jsonObj) {  
    Message<JSONObject> msg = MessageBuilder.withPayload(jsonObj).build();  
    boolean result = producerService.sendMessage().send(msg);  
    if(result){  
        return ResponseCode.buildEnumResponseVO(ResponseCode.RESPONSE_CODE_SUCCESS, false);  
    }  
    return ResponseCode.buildEnumResponseVO(ResponseCode.RESPONSE_CODE_FAILURE, false);  
}  
AI 代码解读

}

  1. 创建commonservice-mq-consumer1消息的消费者项目,在pom里面配置stream-rabbit的依赖

Java代码 收藏代码

<groupId>org.springframework.cloud</groupId>  
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>  
AI 代码解读

  1. 在yml文件中配置:

Java代码 收藏代码
server:
port: 5111
spring:
application:

name: commonservice-mq-consumer1  
AI 代码解读

profiles:

active: dev  
AI 代码解读

cloud:

config:  
  discovery:   
    enabled: true  
    service-id: commonservice-config-server  
      
<span style="color: #ff0000;">stream:  
  bindings:  
    mqScoreInput:  
      group: honghu_queue  
      destination: honghu_exchange  
      contentType: application/json  
        
AI 代码解读

rabbitmq:

 host: localhost  
 port: 5672  
 username: honghu  
 password: honghu</span>  
AI 代码解读

eureka:
client:

service-url:  
  defaultZone: http://honghu:123456@localhost:8761/eureka  
AI 代码解读

instance:

prefer-ip-address: true  
AI 代码解读
  1. 定义接口ConsumerService

Java代码 收藏代码
package com.honghu.cloud.consumer;

import org.springframework.cloud.stream.annotation.Input;
import org.springframework.messaging.SubscribableChannel;

public interface ConsumerService {

  
<span style="color: #ff0000;">String SCORE_INPUT = "mqScoreInput";  

@Input(ConsumerService.SCORE_INPUT)  
SubscribableChannel sendMessage();</span>  
AI 代码解读

}

  1. 定义启动类和消息消费

Java代码 收藏代码
package com.honghu.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;

import com.honghu.cloud.consumer.ConsumerService;
import com.honghu.cloud.entity.User;

@EnableEurekaClient
@SpringBootApplication
@EnableBinding(ConsumerService.class) //可以绑定多个接口
public class ConsumerApplication {

  
public static void main(String[] args) {  
    SpringApplication.run(ConsumerApplication.class, args);  
}  
  
<span style="color: #ff0000;">@StreamListener(ConsumerService.SCORE_INPUT)  
public void onMessage(Object obj) {  
    System.out.println("消费者1,接收到的消息:" + obj);  
}</span>  
AI 代码解读

}

  1. 分别启动commonservice-mq-producer、commonservice-mq-consumer1
  2. 通过postman来验证消息的发送和接收

可以看到接收到了消息,下一章我们介绍mq的集群方案。

到此,整个消息中心方案集成完毕(企业架构源码可以加求球:三五三六二四七二五九)

欢迎大家和我一起学习spring cloud构建微服务云架构,我这边会将近期研发的spring cloud微服务云架构的搭建过程和精髓记录下来,帮助更多有兴趣研发spring cloud框架的朋友,大家来一起探讨spring cloud架构的搭建过程及如何运用于企业项目。

目录
相关文章
Spring Cloud Alibaba与Spring Cloud区别和联系?
Spring Cloud Alibaba与Spring Cloud区别和联系?
🛡️Spring Boot 3 整合 Spring Cloud Gateway 工程实践
本文介绍了如何使用Spring Cloud Alibaba 2023.0.0.0技术栈构建微服务网关,以应对微服务架构中流量治理与安全管控的复杂性。通过一个包含鉴权服务、文件服务和主服务的项目,详细讲解了网关的整合与功能开发。首先,通过统一路由配置,将所有请求集中到网关进行管理;其次,实现了限流防刷功能,防止恶意刷接口;最后,添加了登录鉴权机制,确保用户身份验证。整个过程结合Nacos注册中心,确保服务注册与配置管理的高效性。通过这些实践,帮助开发者更好地理解和应用微服务网关。
220 0
🛡️Spring Boot 3 整合 Spring Cloud Gateway 工程实践
Jeesite5:Star24k,Spring Boot 3.3+Vue3实战开源项目,架构深度拆解!让企业级项目开发效率提升300%的秘密武器
Jeesite5 是一个基于 Spring Boot 3.3 和 Vue3 的企业级快速开发平台,集成了众多优秀开源项目,如 MyBatis Plus、Bootstrap、JQuery 等。它提供了模块化设计、权限管理、多数据库支持、代码生成器和国际化等功能,极大地提高了企业级项目的开发效率。Jeesite5 广泛应用于企业管理系统、电商平台、客户关系管理和知识管理等领域。通过其强大的功能和灵活性,Jeesite5 成为了企业级开发的首选框架之一。访问 [Gitee 页面](https://gitee.com/thinkgem/jeesite5) 获取更多信息。
Jeesite5:Star24k,Spring Boot 3.3+Vue3实战开源项目,架构深度拆解!让企业级项目开发效率提升300%的秘密武器
|
3月前
|
Spring底层架构核心概念解析
理解 Spring 框架的核心概念对于开发和维护 Spring 应用程序至关重要。IOC 和 AOP 是其两个关键特性,通过依赖注入和面向切面编程实现了高效的模块化和松耦合设计。Spring 容器管理着 Beans 的生命周期和配置,而核心模块为各种应用场景提供了丰富的功能支持。通过全面掌握这些核心概念,开发者可以更加高效地利用 Spring 框架开发企业级应用。
123 18
微服务架构设计与实践:用Spring Cloud实现抖音的推荐系统
本文基于Spring Cloud实现了一个简化的抖音推荐系统,涵盖用户行为管理、视频资源管理、个性化推荐和实时数据处理四大核心功能。通过Eureka进行服务注册与发现,使用Feign实现服务间调用,并借助Redis缓存用户画像,Kafka传递用户行为数据。文章详细介绍了项目搭建、服务创建及配置过程,包括用户服务、视频服务、推荐服务和数据处理服务的开发步骤。最后,通过业务测试验证了系统的功能,并引入Resilience4j实现服务降级,确保系统在部分服务故障时仍能正常运行。此示例旨在帮助读者理解微服务架构的设计思路与实践方法。
184 17
AI 时代:从 Spring Cloud Alibaba 到 Spring AI Alibaba
本次分享由阿里云智能集团云原生微服务技术负责人李艳林主讲,主题为“AI时代:从Spring Cloud Alibaba到Spring AI Alibaba”。内容涵盖应用架构演进、AI agent框架发展趋势及Spring AI Alibaba的重磅发布。分享介绍了AI原生架构与传统架构的融合,强调了API优先、事件驱动和AI运维的重要性。同时,详细解析了Spring AI Alibaba的三层抽象设计,包括模型支持、工作流智能体编排及生产可用性构建能力,确保安全合规、高效部署与可观测性。最后,结合实际案例展示了如何利用私域数据优化AI应用,提升业务价值。
324 4
智慧工地云平台的技术架构解析:微服务+Spring Cloud如何支撑海量数据?
慧工地解决方案依托AI、物联网和BIM技术,实现对施工现场的全方位、立体化管理。通过规范施工、减少安全隐患、节省人力、降低运营成本,提升工地管理的安全性、效率和精益度。该方案适用于大型建筑、基础设施、房地产开发等场景,具备微服务架构、大数据与AI分析、物联网设备联网、多端协同等创新点,推动建筑行业向数字化、智能化转型。未来将融合5G、区块链等技术,助力智慧城市建设。
110 0
建筑施工一体化信息管理平台源码,支持微服务架构,采用Java、Spring Cloud、Vue等技术开发。
智慧工地云平台是专为建筑施工领域打造的一体化信息管理平台,利用大数据、云计算、物联网等技术,实现施工区域各系统数据汇总与可视化管理。平台涵盖人员、设备、物料、环境等关键因素的实时监控与数据分析,提供远程指挥、决策支持等功能,提升工作效率,促进产业信息化发展。系统由PC端、APP移动端及项目、监管、数据屏三大平台组成,支持微服务架构,采用Java、Spring Cloud、Vue等技术开发。
157 7
微服务——SpringBoot使用归纳——Spring Boot集成MyBatis——基于 xml 的整合
本教程介绍了基于XML的MyBatis整合方式。首先在`application.yml`中配置XML路径,如`classpath:mapper/*.xml`,然后创建`UserMapper.xml`文件定义SQL映射,包括`resultMap`和查询语句。通过设置`namespace`关联Mapper接口,实现如`getUserByName`的方法。Controller层调用Service完成测试,访问`/getUserByName/{name}`即可返回用户信息。为简化Mapper扫描,推荐在Spring Boot启动类用`@MapperScan`注解指定包路径避免逐个添加`@Mapper`
57 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等