0%

JPA, JMS, JTA 都是 Java 企业级规范。

JPA(java persistence API)

JPA 通过JDK5.0的注解或XML来描述 对象-关系表的映射关系,并将运行期的实体对象持久化存储到数据库中。

JMS(java message service)

JMS是java平台上面向中间件的技术规范,便于消息系统中java应用程序进行信心交换,并且通过提供标准的产生、发送、接收消息的接口简化企业应用的开发,翻译为Java消息服务。

XA

一个事务规范,JTA 就是 XA 规范的 Java 实现。

JTA(java transaction API)

JTA允许应用程序执行分布式事务管理,在两个或多个网络计算机资源上访问并且更新数据。JDBC驱动程序的JTA支持极大地增强了数据访问能力。

JTA 接口

JTA 定义了三个接口,分别是:

TransactionManager

XAResource

XID

JT

JTA 弊端

两阶段提交

事务时间太长,锁数据的时间太长

性能低,吞吐量低

RabbitMQ 和 Spring Boot 整合,使用 rabbitTemplate.convertAndSend 方法发送消息到队列时,报如下错误:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
java.lang.IllegalArgumentException: SimpleMessageConverter only supports String, byte[] and Serializable payloads, received: gy.finolo.springbootmybatisplus.entity.Order

at org.springframework.amqp.support.converter.SimpleMessageConverter.createMessage(SimpleMessageConverter.java:161)
at org.springframework.amqp.support.converter.AbstractMessageConverter.createMessage(AbstractMessageConverter.java:88)
at org.springframework.amqp.support.converter.AbstractMessageConverter.toMessage(AbstractMessageConverter.java:70)
at org.springframework.amqp.support.converter.AbstractMessageConverter.toMessage(AbstractMessageConverter.java:58)
at org.springframework.amqp.rabbit.core.RabbitTemplate.convertMessageIfNecessary(RabbitTemplate.java:1758)
at org.springframework.amqp.rabbit.core.RabbitTemplate.convertAndSend(RabbitTemplate.java:1075)
at gy.finolo.springbootmybatisplus.producer.OrderSender.send(OrderSender.java:19)
at gy.finolo.springbootmybatisplus.rabbitmq.RabbitMQTest.testSend(RabbitMQTest.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:675)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
...

是因为 Order 这个类没有实现 implements Serializable 接口。

通过 yum 在 CentOS 7 环境下安装 RabbitMQ Server 3.8。

配置yum源

这次安装使用官方推荐的,基于 PackageCloud 的 Yum 仓库安装。

进入官网 https://packagecloud.io/rabbitmq/rabbitmq-server/install#bash-rpm

我们可以看到 quick install 的快速安装命令。

1
2
3
4
5
6
7
8
9
10
11
$ curl -s https://packagecloud.io/install/repositories/rabbitmq/rabbitmq-server/script.rpm.sh | sudo bash
...
Complete!
Generating yum cache for rabbitmq_rabbitmq-server...
Importing GPG key 0x4D206F89:
Userid : "https://packagecloud.io/rabbitmq/rabbitmq-server (https://packagecloud.io/docs#gpg_signing) <support@packagecloud.io>"
Fingerprint: 8c69 5b02 19af deb0 4a05 8ed8 f4e7 8920 4d20 6f89
From : https://packagecloud.io/rabbitmq/rabbitmq-server/gpgkey
Generating yum cache for rabbitmq_rabbitmq-server-source...

The repository is setup! You can now install packages.

脚本执行后,会安装一些最基本的底层依赖,比如 yum-utils.noarch。同时还会生成 /etc/yum.repos.d/rabbitmq_rabbitmq-server.repo 文件。

安装 Erlang 依赖。进入页面https://packagecloud.io/rabbitmq/erlang/install#bash-rpm

1
2
3
4
5
$ curl -s https://packagecloud.io/install/repositories/rabbitmq/erlang/script.rpm.sh | sudo bash
...
Generating yum cache for rabbitmq_erlang-source...

