问题描述
在多网卡(虚拟网卡,云服务器内网/外网)的情况下,微服务注册的IP,不是有效IP,造成服务不可用
处理思路
解决方法:
- 服务器配置hosts
- 代码或者配置里规定IP
两种办法维护太麻烦
我的方式:
- 自动解析当前的有效IP并使用此IP注册
代码
提供获取有效/外网IP接口
复制代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54// 接口 /** * IP * @return */ @GetMapping("/ip") public Object ip(HttpServletRequest request) { return RequestUtil.getIpAddress(request); } // 工具方法 /** * ip * @param request * @return */ public static String getIpAddress(HttpServletRequest request) { return getIpAddressList(request).get(0); } /** * ip * @param request * @return */ public static List<String> getIpAddressList(HttpServletRequest request) { List<String> list = Lists.newArrayList(); String xip = request.getHeader("x-forwarded-for"); if (!checkIP(xip)) { list.add(xip); } String pip = request.getHeader("Proxy-Client-IP"); if (!checkIP(pip)) { list.add(pip); } String wip = request.getHeader("WL-Proxy-Client-IP"); if (!checkIP(wip)) { list.add(wip); } String hip = request.getHeader("HTTP_CLIENT_IP"); if (!checkIP(hip)) { list.add(hip); } String hxip = request.getHeader("HTTP_X_FORWARDED_FOR"); if (!checkIP(hxip)) { list.add(hxip); } String ip = request.getRemoteAddr(); if (!checkIP(ip)) { list.add(ip); } return list; } public static boolean checkIP(String ip) { return ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip); }
eureka使用此IP注册
复制代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36@Slf4j public class EurekaIpAddressUtil { public static void initConfig() { Properties properties = EurekaIpAddressUtil.getClasspathProperties("eureka.properties"); if(properties == null) { return; } String serviceUrl = properties.getProperty("eureka.serviceUrl"); log.info(serviceUrl); if(serviceUrl == null || "".equals(serviceUrl.trim())) { return; } String url = serviceUrl.replace("eureka/", "public/ip"); log.info(url); String ipAddress = UrlHttpUtil.get(url); log.info(ipAddress); if(ipAddress == null) { return; } // eureka System.setProperty("eureka.instance.ipAddress", ipAddress); // dubbo使用此设置,代码其他一样 //System.setProperty("dubbo.provider.host", ipAddress); } public static Properties getClasspathProperties(String path) { try { Properties properties = new Properties(); InputStream in = EurekaIpAddressUtil.class.getClassLoader().getResourceAsStream(path); properties.load(in); return properties; } catch (IOException e) { e.printStackTrace(); } return null; } }
其他注册也是类似处理
最后
以上就是彩色中心最近收集整理的关于dubbo + springcloud(eureka) 注册有效/外网IP的全部内容,更多相关dubbo内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复