commit 9b5598f970d7261716b1ae1c3ae9b73850f8fd25 Author: nili Date: Fri Mar 18 16:47:33 2022 +0800 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6e8c874 --- /dev/null +++ b/.gitignore @@ -0,0 +1,12 @@ +.idea +**/*/target +**/*/logs +*.iml +**/*/pid +**/*/idl/gen-java +*.log +*.DS_Store +logs +pid +system.conf +jar diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/build b/build new file mode 100755 index 0000000..c65a072 --- /dev/null +++ b/build @@ -0,0 +1,41 @@ +#!/bin/bash + +mode=`cat system.conf | awk -F = '{print$2}'` + +if [ -z "$mode" ]; then + echo "mode is not set" + exit 1 +fi + +project=`echo $mode | awk -F - '{print $1}'` + +if [ -z "$project" ]; then + echo "mode format is wrong" + exit 1 +fi + +logdir=/var/www/data/log/luigi/$project +if [ ! -d "$logdir" ];then + mkdir -p $logdir +fi + +jardir=jar +if [ ! -d "$jardir" ];then + mkdir $jardir +fi + + +rm -rf logs +ln -s $logdir logs + + +mvn clean package -DskipTests -U > /dev/null + +r=$? +if [ "$r" == "0" ] ; then + echo 'build success' +else + echo 'build fail' + exit 1 +fi + diff --git a/luigi-api/pom.xml b/luigi-api/pom.xml new file mode 100644 index 0000000..70b49b4 --- /dev/null +++ b/luigi-api/pom.xml @@ -0,0 +1,84 @@ + + + + luigi + com.bzgame.server + 1.0-SNAPSHOT + + 4.0.0 + + luigi-api + + + 17 + 17 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-aop + + + com.squareup.okhttp3 + okhttp + + + com.google.code.gson + gson + + + com.bzgame.server + common + + + com.google.guava + guava + + + org.apache.commons + commons-collections4 + + + com.bzgame.server + luigi-dao + 1.0-SNAPSHOT + + + + org.apache.commons + commons-lang3 + + + org.springframework.boot + spring-boot-starter-test + + + + + junit + junit + test + + + + redis.clients + jedis + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + \ No newline at end of file diff --git a/luigi-api/src/main/java/com/bzgame/server/luigi/ApiApplication.java b/luigi-api/src/main/java/com/bzgame/server/luigi/ApiApplication.java new file mode 100644 index 0000000..7a0d154 --- /dev/null +++ b/luigi-api/src/main/java/com/bzgame/server/luigi/ApiApplication.java @@ -0,0 +1,26 @@ +package com.bzgame.server.luigi; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.ApplicationPidFileWriter; +import org.springframework.boot.web.servlet.ServletComponentScan; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.EnableAspectJAutoProxy; + +/** + * Created by cba/kit-backend on 2022-03-18 15:25:35 + */ +@SpringBootApplication +@ComponentScan({"com.bzgame.server.luigi.service", + "com.bzgame.server.luigi.dao", + "com.bzgame.server.luigi.api"}) +@ServletComponentScan +@EnableAspectJAutoProxy(proxyTargetClass = true, exposeProxy = true) +public class ApiApplication { + + public static void main(String[] args) { + SpringApplication application = new SpringApplication(ApiApplication.class); + application.addListeners(new ApplicationPidFileWriter()); + application.run(args); + } +} diff --git a/luigi-api/src/main/java/com/bzgame/server/luigi/api/HelloController.java b/luigi-api/src/main/java/com/bzgame/server/luigi/api/HelloController.java new file mode 100644 index 0000000..f98e898 --- /dev/null +++ b/luigi-api/src/main/java/com/bzgame/server/luigi/api/HelloController.java @@ -0,0 +1,17 @@ +package com.bzgame.server.luigi.api; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author nidaren + */ +@RestController +@RequestMapping("/api/luigi") +public class HelloController { + @GetMapping("hello") + public String hello() { + return "hello, luigi"; + } +} diff --git a/luigi-api/src/main/java/com/bzgame/server/luigi/api/RequestContext.java b/luigi-api/src/main/java/com/bzgame/server/luigi/api/RequestContext.java new file mode 100644 index 0000000..f99aad3 --- /dev/null +++ b/luigi-api/src/main/java/com/bzgame/server/luigi/api/RequestContext.java @@ -0,0 +1,62 @@ +package com.bzgame.server.luigi.api; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.util.Assert; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.util.HashMap; +import java.util.Map; + +public class RequestContext { + + private static final String KEY_REQUEST = "request"; + + private static final String KEY_RESPONSE = "response"; + + private static final String KEY_UID = "uid"; + + private static final Logger LOGGER = LoggerFactory.getLogger(RequestContext.class); + + private static final ThreadLocal> THREAD_LOCAL = new ThreadLocal<>(); + + + public static void init(HttpServletRequest request, HttpServletResponse response, Integer uid) { + + Assert.notNull(request, "request is null"); + Map context = new HashMap<>(); + context.put(KEY_REQUEST, request); + context.put(KEY_RESPONSE, response); + context.put(KEY_UID, uid); + THREAD_LOCAL.set(context); + } + + public static HttpServletRequest getRequest() { + + return (HttpServletRequest) THREAD_LOCAL.get().get(KEY_REQUEST); + } + + public static HttpServletResponse getResponse() { + + return (HttpServletResponse) THREAD_LOCAL.get().get(KEY_RESPONSE); + } + + + public static Integer getUid() { + return (Integer) THREAD_LOCAL.get().get(KEY_UID); + } + + public static String getUidStr() { + Integer uid = (Integer) THREAD_LOCAL.get().get(KEY_UID); + if (uid == null) { + return "empty"; + } + return uid + ""; + } + + public static void clear() { + THREAD_LOCAL.remove(); + } + +} diff --git a/luigi-api/src/main/java/com/bzgame/server/luigi/api/aop/ApiAop.java b/luigi-api/src/main/java/com/bzgame/server/luigi/api/aop/ApiAop.java new file mode 100644 index 0000000..75ed814 --- /dev/null +++ b/luigi-api/src/main/java/com/bzgame/server/luigi/api/aop/ApiAop.java @@ -0,0 +1,109 @@ +package com.bzgame.server.luigi.api.aop; + +import com.bczgame.server.common.exception.BizException; +import com.bczgame.server.common.exception.ExceptionCode; +import com.bczgame.server.common.log.L; +import com.bczgame.server.common.log.Status; +import com.bczgame.server.common.vo.Response; +import com.bzgame.server.luigi.api.RequestContext; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.reflect.MethodSignature; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +@Aspect +@Component +public class ApiAop { + + private static final String EMPTY_STR_IN_LOG = "EMPTY"; + + @Value("${spring.application.name}") + private String appName; + + @Autowired + private ObjectMapper objectMapper; + + + @Around("@annotation(com.bzgame.server.luigi.api.aop.ApiFlag)") + public Object advice(ProceedingJoinPoint joinPoint) { + + MethodSignature signature = (MethodSignature) joinPoint.getSignature(); + String methodName = signature.getMethod().getName(); + ApiFlag flag = signature.getMethod().getAnnotation(ApiFlag.class); + boolean log = flag.log(); + boolean logRet = flag.logRet(); + boolean logParam = flag.logParam(); + boolean needLogin = flag.needLogin(); + + Throwable exception = null; + long start = System.currentTimeMillis(); + Object[] params = joinPoint.getArgs(); + Object ret = null; + Status status = Status.SUCCESS; + try { + if (needLogin && RequestContext.getUid() == null) { + throw new BizException(ExceptionCode.NEED_AUTH, "请先登陆"); + } + ret = joinPoint.proceed(); + return ret; + } catch (Throwable e) { + exception = e; + + Response resp = new Response(); + resp.setMessage(e.getMessage()); + if (e instanceof BizException) { + resp.setCode(((BizException) e).getCode()); + } else if (e instanceof IllegalArgumentException) { + resp.setCode(ExceptionCode.PARAM_ILLEGAL_WARN); + resp.setMessage("服务器错误,请稍后重试"); + } else { + status = Status.FAILED; + resp.setCode(ExceptionCode.GENERAL_ERROR); + resp.setMessage("服务器错误,请稍后重试"); + } + + ret = resp; + return resp; + + } finally { + if (log) { + + int cost = (int) (System.currentTimeMillis() - start); + + String paramStr = EMPTY_STR_IN_LOG; + if (logParam && params != null && params.length > 0) { + try { + paramStr = objectMapper.writeValueAsString(params); + } catch (JsonProcessingException e) { + } + } + String retStr = EMPTY_STR_IN_LOG; + if (logRet && ret != null) { + try { + retStr = objectMapper.writeValueAsString(ret); + } catch (JsonProcessingException e) { + } + } + String cookiesStr = EMPTY_STR_IN_LOG; + if (RequestContext.getRequest().getCookies() != null && RequestContext.getRequest().getCookies().length > 0) { + try { + cookiesStr = objectMapper.writeValueAsString(RequestContext.getRequest().getCookies()); + } catch (JsonProcessingException e) { + } + } + + if (exception == null) { + L.api(cost, status, methodName, RequestContext.getUidStr(), cookiesStr, paramStr, retStr); + } else { + L.api(cost, status, methodName, RequestContext.getUidStr(), cookiesStr, paramStr, retStr, exception); + } + } + + } + } +} diff --git a/luigi-api/src/main/java/com/bzgame/server/luigi/api/aop/ApiFlag.java b/luigi-api/src/main/java/com/bzgame/server/luigi/api/aop/ApiFlag.java new file mode 100644 index 0000000..3da16cc --- /dev/null +++ b/luigi-api/src/main/java/com/bzgame/server/luigi/api/aop/ApiFlag.java @@ -0,0 +1,21 @@ +package com.bzgame.server.luigi.api.aop; + + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +public @interface ApiFlag { + + public boolean log() default true; + + public boolean logParam() default true; + + public boolean logRet() default true; + + public boolean needLogin() default false; + +} diff --git a/luigi-api/src/main/java/com/bzgame/server/luigi/api/config/TomcatConfig.java b/luigi-api/src/main/java/com/bzgame/server/luigi/api/config/TomcatConfig.java new file mode 100644 index 0000000..1aa91ed --- /dev/null +++ b/luigi-api/src/main/java/com/bzgame/server/luigi/api/config/TomcatConfig.java @@ -0,0 +1,40 @@ +package com.bzgame.server.luigi.api.config; + +import org.apache.catalina.Context; +import org.apache.catalina.connector.Connector; +import org.apache.coyote.http11.AbstractHttp11Protocol; +import org.apache.tomcat.util.http.LegacyCookieProcessor; +import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer; +import org.springframework.boot.web.embedded.tomcat.TomcatContextCustomizer; +import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; +import org.springframework.boot.web.servlet.server.ServletWebServerFactory; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * Created by cba/kit-backend on 2022-03-18 15:25:35 + */ +@Configuration +public class TomcatConfig { + + + @Bean + public ServletWebServerFactory servletContainerFactory() { + + TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(); + factory.addConnectorCustomizers(new TomcatConnectorCustomizer() { + @Override + public void customize(Connector connector) { + ((AbstractHttp11Protocol)connector.getProtocolHandler()).setMaxKeepAliveRequests(-1); + } + }); + factory.addContextCustomizers(new TomcatContextCustomizer() { + @Override + public void customize(Context context) { + context.setCookieProcessor(new LegacyCookieProcessor()); + } + }); + + return factory; + } +} diff --git a/luigi-api/src/main/java/com/bzgame/server/luigi/api/filter/LoginFilter.java b/luigi-api/src/main/java/com/bzgame/server/luigi/api/filter/LoginFilter.java new file mode 100644 index 0000000..b8761d1 --- /dev/null +++ b/luigi-api/src/main/java/com/bzgame/server/luigi/api/filter/LoginFilter.java @@ -0,0 +1,41 @@ +package com.bzgame.server.luigi.api.filter; + + +import com.bzgame.server.luigi.api.RequestContext; + +import javax.servlet.*; +import javax.servlet.annotation.WebFilter; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; +import java.io.IOException; + +/** + * @author nidaren + */ +@WebFilter(filterName = "ContextFilter", urlPatterns = {"/api/*"}) +public class LoginFilter implements Filter { + + @Override + public void init(FilterConfig filterConfig) throws ServletException { + + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + try { + HttpServletRequest httpRequest = (HttpServletRequest) request; + HttpSession session = httpRequest.getSession(); + Integer uid = (Integer) session.getAttribute("TODO"); + RequestContext.init(httpRequest, (HttpServletResponse) response, uid); + chain.doFilter(request, response); + } finally { + RequestContext.clear(); + } + } + + @Override + public void destroy() { + + } +} diff --git a/luigi-api/src/main/resources/application-default.yml b/luigi-api/src/main/resources/application-default.yml new file mode 100644 index 0000000..d819500 --- /dev/null +++ b/luigi-api/src/main/resources/application-default.yml @@ -0,0 +1,65 @@ +spring: + application: + name: luigi-api + aop: + auto: true + proxy-target-class: true + http: + encoding: + enabled: true + charset: UTF-8 + pid: + file: pid #pid文件名 + session: + redis: + namespace: sss:session + store-type: redis + redis: + host: 127.0.0.1 + port: 6379 + password: + maxTotal: 128 + poolMaxWaitMs: 50 + timeOutMs: 100 + +logging: + config: classpath:logback-spring.xml + +server: + port: 8001 + connection-timeout: 60000 #Time in milliseconds that connectors will wait for another HTTP request before closing the connection. + tomcat: + accept-count: 1024 # Maximum queue length for incoming connection requests when all possible request processing threads are in use. + max-connections: 256 # Maximum number of connections that the server will accept and process at any given time. + max-threads: 32 # Maximum amount of worker threads. + min-spare-threads: 4 # Minimum amount of worker threads. + accesslog: + enabled: false + directory: logs + prefix: tomcat_access_log + rotate: true + servlet: + session: + cookie: + domain: localhost + path: / + name: MARIO-JSESSIONID + max-age: 15d + timeout: 15d + +datasource: + mario: + type: com.zaxxer.hikari.HikariDataSource + jdbcUrl: jdbc:mysql://localhost:3306/mario?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&connectTimeout=300&socketTimeout=2000&serverTimezone=Asia/Shanghai&useAffectedRows=true + username: root + password: + connectionTimeout: 500 + maximumPoolSize: 32 + minimumIdle: 4 + idleTimeout: 600000 #This property controls the maximum amount of time that a connection is allowed to sit idle in the pool + maxLifetime: 3600000 #This property controls the maximum lifetime of a connection in the pool + connectionInitSql: "SET NAMES 'utf8mb4'" + +mybatis: + configuration: + map-underscore-to-camel-case: true #数据库字段下划线映射java对象属性用驼峰命名 diff --git a/luigi-api/src/main/resources/application-prod.yml b/luigi-api/src/main/resources/application-prod.yml new file mode 100644 index 0000000..7027527 --- /dev/null +++ b/luigi-api/src/main/resources/application-prod.yml @@ -0,0 +1,33 @@ +spring: + application: + name: luigi-api + aop: + auto: true + proxy-target-class: true + http: + encoding: + enabled: true + charset: UTF-8 + pid: + file: pid #pid文件名 + +logging: + config: classpath:logback-spring.xml + +server: + port: 8001 + connection-timeout: 60000 #Time in milliseconds that connectors will wait for another HTTP request before closing the connection. + tomcat: + accept-count: 1024 # Maximum queue length for incoming connection requests when all possible request processing threads are in use. + max-connections: 256 # Maximum number of connections that the server will accept and process at any given time. + max-threads: 32 # Maximum amount of worker threads. + min-spare-threads: 4 # Minimum amount of worker threads. + accesslog: + enabled: false + directory: logs + prefix: tomcat_access_log + rotate: true + +mybatis: + configuration: + map-underscore-to-camel-case: true #数据库字段下划线映射java对象属性用驼峰命名 diff --git a/luigi-api/src/main/resources/application-test.yml b/luigi-api/src/main/resources/application-test.yml new file mode 100644 index 0000000..c779b66 --- /dev/null +++ b/luigi-api/src/main/resources/application-test.yml @@ -0,0 +1,65 @@ +spring: + application: + name: luigi-api + aop: + auto: true + proxy-target-class: true + http: + encoding: + enabled: true + charset: UTF-8 + pid: + file: pid #pid文件名 + session: + redis: + namespace: sss:session + store-type: redis + redis: + host: 127.0.0.1 + port: 6379 + password: + maxTotal: 128 + poolMaxWaitMs: 50 + timeOutMs: 100 + +logging: + config: classpath:logback-spring.xml + +server: + port: 8001 + connection-timeout: 60000 #Time in milliseconds that connectors will wait for another HTTP request before closing the connection. + tomcat: + accept-count: 1024 # Maximum queue length for incoming connection requests when all possible request processing threads are in use. + max-connections: 256 # Maximum number of connections that the server will accept and process at any given time. + max-threads: 32 # Maximum amount of worker threads. + min-spare-threads: 4 # Minimum amount of worker threads. + accesslog: + enabled: false + directory: logs + prefix: tomcat_access_log + rotate: true + servlet: + session: + cookie: + domain: bzgames.cn + path: / + name: MARIO-JSESSIONID + max-age: 15d + timeout: 15d + +datasource: + mario: + type: com.zaxxer.hikari.HikariDataSource + jdbcUrl: jdbc:mysql://localhost:3306/mario?autoReconnect=true&useSSL=false&useUnicode=true&characterEncoding=utf-8&connectTimeout=300&socketTimeout=2000&serverTimezone=Asia/Shanghai&useAffectedRows=true + username: marioServer + password: YO@9A9e%0& + connectionTimeout: 500 + maximumPoolSize: 32 + minimumIdle: 4 + idleTimeout: 600000 #This property controls the maximum amount of time that a connection is allowed to sit idle in the pool + maxLifetime: 3600000 #This property controls the maximum lifetime of a connection in the pool + connectionInitSql: "SET NAMES 'utf8mb4'" + +mybatis: + configuration: + map-underscore-to-camel-case: true #数据库字段下划线映射java对象属性用驼峰命名 \ No newline at end of file diff --git a/luigi-api/src/main/resources/logback-spring.xml b/luigi-api/src/main/resources/logback-spring.xml new file mode 100644 index 0000000..5bfa38f --- /dev/null +++ b/luigi-api/src/main/resources/logback-spring.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/luigi-api/src/test/java/com/bzgame/server/luigi/ApiTestApplication.java b/luigi-api/src/test/java/com/bzgame/server/luigi/ApiTestApplication.java new file mode 100644 index 0000000..20e8cf7 --- /dev/null +++ b/luigi-api/src/test/java/com/bzgame/server/luigi/ApiTestApplication.java @@ -0,0 +1,18 @@ +package com.bzgame.server.luigi; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * Created by cba/kit-backend on 2022-03-18 15:25:35 + */ +@SpringBootApplication() +public class ApiTestApplication { + + public static void main(String[] args) throws Exception { + + SpringApplication app = new SpringApplication(ApiTestApplication.class); + app.run(args); + } + +} diff --git a/luigi-api/src/test/java/com/bzgame/server/luigi/api/test/BaseTest.java b/luigi-api/src/test/java/com/bzgame/server/luigi/api/test/BaseTest.java new file mode 100644 index 0000000..1b8bbf3 --- /dev/null +++ b/luigi-api/src/test/java/com/bzgame/server/luigi/api/test/BaseTest.java @@ -0,0 +1,16 @@ +package com.bzgame.server.luigi.api.test; + +import com.bzgame.server.luigi.ApiTestApplication; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.annotation.Commit; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * Created by cba/kit-backend on 2022-03-18 15:25:35 + */ +@RunWith(SpringJUnit4ClassRunner.class) +@SpringBootTest(classes = ApiTestApplication.class) +@Commit +public class BaseTest { +} diff --git a/luigi-api/src/test/resources/application-default.yml b/luigi-api/src/test/resources/application-default.yml new file mode 100644 index 0000000..9a10198 --- /dev/null +++ b/luigi-api/src/test/resources/application-default.yml @@ -0,0 +1,97 @@ +spring: + profiles: + active: default + aop: + auto: true + proxy-target-class: true + http: + encoding: + enabled: true + charset: utf-8 + pid: + file: pid #pid文件名 + +logging: + config: classpath:logback.xml + +datasource: + topic: + type: com.zaxxer.hikari.HikariDataSource + jdbcUrl: jdbc:mysql://localhost:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&connectTimeout=300&socketTimeout=2000&serverTimezone=Asia/Shanghai + username: xxx + password: xxx + connectionTimeout: 500 + maximumPoolSize: 32 + minimumIdle: 4 + idleTimeout: 600000 #This property controls the maximum amount of time that a connection is allowed to sit idle in the pool + maxLifetime: 3600000 #This property controls the maximum lifetime of a connection in the pool + +mybatis: + configuration: + map-underscore-to-camel-case: true #数据库字段下划线映射java对象属性用驼峰命名 + +redis: + maxTotal: 32 + poolMaxWaitMs: 1000 + timeOutMs: 500 + ip: 127.0.01 + port: 6379 + password: xxx + +server: + port: 8000 + connection-timeout: 60000 #Time in milliseconds that connectors will wait for another HTTP request before closing the connection. + tomcat: + accept-count: 1024 # Maximum queue length for incoming connection requests when all possible request processing threads are in use. + max-connections: 256 # Maximum number of connections that the server will accept and process at any given time. + max-threads: 32 # Maximum amount of worker threads. + min-spare-threads: 4 #Minimum amount of worker threads. + +eureka: + client: + register-with-eureka: true #向eureka注册 + fetchRegistry: true #拉取服务注册信息 + registry-fetch-interval-seconds: 30 #拉取服务注册信息频率 + instance-info-replication-interval-seconds: 30 #心跳频率 + service-url: + defaultZone: http:/localhost:8761/eureka/ #eureka地址.多个地址用逗号分隔 + instance: + prefer-ip-address: true #将ip注册到eureka + +feign: + hystrix: + enabled: true + client: + config: + #默认feign 客户端配置 + default: + connectTimeout: 5000 + readTimeout: 5000 + loggerLevel: BASIC + #特定服务的配置 + account-service: #fegin client name + connectTimeout: 1000 + readTimeout: 3000 + loggerLevel: BASIC + +hystrix: + #无法为每个feign client单独配置hystrix command属性https://github.com/spring-cloud/spring-cloud-netflix/issues/1864 + #采用default hystrix command 全局配置 + command: + default: + metrics: + rollingStats: + timeInMilliseconds: 10000 #滑动统计窗口 + circuitBreaker: + enabled: true #开启过载熔断 + requestVolumeThreshold: 1000 #滑动窗口内可触发按错误比例熔断的最小请求数 + errorThresholdPercentage: 50 #熔断触发错误比例 + sleepWindowInMilliseconds: 5000 #熔断时间 + execution: + isolation: + strategy: SEMAPHORE #采用线程池有性能问题,用SEMAPHORE + semaphore: + maxConcurrentRequests: 100 #信号量的上限 + thread: + timeoutInMilliseconds: 5000 #hystrix的超时时间 + diff --git a/luigi-api/src/test/resources/bootstrap.yml b/luigi-api/src/test/resources/bootstrap.yml new file mode 100644 index 0000000..d880232 --- /dev/null +++ b/luigi-api/src/test/resources/bootstrap.yml @@ -0,0 +1,3 @@ +spring: + application: + name: resource-api diff --git a/luigi-api/src/test/resources/logback.xml b/luigi-api/src/test/resources/logback.xml new file mode 100644 index 0000000..2ef334b --- /dev/null +++ b/luigi-api/src/test/resources/logback.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/luigi-dao/pom.xml b/luigi-dao/pom.xml new file mode 100644 index 0000000..a8c968e --- /dev/null +++ b/luigi-dao/pom.xml @@ -0,0 +1,73 @@ + + + + luigi + com.bzgame.server + 1.0-SNAPSHOT + + 4.0.0 + + luigi-dao + + + 17 + 17 + + + + + org.springframework.boot + spring-boot-starter-jdbc + + + + org.apache.tomcat + tomcat-jdbc + + + + + mysql + mysql-connector-java + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + + + junit + junit + test + + + + + + + org.mybatis.generator + mybatis-generator-maven-plugin + 1.3.7 + + src/main/resources/mybatis-generator/generatorConfig.xml + true + true + + + + mysql + mysql-connector-java + 5.1.35 + + + com.bzgame.server + common + 1.0-SNAPSHOT + + + + + + + \ No newline at end of file diff --git a/luigi-dao/src/main/java/com/bzgame/server/luigi/dao/config/MybatisGeneralConfig.java b/luigi-dao/src/main/java/com/bzgame/server/luigi/dao/config/MybatisGeneralConfig.java new file mode 100644 index 0000000..fccbbaa --- /dev/null +++ b/luigi-dao/src/main/java/com/bzgame/server/luigi/dao/config/MybatisGeneralConfig.java @@ -0,0 +1,23 @@ +package com.bzgame.server.luigi.dao.config; + +import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Scope; + +/** + * Created by cba/kit-backend on 2022-03-18 15:25:35 + */ +@Configuration +public class MybatisGeneralConfig { + + public static final String MBTS_CFG_NAME = "mybatisGeneralCfg"; + + @Bean(MBTS_CFG_NAME) + @ConfigurationProperties("mybatis.configuration") + @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) + public org.apache.ibatis.session.Configuration configuration() { + return new org.apache.ibatis.session.Configuration(); + } +} diff --git a/luigi-dao/src/main/java/com/bzgame/server/luigi/dao/config/TopicDataSourceConfig.java b/luigi-dao/src/main/java/com/bzgame/server/luigi/dao/config/TopicDataSourceConfig.java new file mode 100644 index 0000000..8da3a78 --- /dev/null +++ b/luigi-dao/src/main/java/com/bzgame/server/luigi/dao/config/TopicDataSourceConfig.java @@ -0,0 +1,63 @@ +package com.bzgame.server.luigi.dao.config; + +import com.zaxxer.hikari.HikariDataSource; +import org.apache.ibatis.session.SqlSessionFactory; +import org.mybatis.spring.SqlSessionFactoryBean; +import org.mybatis.spring.SqlSessionTemplate; +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.jdbc.DataSourceBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.jdbc.datasource.DataSourceTransactionManager; + +import javax.sql.DataSource; + +/** + * Created by cba/kit-backend on 2022-03-18 15:25:35 + */ +@Configuration +@MapperScan(basePackages = TopicDataSourceConfig.BASE_PACKAGE,sqlSessionFactoryRef = TopicDataSourceConfig.SSF_NAME,sqlSessionTemplateRef = TopicDataSourceConfig.SST_NAME) +public class TopicDataSourceConfig { + + + //TODO 修改配置常量名 + + public static final String DS_NAME = "topicDataSource"; + + public static final String TM_NAME = "topicTransactionManager"; + + public static final String SSF_NAME = "topicSqlSessionFactory"; + + public static final String SST_NAME = "topicSqlSessionTemplate"; + + public static final String DS_CFG_PREFIX = "datasource.topic"; + + public static final String BASE_PACKAGE = "bcz.app.server.luigi.dao.mapper"; + + @Bean(name = DS_NAME) + @ConfigurationProperties(DS_CFG_PREFIX) + public DataSource dataSource() { + return DataSourceBuilder.create().type(HikariDataSource.class).build(); + } + + @Bean(name = TM_NAME) + public DataSourceTransactionManager transactionManager(@Qualifier(DS_NAME) DataSource dataSource) { + return new DataSourceTransactionManager(dataSource); + } + + + @Bean(name = SSF_NAME) + public SqlSessionFactory sqlSessionFactory(@Qualifier(DS_NAME) DataSource dataSource, @Qualifier(MybatisGeneralConfig.MBTS_CFG_NAME) org.apache.ibatis.session.Configuration configuration) throws Exception { + SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); + factoryBean.setConfiguration(configuration); + factoryBean.setDataSource(dataSource); + return factoryBean.getObject(); + } + + @Bean(name = SST_NAME) + public SqlSessionTemplate sqlSessionTemplate(@Qualifier(SSF_NAME) SqlSessionFactory sqlSessionFactory) { + return new SqlSessionTemplate(sqlSessionFactory); + } +} diff --git a/luigi-dao/src/main/java/com/bzgame/server/luigi/dao/domain/DroolRules.java b/luigi-dao/src/main/java/com/bzgame/server/luigi/dao/domain/DroolRules.java new file mode 100644 index 0000000..9b42d4b --- /dev/null +++ b/luigi-dao/src/main/java/com/bzgame/server/luigi/dao/domain/DroolRules.java @@ -0,0 +1,26 @@ +package com.bzgame.server.luigi.dao.domain; + +/** + * @author chenyang@baicizhan.com + * @date 2020/3/9 6:31 PM + */ +public class DroolRules { + private long id; + private String rules; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getRules() { + return rules; + } + + public void setRules(String rules) { + this.rules = rules; + } +} diff --git a/luigi-dao/src/main/java/com/bzgame/server/luigi/dao/mapper/DroolRulesMapper.java b/luigi-dao/src/main/java/com/bzgame/server/luigi/dao/mapper/DroolRulesMapper.java new file mode 100644 index 0000000..69569fe --- /dev/null +++ b/luigi-dao/src/main/java/com/bzgame/server/luigi/dao/mapper/DroolRulesMapper.java @@ -0,0 +1,13 @@ +package com.bzgame.server.luigi.dao.mapper; + +import com.bzgame.server.luigi.dao.domain.DroolRules; +import org.apache.ibatis.annotations.Select; + +/** + * @author chenyang@baicizhan.com + * @date 2020/3/9 6:32 PM + */ +public interface DroolRulesMapper { + @Select("SELECT *FROM drool_rules WHERE id=1") + DroolRules query(); +} diff --git a/luigi-dao/src/test/resources/application-default.yml b/luigi-dao/src/test/resources/application-default.yml new file mode 100644 index 0000000..09620ea --- /dev/null +++ b/luigi-dao/src/test/resources/application-default.yml @@ -0,0 +1,24 @@ +#dao单元测试配置文件 + +spring: + profiles: + active: default + aop: + auto: true + proxy-target-class: true + +datasource: + topic: + type: com.zaxxer.hikari.HikariDataSource + jdbcUrl: jdbc:mysql://localhost:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&connectTimeout=1000&socketTimeout=3000&serverTimezone=Hongkong + username: xxx + password: xxx + connectionTimeout: 500 + maximumPoolSize: 5 + minimumIdle: 1 + idleTimeout: 600000 #This property controls the maximum amount of time that a connection is allowed to sit idle in the pool + maxLifetime: 3600000 #This property controls the maximum lifetime of a connection in the pool + +mybatis: + configuration: + map-underscore-to-camel-case: true #数据库字段下划线映射java对象属性用驼峰命名 diff --git a/luigi-service/pom.xml b/luigi-service/pom.xml new file mode 100644 index 0000000..9c6404a --- /dev/null +++ b/luigi-service/pom.xml @@ -0,0 +1,54 @@ + + + + luigi + com.bzgame.server + 1.0-SNAPSHOT + + 4.0.0 + + luigi-service + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-aop + + + junit + junit + test + + + org.springframework.boot + spring-boot-starter-test + + + redis.clients + jedis + + + com.google.guava + guava + + + com.fasterxml.jackson.core + jackson-core + + + com.fasterxml.jackson.core + jackson-databind + + + com.fasterxml.jackson.core + jackson-annotations + + + + + diff --git a/luigi-service/src/test/java/com/bzgame/server/luigi/ServiceTestApplication.java b/luigi-service/src/test/java/com/bzgame/server/luigi/ServiceTestApplication.java new file mode 100644 index 0000000..19d14d2 --- /dev/null +++ b/luigi-service/src/test/java/com/bzgame/server/luigi/ServiceTestApplication.java @@ -0,0 +1,17 @@ +package com.bzgame.server.luigi; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * Created by cba/kit-backend on 2022-03-18 15:25:35 + */ +@SpringBootApplication +public class ServiceTestApplication { + + public static void main(String[] args) throws Exception { + + SpringApplication app = new SpringApplication(ServiceTestApplication.class); + app.run(args); + } +} diff --git a/luigi-service/src/test/java/com/bzgame/server/luigi/service/test/BaseTest.java b/luigi-service/src/test/java/com/bzgame/server/luigi/service/test/BaseTest.java new file mode 100644 index 0000000..1660018 --- /dev/null +++ b/luigi-service/src/test/java/com/bzgame/server/luigi/service/test/BaseTest.java @@ -0,0 +1,16 @@ +package com.bzgame.server.luigi.service.test; + +import com.bzgame.server.luigi.ServiceTestApplication; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.annotation.Commit; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * Created by cba/kit-backend on 2022-03-18 15:25:35 + */ +@RunWith(SpringJUnit4ClassRunner.class) +@SpringBootTest(classes = ServiceTestApplication.class) +@Commit +public class BaseTest { +} diff --git a/luigi-service/src/test/resources/application-default.yml b/luigi-service/src/test/resources/application-default.yml new file mode 100644 index 0000000..85d9c7e --- /dev/null +++ b/luigi-service/src/test/resources/application-default.yml @@ -0,0 +1,25 @@ +#service单元测试配置文件 + +spring: + profiles: + active: default + aop: + auto: true + proxy-target-class: true + +datasource: + topic: + type: com.zaxxer.hikari.HikariDataSource + jdbcUrl: jdbc:mysql://localhost:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&connectTimeout=300&socketTimeout=2000&serverTimezone=Hongkong + username: xxx + password: xxx + connectionTimeout: 300 + maximumPoolSize: 5 + minimumIdle: 1 + idleTimeout: 600000 #This property controls the maximum amount of time that a connection is allowed to sit idle in the pool + maxLifetime: 3600000 #This property controls the maximum lifetime of a connection in the pool + +mybatis: + configuration: + map-underscore-to-camel-case: true #数据库字段下划线映射java对象属性用驼峰命名 + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..86394b6 --- /dev/null +++ b/pom.xml @@ -0,0 +1,109 @@ + + + 4.0.0 + + com.bzgame.server + luigi + pom + 1.0-SNAPSHOT + + luigi-api + luigi-service + luigi-dao + + + + 17 + 17 + + + + org.springframework.boot + spring-boot-starter-parent + 2.6.2 + + + + + + mysql + mysql-connector-java + 8.0.27 + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 1.3.2 + + + com.google.code.gson + gson + 2.8.9 + + + com.bzgame.server + common + 1.0-SNAPSHOT + + + com.squareup.okhttp3 + okhttp + 3.14.2 + + + com.google.guava + guava + 21.0 + + + + org.apache.commons + commons-collections4 + 4.1 + + + + + org.apache.commons + commons-lang3 + 3.12.0 + + + + + junit + junit + 4.13.2 + test + + + + redis.clients + jedis + 2.9.0 + + + + + + + + maven-source-plugin + 3.2.1 + + true + + + + compile + + jar + + + + + + + \ No newline at end of file diff --git a/run b/run new file mode 100755 index 0000000..a7513d8 --- /dev/null +++ b/run @@ -0,0 +1,34 @@ +#!/bin/bash +mode=`cat system.conf | awk -F = '{print$2}'` + +if [ -z "$mode" ]; then + echo "mode is not set" + exit 1 +fi + +project=`echo $mode | awk -F - '{print $1}'` +profile=`echo $mode | awk -F - '{print $2}'` + +if [ -z "$project" ]; then + echo "mode format is wrong" + exit 1 +fi + +if [ -z "$profile" ]; then + echo "mode format is wrong" + exit 1 +fi +version=1.0-SNAPSHOT +jar=luigi-$project-$version.jar +targetdir=luigi-$project/target +projectmem=256m + +if [ "$profile" = "prod" ] +then + projectmem=512m +fi + +rm -rf jar/* +cp $targetdir/$jar jar/ + +java -server -Xmx$projectmem -XX:+UseG1GC -XX:MaxGCPauseMillis=100 -XX:+UseCompressedOops -XX:+UseCompressedClassPointers -verbose:gc -Xlog:gc*:logs/gc.log:time,l,tg:filecount=7,filesize=16M -jar jar/$jar --spring.profiles.active=$profile > run.log 2>&1 diff --git a/stop b/stop new file mode 100755 index 0000000..f0daf88 --- /dev/null +++ b/stop @@ -0,0 +1,14 @@ +#!/bin/bash + + +if [ -f pid ]; then + if kill `cat pid` ; then + echo 'kill process succeed' + else + echo 'kill process fail' + exit 1 + fi +else + echo 'pid file not exists' + exit 1 +fi