提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.gateway.util; |
H |
2 |
|
|
3 |
import cn.hutool.core.net.NetUtil; |
|
4 |
import cn.hutool.core.util.IdUtil; |
|
5 |
import cn.hutool.core.util.StrUtil; |
|
6 |
import org.springframework.cloud.client.ServiceInstance; |
|
7 |
import org.springframework.http.HttpHeaders; |
|
8 |
|
|
9 |
import java.util.Objects; |
|
10 |
|
|
11 |
/** |
|
12 |
* 环境 Utils |
|
13 |
* |
|
14 |
* copy from iailab-common-env 的 EnvUtils 类 |
|
15 |
* |
|
16 |
* @author iailab |
|
17 |
*/ |
|
18 |
public class EnvUtils { |
|
19 |
|
|
20 |
private static final String HEADER_TAG = "tag"; |
|
21 |
|
|
22 |
public static final String HOST_NAME_VALUE = "${HOSTNAME}"; |
|
23 |
|
|
24 |
public static String getTag(HttpHeaders headers) { |
|
25 |
String tag = headers.getFirst(HEADER_TAG); |
|
26 |
// 如果请求的是 "${HOSTNAME}",则解析成对应的本地主机名 |
|
27 |
// 目的:特殊逻辑,解决 IDEA Rest Client 不支持环境变量的读取,所以就服务器来做 |
|
28 |
return Objects.equals(tag, HOST_NAME_VALUE) ? getHostName() : tag; |
|
29 |
} |
|
30 |
|
|
31 |
public static String getTag(ServiceInstance instance) { |
|
32 |
return instance.getMetadata().get(HEADER_TAG); |
|
33 |
} |
|
34 |
|
|
35 |
public static String getHostName() { |
|
36 |
return StrUtil.blankToDefault(NetUtil.getLocalHostName(), IdUtil.fastSimpleUUID()); |
|
37 |
} |
|
38 |
|
|
39 |
} |