9 changed files with 174 additions and 8 deletions
@ -0,0 +1,137 @@ |
|||
package com.epmet.jmreport.config; |
|||
|
|||
import com.alibaba.nacos.api.PropertyKeyConst; |
|||
import com.alibaba.nacos.api.exception.NacosException; |
|||
import com.alibaba.nacos.api.naming.NamingFactory; |
|||
import com.alibaba.nacos.api.naming.NamingService; |
|||
import com.alibaba.nacos.api.naming.pojo.Instance; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.beans.factory.annotation.Value; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import javax.annotation.PostConstruct; |
|||
import javax.annotation.PreDestroy; |
|||
import java.io.IOException; |
|||
import java.net.Inet4Address; |
|||
import java.net.InetAddress; |
|||
import java.net.NetworkInterface; |
|||
import java.net.UnknownHostException; |
|||
import java.util.Enumeration; |
|||
import java.util.Properties; |
|||
|
|||
/** |
|||
* nacos注册配置 |
|||
*/ |
|||
@Component |
|||
@Slf4j |
|||
public class NacosRegisterConfig { |
|||
|
|||
@Value("${server.port}") |
|||
private String port; |
|||
|
|||
@Value("${spring.application.name}") |
|||
private String serviceName; |
|||
|
|||
@Value("${nacos.discovery.namespace}") |
|||
private String namespace; |
|||
|
|||
@Value("${nacos.discovery.server-addr}") |
|||
private String nacosServerAddr; |
|||
|
|||
private NamingService naming; |
|||
|
|||
/** |
|||
* 启动之后,手动注册此实例到nacos |
|||
*/ |
|||
@PostConstruct |
|||
public void register() { |
|||
try { |
|||
naming = NamingFactory.createNamingService(initProperties()); |
|||
|
|||
InetAddress localIp = findFirstNonLoopbackAddress(); |
|||
naming.registerInstance(serviceName, localIp.getHostAddress(), Integer.valueOf(port)); |
|||
log.info("【积木报表服务】启动环节-注册到nacos成功"); |
|||
} catch (NacosException e) { |
|||
log.error("【积木报表服务】启动环节-注册到nacos失败:{}", e.getMessage()); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 初始化配置属性 |
|||
* @return |
|||
*/ |
|||
private Properties initProperties() { |
|||
Properties properties = new Properties(); |
|||
properties.setProperty(PropertyKeyConst.NAMESPACE, namespace); |
|||
properties.setProperty(PropertyKeyConst.SERVER_ADDR, nacosServerAddr); |
|||
//properties.setProperty(PropertyKeyConst.CLUSTER_NAME, nacosServerAddr);
|
|||
|
|||
return properties; |
|||
} |
|||
|
|||
/** |
|||
* 关闭之前,手动调用注销接口,从nacos注销此实例 |
|||
*/ |
|||
@PreDestroy |
|||
public void deregister() { |
|||
try { |
|||
InetAddress localIp = findFirstNonLoopbackAddress(); |
|||
naming.deregisterInstance(serviceName, localIp.getHostAddress(), Integer.valueOf(port)); |
|||
log.info("【积木报表服务】启动环节-从nacos注销成功"); |
|||
} catch (NacosException e) { |
|||
log.error("【积木报表服务】启动环节-从nacos注销失败:{}", e.getMessage()); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 获取本机的IP地址 |
|||
* from org.springframework.cloud.commons.util.InetUtils#findFirstNonLoopbackAddress |
|||
* @return |
|||
*/ |
|||
public InetAddress findFirstNonLoopbackAddress() { |
|||
InetAddress result = null; |
|||
try { |
|||
int lowest = Integer.MAX_VALUE; |
|||
for (Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces(); nics.hasMoreElements();) { |
|||
NetworkInterface ifc = nics.nextElement(); |
|||
if (ifc.isUp()) { |
|||
this.log.trace("Testing interface: " + ifc.getDisplayName()); |
|||
if (ifc.getIndex() < lowest || result == null) { |
|||
lowest = ifc.getIndex(); |
|||
} |
|||
else if (result != null) { |
|||
continue; |
|||
} |
|||
|
|||
for (Enumeration<InetAddress> addrs = ifc |
|||
.getInetAddresses(); addrs.hasMoreElements();) { |
|||
InetAddress address = addrs.nextElement(); |
|||
if (address instanceof Inet4Address |
|||
&& !address.isLoopbackAddress()) { |
|||
this.log.trace("Found non-loopback interface: " |
|||
+ ifc.getDisplayName()); |
|||
result = address; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
catch (IOException ex) { |
|||
this.log.error("Cannot get first non-loopback address", ex); |
|||
} |
|||
|
|||
if (result != null) { |
|||
return result; |
|||
} |
|||
|
|||
try { |
|||
return InetAddress.getLocalHost(); |
|||
} |
|||
catch (UnknownHostException e) { |
|||
this.log.warn("Unable to retrieve localhost"); |
|||
} |
|||
|
|||
return null; |
|||
} |
|||
|
|||
} |
Loading…
Reference in new issue