Browse Source

更新cloudbus的组织方式

tuonina 6 years ago
parent
commit
ecc5d43e36

+ 3 - 0
build.gradle

@@ -129,6 +129,9 @@ subprojects {
         compile("io.springfox:springfox-swagger2:$swagger_version")
         compile("io.springfox:springfox-swagger-ui:$swagger_version")
 
+        compile("cn.gygxzc.cloud:jwt-session:$jwtVersion")
+        compile("cn.gygxzc.cloud:tina-auth:$authVersion")
+
         ext.jarTree = fileTree(dir: 'libs', include: '**/*.jar')
         compile jarTree
     }

BIN
classes/production/zen-web/META-INF/zen-web.kotlin_module


+ 1 - 0
cloud-bus/build.gradle

@@ -10,4 +10,5 @@ repositories {
 
 dependencies {
     compile 'org.springframework.cloud:spring-cloud-starter-bus-amqp'
+    compile project(":zen-core")
 }

+ 2 - 1
cloud-bus/src/main/java/cn/gygxzc/envir/RabbitConfiguration.java

@@ -10,7 +10,8 @@ import org.springframework.context.annotation.Configuration;
  * Created by niantuo on 2018/9/29.
  * rabbit配置.
  * 暂时对这个不了解,暂时使用自动配置结果
- * 注意分包方式
+ * 注意分包方式。
+ * 只要导入该包,即可自动配置,无需再用注解。
  */
 
 @Configuration

+ 0 - 1
cloud-bus/src/main/java/cn/gygxzc/tina/cloud/bus/framweork/receiver/FrameworkReceiver.java

@@ -1,7 +1,6 @@
 package cn.gygxzc.tina.cloud.bus.framweork.receiver;
 
 import cn.gygxzc.tina.cloud.bus.constant.QueueName;
-import cn.gygxzc.tina.cloud.bus.message.LogMessage;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.amqp.rabbit.annotation.RabbitHandler;

+ 0 - 8
cloud-bus/src/main/java/cn/gygxzc/tina/cloud/bus/framweork/receiver/LoggerReceiver.java

@@ -1,16 +1,8 @@
 package cn.gygxzc.tina.cloud.bus.framweork.receiver;
 
-import cn.gygxzc.tina.cloud.bus.constant.QueueName;
-import cn.gygxzc.tina.cloud.bus.message.LogMessage;
-import cn.gygxzc.tina.cloud.bus.utils.GsonUtils;
-import org.springframework.amqp.rabbit.annotation.RabbitHandler;
-import org.springframework.amqp.rabbit.annotation.RabbitListener;
-import org.springframework.stereotype.Component;
-
 /**
  * Created by niantuo on 2018/11/5.
  * 日志收集器.
- *
  */
 //@Component
 //@RabbitListener(queues = QueueName.LOG)

+ 2 - 3
cloud-bus/src/main/java/cn/gygxzc/tina/cloud/bus/messager/sender/IMessagerSender.java

@@ -1,6 +1,6 @@
 package cn.gygxzc.tina.cloud.bus.messager.sender;
 
-import cn.gygxzc.tina.cloud.bus.message.LogMessage;
+import cn.gygxzc.envir.core.logger.ILoggerSender;
 import cn.gygxzc.tina.cloud.bus.messager.bean.MessageBean;
 
 /**
@@ -8,9 +8,8 @@ import cn.gygxzc.tina.cloud.bus.messager.bean.MessageBean;
  * 定义发送消息给消息中心的消息发送者
  */
 
-public interface IMessagerSender {
+public interface IMessagerSender extends ILoggerSender {
 
     void send(MessageBean message);
 
-    void send(LogMessage message);
 }

+ 2 - 1
cloud-bus/src/main/java/cn/gygxzc/tina/cloud/bus/messager/sender/impl/MessagerSender.java

@@ -1,7 +1,7 @@
 package cn.gygxzc.tina.cloud.bus.messager.sender.impl;
 
+import cn.gygxzc.envir.core.logger.LogMessage;
 import cn.gygxzc.tina.cloud.bus.constant.QueueName;
-import cn.gygxzc.tina.cloud.bus.message.LogMessage;
 import cn.gygxzc.tina.cloud.bus.messager.bean.MessageBean;
 import cn.gygxzc.tina.cloud.bus.messager.sender.IMessagerSender;
 import cn.gygxzc.tina.cloud.bus.utils.GsonUtils;
@@ -33,6 +33,7 @@ public class MessagerSender implements IMessagerSender {
         amqpTemplate.convertAndSend(QueueName.Messager, message);
     }
 
+
     @Override
     public void send(LogMessage message) {
         amqpTemplate.convertAndSend(QueueName.LOG, GsonUtils.toJson(message));

+ 0 - 3
zen-core/build.gradle

@@ -18,7 +18,4 @@ dependencies {
     compile 'io.jsonwebtoken:jjwt-api:0.10.5'
     runtime 'io.jsonwebtoken:jjwt-impl:0.10.5',
             'io.jsonwebtoken:jjwt-jackson:0.10.5'
-    compile("cn.gygxzc.cloud:jwt-session:$jwtVersion")
-    compile("cn.gygxzc.cloud:tina-auth:$authVersion")
-    compile project(':cloud-bus')
 }

+ 12 - 0
zen-core/src/main/java/cn/gygxzc/envir/core/logger/ILoggerSender.java

@@ -0,0 +1,12 @@
+package cn.gygxzc.envir.core.logger;
+
+/**
+ * @program: Zen-Framework
+ * @description: ${description}
+ * @author: tuonina
+ * @create: 2018-12-15 18:48
+ **/
+public interface ILoggerSender {
+
+    public void send(LogMessage message);
+}

+ 2 - 2
cloud-bus/src/main/java/cn/gygxzc/tina/cloud/bus/message/LogMessage.java → zen-core/src/main/java/cn/gygxzc/envir/core/logger/LogMessage.java

@@ -1,7 +1,7 @@
-package cn.gygxzc.tina.cloud.bus.message;
+package cn.gygxzc.envir.core.logger;
 
 import ch.qos.logback.classic.spi.ILoggingEvent;
-import cn.gygxzc.tina.cloud.bus.utils.GsonUtils;
+import cn.gygxzc.envir.core.utils.GsonUtils;
 
 /**
  * Created by niantuo on 2018/11/5.

+ 15 - 7
zen-core/src/main/java/cn/gygxzc/envir/core/logger/MQAppender.java

@@ -3,8 +3,6 @@ package cn.gygxzc.envir.core.logger;
 import ch.qos.logback.classic.spi.ILoggingEvent;
 import ch.qos.logback.core.AppenderBase;
 import cn.gygxzc.envir.core.utils.ApplicationUtils;
-import cn.gygxzc.tina.cloud.bus.message.LogMessage;
-import cn.gygxzc.tina.cloud.bus.messager.sender.IMessagerSender;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -16,8 +14,10 @@ import org.slf4j.LoggerFactory;
 public class MQAppender extends AppenderBase<ILoggingEvent> {
     private final static Logger logger = LoggerFactory.getLogger(MQAppender.class);
 
-    private IMessagerSender mMessageSender;
+    private ILoggerSender mMessageSender;
     private String sysName;
+    private int maxRetryCount = 5;
+    private int currentCount = 0;
 
     public MQAppender() {
         logger.debug("construct");
@@ -32,19 +32,27 @@ public class MQAppender extends AppenderBase<ILoggingEvent> {
     }
 
     private void ensureSender() {
-        if (mMessageSender == null) {
-            mMessageSender = ApplicationUtils.getBean(IMessagerSender.class);
+        if (mMessageSender == null && currentCount < maxRetryCount) {
+            mMessageSender = ApplicationUtils.getBean(ILoggerSender.class);
+            currentCount++;
         }
     }
 
 
-
     @Override
     protected void append(ILoggingEvent eventObject) {
-        LogMessage message = new LogMessage(eventObject,sysName);
+        LogMessage message = new LogMessage(eventObject, sysName);
         ensureSender();
         if (mMessageSender != null) {
             mMessageSender.send(message);
         }
     }
+
+    public void setMaxRetryCount(int maxRetryCount) {
+        this.maxRetryCount = maxRetryCount;
+    }
+
+    public int getMaxRetryCount() {
+        return maxRetryCount;
+    }
 }

+ 7 - 2
zen-core/src/main/java/cn/gygxzc/envir/core/utils/ApplicationUtils.java

@@ -18,12 +18,17 @@ public class ApplicationUtils implements ApplicationContextAware {
     private final static Logger logger = LoggerFactory.getLogger(ApplicationUtils.class);
 
 
+    /**
+     * @param clz s
+     * @param <T>
+     * @return
+     */
     public static <T> T getBean(Class<T> clz) {
         if (mContext == null) return null;
         try {
             return mContext.getBean(clz);
-        } catch (Exception e) {
-            logger.error("getBean", e);
+        } catch (Exception ignored) {
+            logger.info("no bean found {}",clz.getName());
         }
         return null;
     }

+ 26 - 0
zen-core/src/main/java/cn/gygxzc/envir/core/utils/GsonUtils.java

@@ -0,0 +1,26 @@
+package cn.gygxzc.envir.core.utils;
+
+import com.google.gson.Gson;
+
+/**
+ * Created by niantuo on 2018/11/5.
+ */
+
+public class GsonUtils {
+    private final static Gson mGson = new Gson();
+
+    public static String toJson(Object obj) {
+        return mGson.toJson(obj);
+    }
+
+    /**
+     * 实际上如果是数组,这样子是解析不出来的吧。
+     * @param clz 要返回的类型
+     * @param json 原始json数据
+     * @param <T> 泛型
+     * @return
+     */
+    public static <T> T fromJson(Class<T> clz, String json) {
+        return mGson.fromJson(json, clz);
+    }
+}

+ 1 - 1
zen-web/build.gradle

@@ -12,7 +12,7 @@ buildscript {
 
 dependencies {
     compile project(":zen-api")
-    compile project(':cloud-bus')
+//    compile project(':cloud-bus')
     compile project(':fastdfs-client')
 }
 

+ 1 - 2
zen-web/src/main/kotlin/cn/gygxzc/envir/MainApplication.kt

@@ -18,12 +18,11 @@ import org.springframework.cloud.openfeign.EnableFeignClients
 @EnableTinaAuth
 @EnableFeignClients
 @EnableFastDFSClient
-@EnableRabbitMessage
 @EnableDiscoveryClient
 @EnableJwtRedisSession
 @SpringBootApplication(scanBasePackages = ["cn.gygxzc.envir", "com.gxzc"], exclude = [RedisAutoConfiguration::class])
 @MapperScan(basePackages = ["cn.gygxzc.**.dao", "cn.gygxzc.**.mapper"])
-open class MainApplication : SpringBootServletInitializer() {
+class MainApplication : SpringBootServletInitializer() {
 
 
     override fun configure(builder: SpringApplicationBuilder?): SpringApplicationBuilder? {

+ 0 - 27
zen-web/src/main/kotlin/cn/gygxzc/envir/api/rabbit/controller/RabbitMQController.kt

@@ -1,27 +0,0 @@
-package cn.gygxzc.envir.api.rabbitmq.controller
-
-import cn.gygxzc.tina.cloud.bus.messager.bean.MessageBean
-import cn.gygxzc.tina.cloud.bus.messager.sender.IMessagerSender
-import cn.gygxzc.tina.cloud.jwt.session.filter.JwtSessionRepositoryFilter
-import org.springframework.beans.factory.annotation.Autowired
-import org.springframework.web.bind.annotation.PostMapping
-import org.springframework.web.bind.annotation.RequestMapping
-import org.springframework.web.bind.annotation.RestController
-
-/**
- * Created by niantuo on 2018/9/29.
- * 这个是一个消息总线的测试controller
- */
-@RestController
-@RequestMapping("/v1/rabbit/send")
-class RabbitMQController {
-
-    @Autowired
-    private lateinit var messageSender: IMessagerSender
-
-    @PostMapping
-    fun send(message: MessageBean) {
-        messageSender.send(message)
-    }
-
-}