Spring Cloud 2.x系列之springboot集成ActiveMQ

简介: 消息队列中间件是分布式系统中重要的组件,主要解决应用耦合,异步消息,流量削锋等问题。实现高性能,高可用,可伸缩和最终一致性架构;是大型分布式系统不可缺少的中间件。目前使用较多的消息队列有ActiveMQ、RabbitMQ、Kafka、RocketMQ、MetaMQ等。

消息队列中间件是分布式系统中重要的组件,主要解决应用耦合,异步消息,流量削锋等问题。实现高性能,高可用,可伸缩和最终一致性架构;是大型分布式系统不可缺少的中间件。目前使用较多的消息队列有ActiveMQ、RabbitMQ、Kafka、RocketMQ、MetaMQ等。springboot提供了对JMS系统的支持;springboot很方便就可以集成这些消息中间件。

对于异步消息在实际的应用之中会有两类:

JMS:代表作就是 ActiveMQ,但是其性能不高,因为其是用 java 程序实现的。

AMQP:直接利用协议实现的消息组件,其大众代表作为RabbitMQ,高性能代表作为Kafka。

1、新建项目,对应的pom.xml文件如下

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

   <modelVersion>4.0.0</modelVersion>


   <groupId>spring-cloud</groupId>

   <artifactId>sc-activeMQ</artifactId>

   <version>0.0.1-SNAPSHOT</version>

   <packaging>jar</packaging>


   <name>sc-activeMQ</name>

   <url>http://maven.apache.org</url>



   <parent>

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-starter-parent</artifactId>

      <version>2.0.4.RELEASE</version>

   </parent>



   <dependencyManagement>

      <dependencies>

        <dependency>

           <groupId>org.springframework.cloud</groupId>

           <artifactId>spring-cloud-dependencies</artifactId>

           <version>Finchley.RELEASE</version>

           <type>pom</type>

           <scope>import</scope>

        </dependency>



      </dependencies>

   </dependencyManagement>



   <properties>

      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

      <maven.compiler.source>1.8</maven.compiler.source>

      <maven.compiler.target>1.8</maven.compiler.target>

   </properties>


   <dependencies>

      <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-activemq</artifactId>

      </dependency>

      <dependency>

        <groupId>org.apache.activemq</groupId>

        <artifactId>activemq-core</artifactId>

        <version>5.7.0</version>

      </dependency>


      <dependency>

        <groupId>org.apache.activemq</groupId>

        <artifactId>activemq-pool</artifactId>

      </dependency>



      <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-web</artifactId>

      </dependency>

      <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-test</artifactId>

        <scope>test</scope>

      </dependency>

   </dependencies>

</project>

2、新建springboot启动类ActiveMqApplication.java

package sc.activemq;


import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;



@SpringBootApplication

publi cclass ActiveMqApplication {


   public static void main(String[] args){

      SpringApplication.run(ActiveMqApplication.class, args);

   }



}

3、新建配置文件application.yml

server:

  port: 9080

spring:

  appliction:

   name: sc-activemq

  activemq:

    broker-url:tcp://localhost:61616

    in-memory: true 

    user: admin

    password: admin

    pool:

      enabled: true

      max-connections: 50

      expiry-timeout:10000

      idle-timeout: 30000

  jms:

    pub-sub-domain: false #默认情况下activemq提供的是queue模式,若要使用topic模式需要配置pub-sub-domain为true

说明:默认情况下activemq提供的是queue模式,若要使用topic模式需要配置spring.jms.pub-sub-domain为true

4、新建消费生产者

package sc.activemq.service.impl;

import javax.jms.Destination;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.jms.core.JmsMessagingTemplate;

import org.springframework.stereotype.Service;

import sc.activemq.service.ProductService;

@Service
publicclass ProductServiceImpl implements ProductService {

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Override

    public void sendMessage(Destinationdestination, String message) {

       jmsMessagingTemplate.convertAndSend(destination,message);

    }
}

5、新建消息消费者

队列模式:

package sc.activemq.service.consumer;

import org.springframework.jms.annotation.JmsListener;

import org.springframework.stereotype.Component;


@Component

