整合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架构的搭建过程及如何运用于企业项目。

目录
相关文章
|
1月前
|
Cloud Native Java 对象存储
面向未来的架构设计:Spring Cloud和Netflix OSS在云原生环境下的发展趋势
展望未来,随着5G、边缘计算等新技术的兴起,微服务架构的设计理念将会更加深入人心,Spring Cloud和Netflix OSS也将继续引领技术潮流,为企业带来更为高效、灵活且强大的解决方案。无论是对于初创公司还是大型企业而言,掌握这些前沿技术都将是在激烈市场竞争中脱颖而出的关键所在。
49 0
|
1月前
|
Java 对象存储 开发者
解析Spring Cloud与Netflix OSS:微服务架构中的左右手如何协同作战
Spring Cloud与Netflix OSS不仅是现代微服务架构中不可或缺的一部分,它们还通过不断的技术创新和社区贡献推动了整个行业的发展。无论是对于初创企业还是大型组织来说,掌握并合理运用这两套工具,都能极大地提升软件系统的灵活性、可扩展性以及整体性能。随着云计算和容器化技术的进一步普及,Spring Cloud与Netflix OSS将继续引领微服务技术的发展潮流。
34 0
|
10天前
|
Java Spring
Spring底层架构源码解析(三)
Spring底层架构源码解析(三)
|
10天前
|
XML Java 数据格式
Spring底层架构源码解析(二)
Spring底层架构源码解析(二)
|
13天前
|
JSON 前端开发 Java
Spring Boot框架中的响应与分层解耦架构
在Spring Boot框架中,响应与分层解耦架构是两个核心概念,它们共同促进了应用程序的高效性、可维护性和可扩展性。
38 3
|
19天前
|
Cloud Native Java 对象存储
面向未来的架构设计:Spring Cloud和Netflix OSS在云原生环境下的发展趋势
面向未来的架构设计:Spring Cloud和Netflix OSS在云原生环境下的发展趋势
36 1
|
1月前
|
存储 Java 数据库
Spring Boot 优雅实现多租户架构
本文详细介绍如何使用Spring Boot和Spring Cloud实现多租户架构。多租户架构允许多个租户共用一个应用,各自拥有独立资源和数据。其优势包括满足个性化需求、降低成本、复用代码以及增强可扩展性。文中探讨了架构选型、数据库设计、应用部署及租户管理等内容,并提供了具体实现步骤和技术细节。适用于SaaS应用和多租户云服务等场景。
|
前端开发 Java 数据库
Spring架构及核心模块
1.Hello Spring Spring是一个轻量级控制反转(IOC)和面向切面(AOP)的容器框架,主要是为了解决企业应用开发的复杂性而诞生的。它采用模块化分层设计,具有以下特点: 轻量低侵入式设计,代码污染极低; 控制反转和依赖注入实现了松散耦合; 切面编程降低业务耦合度,提高程序的可重用性及开发效率; ORM和DAO简化了底层的数据库访问; 方便集成各种优秀框架等。
101 1
Spring架构及核心模块
|
XML Java 数据库连接
今天说一下Spring的架构模块!
这是一个基础知识篇,看到这篇文章的,希望去多多理解一些更多的底层知识,不要不求甚解,要知其然知己所以然。
109 0
|
1月前
|
SQL 监控 druid
springboot-druid数据源的配置方式及配置后台监控-自定义和导入stater(推荐-简单方便使用)两种方式配置druid数据源
这篇文章介绍了如何在Spring Boot项目中配置和监控Druid数据源,包括自定义配置和使用Spring Boot Starter两种方法。