整合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>  

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

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

name: commonservice-mq-producer  

profiles:

active: dev  

cloud:

config:  
  discovery:   
    enabled: true  
    service-id: commonservice-config-server  

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

stream:  
  bindings:  
    mqScoreOutput:   
      destination: honghu_exchange  
      contentType: application/json  
        

rabbitmq:

 host: localhost  
 port: 5672  
 username: honghu  
 password: honghu</span>  

eureka:
client:

service-url:  
  defaultZone: http://honghu:123456@localhost:8761/eureka  

instance:

prefer-ip-address: true</span>  
  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();  

}

  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);  
}  

}

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

Java代码 收藏代码

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

  1. 在yml文件中配置:

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

name: commonservice-mq-consumer1  

profiles:

active: dev  

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  
        

rabbitmq:

 host: localhost  
 port: 5672  
 username: honghu  
 password: honghu</span>  

eureka:
client:

service-url:  
  defaultZone: http://honghu:123456@localhost:8761/eureka  

instance:

prefer-ip-address: true  
  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>  

}

  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>  

}

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

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

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

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

目录
相关文章
|
23天前
|
人工智能 运维 安全
配置驱动的动态 Agent 架构网络:实现高效编排、动态更新与智能治理
本文所阐述的配置驱动智能 Agent 架构,其核心价值在于为 Agent 开发领域提供了一套通用的、可落地的标准化范式。
282 30
|
21天前
|
人工智能 安全 数据可视化
配置驱动的动态Agent架构网络:实现高效编排、动态更新与智能治理
本文系统性地提出并阐述了一种配置驱动的独立运行时Agent架构,旨在解决当前低代码/平台化Agent方案在企业级落地时面临困难,为Agent开发领域提供了一套通用的、可落地的标准化范式。
238 15
配置驱动的动态Agent架构网络:实现高效编排、动态更新与智能治理
|
1月前
|
消息中间件 存储 Java
RabbitMQ 和 Spring Cloud Stream 实现异步通信
本文介绍了在微服务架构中,如何利用 RabbitMQ 作为消息代理,并结合 Spring Cloud Stream 实现高效的异步通信。内容涵盖异步通信的优势、RabbitMQ 的核心概念与特性、Spring Cloud Stream 的功能及其与 RabbitMQ 的集成方式。通过这种组合,开发者可以构建出具备高可用性、可扩展性和弹性的分布式系统,满足现代应用对快速响应和可靠消息传递的需求。
111 2
RabbitMQ 和 Spring Cloud Stream 实现异步通信
|
1月前
|
Java 数据库 数据安全/隐私保护
Spring Boot四层架构深度解析
本文详解Spring Boot四层架构(Controller-Service-DAO-Database)的核心思想与实战应用,涵盖职责划分、代码结构、依赖注入、事务管理及常见问题解决方案,助力构建高内聚、低耦合的企业级应用。
450 1
|
1月前
|
Kubernetes Java 微服务
Spring Cloud 微服务架构技术解析与实践指南
本文档全面介绍 Spring Cloud 微服务架构的核心组件、设计理念和实现方案。作为构建分布式系统的综合工具箱,Spring Cloud 为微服务架构提供了服务发现、配置管理、负载均衡、熔断器等关键功能的标准化实现。本文将深入探讨其核心组件的工作原理、集成方式以及在实际项目中的最佳实践,帮助开发者构建高可用、可扩展的分布式系统。
288 0
|
3月前
|
数据可视化 IDE Java
OneCode图生代码技术深度解析:从可视化设计到注解驱动实现的全链路架构
OneCode图生代码技术通过可视化设计与Java注解驱动,实现UI到代码的高效转换,支持设计即开发、组件复用与动态加载,提升企业应用开发效率与协作能力。
OneCode图生代码技术深度解析:从可视化设计到注解驱动实现的全链路架构
|
3月前
|
存储 Java 数据库连接
简单学Spring Boot | 博客项目的三层架构重构
本案例通过采用三层架构(数据访问层、业务逻辑层、表现层)重构项目,解决了集中式开发导致的代码臃肿问题。各层职责清晰,结合依赖注入实现解耦,提升了系统的可维护性、可测试性和可扩展性,为后续接入真实数据库奠定基础。
305 0
|
5月前
|
Java 开发者 Spring
Spring框架 - 深度揭秘Spring框架的基础架构与工作原理
所以,当你进入这个Spring的世界,看似一片混乱,但细看之下,你会发现这里有个牢固的结构支撑,一切皆有可能。不论你要建设的是一座宏大的城堡,还是个小巧的花园,只要你的工具箱里有Spring,你就能轻松搞定。
218 9

热门文章

最新文章