publicclass ConsumerQueue{


   // 使用JmsListener配置消费者监听的队列,其中text是接收到的消息

   @JmsListener(destination = "jms-queue")

   public void receiveQueue(String text) {

      System.out.println("ConsumerQueue收到:" + text);

   }

}

订阅模式:

package sc.activemq.service.consumer;


import org.springframework.jms.annotation.JmsListener;

import org.springframework.stereotype.Component;



@Component

publicclass ConsumerTopic{


   // 使用JmsListener配置消费者监听的队列,其中text是接收到的消息

   @JmsListener(destination = "jms-topic")

   public void receiveQueue(String text) {

      System.out.println("ConsumerTopic收到:" + text);

   }

}

6、新建测试类

package sc.activemq;


import org.apache.activemq.command.ActiveMQQueue;

import org.apache.activemq.command.ActiveMQTopic;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.test.context.junit4.SpringRunner;



import sc.activemq.service.ProductService;



@RunWith(SpringRunner.class)

@SpringBootTest

publicclass ActiveMqTest {



   @Autowired

   private ActiveMQQueue queue;



   @Autowired

   private ActiveMQTopic topic;



   @Autowired

   private ProductService productService;



   @Test

   public void testJms() {

      String msgQueue = "send 黄金 ";

      for(inti=0; i<5; i++){

        productService.sendMessage(this.queue, msgQueue+i);

      }

      String msgTopic = "send 白银 ";

      for(inti=0; i<5; i++){

        productService.sendMessage(this.topic, msgTopic+i);

      }

      try {

        Thread.sleep(10000);

      } catch(InterruptedException e) {

      }

   }

}

7、进行测试

先登录ActiveMq管理平台:http://localhost:8161/

  队列模式:

(1)配置spring.jms.pub-sub-domain为false

94388ff6c07ea275d15913135b19f6149e8a3d8c

(2)注释测试类的如下代码

8677ff31108d0374fe3853360c21f3765efd5441

(3)运行测试类

6a724926f09e23d8f763941d341a908f3a64ff2b

订阅模式:

(1) 配置spring.jms.pub-sub-domain为true

35833551cee9d1776f01346082dd522867a96f79

(2)  注释测试类的如下代码

1d7d36af49512fb710a1c71793bb22451b318d4b

(3)  运行测试类

d94422bc1165ebc1bdf64ff246062bb79213192d

源码:

https://gitee.com/hjj520/spring-cloud-2.x/tree/master/sc-activemq

本文作者: java乐园

本文来自云栖社区合作伙伴“JAVA乐园”,了解相关信息可以关注“JAVA乐园

