Przeglądaj źródła

新增功能:
1. 后端异常完善
2. RestTemplateUtils请求header参数完善
3. RocketMQ配置完善.

liukx@elab 6 lat temu
rodzic
commit
ecbfd14f19

+ 1 - 1
elab-alert/pom.xml

@@ -5,7 +5,7 @@
     <parent>
         <artifactId>elab-parent</artifactId>
         <groupId>com.elab.core</groupId>
-        <version>2.0.4.10-SNAPSHOT</version>
+        <version>2.0.4.11-SNAPSHOT</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <artifactId>elab-alert</artifactId>

+ 1 - 1
elab-annotation/pom.xml

@@ -5,7 +5,7 @@
     <parent>
         <artifactId>elab-parent</artifactId>
         <groupId>com.elab.core</groupId>
-        <version>2.0.4.10-SNAPSHOT</version>
+        <version>2.0.4.11-SNAPSHOT</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
 

+ 1 - 1
elab-cache/pom.xml

@@ -6,7 +6,7 @@
     <parent>
         <groupId>com.elab.core</groupId>
         <artifactId>elab-parent</artifactId>
-        <version>2.0.4.10-SNAPSHOT</version>
+        <version>2.0.4.11-SNAPSHOT</version>
     </parent>
     <groupId>com.elab.cache</groupId>
     <artifactId>elab-cache</artifactId>

+ 1 - 1
elab-core/pom.xml

@@ -7,7 +7,7 @@
     <parent>
         <groupId>com.elab.core</groupId>
         <artifactId>elab-parent</artifactId>
-        <version>2.0.4.10-SNAPSHOT</version>
+        <version>2.0.4.11-SNAPSHOT</version>
     </parent>
 
     <groupId>com.elab.core</groupId>

+ 312 - 0
elab-db/REDEME.md