The repository is setup! You can now install packages.

安装

安装Erlang

首先安装Erlang语言依赖。

1
$ sudo yum install erlang

安装RabbitMQ

1
$ sudo yum install rabbitmq-server

设置开机自动启动

1
chkconfig rabbitmq-server on

启动/关闭 RabbitMQ 服务

1
2
3
systemctl start rabbitmq-server

systemctl stop rabbitmq-server

安装插件

查看插件列表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
$ sudo rabbitmq-plugins list
Listing plugins with pattern ".*" ...
Configured: E = explicitly enabled; e = implicitly enabled
| Status: * = running on rabbit@localhost
|/
[ ] rabbitmq_amqp1_0 3.8.3
[ ] rabbitmq_auth_backend_cache 3.8.3
[ ] rabbitmq_auth_backend_http 3.8.3
[ ] rabbitmq_auth_backend_ldap 3.8.3
[ ] rabbitmq_auth_backend_oauth2 3.8.3
[ ] rabbitmq_auth_mechanism_ssl 3.8.3
[ ] rabbitmq_consistent_hash_exchange 3.8.3
[ ] rabbitmq_event_exchange 3.8.3
[ ] rabbitmq_federation 3.8.3
[ ] rabbitmq_federation_management 3.8.3
[ ] rabbitmq_jms_topic_exchange 3.8.3
[ ] rabbitmq_management 3.8.3
[ ] rabbitmq_management_agent 3.8.3
[ ] rabbitmq_mqtt 3.8.3
[ ] rabbitmq_peer_discovery_aws 3.8.3
[ ] rabbitmq_peer_discovery_common 3.8.3
[ ] rabbitmq_peer_discovery_consul 3.8.3
[ ] rabbitmq_peer_discovery_etcd 3.8.3
[ ] rabbitmq_peer_discovery_k8s 3.8.3
[ ] rabbitmq_prometheus 3.8.3
[ ] rabbitmq_random_exchange 3.8.3
[ ] rabbitmq_recent_history_exchange 3.8.3
[ ] rabbitmq_sharding 3.8.3
[ ] rabbitmq_shovel 3.8.3
[ ] rabbitmq_shovel_management 3.8.3
[ ] rabbitmq_stomp 3.8.3
[ ] rabbitmq_top 3.8.3
[ ] rabbitmq_tracing 3.8.3
[ ] rabbitmq_trust_store 3.8.3
[ ] rabbitmq_web_dispatch 3.8.3
[ ] rabbitmq_web_mqtt 3.8.3
[ ] rabbitmq_web_mqtt_examples 3.8.3
[ ] rabbitmq_web_stomp 3.8.3
[ ] rabbitmq_web_stomp_examples 3.8.3

启动管理平台插件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ sudo rabbitmq-plugins enable rabbitmq_management
Enabling plugins on node rabbit@localhost:
rabbitmq_management
The following plugins have been configured:
rabbitmq_management
rabbitmq_management_agent
rabbitmq_web_dispatch
Applying plugin configuration to rabbit@localhost...
The following plugins have been enabled:
rabbitmq_management
rabbitmq_management_agent
rabbitmq_web_dispatch

started 3 plugins.

查看端口占用情况。

1
2
3
4
$ ss -tunlp | grep 5672
tcp LISTEN 0 128 *:15672 *:*
tcp LISTEN 0 128 *:25672 *:*
tcp LISTEN 0 128 [::]:5672 [::]:*

5672 是 RabbitMQ Server 端口,25672 是集群端口,15672 是web管理平台端口。

配置

设置登录用户,编译文件:

1
$ sudo vi /usr/lib/rabbitmq/lib/rabbitmq_server-3.8.3/ebin/rabbit.app

把如下内容:

{loopback_users, [<<"guest">>]} 改为 {loopback_users, ["guest"]}

重启。

访问页面:http://<server-ip>:15672/ ,通过用户名 guest 和密码 guest 就可以成功登录了。