相关文章
|
29天前
|
安全 Java Apache
微服务——SpringBoot使用归纳——Spring Boot中集成 Shiro——Shiro 身份和权限认证
本文介绍了 Apache Shiro 的身份认证与权限认证机制。在身份认证部分,分析了 Shiro 的认证流程,包括应用程序调用 `Subject.login(token)` 方法、SecurityManager 接管认证以及通过 Realm 进行具体的安全验证。权限认证部分阐述了权限(permission)、角色(role)和用户(user)三者的关系,其中用户可拥有多个角色,角色则对应不同的权限组合,例如普通用户仅能查看或添加信息,而管理员可执行所有操作。
69 0
|
29天前
|
安全 Java 数据安全/隐私保护
微服务——SpringBoot使用归纳——Spring Boot中集成 Shiro——Shiro 三大核心组件
本课程介绍如何在Spring Boot中集成Shiro框架,主要讲解Shiro的认证与授权功能。Shiro是一个简单易用的Java安全框架,用于认证、授权、加密和会话管理等。其核心组件包括Subject(认证主体)、SecurityManager(安全管理员)和Realm(域)。Subject负责身份认证,包含Principals(身份)和Credentials(凭证);SecurityManager是架构核心,协调内部组件运作;Realm则是连接Shiro与应用数据的桥梁,用于访问用户账户及权限信息。通过学习,您将掌握Shiro的基本原理及其在项目中的应用。
80 0
|
29天前
|
消息中间件 存储 Java
微服务——SpringBoot使用归纳——Spring Boot中集成ActiveMQ——ActiveMQ安装
本教程介绍ActiveMQ的安装与基本使用。首先从官网下载apache-activemq-5.15.3版本,解压后即可完成安装,非常便捷。启动时进入解压目录下的bin文件夹,根据系统选择win32或win64,运行activemq.bat启动服务。通过浏览器访问`http://127.0.0.1:8161/admin/`可进入管理界面,默认用户名密码为admin/admin。ActiveMQ支持两种消息模式:点对点(Queue)和发布/订阅(Topic)。前者确保每条消息仅被一个消费者消费,后者允许多个消费者同时接收相同消息。
64 0
微服务——SpringBoot使用归纳——Spring Boot中集成ActiveMQ——ActiveMQ安装
|
29天前
|
消息中间件 Java 微服务
微服务——SpringBoot使用归纳——Spring Boot中集成ActiveMQ——发布/订阅消息的生产和消费
本文详细讲解了Spring Boot中ActiveMQ的发布/订阅消息机制,包括消息生产和消费的具体实现方式。生产端通过`sendMessage`方法发送订阅消息,消费端则需配置`application.yml`或自定义工厂以支持topic消息监听。为解决点对点与发布/订阅消息兼容问题,可通过设置`containerFactory`实现两者共存。最后,文章还提供了测试方法及总结,帮助读者掌握ActiveMQ在异步消息处理中的应用。
79 0
|
29天前
|
消息中间件 网络协议 Java
微服务——SpringBoot使用归纳——Spring Boot中集成ActiveMQ——ActiveMQ集成
本文介绍了在 Spring Boot 中集成 ActiveMQ 的详细步骤。首先通过引入 `spring-boot-starter-activemq` 依赖并配置 `application.yml` 文件实现基本设置。接着,创建 Queue 和 Topic 消息类型,分别使用 `ActiveMQQueue` 和 `ActiveMQTopic` 类完成配置。随后,利用 `JmsMessagingTemplate` 实现消息发送功能,并通过 Controller 和监听器实现点对点消息的生产和消费。最后,通过浏览器访问测试接口验证消息传递的成功性。
44 0
|
29天前
|
消息中间件 Java API
微服务——SpringBoot使用归纳——Spring Boot中集成ActiveMQ—— JMS 和 ActiveMQ 介绍
本文介绍如何在Spring Boot中集成ActiveMQ,首先阐述了JMS(Java消息服务)的概念及其作为与具体平台无关的API在异步通信中的作用。接着说明了JMS的主要对象模型,如连接工厂、会话、生产者和消费者等,并指出JMS支持点对点和发布/订阅两种消息类型。随后重点讲解了ActiveMQ,作为Apache开源的消息总线,它完全支持JMS规范,适用于异步消息处理。最后,文章探讨了在Spring Boot中使用队列(Queue)和主题(Topic)这两种消息通信形式的方法。
43 0
|
6月前
|
Java Maven Docker
gitlab-ci 集成 k3s 部署spring boot 应用
gitlab-ci 集成 k3s 部署spring boot 应用
|
5月前
|
消息中间件 监控 Java
您是否已集成 Spring Boot 与 ActiveMQ?
您是否已集成 Spring Boot 与 ActiveMQ?
211 0
|
9月前
|
监控 druid Java
spring boot 集成配置阿里 Druid监控配置
spring boot 集成配置阿里 Druid监控配置
378 6
|
9月前
|
Java 关系型数据库 MySQL
如何实现Springboot+camunda+mysql的集成
【7月更文挑战第2天】集成Spring Boot、Camunda和MySQL的简要步骤: 1. 初始化Spring Boot项目,添加Camunda和MySQL驱动依赖。 2. 配置`application.properties`,包括数据库URL、用户名和密码。 3. 设置Camunda引擎属性,指定数据源。 4. 引入流程定义文件(如`.bpmn`)。 5. 创建服务处理流程操作,创建控制器接收请求。 6. Camunda自动在数据库创建表结构。 7. 启动应用,测试流程启动,如通过服务和控制器开始流程实例。 示例代码包括服务类启动流程实例及控制器接口。实际集成需按业务需求调整。
586 4