@@ -0,0 +1,312 @@
+# Elab-db 使用介绍
+
+## 更新介绍:
+
+版本号: 2.0.4.10
+
+- 新增批量新增功能
+- 批量修改,参考`IBaseDaoSupport`
+- 新增SQL检测功能
+  - join 表超过3张会提示警告。
+  - union 超过3张会提示警告。
+
+#### 版本号 : 2.0.4.4
+- 增加通用的service业务处理
+	- 接口继承 : ICommonService<?>
+	- 实现继承 : CommonServiceAdaptor<?>
+**业务特殊可以通过重写来覆盖**
+
+- 查询分页结果的时候,可以指定order by
+```
+pageModel.setOrderby("order by id desc");
+```
+
+- 如果涉及多个库的增删改查操作如何指定库?
+**通过catalog来指定,效果是在生成的sql语句中表名会变成 mvp.file 查询**
+```
+@Table(name = "file",catalog = "mvp")
+```
+
+
+修复若干BUG.
+
+
+## 功能描述
+- 单表的增删改差不需要写单独的SQL
+- 动态参数通过$指定,但前提是必须要有值
+- DAO只需要接口,就能够找到对应的sql文件中的sql
+- 实现基于一对多的关系映射
+- 参数可以通过对象或者Map的方式指定,SQL文件中的参数可以自由指定,没有限制
+
+### 配置
+基于注解配置:
+```java
+@Configuration
+public class JdbcBeanConfig {
+
+    @Bean
+  public JdbcTemplate jdbcTemplate(@Autowired DataSource dataSource) {
+	 JdbcTemplate jdbcTemplate = new JdbcTemplate();
+	 jdbcTemplate.setDataSource(dataSource);
+	 return jdbcTemplate;
+  }
+
+    @Bean
+  public BasicBaseDao basicBaseDao(@Autowired JdbcTemplate jdbcTemplate) {
+	 BasicBaseDao basicBaseDao = new BasicBaseDao();
+	 basicBaseDao.setJdbcTemplate(jdbcTemplate);
+	 return basicBaseDao;
+  }
+
+    @Bean
+  public ConfigurableFactory getConfigurableFactory() {
+	ConfigurableFactory configurableFactory = new ConfigurableFactory();
+	// 配置文件的文件位置
+	configurableFactory.setSqlConfigurableLocations("sql");
+    return configurableFactory;
+  }
+
+    @Bean
+  public DaoScannerConfigurer daoScannerConfigurer(@Autowired ConfigurableFactory configurableFactory) {
+	  DaoScannerConfigurer daoScannerConfigurer = new DaoScannerConfigurer();
+	  // db层要扫描的包
+	  daoScannerConfigurer.setBasePackage("com.elab.service.business.daos.*");
+	  // 持久层操作对象
+	  daoScannerConfigurer.setBasicBaseDaoName("basicBaseDao");
+	  daoScannerConfigurer.setConfigurableFactory(configurableFactory);
+	 return daoScannerConfigurer;
+  }
+
+}
+
+```
+
+基于配置文件配置:
+```xml
+
+<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
+        <property name="dataSource" ref="mysqlDataSource"/>
+    </bean>
+
+    <bean id="basicBaseDao" class="com.elab.core.dao.BasicBaseDao">
+        <property name="jdbcTemplate" ref="jdbcTemplate"/>
+    </bean>
+
+    <!-- 构建一个配置工厂 -->
+    <bean id="configurableFactory" class="com.elab.core.sql.ConfigurableFactory">
+        <property name="sqlConfigurableLocations" value="sql"/>
+    </bean>
+
+    <!-- 扫描包 -->
+    <bean id="daoScannerConfigurer" class="com.elab.core.spring.DaoScannerConfigurer">
+        <property name="basePackage" value="com.db.service.dao"/>
+        <property name="basicBaseDaoName" value="basicBaseDao"/>
+        <property name="configurableFactory" ref="configurableFactory"/>
+    </bean>
+```
+
+
+### 强制操作
+#### 接口层
+1. 必须实现IBaseDaoSupport接口,这样才会受框架的扫描
+```java
+// 指定配置文件的前缀路径 , 注意这里的配置文件为test-sql.xml , <TTest>构造参数对应的就是数据库的实体
+@XmlGroupName("test")
+public interface ITestDao extends IBaseDaoSupport<TTest> {
+
+   public TTest getTestObject(String id);
+
+   public List getTestList(TTest id);
+
+}
+
+```
+2. SQL文件通常放在resources/sql下面
+
+SQL文件的规范参考
+
+```xml
+<!-- 这里面的name对应dao的接口层指定的XmlGroupName的值 -->
+<sqlGroup name="test">
+    <!-- 对应dao的接口层的方法 -->
+    <sql id="getTestObject">
+        select
+        id
+        ,username,name,sex,status,created,time,test_id,love_name
+        from t_test
+        where
+        id = :id
+    </sql>
+</sqlGroup>
+```
+
+- 规范类似Mybatis.
+- 增删改不需要指定,框架会通过语句识别执行sql类型
+- 简单的增删改查不需要写SQL,复杂的增删改查需要手动写。不用写的sql在IBaseDaoSupport已经写明了。(Mybatis类似)
+
+#### 实体层
+
+- @Table 指定表名
+- @Id : 指定主键, 默认以id字段为主键
+- @Column 当数据库列名和实体属性名不一致时,可以使用该注解指定
+- @Ignore 忽略当前实体的字段,非数据库字段可以通过这个注解指定
+- @JoinTable 表关联操作
+	- schema 对应的sql.xml配置文件中的,sqlGroup的name加上sql的id , 参考test.selectByExample
+	- joinColumns 关联对象的属性名和数据库列名对应
+		- name 属性名称
+		- referencedColumnName  -> sql语句中的条件名称
+
+
+参考实体 : **这里的一对多关系会根据你的属性对象来确定是返回一个或者多个**
+```java
+
+@Table(name = "t_test")
+public class TTest {
+ // 表字段 : t_test.id//    @javax.persistence.Column(name="ids")
+  @Id
+  private Integer id;
+
+ // 表字段 : t_test.test_id  @Column(name = "test_id")
+  @Column(name = "test_id")
+  private String testId;
+
+  @Ignore
+  private String girlName;
+
+// 对应的sql文件中的编号,参数就是JoinColumn中需要将实体和sql中的参数对应
+  @JoinTable(schema = "test.selectByExample", joinColumns = {
+            @JoinColumn(name = "id", referencedColumnName = "id"),
+  @JoinColumn(name = "status", referencedColumnName = "status")
+    })
+    private TTest test;
+
+  @JoinTable(schema = "test.selectByExample", joinColumns = {
+            @JoinColumn(name = "testId", referencedColumnName = "test_id")
+    })
+    private List testList;
+```
+
+## 常用操作
+
+下面是集成自IBaseDaoSupport的操作
+
+1. 添加操作
+```java
+@Test
+public void testInserCase() throws Exception {
+    TTest test = new TTest();
+	test.setStatus("1");
+	test.setUsername("某某某xx111");
+	// 这里是非手写的语句,集成自IBaseDaoSupport操作
+	int insert = testDao.insert(test);
+	System.out.println(insert);
+}
+```
+2. 修改
+```java
+
+@Test
+public void testUpdateCase() throws Exception {
+	TTest test = new TTest();
+	test.setId(1);
+	test.setStatus("1");
+	test.setUsername("某某某xx33333333");
+	int insert = testDao.updateById(test);
+	System.out.println(insert);
+}
+```
+
+3. 查询
+
+```java
+@Test
+  public void testQueryCase() throws Exception {
+        TTest test = new TTest();
+	test.setId(1123);
+	test.setStatus("1");
+  //        test.setUsername("某某某xx2121212");
+	List tTests = testDao.selectByList(test);
+	test.setId(1);
+	TTest test1 = testDao.selectByObject(test);
+	System.out.println(test1.toString());
+	System.out.println(tTests.size());
+  }
+
+```
+
+
+
+## 特殊操作
+
+### 动态参数
+1. 例如动态表名,或者动态字段
+```xml
+ 1. select * from $table
+ 2. selct * from table order  by $column
+```
+1. 传值的时候,就传table这个字段
+2. 传值的时候就传column -> id asc 等等
+
+## bean对象手动设置
+1. 实现RowMapper接口
+```
+// 该类已经是最底层的赋值方法了,可以根据具体业务去实现不同的处理
+public class TestRowMapper implements RowMapper {
+
+    @Override
+  public TTest mapRow(ResultSet resultSet, int i) throws SQLException {
+	  TTest test = new TTest();
+	  int id = resultSet.getInt("id");
+	  String username = resultSet.getString("username");
+	  String name = resultSet.getString("name");
+	  test.setId(id);
+	  test.setUsername(username);
+	  test.setName(name);
+	  return test;
+  }
+}
+
+// 然后接口层
+可以通过公用的selectByMapper(参数,new TestRowMapper());
+
+TTest test = new TTest();
+test.setStatus("1");
+TestRowMapper mapper = new TestRowMapper();
+List tTests1 = testDao.selectByList(test);
+List tTests = testDao.selectByMapper(test, mapper);
+
+```
+
+### in like 操作
+如果是in like 等函数:
+sql.xml中
+```java
+// sql
+select * from table where name like :name
+// java
+model.setName("%mmm%");
+
+// sql
+select * from table where id in :idList
+// java
+List<String> list = new ArrayList();
+list.add("1");
+list.add("2");
+list.add("3");
+// 上面最好用对象包这这个list
+
+```
+
+# 使用建议
+1. 如果出现(columnA = :A or columnB = :B) 这种括号条件的话....希望传值不能为空!
+2. 传值的时候参数必须带有一个以上!
+
+## 加速开发
+
+### 自动生成代码
+
+[内部生成代码网站](http://192.168.0.25:8889/share;JSESSIONID=aa82cbce-e6d8-4816-af7f-fe0e01eb77f5)
+
+> 帐号: admin 密码:111111
+
+选择表结构生成特定的内部代码结构。

+ 1 - 1
elab-db/pom.xml

@@ -5,7 +5,7 @@
     <parent>
         <groupId>com.elab.core</groupId>
         <artifactId>elab-parent</artifactId>
-        <version>2.0.4.10-SNAPSHOT</version>
+        <version>2.0.4.11-SNAPSHOT</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <artifactId>elab-db</artifactId>

+ 1 - 1
elab-db/src/main/java/com/elab/core/spring/method/DefaultSQLBuilderSupport.java

@@ -75,7 +75,7 @@ public class DefaultSQLBuilderSupport implements ISQLBuliderSupport {
         String id = entityOperation.pkField.getName();
         StringBuffer sb = new StringBuffer();
         // 这里默认会将主键放在第一位
-        sb.append(" select " + id + "," + allColumn + " from " + tableName + " where " + id + " = ? ");
+        sb.append(" select " + allColumn + " from " + tableName + " where " + id + " = ? ");
         return sb.toString();
     }
 

+ 1 - 1
elab-log/pom.xml

@@ -7,7 +7,7 @@
     <parent>
         <groupId>com.elab.core</groupId>
         <artifactId>elab-parent</artifactId>
-        <version>2.0.4.10-SNAPSHOT</version>
+        <version>2.0.4.11-SNAPSHOT</version>
     </parent>
 
     <groupId>com.elab.log</groupId>

+ 1 - 1
elab-mongodb/pom.xml

@@ -5,7 +5,7 @@
     <parent>
         <artifactId>elab-parent</artifactId>
         <groupId>com.elab.core</groupId>
-        <version>2.0.4.10-SNAPSHOT</version>
+        <version>2.0.4.11-SNAPSHOT</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
 

+ 12 - 2
elab-mq/pom.xml

@@ -5,7 +5,7 @@
     <parent>
         <artifactId>elab-parent</artifactId>
         <groupId>com.elab.core</groupId>
-        <version>2.0.4.10-SNAPSHOT</version>
+        <version>2.0.4.11-SNAPSHOT</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
 
@@ -17,10 +17,20 @@
             <artifactId>elab-core</artifactId>
             <version>${project.version}</version>
         </dependency>
+        <dependency>
+            <groupId>com.elab.core</groupId>
+            <artifactId>elab-spring</artifactId>
+            <version>${project.version}</version>
+        </dependency>
         <dependency>
             <groupId>com.aliyun.openservices</groupId>
             <artifactId>ons-client</artifactId>
-            <version>1.7.0.Final</version>
+            <version>1.8.0.Final</version>
+        </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <scope>test</scope>
         </dependency>
     </dependencies>
     <build>

+ 108 - 0
elab-mq/src/main/java/com/elab/mq/config/RocketMQConfiguration.java

@@ -0,0 +1,108 @@
+package com.elab.mq.config;
+
+import com.aliyun.openservices.ons.api.Consumer;
+import com.aliyun.openservices.ons.api.ONSFactory;
+import com.aliyun.openservices.ons.api.PropertyKeyConst;
+import com.aliyun.openservices.ons.api.bean.ProducerBean;
+import com.elab.mq.listener.AbstractMessageListener;
+import com.elab.mq.listener.MessageListenerWrapper;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.core.env.Environment;
+
+import java.util.List;
+import java.util.Properties;
+
+/**
+ * rocketMQ消息队列的配置
+ *
+ * @author : liukx
+ * @time : 2019/4/19 - 16:41
+ */
+@Configuration
+public class RocketMQConfiguration {
+
+
+    private final String prefix = "elab.mq";
+
+    private final String NAMESRV_ADDR = "http://MQ_INST_1819241776271348_Bak3xjk0.mq-internet-access.mq-internet" +
+            ".aliyuncs.com:80";
+
+    @Bean
+    public ProducerBean producerBean(@Autowired Environment env) {
+        ProducerBean producerBean = new ProducerBean();
+        producerBean.setProperties(mqProperties(env));
+        producerBean.start();
+        return producerBean;
+    }
+
+    @Bean
+    public Consumer consumerBean(@Autowired Environment env, @Autowired MessageListenerWrapper
+            messageListenerWrapper, @Autowired List<AbstractMessageListener> messageListeners) {
+        Consumer consumer = ONSFactory.createConsumer(mqProperties(env));
+
+        if (messageListeners.size() > 0) {
+            for (int i = 0; i < messageListeners.size(); i++) {
+                AbstractMessageListener abstractMessageListener = messageListeners.get(i);
+                String topic = abstractMessageListener.topic();
+                consumer.subscribe(topic, "*", messageListenerWrapper);
+            }
+            consumer.start();
+
+        }
+        return consumer;
+    }
+
+    /**
+     * 通用的参数属性
+     *
+     * @param env
+     * @return
+     */
+    public Properties mqProperties(Environment env) {
+        Properties properties = new Properties();
+        // AccessKey 阿里云身份验证,在阿里云服务器管理控制台创建
+        properties.put(PropertyKeyConst.AccessKey, env.getProperty(getPrefix(PropertyKeyConst.AccessKey)));
+        // SecretKey 阿里云身份验证,在阿里云服务器管理控制台创建
+        properties.put(PropertyKeyConst.SecretKey, env.getProperty(getPrefix(PropertyKeyConst.SecretKey)));
+        // 设置 TCP 接入域名,进入控制台的实例管理页面的“获取接入点信息”区域查看
+        properties.put(PropertyKeyConst.NAMESRV_ADDR, env.getProperty(getPrefix(PropertyKeyConst.NAMESRV_ADDR)));
+        properties.put(PropertyKeyConst.GROUP_ID, groupId(env));
+        //设置发送超时时间,单位毫秒
+        properties.setProperty(PropertyKeyConst.SendMsgTimeoutMillis, env.getProperty(getPrefix(PropertyKeyConst.SendMsgTimeoutMillis),
+                "5000"));
+        properties.put(PropertyKeyConst.OnsChannel, "ALIYUN");
+        // 顺序消息消费者失败进行重试前的等待时间(单位毫秒)
+        properties.put(PropertyKeyConst.SuspendTimeMillis, env.getProperty(getPrefix(PropertyKeyConst
+                .SuspendTimeMillis), "100"));
+        // 消费失败的重试次数
+        properties.put(PropertyKeyConst.MaxReconsumeTimes, env.getProperty(getPrefix(PropertyKeyConst
+                .MaxReconsumeTimes), "20"));
+        return properties;
+    }
+
+    /**
+     * 默认的配置文件前缀
+     *
+     * @param val
+     * @return
+     */
+    private String getPrefix(String val) {
+        return prefix + "." + val;
+    }
+
+    /**
+     * 默认的groupId是运行环境+项目名称为一组
+     *
+     * @param env
+     * @return
+     */
+    private String groupId(Environment env) {
+        String defaultGroupId = "GID-" + env.getProperty("spring.profiles.active") + "-" + env.getProperty("spring" +
+                ".application.name");
+        return env.getProperty(getPrefix(PropertyKeyConst.GROUP_ID), defaultGroupId);
+    }
+
+
+}

+ 4 - 0
elab-mq/src/main/java/com/elab/mq/config/RocketMqConfigBean.java

@@ -14,12 +14,14 @@ import com.elab.mq.msg.adptor.TransactionCheckListenerAdaptor;
 import com.elab.mq.msg.impl.MsgConsumerImpl;
 import com.elab.mq.msg.impl.MsgProducerImpl;
 import com.elab.mq.msg.impl.MsgTransactionImpl;
+import org.springframework.context.annotation.Configuration;
 
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 
+@Configuration
 public class RocketMqConfigBean {
 
     private String accessKey;
@@ -113,6 +115,8 @@ public class RocketMqConfigBean {
     public IMsgTransactionFacade createTransactionProducer(TransactionCheckListenerAdaptor transactionCheckListener) {
         Properties properties = getProperties();
         properties.put(PropertyKeyConst.ProducerId, this.producerId);
+        properties.put(PropertyKeyConst.GROUP_ID, this.producerId);
+
         MsgTransactionImpl transactionProducer = new MsgTransactionImpl(properties, transactionCheckListener);
         return transactionProducer;
     }

+ 51 - 0
elab-mq/src/main/java/com/elab/mq/listener/AbstractMessageListener.java

@@ -0,0 +1,51 @@
+package com.elab.mq.listener;
+
+import com.aliyun.openservices.ons.api.Action;
+import com.aliyun.openservices.ons.api.ConsumeContext;
+import com.aliyun.openservices.ons.api.Message;
+import com.aliyun.openservices.ons.api.MessageListener;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * 抽象成客户端能够使用的方法
+ *
+ * @author : liukx
+ * @time : 2019/4/22 - 18:02
+ */
+public abstract class AbstractMessageListener implements MessageListener {
+
+    private Logger logger = LoggerFactory.getLogger(getClass());
+
+    /**
+     * 客户端关注的topic消息,实现了该方法,消息监听被触发时会与之匹配。
+     *
+     * @return
+     */
+    public abstract String topic();
+
+    /**
+     * 客户端关注的topic下的tag子类标签
+     *
+     * @return
+     */
+    public abstract String tag();
+
+    /**
+     * 客户端的业务逻辑实现
+     *
+     * @param message        消息对象
+     * @param consumeContext 消息上下文
+     * @return
+     */
+    public abstract Action consume0(Message message, ConsumeContext consumeContext);
+
+    @Override
+    public Action consume(Message message, ConsumeContext consumeContext) {
+        logger.debug("消息处理被触发 : " + message.toString());
+        Action action = consume0(message, consumeContext);
+        logger.debug("消息处理结果 : " + action);
+        return action;
+    }
+
+}

+ 43 - 0
elab-mq/src/main/java/com/elab/mq/listener/MessageListenerWrapper.java

@@ -0,0 +1,43 @@
+package com.elab.mq.listener;
+
+import com.aliyun.openservices.ons.api.Action;
+import com.aliyun.openservices.ons.api.ConsumeContext;
+import com.aliyun.openservices.ons.api.Message;
+import com.aliyun.openservices.ons.api.MessageListener;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import java.util.List;
+
+/**
+ * 订阅组件适配器
+ *
+ * @author : liukx
+ * @time : 2019/4/22 - 17:59
+ */
+@Component
+public class MessageListenerWrapper implements MessageListener {
+
+    @Autowired
+    private List<AbstractMessageListener> messageListeners;
+
+    @Override
+    public Action consume(Message message, ConsumeContext consumeContext) {
+        try {
+            for (int i = 0; i < messageListeners.size(); i++) {
+                AbstractMessageListener abstractMessageListener = messageListeners.get(i);
+                String currentTopic = message.getTopic();
+                String topic = abstractMessageListener.topic();
+                if (topic.equals(currentTopic)) {
+                    abstractMessageListener.consume(message, consumeContext);
+                }
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+            return Action.ReconsumeLater;
+        }
+        return Action.CommitMessage;
+    }
+
+
+}

+ 106 - 0
elab-mq/src/test/java/com/elab/mq/config/RocketMQConfigurationTest.java

@@ -0,0 +1,106 @@
+package com.elab.mq.config;
+
+
+import com.aliyun.openservices.ons.api.*;
+import com.aliyun.openservices.ons.api.bean.ProducerBean;
+import com.elab.core.utils.RandomUtils;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.util.Properties;
+
+public class RocketMQConfigurationTest {
+    String topic = "T_consumer2";
+
+    @Test
+    public void mqProperties() {
+        Properties properties = getProperties();
+        ProducerBean producerBean = new ProducerBean();
+        producerBean.setProperties(properties);
+//        producerBean.setCallbackExecutor(new AbstractExecutorService() {
+//            @Override
+//            public void shutdown() {
+//                System.out.println("-->shutdown");
+//            }
+//
+//            @Override
+//            public List<Runnable> shutdownNow() {
+//                return null;
+//            }
+//
+//            @Override
+//            public boolean isShutdown() {
+//                return false;
+//            }
+//
+//            @Override
+//            public boolean isTerminated() {
+//                return false;
+//            }
+//
+//            @Override
+//            public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
+//                return false;
+//            }
+//
+//            @Override
+//            public void execute(Runnable command) {
+//                System.out.println("execute ... ");
+//            }
+//        });
+        String text = "大家好 ... ";
+        producerBean.start();
+        for (int i = 0; i < 10; i++) {
+            String key = RandomUtils.randomString(10);
+            Message message = new Message("T-consumer1", "*", key, text.getBytes());
+            SendResult send = producerBean.send(message);
+            System.out.println("发送消息返回结果 : " + send.toString());
+        }
+    }
+
+    /**
+     * 对于全局顺序消息,建议创建实例个数 >=2。 同时运行多个实例的作用是为了防止工作实例意外退出时,业务中断。 当工作实例退出时,其他实例可以立即接手工作,不会导致业务中断,实际同时工作的只会有一个实例。
+     *
+     * @throws IOException
+     */
+    @Test
+    public void consumer() throws IOException {
+        Properties properties = getProperties();
+        properties.put(PropertyKeyConst.GROUP_ID, "GID-producer1");
+        Consumer consumer = ONSFactory.createConsumer(properties);
+        MessageListener messageListener = new MessageListener() {
+            @Override
+            public Action consume(Message message, ConsumeContext consumeContext) {
+                System.out.println("消费到的消息 : " + message.toString());
+                return Action.CommitMessage;
+            }
+        };
+
+        consumer.subscribe(topic, "*", messageListener);
+        consumer.subscribe("T-consumer1", "*", messageListener);
+
+        consumer.start();
+        System.out.println("Consumer Started");
+        System.in.read();
+    }
+
+    public Properties getProperties() {
+        Properties properties = new Properties();
+        // AccessKey 阿里云身份验证,在阿里云服务器管理控制台创建
+        properties.put(PropertyKeyConst.AccessKey, "LTAImNZed054h0YV");
+        // SecretKey 阿里云身份验证,在阿里云服务器管理控制台创建
+        properties.put(PropertyKeyConst.SecretKey, "8hmhlhiQ2ikmVeLKujwMNWsktFpSzm");
+        // 设置 TCP 接入域名,进入控制台的实例管理页面的“获取接入点信息”区域查看
+        properties.put(PropertyKeyConst.NAMESRV_ADDR, "http://MQ_INST_1819241776271348_Bak3xjk0.mq-internet-access.mq-internet.aliyuncs.com:80");
+        properties.put(PropertyKeyConst.GROUP_ID, "GID-producer1");
+        //设置发送超时时间,单位毫秒
+        properties.setProperty(PropertyKeyConst.SendMsgTimeoutMillis, "5000");
+
+        properties.put(PropertyKeyConst.OnsChannel, "ALIYUN");
+        // 顺序消息消费者失败进行重试前的等待时间(单位毫秒)
+        properties.put(PropertyKeyConst.SuspendTimeMillis, "100");
+        // 消费失败的重试次数
+        properties.put(PropertyKeyConst.MaxReconsumeTimes, "20");
+        return properties;
+    }
+}

+ 14 - 1
elab-spring/pom.xml

@@ -5,7 +5,7 @@
     <parent>
         <artifactId>elab-parent</artifactId>
         <groupId>com.elab.core</groupId>
-        <version>2.0.4.10-SNAPSHOT</version>
+        <version>2.0.4.11-SNAPSHOT</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
 
@@ -145,4 +145,17 @@
         <!--</dependency>-->
     </dependencies>
 
+    <distributionManagement>
+        <!--<repository>-->
+        <!--<id>releases</id>-->
+        <!--<name>Nexus Release Repository</name>-->
+        <!--<url>http://192.168.0.11:8081/nexus/content/repositories/elab/</url>-->
+        <!--</repository>-->
+        <snapshotRepository>
+            <id>snapshots</id>
+            <name>User Project SNAPSHOTS</name>
+            <url>http://192.168.0.11:8081/nexus/content/repositories/snapshots/</url>
+        </snapshotRepository>
+    </distributionManagement>
+
 </project>

+ 15 - 2
elab-spring/src/main/java/com/elab/spring/exception/CommonException.java

@@ -8,8 +8,10 @@ import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.core.env.Environment;
 import org.springframework.http.HttpStatus;
+import org.springframework.http.converter.HttpMessageNotReadableException;
 import org.springframework.validation.FieldError;
 import org.springframework.validation.ObjectError;
+import org.springframework.web.HttpMediaTypeNotSupportedException;
 import org.springframework.web.HttpRequestMethodNotSupportedException;
 import org.springframework.web.bind.MethodArgumentNotValidException;
 import org.springframework.web.bind.annotation.ControllerAdvice;
@@ -84,12 +86,23 @@ public class CommonException {
         return Info.NO(errorCode, message);
     }
 
-    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
+    @ExceptionHandler(value = {HttpRequestMethodNotSupportedException.class, HttpMediaTypeNotSupportedException.class})
     @ResponseBody
-    @ResponseStatus(value = HttpStatus.NOT_FOUND)
+    @ResponseStatus(value = HttpStatus.METHOD_NOT_ALLOWED)
     public Object notFondMethod(Exception ex) {
         // 将匹配不到方法的异常状态定位404。
         Info info = getInfo(defaultNotFoundError, "not fond");
         return info;
     }
+
+    @ExceptionHandler(HttpMessageNotReadableException.class)
+    @ResponseBody
+    @ResponseStatus(value = HttpStatus.NOT_FOUND)
+    public Object notReadable(Exception ex) {
+        // 将参数有异常的数据拦截返回
+        Info info = getInfo(defaultNotFoundError, "请检查请求数据..");
+        return info;
+    }
+
+
 }

+ 15 - 1
elab-spring/src/main/java/com/elab/spring/utils/RestTemplateUtils.java

@@ -74,6 +74,20 @@ public class RestTemplateUtils {
      * @return
      */
     public <T> T post(String url, Object reqParam, Class<T> clazz) {
+        return post(url, headers, reqParam, clazz);
+    }
+
+    /**
+     * 执行post请求
+     *
+     * @param url         url
+     * @param httpHeaders head参数
+     * @param reqParam    请求参数
+     * @param clazz       类
+     * @param <T>
+     * @return
+     */
+    public <T> T post(String url, HttpHeaders httpHeaders, Object reqParam, Class<T> clazz) {
         String newUrl = getUrl(url);
         Transaction t = Cat.getProducer().newTransaction(CatMsgConstants.THIRD_PARTY, newUrl);
         logger.info(" URL : " + url);
@@ -83,7 +97,7 @@ public class RestTemplateUtils {
         T responseData = null;
         try {
             //利用容器实现数据封装,发送
-            HttpEntity<Object> entity = new HttpEntity<Object>(reqParam, headers);
+            HttpEntity<Object> entity = new HttpEntity<Object>(reqParam, httpHeaders);
             responseData = restTemplate.postForObject(url, entity, clazz);
             t.setStatus(Transaction.SUCCESS);
             logResponse(responseData);

+ 1 - 1
pom.xml

@@ -7,7 +7,7 @@
     <groupId>com.elab.core</groupId>
     <artifactId>elab-parent</artifactId>
     <packaging>pom</packaging>
-    <version>2.0.4.10-SNAPSHOT</version>
+    <version>2.0.4.11-SNAPSHOT</version>
     <modules>
         <module>elab-core</module>
         <module>elab-cache</module>