Commit 9d7ba9dce896d62c87a09c58280e74a8ffd9e8ac
Exists in
master
and in
6 other branches
Merge remote-tracking branch 'origin/master'
Showing 15 changed files
- platform-biz-patient-service/src/main/java/com/lyms/platform/biz/RemoteService.java
- platform-operate-api/pom.xml
- platform-operate-api/src/main/java/com/lyms/platform/operate/web/controller/CouponController.java
- platform-operate-api/src/main/resources/config.properties
- platform-operate-api/src/main/webapp/WEB-INF/web.xml
- regional-etl/pom.xml
- regional-etl/src/main/java/com/lyms/etl/ApplicationRunner.java
- regional-etl/src/main/java/com/lyms/etl/datasource/DynamicDataSource.java
- regional-etl/src/main/java/com/lyms/etl/datasource/DynamicDataSourceAnno.java
- regional-etl/src/main/java/com/lyms/etl/datasource/DynamicDataSourceAspect.java
- regional-etl/src/main/java/com/lyms/etl/datasource/DynamicDataSourceContextHolder.java
- regional-etl/src/main/java/com/lyms/etl/datasource/DynamicDataSourceRegister.java
- regional-etl/src/main/java/com/lyms/etl/util/ReflectUtil.java
- regional-etl/src/main/resources/application.properties
- regional-etl/src/main/resources/application.yml
platform-biz-patient-service/src/main/java/com/lyms/platform/biz/RemoteService.java
View file @
9d7ba9d
1 | 1 | package com.lyms.platform.biz; |
2 | 2 | |
3 | 3 | import com.lyms.platform.common.utils.HttpClientUtil; |
4 | -import com.lyms.platform.common.utils.PropertiesUtil; | |
5 | -import org.apache.commons.lang3.StringUtils; | |
6 | 4 | import org.springframework.stereotype.Service; |
7 | 5 | |
8 | -import javax.annotation.PostConstruct; | |
9 | -import java.text.MessageFormat; | |
10 | 6 | import java.util.HashMap; |
11 | 7 | import java.util.Map; |
12 | 8 | |
13 | 9 | |
... | ... | @@ -18,18 +14,9 @@ |
18 | 14 | @Service |
19 | 15 | public class RemoteService { |
20 | 16 | |
21 | - private String BASE_URL; | |
17 | +// private String BASE_URL = PropertiesUtils.getPropertyValue("platform.operate.api.validate.url"); | |
18 | + private String BASE_URL = "http://dev-rp-api.healthbaby.com.cn:8082/"; | |
22 | 19 | |
23 | - public static final String INVALID_COUPON_URL = "coupon/invalid"; | |
24 | - | |
25 | - @PostConstruct | |
26 | - public void init() { | |
27 | - String type = PropertiesUtil.getInstance().get("config","remote.url.type"); | |
28 | - if(StringUtils.isNotBlank(type)) { | |
29 | - this.BASE_URL = PropertiesUtil.getInstance().get("config", MessageFormat.format("platform.operate.api.{0}.url", type)); | |
30 | - } | |
31 | - } | |
32 | - | |
33 | 20 | /** |
34 | 21 | * 作废优惠券相关 |
35 | 22 | * @param personId |
... | ... | @@ -39,7 +26,7 @@ |
39 | 26 | params.put("personId", personId); |
40 | 27 | params.put("couponTypes", couponTypes); |
41 | 28 | String rest = HttpClientUtil.doPost(BASE_URL + urlEnum.getUrl(), params, "utf-8"); |
42 | - System.out.println("远程调用作废优惠券: personId: " + personId + ", couponTypes: " + couponTypes + ", 返回值: " + rest); | |
29 | + System.out.println("远程调用作废优惠券: personId: " + personId + ", couponTypes: " + couponTypes + ", 返回值: " + rest + " url: " + BASE_URL + urlEnum.getUrl()); | |
43 | 30 | } |
44 | 31 | |
45 | 32 | } |
platform-operate-api/pom.xml
View file @
9d7ba9d
platform-operate-api/src/main/java/com/lyms/platform/operate/web/controller/CouponController.java
View file @
9d7ba9d
... | ... | @@ -115,9 +115,11 @@ |
115 | 115 | * @param couponTypes 要删除的类型 |
116 | 116 | */ |
117 | 117 | @RequestMapping(method = RequestMethod.POST, value = "/invalid") |
118 | - public void invalid(String personId, String couponTypes){ | |
118 | + @ResponseBody | |
119 | + public BaseObjectResponse invalid(HttpServletRequest request,String personId, String couponTypes){ | |
119 | 120 | LOG.info("invalid>>> personId: {}, couponTypes: {} ", personId, couponTypes); |
120 | 121 | couponService.invalid(personId, couponTypes); |
122 | + return RespBuilder.buildSuccess(); | |
121 | 123 | } |
122 | 124 | |
123 | 125 |
platform-operate-api/src/main/resources/config.properties
View file @
9d7ba9d
... | ... | @@ -78,10 +78,5 @@ |
78 | 78 | jdbc.7.username=LYMS_ODS |
79 | 79 | jdbc.7.password=Welcome1 |
80 | 80 | |
81 | - | |
82 | -# 远程调用类型 1=本地 2=测试环境 3=线上环境 | |
83 | -remote.url.type=2 | |
84 | -platform.operate.api.1.url=http://localhost:9091/ | |
85 | -platform.operate.api.2.url=https://dev-rp-api.healthbaby.com.cn/ | |
86 | -platform.operate.api.3.url=https://dev-rp.healthbaby.com.cn/ | |
81 | +platform.operate.api.validate.url=http://localhost:8080/ |
platform-operate-api/src/main/webapp/WEB-INF/web.xml
View file @
9d7ba9d
regional-etl/pom.xml
View file @
9d7ba9d
... | ... | @@ -21,6 +21,11 @@ |
21 | 21 | <artifactId>spring-boot-starter</artifactId> |
22 | 22 | </dependency> |
23 | 23 | |
24 | + <dependency> | |
25 | + <groupId>org.springframework.boot</groupId> | |
26 | + <artifactId>spring-boot-starter-aop</artifactId> | |
27 | + </dependency> | |
28 | + | |
24 | 29 | <dependency> |
25 | 30 | <groupId>org.mybatis.spring.boot</groupId> |
26 | 31 | <artifactId>mybatis-spring-boot-starter</artifactId> |
... | ... | @@ -47,7 +52,7 @@ |
47 | 52 | <dependency> |
48 | 53 | <groupId>com.alibaba</groupId> |
49 | 54 | <artifactId>druid</artifactId> |
50 | - <version>1.0.28</version> | |
55 | + <version>RELEASE</version> | |
51 | 56 | </dependency> |
52 | 57 | |
53 | 58 | <dependency> |
regional-etl/src/main/java/com/lyms/etl/ApplicationRunner.java
View file @
9d7ba9d
1 | 1 | package com.lyms.etl; |
2 | 2 | |
3 | +import com.lyms.etl.datasource.DynamicDataSourceRegister; | |
3 | 4 | import com.lyms.etl.service.IInvokeHandler; |
4 | 5 | import com.lyms.etl.util.AopTargetUtil; |
5 | 6 | import org.mybatis.spring.annotation.MapperScan; |
... | ... | @@ -9,6 +10,7 @@ |
9 | 10 | import org.springframework.boot.CommandLineRunner; |
10 | 11 | import org.springframework.boot.SpringApplication; |
11 | 12 | import org.springframework.boot.autoconfigure.SpringBootApplication; |
13 | +import org.springframework.context.annotation.Import; | |
12 | 14 | import org.springframework.transaction.annotation.EnableTransactionManagement; |
13 | 15 | |
14 | 16 | import java.util.Date; |
... | ... | @@ -21,6 +23,7 @@ |
21 | 23 | */ |
22 | 24 | @SpringBootApplication |
23 | 25 | @EnableTransactionManagement |
26 | +@Import({ DynamicDataSourceRegister.class }) | |
24 | 27 | @MapperScan("com.lyms.etl.dao") |
25 | 28 | public class ApplicationRunner implements CommandLineRunner{ |
26 | 29 |
regional-etl/src/main/java/com/lyms/etl/datasource/DynamicDataSource.java
View file @
9d7ba9d
1 | +package com.lyms.etl.datasource; | |
2 | + | |
3 | +import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; | |
4 | + | |
5 | +/** | |
6 | + * 动态数据源 | |
7 | + * @Author: litao | |
8 | + * @Date: 2017/5/17 0017 10:25 | |
9 | + * @Version: V1.0 | |
10 | + */ | |
11 | +public class DynamicDataSource extends AbstractRoutingDataSource { | |
12 | + | |
13 | + @Override | |
14 | + protected Object determineCurrentLookupKey() { | |
15 | + return DynamicDataSourceContextHolder.getDataSourceType(); | |
16 | + } | |
17 | +} |
regional-etl/src/main/java/com/lyms/etl/datasource/DynamicDataSourceAnno.java
View file @
9d7ba9d
1 | +package com.lyms.etl.datasource; | |
2 | + | |
3 | +import java.lang.annotation.*; | |
4 | + | |
5 | +/** | |
6 | + * 在方法或类上使用,用于指定使用哪个数据源 | |
7 | + * @Author: litao | |
8 | + * @Date: 2017/5/17 0017 10:31 | |
9 | + * @Version: V1.0 | |
10 | + */ | |
11 | +@Target({ ElementType.METHOD, ElementType.TYPE }) | |
12 | +@Retention(RetentionPolicy.RUNTIME) | |
13 | +@Documented | |
14 | +public @interface DynamicDataSourceAnno { | |
15 | + String value() default ""; | |
16 | +} |
regional-etl/src/main/java/com/lyms/etl/datasource/DynamicDataSourceAspect.java
View file @
9d7ba9d
1 | +package com.lyms.etl.datasource; | |
2 | + | |
3 | +import com.lyms.etl.util.ReflectUtil; | |
4 | +import org.aspectj.lang.JoinPoint; | |
5 | +import org.aspectj.lang.annotation.Aspect; | |
6 | +import org.aspectj.lang.annotation.Before; | |
7 | +import org.aspectj.lang.annotation.Pointcut; | |
8 | +import org.aspectj.lang.reflect.MethodSignature; | |
9 | +import org.slf4j.Logger; | |
10 | +import org.slf4j.LoggerFactory; | |
11 | +import org.springframework.stereotype.Component; | |
12 | + | |
13 | +import java.lang.reflect.Method; | |
14 | + | |
15 | +/** | |
16 | + * 动态切换数据源aop | |
17 | + * @Author: litao | |
18 | + * @Date: 2017/5/17 0017 10:48 | |
19 | + * @Version: V1.0 | |
20 | + */ | |
21 | +@Aspect | |
22 | +@Component | |
23 | +//@Order(1) /** 保证在方法执行前执行 */ | |
24 | +public class DynamicDataSourceAspect { | |
25 | + private static final Logger logger = LoggerFactory.getLogger(DynamicDataSourceAspect.class); | |
26 | + | |
27 | + @Pointcut("@within(DynamicDataSourceAnno) || @annotation(DynamicDataSourceAnno)") | |
28 | + private void advice() { | |
29 | + } | |
30 | + | |
31 | + @Before("advice()") | |
32 | + public void setDataSource(JoinPoint point) { | |
33 | + try { | |
34 | + Class<?> clazz = point.getTarget().getClass(); | |
35 | + MethodSignature signature = (MethodSignature) point.getSignature(); | |
36 | + Method method = signature.getMethod(); | |
37 | + | |
38 | + /** 可能是通过接口调用方法 所以通过反射获取实现类方法上面的注解 */ | |
39 | + DynamicDataSourceAnno anno = ReflectUtil.getMethodAnno(clazz, method.getName(), DynamicDataSourceAnno.class); | |
40 | + if(anno == null) { /** 方法上沒注解获取类上面的注解 由于只拦截了类和方法上的注解 所以不可能为空 */ | |
41 | + anno = clazz.getAnnotation(DynamicDataSourceAnno.class); | |
42 | + } | |
43 | + | |
44 | + String datasource = anno.value(); | |
45 | + if(DynamicDataSourceContextHolder.containsDataSource(datasource)) { | |
46 | + DynamicDataSourceContextHolder.setDataSourceType(datasource); | |
47 | + logger.info("切换到 [{}] 数据源", datasource); | |
48 | + } else { | |
49 | + logger.info("数据源 [{}] 未找到,使用默认数据源", datasource); | |
50 | + } | |
51 | + } catch (Exception e) { | |
52 | + logger.error("切换数据源失败: ", e.fillInStackTrace()); | |
53 | + } | |
54 | + } | |
55 | +} |
regional-etl/src/main/java/com/lyms/etl/datasource/DynamicDataSourceContextHolder.java
View file @
9d7ba9d
1 | +package com.lyms.etl.datasource; | |
2 | + | |
3 | +import java.util.ArrayList; | |
4 | +import java.util.List; | |
5 | + | |
6 | +/** | |
7 | + * 通过ThreadLocal来存储当前所使用数据源对应的key | |
8 | + * @Author: litao | |
9 | + * @Date: 2017/5/17 0017 10:25 | |
10 | + * @Version: V1.0 | |
11 | + */ | |
12 | +public class DynamicDataSourceContextHolder { | |
13 | + | |
14 | + private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>(); | |
15 | + | |
16 | + public static List<String> dataSourceIds = new ArrayList<>(); | |
17 | + | |
18 | + public static void setDataSourceType(String dataSourceType) { | |
19 | + contextHolder.set(dataSourceType); | |
20 | + } | |
21 | + | |
22 | + public static String getDataSourceType() { | |
23 | + return contextHolder.get(); | |
24 | + } | |
25 | + | |
26 | + public static void clearDataSourceType() { | |
27 | + contextHolder.remove(); | |
28 | + } | |
29 | + | |
30 | + public static boolean containsDataSource(String dataSourceId){ | |
31 | + return dataSourceIds.contains(dataSourceId); | |
32 | + } | |
33 | +} |
regional-etl/src/main/java/com/lyms/etl/datasource/DynamicDataSourceRegister.java
View file @
9d7ba9d
1 | +package com.lyms.etl.datasource; | |
2 | + | |
3 | +import com.alibaba.druid.pool.DruidDataSourceFactory; | |
4 | +import org.slf4j.Logger; | |
5 | +import org.slf4j.LoggerFactory; | |
6 | +import org.springframework.beans.factory.support.BeanDefinitionRegistry; | |
7 | +import org.springframework.boot.bind.RelaxedPropertyResolver; | |
8 | +import org.springframework.boot.context.properties.ConfigurationProperties; | |
9 | +import org.springframework.context.EnvironmentAware; | |
10 | +import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; | |
11 | +import org.springframework.core.env.Environment; | |
12 | +import org.springframework.core.type.AnnotationMetadata; | |
13 | + | |
14 | +import javax.sql.DataSource; | |
15 | +import java.util.HashMap; | |
16 | +import java.util.List; | |
17 | +import java.util.Map; | |
18 | + | |
19 | +/** | |
20 | + * @Author: litao | |
21 | + * @Date: 2017/5/18 0018 20:08 | |
22 | + * @Version: V1.0 | |
23 | + */ | |
24 | +public class DynamicDataSourceRegister | |
25 | + implements ImportBeanDefinitionRegistrar, EnvironmentAware { | |
26 | + | |
27 | + private static final Logger LOG = LoggerFactory.getLogger(DynamicDataSourceRegister.class); | |
28 | + | |
29 | + private DataSource defaultDataSource; /** 主数据源 */ | |
30 | + private Map<String, DataSource> customDataSources = new HashMap<>(); /** 其他数据源 */ | |
31 | + | |
32 | + @Override | |
33 | + public void setEnvironment(Environment environment) { | |
34 | + initDefaultDataSource(environment); | |
35 | + initCustomDataSources(environment); | |
36 | + } | |
37 | + | |
38 | + /** | |
39 | + * 初始化多数据源 | |
40 | + * @param environment | |
41 | + */ | |
42 | + private void initCustomDataSources(Environment environment) { | |
43 | + try { | |
44 | + RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(environment, "spring.datasource"); | |
45 | + | |
46 | + Map<String, Object> connProperties = propertyResolver.getSubProperties("."); | |
47 | + System.err.println("connProperties>>> " + connProperties); | |
48 | + List<String> customs = (List<String>) connProperties.get("customs"); | |
49 | + } catch (Exception e) { | |
50 | + LOG.error("初始化多数据源失败:", e.fillInStackTrace()); | |
51 | + } | |
52 | + } | |
53 | + | |
54 | + /** | |
55 | + * 初始化主数据源(默认数据源) | |
56 | + * @param environment | |
57 | + */ | |
58 | + @ConfigurationProperties | |
59 | + private void initDefaultDataSource(Environment environment) { | |
60 | + try { | |
61 | + RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(environment, "spring.datasource"); | |
62 | + | |
63 | + Map<String, Object> connProperties = propertyResolver.getSubProperties("."); | |
64 | + defaultDataSource = DruidDataSourceFactory.createDataSource(connProperties); | |
65 | + } catch (Exception e) { | |
66 | + LOG.error("初始化主数据源出错:", e.fillInStackTrace()); | |
67 | + } | |
68 | + } | |
69 | + | |
70 | + @Override | |
71 | + public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) { | |
72 | + System.err.println("registerBeanDefinitions"); | |
73 | + } | |
74 | +} |
regional-etl/src/main/java/com/lyms/etl/util/ReflectUtil.java
View file @
9d7ba9d
1 | +package com.lyms.etl.util; | |
2 | + | |
3 | +import org.slf4j.Logger; | |
4 | +import org.slf4j.LoggerFactory; | |
5 | + | |
6 | +import java.lang.annotation.Annotation; | |
7 | + | |
8 | +/** | |
9 | + * 反射相关工具类 | |
10 | + * @Author: litao | |
11 | + * @Date: 2017/5/17 0017 17:41 | |
12 | + * @Version: V1.0 | |
13 | + */ | |
14 | +public class ReflectUtil { | |
15 | + | |
16 | + private static final Logger log = LoggerFactory.getLogger(ReflectUtil.class); | |
17 | + | |
18 | + public static <T extends Annotation> T getMethodAnno(Class<?> clazz, String methodName, Class<T> annoClass) { | |
19 | + T anno = null; | |
20 | + try { | |
21 | + anno = clazz.getMethod(methodName).getAnnotation(annoClass); | |
22 | + } catch (Exception e) { | |
23 | + log.error("反射获取注解出错:", e.fillInStackTrace()); | |
24 | + } | |
25 | + return anno; | |
26 | + } | |
27 | + | |
28 | +} |
regional-etl/src/main/resources/application.properties
View file @
9d7ba9d
regional-etl/src/main/resources/application.yml
View file @
9d7ba9d
... | ... | @@ -4,6 +4,12 @@ |
4 | 4 | username: platform |
5 | 5 | password: platform123 |
6 | 6 | driver-class-name: com.mysql.jdbc.Driver |
7 | + # 必须配置为字符类型 @see com.alibaba.druid.pool.DruidDataSourceFactory > value = (String)properties.get("init"); | |
8 | + init: "true" | |
9 | + | |
10 | + customs: | |
11 | + - ds1 | |
12 | + - ds2 | |
7 | 13 | |
8 | 14 | data: |
9 | 15 | mongodb: |