正在显示
14 个修改的文件
包含
411 行增加
和
45 行删除
Dockerfile
0 → 100644
1 | +FROM java:8u111 | ||
2 | + | ||
3 | +VOLUME /tmp | ||
4 | + | ||
5 | +ADD target/*.jar app.jar | ||
6 | + | ||
7 | +EXPOSE 11113 | ||
8 | +# Ubuntu 时区 同步主机与docker容器时间 | ||
9 | +RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime | ||
10 | +RUN echo "Asia/Shanghai" > /etc/timezone && dpkg-reconfigure -f noninteractive tzdata | ||
11 | + | ||
12 | +ENTRYPOINT ["java","-Xms128m", "-Xmx256m","-jar","/app.jar"] | ||
13 | + | ||
14 | + |
@@ -18,7 +18,7 @@ spring: | @@ -18,7 +18,7 @@ spring: | ||
18 | static-locations: classpath:/META-INF/resources/,classpath:/static,classpath:/resources/,file:${web.upload-path} | 18 | static-locations: classpath:/META-INF/resources/,classpath:/static,classpath:/resources/,file:${web.upload-path} |
19 | 19 | ||
20 | application: | 20 | application: |
21 | - name: message-bus-trans-message | 21 | + name: wlpt2-cdhz-parse |
22 | redis: | 22 | redis: |
23 | # host: 127.0.0.1 | 23 | # host: 127.0.0.1 |
24 | host: 8.131.245.248 | 24 | host: 8.131.245.248 |
@@ -138,7 +138,8 @@ logging: | @@ -138,7 +138,8 @@ logging: | ||
138 | name: system.log | 138 | name: system.log |
139 | config: config/logback-dev.xml | 139 | config: config/logback-dev.xml |
140 | #转移到logback配置文件中 | 140 | #转移到logback配置文件中 |
141 | - #level: | 141 | + level: |
142 | + com.tianbo.messagebus.myinterface: DEBUG | ||
142 | #org.apache.tomcat: info | 143 | #org.apache.tomcat: info |
143 | #com.tianbo.warehouse.dao: DEBUG | 144 | #com.tianbo.warehouse.dao: DEBUG |
144 | #org.springframework.security: trace | 145 | #org.springframework.security: trace |
@@ -165,8 +166,9 @@ message-bus: | @@ -165,8 +166,9 @@ message-bus: | ||
165 | #报文接收地址 | 166 | #报文接收地址 |
166 | get-url: https://www.zzecargo.com/api/kafka-server-consumer/kafka/receive | 167 | get-url: https://www.zzecargo.com/api/kafka-server-consumer/kafka/receive |
167 | auth: | 168 | auth: |
168 | - username: NMMS | ||
169 | - password: vmvnv1v2 | 169 | + username: yangyucheng |
170 | + password: ZZdsly123! | ||
171 | + consumer: NMMS | ||
170 | #心跳间隔时间默认10秒,单位毫秒 | 172 | #心跳间隔时间默认10秒,单位毫秒 |
171 | heartbit-interval: 10000 | 173 | heartbit-interval: 10000 |
172 | info: | 174 | info: |
@@ -199,6 +199,7 @@ | @@ -199,6 +199,7 @@ | ||
199 | <springProfile name="dev"> | 199 | <springProfile name="dev"> |
200 | <logger name="com.tianbo.messagebus.service.MessageBusProcessor" level="ALL" /> | 200 | <logger name="com.tianbo.messagebus.service.MessageBusProcessor" level="ALL" /> |
201 | <logger name="com.tianbo.messagebus.dao" level="DEBUG" /> | 201 | <logger name="com.tianbo.messagebus.dao" level="DEBUG" /> |
202 | + <logger name="com.tianbo.messagebus.myinterface" level="DEBUG" additivity="false" /> | ||
202 | <root level="INFO"> | 203 | <root level="INFO"> |
203 | <appender-ref ref="CONSOLE" /> | 204 | <appender-ref ref="CONSOLE" /> |
204 | <appender-ref ref="DEBUG_FILE" /> | 205 | <appender-ref ref="DEBUG_FILE" /> |
1 | +package com.tianbo.messagebus.bean; | ||
2 | + | ||
3 | +import com.tianbo.messagebus.model.Auth; | ||
4 | +import feign.RequestInterceptor; | ||
5 | +import feign.RequestTemplate; | ||
6 | +import lombok.extern.slf4j.Slf4j; | ||
7 | +import org.apache.commons.lang.StringUtils; | ||
8 | +import org.apache.http.HttpHeaders; | ||
9 | +import org.springframework.context.annotation.Bean; | ||
10 | +import org.springframework.context.annotation.Configuration; | ||
11 | +import org.springframework.web.context.request.RequestContextHolder; | ||
12 | +import org.springframework.web.context.request.ServletRequestAttributes; | ||
13 | + | ||
14 | +import javax.servlet.http.HttpServletRequest; | ||
15 | + | ||
16 | +@Configuration | ||
17 | +@Slf4j | ||
18 | +public class DslyFeignClientConfiguration { | ||
19 | + | ||
20 | + @Bean | ||
21 | + public RequestInterceptor requestInterceptor() { | ||
22 | + return new RequestInterceptor() { | ||
23 | + @Override | ||
24 | + public void apply(RequestTemplate template) { | ||
25 | + | ||
26 | + // 替换 "yourBearerTokenValue" 为实际的 Bearer token 值 | ||
27 | + String token = "Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI2MCIsIm5hbWUiOiLlubPlj7DnrqHnkIblkZgiLCJpZCI6NjAsImlhdCI6MTcwMjUyNzI1NywiYWNjb3VudCI6Inlhbmd5dWNoZW5nIn0.MJGCuVMSkjKHUrt35XDM1srDVSXLzZsMVXTGACeAiMw"; | ||
28 | + | ||
29 | + | ||
30 | + template.header("Authorization", token); | ||
31 | + log.info("已设置Authorization的值为:{}",token); | ||
32 | + log.info("request:{}",template.toString()); | ||
33 | + template.headers().forEach((name, values) -> { | ||
34 | + System.out.println(name + ": " + values); | ||
35 | + }); | ||
36 | + } | ||
37 | + }; | ||
38 | + } | ||
39 | +} |
1 | +package com.tianbo.messagebus.bean; | ||
2 | + | ||
3 | +import org.springframework.context.annotation.Bean; | ||
4 | +import org.springframework.context.annotation.Configuration; | ||
5 | +import org.springframework.core.task.TaskExecutor; | ||
6 | +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||
7 | + | ||
8 | +import java.util.concurrent.ThreadPoolExecutor; | ||
9 | + | ||
10 | +@Configuration | ||
11 | +public class ParseThreadConfig { | ||
12 | + | ||
13 | + @Bean("PARSE-POOL") | ||
14 | + public TaskExecutor executor(){ | ||
15 | + ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); | ||
16 | + //配置核心线程数 | ||
17 | + executor.setCorePoolSize(24); | ||
18 | + //配置最大线程数 | ||
19 | + executor.setMaxPoolSize(128); | ||
20 | + //配置队列大小 | ||
21 | + executor.setQueueCapacity(2000); | ||
22 | + //线程的名称前缀 | ||
23 | + executor.setThreadNamePrefix("PARSE-"); | ||
24 | + //线程活跃时间(秒) | ||
25 | + executor.setKeepAliveSeconds(60); | ||
26 | + //等待所有任务结束后再关闭线程池 | ||
27 | + executor.setWaitForTasksToCompleteOnShutdown(false); | ||
28 | + // 设置拒绝策略 | ||
29 | + executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); | ||
30 | + //执行初始化 | ||
31 | + executor.initialize(); | ||
32 | + return executor; | ||
33 | + } | ||
34 | +} | ||
35 | + | ||
36 | + | ||
37 | + |
@@ -11,6 +11,11 @@ public class ResultJson<T> implements Serializable{ | @@ -11,6 +11,11 @@ public class ResultJson<T> implements Serializable{ | ||
11 | // 描述 | 11 | // 描述 |
12 | private String msg = ""; | 12 | private String msg = ""; |
13 | 13 | ||
14 | + // 描述 | ||
15 | + private String message = ""; | ||
16 | + | ||
17 | + private Boolean success; | ||
18 | + | ||
14 | private String error; | 19 | private String error; |
15 | // 返回对象 | 20 | // 返回对象 |
16 | private T data; | 21 | private T data; |
@@ -86,11 +91,32 @@ public class ResultJson<T> implements Serializable{ | @@ -86,11 +91,32 @@ public class ResultJson<T> implements Serializable{ | ||
86 | this.jwtToken = jwtToken; | 91 | this.jwtToken = jwtToken; |
87 | } | 92 | } |
88 | 93 | ||
94 | + public String getMessage() { | ||
95 | + return message; | ||
96 | + } | ||
97 | + | ||
98 | + public void setMessage(String message) { | ||
99 | + this.message = message; | ||
100 | + } | ||
101 | + | ||
102 | + public Boolean getSuccess() { | ||
103 | + return success; | ||
104 | + } | ||
105 | + | ||
106 | + public void setSuccess(Boolean success) { | ||
107 | + this.success = success; | ||
108 | + } | ||
109 | + | ||
89 | @Override | 110 | @Override |
90 | public String toString() { | 111 | public String toString() { |
91 | return "ResultJson{" + | 112 | return "ResultJson{" + |
92 | "code='" + code + '\'' + | 113 | "code='" + code + '\'' + |
93 | ", msg='" + msg + '\'' + | 114 | ", msg='" + msg + '\'' + |
115 | + ", message='" + message + '\'' + | ||
116 | + ", success=" + success + | ||
117 | + ", error='" + error + '\'' + | ||
118 | + ", data=" + data + | ||
119 | + ", jwtToken='" + jwtToken + '\'' + | ||
94 | '}'; | 120 | '}'; |
95 | } | 121 | } |
96 | } | 122 | } |
1 | +package com.tianbo.messagebus.model; | ||
2 | + | ||
3 | +public class Auth { | ||
4 | + | ||
5 | + /** | ||
6 | + * 存储登录后的token | ||
7 | + */ | ||
8 | + public static String TOKEN = ""; | ||
9 | + /** | ||
10 | + * 登录成功状态 | ||
11 | + */ | ||
12 | + public static Boolean LOGIN_STATUS=false; | ||
13 | + | ||
14 | + /** | ||
15 | + * 失败重发请求次数 | ||
16 | + */ | ||
17 | + public static final int RETRY_TIMES= 10; | ||
18 | +} |
1 | package com.tianbo.messagebus.myinterface; | 1 | package com.tianbo.messagebus.myinterface; |
2 | 2 | ||
3 | +import com.tianbo.messagebus.bean.DslyFeignClientConfiguration; | ||
3 | import com.tianbo.messagebus.controller.response.ResultJson; | 4 | import com.tianbo.messagebus.controller.response.ResultJson; |
4 | import org.springframework.cloud.openfeign.FeignClient; | 5 | import org.springframework.cloud.openfeign.FeignClient; |
5 | import org.springframework.web.bind.annotation.*; | 6 | import org.springframework.web.bind.annotation.*; |
6 | 7 | ||
7 | import java.util.List; | 8 | import java.util.List; |
8 | 9 | ||
9 | -@FeignClient(name = "kafka-server-consumer", | ||
10 | - fallbackFactory = KafkaReciveFallbackFactory.class ) | 10 | +@FeignClient( |
11 | + name = "kafka-server-consumer", | ||
12 | + url = "https://www.zzecargo.com", | ||
13 | + fallbackFactory = KafkaReciveFallbackFactory.class, | ||
14 | + configuration = {DslyFeignClientConfiguration.class}) | ||
11 | public interface KafkaReciveApi { | 15 | public interface KafkaReciveApi { |
12 | 16 | ||
13 | @ResponseBody | 17 | @ResponseBody |
14 | @RequestMapping(value = "/kafka/receive",method = RequestMethod.GET) | 18 | @RequestMapping(value = "/kafka/receive",method = RequestMethod.GET) |
15 | ResultJson<List<String>> recive(@RequestParam(value = "username",required = true) String username); | 19 | ResultJson<List<String>> recive(@RequestParam(value = "username",required = true) String username); |
20 | + | ||
21 | + @ResponseBody | ||
22 | + @RequestMapping(value = "/api/kafka-server-consumer/kafka/receive",method = RequestMethod.POST) | ||
23 | + ResultJson<List<String>> getMessages(@RequestParam(value = "username",required = true) String username); | ||
16 | } | 24 | } |
1 | -package com.tianbo.messagebus.myinterface; | ||
2 | - | ||
3 | - | ||
4 | -import com.tianbo.messagebus.controller.response.ResultJson; | ||
5 | -import lombok.extern.slf4j.Slf4j; | ||
6 | -import org.springframework.stereotype.Component; | ||
7 | -import org.springframework.stereotype.Service; | ||
8 | - | ||
9 | -import java.util.ArrayList; | ||
10 | -import java.util.List; | ||
11 | - | ||
12 | -@Slf4j | ||
13 | -//@Service("myKafkaRecive") | ||
14 | -public class KafkaReciveFallback implements KafkaReciveApi { | ||
15 | - | ||
16 | - @Override | ||
17 | - public ResultJson<List<String>> recive(String username) { | ||
18 | - log.info("[FEGIN-ERR]获取消息失败"); | ||
19 | - return new ResultJson<>("400","获取消息失败",new ArrayList<>()); | ||
20 | - } | ||
21 | -} |
@@ -21,6 +21,12 @@ public class KafkaReciveFallbackFactory implements FallbackFactory<KafkaReciveAp | @@ -21,6 +21,12 @@ public class KafkaReciveFallbackFactory implements FallbackFactory<KafkaReciveAp | ||
21 | log.info("[FEGIN-ERR]获取消息失败"); | 21 | log.info("[FEGIN-ERR]获取消息失败"); |
22 | return new ResultJson<>("400","获取消息失败",new ArrayList<>()); | 22 | return new ResultJson<>("400","获取消息失败",new ArrayList<>()); |
23 | } | 23 | } |
24 | + | ||
25 | + @Override | ||
26 | + public ResultJson<List<String>> getMessages(String username) { | ||
27 | + log.info("[FEGIN-ERR]获取消息失败"); | ||
28 | + return new ResultJson<>("400","获取消息失败",new ArrayList<>()); | ||
29 | + } | ||
24 | }; | 30 | }; |
25 | } | 31 | } |
26 | } | 32 | } |
@@ -4,6 +4,7 @@ package com.tianbo.messagebus.service; | @@ -4,6 +4,7 @@ package com.tianbo.messagebus.service; | ||
4 | import com.alibaba.fastjson.JSON; | 4 | import com.alibaba.fastjson.JSON; |
5 | import com.alibaba.fastjson.JSONObject; | 5 | import com.alibaba.fastjson.JSONObject; |
6 | import com.tianbo.messagebus.controller.response.ResultJson; | 6 | import com.tianbo.messagebus.controller.response.ResultJson; |
7 | +import com.tianbo.messagebus.model.Auth; | ||
7 | import com.tianbo.messagebus.model.CUSTOM_RESPONSE; | 8 | import com.tianbo.messagebus.model.CUSTOM_RESPONSE; |
8 | import com.tianbo.messagebus.model.CustomReception; | 9 | import com.tianbo.messagebus.model.CustomReception; |
9 | import com.tianbo.messagebus.model.HEADER; | 10 | import com.tianbo.messagebus.model.HEADER; |
@@ -27,8 +28,10 @@ public class Custom_Response_Processor { | @@ -27,8 +28,10 @@ public class Custom_Response_Processor { | ||
27 | /** | 28 | /** |
28 | * 账号名 | 29 | * 账号名 |
29 | */ | 30 | */ |
30 | - @Value("${message-bus.auth.username}") | ||
31 | - private String USER_NAME; | 31 | + @Value("${message-bus.auth.consumer}") |
32 | + private String CONSUMER_NAME; | ||
33 | + | ||
34 | + | ||
32 | 35 | ||
33 | @Autowired | 36 | @Autowired |
34 | KafkaReciveApi kafkaReciveApi; | 37 | KafkaReciveApi kafkaReciveApi; |
@@ -41,13 +44,13 @@ public class Custom_Response_Processor { | @@ -41,13 +44,13 @@ public class Custom_Response_Processor { | ||
41 | * feigin从服务直接获取消息 | 44 | * feigin从服务直接获取消息 |
42 | */ | 45 | */ |
43 | @Async | 46 | @Async |
44 | - @Scheduled(fixedRate = 5000) | 47 | + @Scheduled(fixedRate = 15000) |
45 | public void getDataFromFeigin(){ | 48 | public void getDataFromFeigin(){ |
46 | try{ | 49 | try{ |
47 | //初始化数据库 | 50 | //初始化数据库 |
48 | CUSTOM_RESPONSE test = custom_response_service.selectByPrimaryKey("111"); | 51 | CUSTOM_RESPONSE test = custom_response_service.selectByPrimaryKey("111"); |
49 | - log.info("1-开始执行获取任务,获取账号为:{}",USER_NAME); | ||
50 | - if(!StringUtils.isEmpty(USER_NAME)){ | 52 | + log.info("1-开始执行获取任务,获取消息账号为:{}",CONSUMER_NAME); |
53 | + if(!StringUtils.isEmpty(CONSUMER_NAME)){ | ||
51 | analysis(); | 54 | analysis(); |
52 | } | 55 | } |
53 | }catch (Exception e){ | 56 | }catch (Exception e){ |
@@ -62,18 +65,22 @@ public class Custom_Response_Processor { | @@ -62,18 +65,22 @@ public class Custom_Response_Processor { | ||
62 | * 通过总线消费服务获取回执数据 | 65 | * 通过总线消费服务获取回执数据 |
63 | */ | 66 | */ |
64 | public void analysis(){ | 67 | public void analysis(){ |
65 | - try { | ||
66 | - ResultJson<List<String>> listResultJson = kafkaReciveApi.recive(USER_NAME); | ||
67 | - log.info("2-获取结果为:{}",listResultJson.toString()); | ||
68 | - if ("200".equals(listResultJson.getCode()) && listResultJson.getData()!=null && !listResultJson.getData().isEmpty()){ | ||
69 | - log.info("2-获取数量为:{}",listResultJson.getData().size()); | ||
70 | - responseResolve(listResultJson); | ||
71 | - }else { | ||
72 | - log.info("[CONSUMER-RESULT] - 未获取到消息,code:{},msg:{},data:{}",listResultJson.getCode(),listResultJson.getMsg(),listResultJson.getData()); | 68 | + if (!StringUtils.isEmpty(Auth.TOKEN)){ |
69 | + try { | ||
70 | + ResultJson<List<String>> listResultJson = kafkaReciveApi.getMessages(CONSUMER_NAME); | ||
71 | + log.info("2-获取结果为:{}",listResultJson.toString()); | ||
72 | + if ("200".equals(listResultJson.getCode()) && listResultJson.getData()!=null && !listResultJson.getData().isEmpty()){ | ||
73 | + log.info("2-获取数量为:{}",listResultJson.getData().size()); | ||
74 | + responseResolve(listResultJson); | ||
75 | + }else { | ||
76 | + log.info("[CONSUMER-RESULT] - 未获取到消息,code:{},msg:{},data:{}",listResultJson.getCode(),listResultJson.getMsg(),listResultJson.getData()); | ||
77 | + } | ||
78 | + } catch (Exception e) { | ||
79 | + log.error("[CONSUMER-ERR]!!!获取消息异常,开始获取下一条消息!!!->{}",e.toString()); | ||
80 | + e.printStackTrace(); | ||
73 | } | 81 | } |
74 | - } catch (Exception e) { | ||
75 | - log.error("[CONSUMER-ERR]!!!获取消息异常,开始获取下一条消息!!!->{}",e.toString()); | ||
76 | - e.printStackTrace(); | 82 | + }else{ |
83 | + log.error("[CONSUMER-ERR]:未登录,无法获取消息"); | ||
77 | } | 84 | } |
78 | } | 85 | } |
79 | 86 | ||
@@ -141,8 +148,10 @@ public class Custom_Response_Processor { | @@ -141,8 +148,10 @@ public class Custom_Response_Processor { | ||
141 | 148 | ||
142 | if("MT2201".equals(messageType) || "MT9999".equals(messageType) || "MT3201".equals(messageType)){ | 149 | if("MT2201".equals(messageType) || "MT9999".equals(messageType) || "MT3201".equals(messageType)){ |
143 | 150 | ||
144 | - analysisBody(messageType,manifest,messageID,sendTime,senderID,receiverID,version,functionCode); | ||
145 | } | 151 | } |
152 | + //所有回执类型都解析 | ||
153 | + analysisBody(messageType,manifest,messageID,sendTime,senderID,receiverID,version,functionCode); | ||
154 | + | ||
146 | }else { | 155 | }else { |
147 | log.info("[CDHZ]-@[四零一]@缺少Manifest或Head节点"); | 156 | log.info("[CDHZ]-@[四零一]@缺少Manifest或Head节点"); |
148 | } | 157 | } |
@@ -159,7 +168,7 @@ public class Custom_Response_Processor { | @@ -159,7 +168,7 @@ public class Custom_Response_Processor { | ||
159 | ){ | 168 | ){ |
160 | log.info("@[二]@开始解析:{}",messageType); | 169 | log.info("@[二]@开始解析:{}",messageType); |
161 | CUSTOM_RESPONSE custom_response_nmms2 = new CUSTOM_RESPONSE(); | 170 | CUSTOM_RESPONSE custom_response_nmms2 = new CUSTOM_RESPONSE(); |
162 | - messageType = "MT2201"; | 171 | +// messageType = "MT2201"; |
163 | // 航班信息 | 172 | // 航班信息 |
164 | JSONObject response = manifest.getJSONObject("Response"); | 173 | JSONObject response = manifest.getJSONObject("Response"); |
165 | if (response!=null){ | 174 | if (response!=null){ |
1 | +package com.tianbo.messagebus.service; | ||
2 | + | ||
3 | +/** | ||
4 | + * @author mrz | ||
5 | + */ | ||
6 | +public interface LoginService { | ||
7 | + | ||
8 | + /** | ||
9 | + * 发起登录,存储token | ||
10 | + * | ||
11 | + * @return | ||
12 | + */ | ||
13 | + void login(); | ||
14 | + | ||
15 | + /** | ||
16 | + * 定时心跳,维持在线状态,每10秒访问一次心跳接口 | ||
17 | + */ | ||
18 | + void heartBit(); | ||
19 | + | ||
20 | + /** | ||
21 | + * 开始心跳 | ||
22 | + */ | ||
23 | + void startHeartBit(); | ||
24 | +} |
1 | +package com.tianbo.messagebus.service.impl; | ||
2 | + | ||
3 | + | ||
4 | +import com.tianbo.messagebus.service.LoginService; | ||
5 | +import lombok.extern.slf4j.Slf4j; | ||
6 | +import org.springframework.beans.factory.annotation.Autowired; | ||
7 | +import org.springframework.boot.CommandLineRunner; | ||
8 | +import org.springframework.stereotype.Component; | ||
9 | + | ||
10 | +@Component | ||
11 | +@Slf4j | ||
12 | +public class LoginCommandRunner implements CommandLineRunner { | ||
13 | + | ||
14 | + @Autowired | ||
15 | + LoginService loginService; | ||
16 | + | ||
17 | + @Override | ||
18 | + public void run(String... args) throws Exception { | ||
19 | + log.info("start loggin"); | ||
20 | + loginService.login(); | ||
21 | + | ||
22 | +// log.info("[HEART-BIT]-start heartBit"); | ||
23 | +// loginService.startHeartBit(); | ||
24 | + } | ||
25 | +} |
1 | +package com.tianbo.messagebus.service.impl; | ||
2 | + | ||
3 | +import com.alibaba.fastjson.JSON; | ||
4 | +import com.alibaba.fastjson.JSONObject; | ||
5 | +import com.tianbo.messagebus.model.Auth; | ||
6 | +import com.tianbo.messagebus.service.LoginService; | ||
7 | +import lombok.extern.slf4j.Slf4j; | ||
8 | +import org.springframework.beans.factory.annotation.Value; | ||
9 | +import org.springframework.http.*; | ||
10 | +import org.springframework.scheduling.annotation.Scheduled; | ||
11 | +import org.springframework.stereotype.Service; | ||
12 | +import org.springframework.util.LinkedMultiValueMap; | ||
13 | +import org.springframework.util.MultiValueMap; | ||
14 | +import org.springframework.util.StringUtils; | ||
15 | +import org.springframework.web.client.RestTemplate; | ||
16 | + | ||
17 | +import javax.annotation.Resource; | ||
18 | + | ||
19 | +@Service | ||
20 | +@Slf4j | ||
21 | +public class LoginServiceImpl implements LoginService { | ||
22 | + | ||
23 | + /** 账户登录地址 | ||
24 | + */ | ||
25 | + @Value("${message-bus.url.login-url}") | ||
26 | + private String LOGIN_URL; | ||
27 | + /** | ||
28 | + * 账号名 | ||
29 | + */ | ||
30 | + @Value("${message-bus.auth.username}") | ||
31 | + private String USER_NAME; | ||
32 | +// private static final String USER_NAME = "HYYW"; | ||
33 | + /** | ||
34 | + * 登陆密码 | ||
35 | + */ | ||
36 | + @Value("${message-bus.auth.password}") | ||
37 | + private String USER_PASS; | ||
38 | + | ||
39 | + /** | ||
40 | + * 心跳接口地址 | ||
41 | + */ | ||
42 | + @Value("${message-bus.url.hearbit-url}") | ||
43 | + private String HEARTBEAT_URL; | ||
44 | + /** | ||
45 | + * 心跳间隔时间 单位S | ||
46 | + */ | ||
47 | + @Value("${message-bus.heartbit-interval}") | ||
48 | + private int HEARTBIT_INTERVAL; | ||
49 | + | ||
50 | + /** | ||
51 | + * HTTP请求框架 | ||
52 | + */ | ||
53 | + @Resource | ||
54 | + private RestTemplate restTemplate; | ||
55 | + | ||
56 | + @Override | ||
57 | + public void login() { | ||
58 | + try { | ||
59 | + /* | ||
60 | + * 发起HTTP 登录请求 | ||
61 | + * 登录接口的请求头为application/x-www-form-urlencoded | ||
62 | + */ | ||
63 | + HttpHeaders headers = new HttpHeaders(); | ||
64 | + headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); | ||
65 | + | ||
66 | + /* | ||
67 | + * 请求参数 | ||
68 | + */ | ||
69 | + MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>(); | ||
70 | + params.add("username", USER_NAME); | ||
71 | + params.add("password", USER_PASS); | ||
72 | + log.info("[LOGIN]-登录用户:[{}],登录密码:[{}]",USER_NAME,USER_PASS); | ||
73 | + | ||
74 | + HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(params, headers); | ||
75 | + | ||
76 | + /* | ||
77 | + * 提交HTTP访问,获取返回信息 | ||
78 | + */ | ||
79 | + ResponseEntity<String> response = restTemplate.postForEntity(LOGIN_URL, request, String.class); | ||
80 | + // 输出结果 | ||
81 | + log.info(response.getBody()); | ||
82 | + | ||
83 | + | ||
84 | + /* | ||
85 | + 校验是否登录成功 | ||
86 | + */ | ||
87 | + if (response.getStatusCode().equals(HttpStatus.OK)) { | ||
88 | + /** | ||
89 | + * 从返回信息中确定是否登录成功,并取得token | ||
90 | + * 返回格式 | ||
91 | + * { | ||
92 | + "code":200, | ||
93 | + "data":{ | ||
94 | + "account":"yangyucheng", | ||
95 | + "token":"eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI2MCIsIm5hbWUiOiLmnajnjonmiJDmtYvor5UiLCJpZCI6NjAsImlhdCI6MTYxNzM0ODM3MiwiYWNjb3VudCI6Inlhbmd5dWNoZW5nIn0.ElAs7BtV1tu6ApQXuPXzgXUgvja76bjEb-zxqhUON48" | ||
96 | + }, | ||
97 | + "message":"success", | ||
98 | + "success":true, | ||
99 | + "time":20210402152612604 | ||
100 | + } | ||
101 | + */ | ||
102 | + JSONObject resJson = JSON.parseObject(response.getBody()); | ||
103 | + JSONObject resData = resJson.getJSONObject("data"); | ||
104 | + String resCode = resJson.getString("code"); | ||
105 | + | ||
106 | + | ||
107 | + | ||
108 | + /* | ||
109 | + 校验并获取登陆成功后返回的token | ||
110 | + */ | ||
111 | + String authToken = resData.getString("token"); | ||
112 | + if ("200".equals(resCode) && StringUtils.hasLength(authToken) && authToken.length() > 10) { | ||
113 | + Auth.LOGIN_STATUS = true; | ||
114 | + | ||
115 | + //设置请求头部Authorization为token, token的类型为Bearer | ||
116 | + Auth.TOKEN = authToken; | ||
117 | + | ||
118 | + log.info("登录成功:token=[{}]",Auth.TOKEN); | ||
119 | + }else { | ||
120 | + log.error("登录失败"); | ||
121 | + } | ||
122 | + } else { | ||
123 | + log.error("登录失败"); | ||
124 | + } | ||
125 | + | ||
126 | + } catch (Exception e) { | ||
127 | + log.error("登录失败->{}",e.toString()); | ||
128 | + } | ||
129 | + } | ||
130 | + | ||
131 | + @Override | ||
132 | + public void heartBit() { | ||
133 | + if (StringUtils.hasLength(Auth.TOKEN) && Auth.LOGIN_STATUS){ | ||
134 | + /* | ||
135 | + * 发起HTTP 登录请求 | ||
136 | + * 登录接口的请求头为application/x-www-form-urlencoded | ||
137 | + */ | ||
138 | + HttpHeaders headers = new HttpHeaders(); | ||
139 | + headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); | ||
140 | + /* | ||
141 | + * 设置获取到的token到头部信息Authorization节点中 | ||
142 | + */ | ||
143 | + headers.setBearerAuth(Auth.TOKEN); | ||
144 | + | ||
145 | + /* | ||
146 | + * 心跳接口无参数,访问接口即可 | ||
147 | + */ | ||
148 | + MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>(); | ||
149 | + | ||
150 | + HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(params, headers); | ||
151 | + | ||
152 | + /* | ||
153 | + * 提交HTTP访问,获取返回信息 | ||
154 | + */ | ||
155 | + ResponseEntity<String> response = restTemplate.postForEntity(HEARTBEAT_URL, request, String.class); | ||
156 | + | ||
157 | + // 输出结果 | ||
158 | + log.debug(response.getBody()); | ||
159 | + | ||
160 | + if (response.getStatusCode().equals(HttpStatus.OK)) { | ||
161 | + log.debug("[HEART-BIT]-心跳成功"); | ||
162 | + } else { | ||
163 | + log.error("[HEART-BIT-ERR]-心跳失败"); | ||
164 | + } | ||
165 | + }else { | ||
166 | + log.error("[HEART-BIT-ERR]-未登录,心跳失败"); | ||
167 | + } | ||
168 | + } | ||
169 | + | ||
170 | + | ||
171 | + @Scheduled(fixedRate = 9000) | ||
172 | + @Override | ||
173 | +// @Async("HEART-POOL") | ||
174 | + public void startHeartBit() { | ||
175 | + log.info("[LOGINED]-开始心跳"); | ||
176 | + heartBit(); | ||
177 | + } | ||
178 | +} |
-
请 注册 或 登录 后发表评论