Commit 99eb8a397ea9b20a620011f1bdea9f6b8b469b34

Authored by Kurisu
1 parent 3d8b2ff6

feat(*): 加入协程

- 加入协程
fw-hestia-rpc/pom.xml
... ... @@ -42,6 +42,10 @@
42 42 <artifactId>spring-data-redis</artifactId>
43 43 <optional>true</optional>
44 44 </dependency>
  45 + <dependency>
  46 + <groupId>org.jetbrains.kotlinx</groupId>
  47 + <artifactId>kotlinx-coroutines-core</artifactId>
  48 + </dependency>
45 49 </dependencies>
46 50  
47 51 </project>
48 52 \ No newline at end of file
... ...
fw-hestia-rpc/src/main/java/cn/fw/hestia/rpc/member/MemberRpcService.kt
1   -package cn.fw.hestia.rpc.member;
  1 +package cn.fw.hestia.rpc.member
2 2  
3   -import cn.fw.common.util.MobileUtil;
4   -import cn.fw.data.base.domain.common.Message;
5   -import cn.fw.hestia.common.constant.Constant;
6   -import cn.fw.hestia.rpc.member.dto.MemberUserDTO;
7   -import cn.fw.member.sdk.api.FunctionApi;
8   -import cn.fw.member.sdk.api.MemberApi;
9   -import cn.fw.member.sdk.vo.MemberInfoVo;
10   -import cn.fw.member.sdk.vo.MobileLocation;
11   -import lombok.extern.slf4j.Slf4j;
12   -import org.apache.commons.lang3.StringUtils;
13   -import org.springframework.beans.BeanUtils;
14   -import org.springframework.beans.factory.annotation.Autowired;
15   -import org.springframework.cache.annotation.Cacheable;
16   -import org.springframework.stereotype.Service;
17   -
18   -import java.util.Objects;
  3 +import cn.fw.common.util.MobileUtil
  4 +import cn.fw.hestia.common.constant.Constant
  5 +import cn.fw.hestia.rpc.member.dto.MemberUserDTO
  6 +import cn.fw.member.sdk.api.FunctionApi
  7 +import cn.fw.member.sdk.api.MemberApi
  8 +import kotlinx.coroutines.Dispatchers
  9 +import kotlinx.coroutines.async
  10 +import kotlinx.coroutines.runBlocking
  11 +import kotlinx.coroutines.selects.select
  12 +import kotlinx.coroutines.supervisorScope
  13 +import org.slf4j.LoggerFactory
  14 +import org.springframework.cache.annotation.Cacheable
  15 +import org.springframework.stereotype.Service
19 16  
20 17 /**
21 18 * 会员服务
22 19 *
23 20 * @author kurisu
24 21 */
25   -@Slf4j
  22 +
