|
@@ -1,14 +1,14 @@
|
1
|
1
|
package com.gct.edge.actuator.config;
|
2
|
2
|
|
3
|
3
|
|
|
4
|
+import com.gct.common.util.StringUtil;
|
4
|
5
|
import feign.RequestInterceptor;
|
5
|
6
|
import feign.RequestTemplate;
|
6
|
7
|
import lombok.extern.slf4j.Slf4j;
|
7
|
|
-import org.springframework.context.EnvironmentAware;
|
|
8
|
+import org.springframework.beans.factory.annotation.Value;
|
8
|
9
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
9
|
10
|
import org.springframework.context.annotation.Bean;
|
10
|
11
|
import org.springframework.context.annotation.Configuration;
|
11
|
|
-import org.springframework.core.env.Environment;
|
12
|
12
|
import org.springframework.web.client.RestTemplate;
|
13
|
13
|
|
14
|
14
|
import java.util.Arrays;
|
|
@@ -16,43 +16,40 @@ import java.util.List;
|
16
|
16
|
import java.util.concurrent.ThreadLocalRandom;
|
17
|
17
|
|
18
|
18
|
|
19
|
|
-/**
|
20
|
|
- * feign 调用配置,统一添加token请求头
|
21
|
|
- * @author: NewMeanning
|
22
|
|
- * @create: 2021-01-29 00:10
|
23
|
|
- **/
|
24
|
19
|
@Slf4j
|
25
|
20
|
@Configuration
|
26
|
|
-public class FeignConfig implements RequestInterceptor, EnvironmentAware {
|
|
21
|
+public class FeignConfig implements RequestInterceptor {
|
27
|
22
|
|
|
23
|
+ @Value("${auth.hostList}")
|
|
24
|
+ private String tokeAuthUrl;
|
28
|
25
|
private static String token;
|
29
|
26
|
|
30
|
|
- private static Environment env;
|
|
27
|
+ private final static String lockStr = "token";
|
31
|
28
|
|
32
|
29
|
@Bean(name = "restTemplate")
|
33
|
|
- RestTemplate getRestTemplate(){
|
|
30
|
+ RestTemplate getRestTemplate() {
|
34
|
31
|
return new RestTemplate();
|
35
|
32
|
}
|
36
|
33
|
|
37
|
34
|
@Override
|
38
|
35
|
public void apply(RequestTemplate requestTemplate) {
|
39
|
|
- requestTemplate.header("token",token);//请求头添加token
|
40
|
|
- }
|
41
|
|
-
|
42
|
|
- @Override
|
43
|
|
- public void setEnvironment(Environment environment) {
|
44
|
|
- //获取配置的认证中心主机地址 todo 升级为自动从Nacos中读取,目前这里读取不到
|
45
|
|
- this.env = environment;
|
46
|
|
- List<String> hostList = Arrays.asList(this.env.getProperty("auth.hostList").split(","));
|
47
|
|
- //手动获取RestTemplate 实例
|
48
|
|
- AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
|
49
|
|
- applicationContext.register(RestTemplate.class);
|
50
|
|
- applicationContext.refresh();
|
51
|
|
- RestTemplate restTemplate= (RestTemplate) applicationContext.getBean("restTemplate");
|
52
|
|
- //自定义轮询方式负载
|
53
|
|
- int index = ThreadLocalRandom.current().nextInt(hostList.size());
|
54
|
|
- String url = "http://"+hostList.get(index)+"/token";
|
55
|
|
- token = restTemplate.getForObject(url,String.class);
|
56
|
|
- log.info("token:{}",token);
|
|
36
|
+ if (StringUtil.isBlankOrEmpty(token)) {
|
|
37
|
+ synchronized (lockStr) {
|
|
38
|
+ if (StringUtil.isBlankOrEmpty(token)) {
|
|
39
|
+ List<String> hostList = Arrays.asList(tokeAuthUrl.split(","));
|
|
40
|
+ //手动获取RestTemplate 实例
|
|
41
|
+ AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
|
|
42
|
+ applicationContext.register(RestTemplate.class);
|
|
43
|
+ applicationContext.refresh();
|
|
44
|
+ RestTemplate restTemplate = (RestTemplate) applicationContext.getBean("restTemplate");
|
|
45
|
+ //自定义轮询方式负载
|
|
46
|
+ int index = ThreadLocalRandom.current().nextInt(hostList.size());
|
|
47
|
+ String url = "http://" + hostList.get(index) + "/token";
|
|
48
|
+ token = restTemplate.getForObject(url, String.class);
|
|
49
|
+ log.info("token:{}", token);
|
|
50
|
+ }
|
|
51
|
+ }
|
|
52
|
+ }
|
|
53
|
+ requestTemplate.header("token", token);//请求头添加token
|
57
|
54
|
}
|
58
|
55
|
}
|