From 09386afca773e3a6f99c5318d123b75ffeb7964e Mon Sep 17 00:00:00 2001
From: Helius <wangdoubleone@gmail.com>
Date: Mon, 25 May 2020 18:28:48 +0800
Subject: [PATCH] init rabbitmq
---
src/main/java/com/xcong/excoin/rabbit/producer/TestProducer.java | 42 ++++++++++
src/main/java/com/xcong/excoin/rabbit/consumer/TestConsumer.java | 21 +++++
src/main/java/com/xcong/excoin/configurations/properties/CustomRabbitProperties.java | 19 ++++
src/main/java/com/xcong/excoin/configurations/RabbitMqConfig.java | 63 +++++++++++++++
src/test/java/com/xcong/excoin/RabbitMqTest.java | 24 ++++++
pom.xml | 11 ++
src/main/resources/application.yml | 15 +++
src/test/java/com/xcong/excoin/RSATest.java | 7 +
8 files changed, 200 insertions(+), 2 deletions(-)
diff --git a/pom.xml b/pom.xml
index 20f2f96..eaea49e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -27,6 +27,8 @@
<mapstruct.version>1.3.1.Final</mapstruct.version>
<hutool.version>5.3.1</hutool.version>
<fastjson.version>1.2.61</fastjson.version>
+ <netty.version>4.1.33.Final</netty.version>
+ <dom4j.version>1.6.1</dom4j.version>
</properties>
<dependencies>
@@ -43,6 +45,11 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
+ </dependency>
+
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!-- <dependency>-->
@@ -166,14 +173,14 @@
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
- <version>1.6.1</version>
+ <version>${dom4j.version}</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
- <version>4.1.33.Final</version>
+ <version>${netty.version}</version>
</dependency>
</dependencies>
diff --git a/src/main/java/com/xcong/excoin/configurations/RabbitMqConfig.java b/src/main/java/com/xcong/excoin/configurations/RabbitMqConfig.java
new file mode 100644
index 0000000..eec7531
--- /dev/null
+++ b/src/main/java/com/xcong/excoin/configurations/RabbitMqConfig.java
@@ -0,0 +1,63 @@
+package com.xcong.excoin.configurations;
+
+import com.xcong.excoin.configurations.properties.CustomRabbitProperties;
+import org.springframework.amqp.core.Binding;
+import org.springframework.amqp.core.BindingBuilder;
+import org.springframework.amqp.core.DirectExchange;
+import org.springframework.amqp.core.Queue;
+import org.springframework.amqp.rabbit.connection.ConnectionFactory;
+import org.springframework.amqp.rabbit.core.RabbitTemplate;
+import org.springframework.beans.factory.config.ConfigurableBeanFactory;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Scope;
+
+import javax.annotation.Resource;
+
+/**
+ * @author wzy
+ * @date 2020-05-25
+ **/
+@Configuration
+public class RabbitMqConfig {
+
+ public static final String EXCHANGE_ONE = "excoin-exchange-one";
+
+ public static final String QUEUE_TEST = "test-queue";
+
+ public static final String ROUTING_KEY_TEST = "test-routingKey";
+
+ @Resource
+ private ConnectionFactory connectionFactory;
+
+// @Bean
+// public ConnectionFactory connectionFactory() {
+// CachingConnectionFactory connectionFactory = new CachingConnectionFactory(customRabbitProperties.getHost(), customRabbitProperties.getPort());
+// connectionFactory.setUsername(customRabbitProperties.getUsername());
+// connectionFactory.setPassword(customRabbitProperties.getPassword());
+// connectionFactory.setPublisherConfirmType(CachingConnectionFactory.ConfirmType.CORRELATED);
+// return connectionFactory;
+// }
+
+ @Bean
+ @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
+ public RabbitTemplate rabbitTemplate() {
+ return new RabbitTemplate(connectionFactory);
+ }
+
+ @Bean
+ public DirectExchange defaultExchange() {
+ return new DirectExchange(EXCHANGE_ONE);
+ }
+
+ @Bean
+ public Queue testQueue() {
+ return new Queue(QUEUE_TEST, true);
+ }
+
+ @Bean
+ public Binding binding() {
+ return BindingBuilder.bind(testQueue()).to(defaultExchange()).with(ROUTING_KEY_TEST);
+ }
+
+}
diff --git a/src/main/java/com/xcong/excoin/configurations/properties/CustomRabbitProperties.java b/src/main/java/com/xcong/excoin/configurations/properties/CustomRabbitProperties.java
new file mode 100644
index 0000000..47ddc4c
--- /dev/null
+++ b/src/main/java/com/xcong/excoin/configurations/properties/CustomRabbitProperties.java
@@ -0,0 +1,19 @@
+package com.xcong.excoin.configurations.properties;
+
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * @author wzy
+ * @date 2020-05-25
+ **/
+@Data
+@Configuration
+@ConfigurationProperties(prefix = "custom.rabbitmq")
+public class CustomRabbitProperties {
+ private String host;
+ private int port;
+ private String username;
+ private String password;
+}
diff --git a/src/main/java/com/xcong/excoin/rabbit/consumer/TestConsumer.java b/src/main/java/com/xcong/excoin/rabbit/consumer/TestConsumer.java
new file mode 100644
index 0000000..aea3c07
--- /dev/null
+++ b/src/main/java/com/xcong/excoin/rabbit/consumer/TestConsumer.java
@@ -0,0 +1,21 @@
+package com.xcong.excoin.rabbit.consumer;
+
+import com.xcong.excoin.configurations.RabbitMqConfig;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.amqp.rabbit.annotation.RabbitListener;
+import org.springframework.stereotype.Component;
+
+/**
+ * @author wzy
+ * @date 2020-05-25
+ **/
+@Slf4j
+@Component
+public class TestConsumer {
+
+
+ @RabbitListener(queues = RabbitMqConfig.QUEUE_TEST)
+ public void doSomething(String content) {
+ log.info("#---->{}#", content);
+ }
+}
diff --git a/src/main/java/com/xcong/excoin/rabbit/producer/TestProducer.java b/src/main/java/com/xcong/excoin/rabbit/producer/TestProducer.java
new file mode 100644
index 0000000..00ffd52
--- /dev/null
+++ b/src/main/java/com/xcong/excoin/rabbit/producer/TestProducer.java
@@ -0,0 +1,42 @@
+package com.xcong.excoin.rabbit.producer;
+
+import cn.hutool.core.util.IdUtil;
+import com.xcong.excoin.configurations.RabbitMqConfig;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.amqp.rabbit.connection.CorrelationData;
+import org.springframework.amqp.rabbit.core.RabbitTemplate;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+/**
+ * @author wzy
+ * @date 2020-05-25
+ **/
+@Slf4j
+@Component
+public class TestProducer implements RabbitTemplate.ConfirmCallback {
+
+ private RabbitTemplate rabbitTemplate;
+
+ @Autowired
+ public TestProducer(RabbitTemplate rabbitTemplate) {
+ this.rabbitTemplate = rabbitTemplate;
+ rabbitTemplate.setConfirmCallback(this);
+ }
+
+ public void sendTestMsg(String content) {
+ CorrelationData correlationData = new CorrelationData(IdUtil.simpleUUID());
+ rabbitTemplate.convertAndSend(RabbitMqConfig.EXCHANGE_ONE, RabbitMqConfig.ROUTING_KEY_TEST, content, correlationData);
+ }
+
+
+ @Override
+ public void confirm(CorrelationData correlationData, boolean ack, String cause) {
+ log.info("#----->{}#", correlationData);
+ if (ack) {
+ log.info("success");
+ } else {
+ log.info("--->{}", cause);
+ }
+ }
+}
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
index 820acc1..8a849c0 100644
--- a/src/main/resources/application.yml
+++ b/src/main/resources/application.yml
@@ -69,6 +69,21 @@
min-idle: 8
## 连接超时时间(毫秒)
timeout: 30000
+ rabbitmq:
+ host: 120.27.238.55
+ port: 5672
+ username: ct_rabbit
+ password: 123456
+ publisher-confirm-type: correlated
+
+
+#custom:
+# rabbitmq:
+# host: 120.27.238.55
+# port: 5672
+# username: ct_rabbit
+# password: 123456
+
mybatis-plus:
mapper-locations: classpath:mapper/**/*.xml
diff --git a/src/test/java/com/xcong/excoin/RSATest.java b/src/test/java/com/xcong/excoin/RSATest.java
index 4d4d3b6..45c6488 100644
--- a/src/test/java/com/xcong/excoin/RSATest.java
+++ b/src/test/java/com/xcong/excoin/RSATest.java
@@ -13,6 +13,7 @@
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
+import java.math.BigDecimal;
import java.security.KeyPair;
/**
@@ -101,4 +102,10 @@
String md5str = SecureUtil.md5("123456");
log.info("{}", md5str);
}
+
+ @Test
+ public void bigdecimalTest() {
+ BigDecimal bigDecimal = new BigDecimal("123.12345678").setScale(4, BigDecimal.ROUND_DOWN);
+ log.info("--->{}", bigDecimal);
+ }
}
diff --git a/src/test/java/com/xcong/excoin/RabbitMqTest.java b/src/test/java/com/xcong/excoin/RabbitMqTest.java
new file mode 100644
index 0000000..82b668b
--- /dev/null
+++ b/src/test/java/com/xcong/excoin/RabbitMqTest.java
@@ -0,0 +1,24 @@
+package com.xcong.excoin;
+
+import com.xcong.excoin.rabbit.producer.TestProducer;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+
+import javax.annotation.Resource;
+
+/**
+ * @author wzy
+ * @date 2020-05-25
+ **/
+@SpringBootTest
+public class RabbitMqTest {
+
+ @Autowired
+ private TestProducer testProducer;
+
+ @Test
+ public void sendTestMsg() {
+ testProducer.sendTestMsg("this is test msg");
+ }
+}
--
Gitblit v1.9.1