26 23 @Service
27   -public class MemberRpcService {
  24 +class MemberRpcService
  25 + (
28 26 /**
29 27 * 会员服务
30 28 */
31   - private final MemberApi memberApi;
32   - private final FunctionApi functionApi;
33   -
34   - /**
35   - * 默认构造器
36   - */
37   - @Autowired
38   - public MemberRpcService(final MemberApi memberApi,
39   - final FunctionApi functionApi) {
40   - this.memberApi = memberApi;
41   - this.functionApi = functionApi;
42   - }
  29 + private val memberApi: MemberApi,
  30 + private val functionApi: FunctionApi
  31 +) {
  32 + val logger = LoggerFactory.getLogger(this.javaClass)
43 33  
44 34 /**
45 35 * 根据会员ID获取会员信息
... ... @@ -47,27 +37,28 @@ public class MemberRpcService {
47 37 * @param userId 会员ID
48 38 * @return 会员信息
49 39 */
50   - @Cacheable(cacheNames = Constant.MEMBER_INFO_CACHE, key = "#userId", unless = "#result == null ")
51   - public MemberUserDTO user(final Long userId) {
52   - if (userId == null) {
53   - return null;
54   - }
  40 + @Cacheable(cacheNames = [Constant.MEMBER_INFO_CACHE], key = "#userId", unless = "#result == null ")
  41 + fun user(userId: Long?): MemberUserDTO? {
55 42 try {
56   - final Message<MemberInfoVo> msg = memberApi.getMemberById(userId);
57   - if (!msg.isSuccess()) {
58   - log.warn("调用Member[根据会员ID[{}]获取会员信息]系统失败, 原因:{}", userId, msg.getResult());
59   - return null;
  43 + val msg = memberApi.getMemberById(userId)
  44 + if (!msg.isSuccess) {
  45 + logger.warn("调用Member[根据会员ID[{}]获取会员信息]系统失败, 原因:{}", userId, msg.result)
  46 + return null
60 47 }
61   - final MemberInfoVo user = msg.getData();
62   - if (user != null) {
63   - final MemberUserDTO userDTO = new MemberUserDTO();
64   - BeanUtils.copyProperties(user, userDTO);
65   - return userDTO;
  48 + return msg.data?.run {
  49 + MemberUserDTO(
  50 + id,
  51 + latestAccountId,
  52 + realName,
  53 + credentType,
  54 + credentNo,
  55 + birthday = birthDate
  56 + )
66 57 }
67   - } catch (Exception e) {
68   - log.error("调用Member[根据会员ID[{}]获取会员信息]系统失败", userId, e);
  58 + } catch (e: Exception) {
  59 + logger.error("调用Member[根据会员ID[{}]获取会员信息]系统失败", userId, e)
  60 + return null
69 61 }
70   - return null;
71 62 }
72 63  
73 64  
... ... @@ -77,37 +68,55 @@ public class MemberRpcService {
77 68 * @param mobile
78 69 * @return
79 70 */
80   - @Cacheable(cacheNames = "mobile:attribution", key = "#mobile", unless = "#result.isEmpty()")
81   - public String attribution(final String mobile) {
82   - if (StringUtils.isBlank(mobile)) {
83   - return "";
  71 + @Cacheable(cacheNames = ["mobile:attribution"], key = "#mobile", unless = "#result.isEmpty()")
  72 + fun attribution(mobile: String?): String {
  73 + if (mobile.isNullOrEmpty()) {
  74 + return ""
84 75 }
85   - try {
86   - MobileUtil.Result result = MobileUtil.attribution(mobile);
87   - String att = "";
88   - if (Objects.nonNull(result)) {
89   - att = result.getProvince().concat(" ").concat(result.getCity());
90   - }
91   -
92   - if (!StringUtils.isBlank(att.trim())) {
93   - return att.trim();
94   - }
95   -
96   - Message<MobileLocation> msg = functionApi.queryMobileLocation(mobile);
97   - if (!msg.isSuccess()) {
98   - return "";
99   - }
100   - MobileLocation data = msg.getData();
101   - if (data != null) {
102   - String province = data.getProvince();
103   - String city = data.getCity();
104   - att = province.concat(" ").concat(city);
105   - }
106   - return att.trim();
107   - } catch (Exception e) {
108   - log.warn("查询手机号归属地失败:", e);
  76 + return runBlocking(Dispatchers.IO) {
  77 + supervisorScope {
  78 + val j1 = async {
  79 + try {
  80 + val result = MobileUtil.attribution(mobile)
  81 + result?.run {
  82 + "$province $city"
  83 + }
  84 + } catch (ex: Exception) {
  85 + logger.error("查询号码归属地失败:", ex)
  86 + ""
  87 + }
  88 + }
  89 + val j2 = async {
  90 + try {
  91 + val msg = functionApi.queryMobileLocation(mobile)
  92 + if (msg.isSuccess) {
  93 + logger.info("调用会员系统查询手机号归属地: {}", msg.data)
  94 + msg.data?.run {
  95 + "$province $city"
  96 + }
  97 + } else ""
  98 + } catch (ex: Exception) {
  99 + logger.error("调用会员系统查询手机号归属地失败:", ex)
  100 + ""
  101 + }
  102 + }
  103 + select {
  104 + j1.onAwait {
  105 + println(it)
  106 + if (!it.isNullOrEmpty()) {
  107 + j2.cancel()
  108 + }
  109 + it
  110 + }
  111 + j2.onAwait {
  112 + println(it)
  113 + if (!it.isNullOrEmpty()) {
  114 + j1.cancel()
  115 + }
  116 + it
  117 + }
  118 + }
  119 + } ?: ""
109 120 }
110   - return "";
111 121 }
112   -
113   -}
  122 +}
114 123 \ No newline at end of file
... ...
fw-hestia-rpc/src/main/java/cn/fw/hestia/rpc/member/dto/MemberUserDTO.kt
1   -package cn.fw.hestia.rpc.member.dto;
  1 +package cn.fw.hestia.rpc.member.dto
2 2  
3   -import lombok.Data;
4   -
5   -import java.io.Serializable;
6   -import java.util.Date;
  3 +import java.util.*
7 4  
8 5 /**
9 6 * 会员信息
10   - * <p>
  7 + *
  8 + *
11 9 * create at 2019-05-09
12 10 *
13 11 * @author kurisu
14 12 */
15   -@Data
16   -public class MemberUserDTO implements Serializable {
  13 +data class MemberUserDTO(
17 14 /**
18 15 * 用户Id
19 16 */
20   - private Long id;
  17 + val id: Long,
  18 +
21 19 /**
22 20 * 最后一个账号id
23 21 */
24   - private Long latestAccountId;
  22 + val latestAccountId: Long? = null,
  23 +
25 24 /**
26 25 * 真实姓名
27 26 */
28   - private String realName;
  27 + val realName: String? = null,
29 28  
30 29 /**
31 30 * 证件类型
32 31 */
33   - private Integer credentType;
  32 + val credentType: Int? = null,
34 33  
35 34 /**
36 35 * 证件号码
37 36 */
38   - private String credentNo;
  37 + val credentNo: String? = null,
39 38  
40 39 /**
41 40 * 生日
42 41 */
43   - private Date birthday;
44   -
45   - public Long getUserId() {
46   - return id;
47   - }
48   -
49   - public Long getMemberId() {
50   - return id;
51   - }
52   -}
  42 + val birthday: Date? = null
  43 +) {
  44 + val userId: Long
  45 + get() = id
  46 + val memberId: Long
  47 + get() = id
  48 +
  49 +}
53 50 \ No newline at end of file
... ...
fw-hestia-rpc/src/main/java/cn/fw/hestia/rpc/member/dto/MemberUserLevelDTO.java deleted
1   -package cn.fw.hestia.rpc.member.dto;
2   -
3   -import lombok.Data;
4   -
5   -/**
6   - * 客户level
7   - * <p>
8   - * create at 2019-05-16
9   - *
10   - * @author kurisu
11   - */
12   -@Data
13   -public class MemberUserLevelDTO {
14   - private Long cusId;
15   - private Integer levelType;
16   - private Integer growth;
17   - private String cusName;
18   - private String phone;
19   -}
fw-hestia-server/src/main/java/cn/fw/hestia/server/controller/common/CommonController.kt
... ... @@ -3,14 +3,12 @@ package cn.fw.hestia.server.controller.common
3 3 import cn.fw.common.web.annotation.ControllerMethod
4 4 import cn.fw.common.web.util.ResultBuilder.success
5 5 import cn.fw.data.base.domain.common.Message
  6 +import cn.fw.hestia.rpc.member.MemberRpcService
6 7 import cn.fw.hestia.service.buz.MessageCenterBizService
7 8 import cn.fw.security.auth.client.annotation.Authorization
8 9 import cn.fw.security.auth.client.enums.AuthType
9 10 import org.springframework.validation.annotation.Validated
10   -import org.springframework.web.bind.annotation.GetMapping
11   -import org.springframework.web.bind.annotation.PostMapping
12   -import org.springframework.web.bind.annotation.RequestMapping
13   -import org.springframework.web.bind.annotation.RestController
  11 +import org.springframework.web.bind.annotation.*
14 12 import javax.validation.constraints.NotNull
15 13  
16 14 /**
... ... @@ -23,7 +21,10 @@ import javax.validation.constraints.NotNull
23 21 @RestController
24 22 @Authorization(AuthType.NONE)
25 23 @RequestMapping("/common/message")
26   -class CommonController(private val messageCenterBizService: MessageCenterBizService) {
  24 +class CommonController(
  25 + private val messageCenterBizService: MessageCenterBizService,
  26 + private val memberRpcService: MemberRpcService
  27 +) {
27 28 @PostMapping("/send")
28 29 @ControllerMethod("手动发送消息")
29 30 fun manualSend(sceneToken: @NotNull(message = "sceneToken不能为空") Long?): Message<String> {
... ... @@ -34,4 +35,9 @@ class CommonController(private val messageCenterBizService: MessageCenterBizServ
34 35 @get:GetMapping("/query/code")
35 36 val code: Message<String>
36 37 get() = success(messageCenterBizService.rdsTemplateCode)
  38 +
  39 + @ControllerMethod("查询手机号归属地")
  40 + @GetMapping("/query/mobile")
  41 + fun mobileAttr(@RequestParam(value = "mobile") mobile: String): Message<String> =
  42 + success(memberRpcService.attribution(mobile))
37 43 }
38 44 \ No newline at end of file
... ...
fw-hestia-server/src/main/resources/logfile.xml
... ... @@ -55,7 +55,9 @@
55 55  
56 56 <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
57 57 <encoder>
58   - <pattern>%highlight([%-1p]) %highlight(%d{yyyy-MM-dd HH:mm:ss} [%X{X-APP}/%X{X-IP}/%X{X-TRACE-ID}]) %green([%thread]) %boldMagenta(%logger) %cyan(%msg%n)</pattern>
  58 + <pattern>%highlight([%-1p]) %highlight(%d{yyyy-MM-dd HH:mm:ss.SSS} [%X{X-APP}/%X{X-IP}/%X{X-TRACE-ID}])
  59 + %green([%thread]) %boldMagenta(%logger) %cyan(%msg%n)
  60 + </pattern>
59 61 <charset>${charSet}</charset>
60 62 </encoder>
61 63 </appender>
... ...
fw-hestia-service/pom.xml
... ... @@ -94,10 +94,6 @@
94 94 <groupId>org.redisson</groupId>
95 95 <artifactId>redisson</artifactId>
96 96 </dependency>
97   - <dependency>
98   - <groupId>org.jetbrains.kotlinx</groupId>
99   - <artifactId>kotlinx-coroutines-core</artifactId>
100   - </dependency>
101 97 </dependencies>
102 98  
103 99 <build>
... ...