Programmatic service registration base on nacos and eureka, also support multi service registry center #368
This commit is contained in:
@@ -43,7 +43,7 @@ spring:
|
||||
# if use Nacos discovery set this flag to true
|
||||
enabled: false #use Nacos Discovery? (default:false)
|
||||
# need replace
|
||||
server-addr: localhost:8848 #please input the nacos discovery server-addr (default:localhost:8848)
|
||||
server-addr: 6.6.6.6:8848 #please input the nacos discovery server-addr (default:localhost:8848)
|
||||
namespace: null #please input the nacos config type (default:null)
|
||||
group: DEFAULT_GROUP #please input the nacos discovery register group (default:DEFAULT_GROUP)
|
||||
|
||||
@@ -125,6 +125,17 @@ fizz:
|
||||
timeout: 0 # default no timeout
|
||||
retry-count: 0 # default no retry
|
||||
retry-interval: 0 # default no retry interval
|
||||
service-registration:
|
||||
# type: eureka # service registration type, can be eureka or nacos
|
||||
# application: ax # register the name of this application to server
|
||||
# ip-address: 6.6.6.6 # this application server ip
|
||||
# port: 8601 # the port to be registered
|
||||
# service-url: http://6.6.6.6:6600/eureka/ # server address
|
||||
type: nacos
|
||||
application: ax
|
||||
ip-address: 6.6.6.6
|
||||
port: 8601
|
||||
service-url: 6.6.6.6:8848
|
||||
|
||||
fizz-trace-id:
|
||||
header: X-Trace-Id
|
||||
|
||||
@@ -17,15 +17,7 @@
|
||||
|
||||
package we.api.pairing;
|
||||
|
||||
import com.alibaba.cloud.nacos.NacosServiceManager;
|
||||
import com.alibaba.cloud.nacos.discovery.NacosDiscoveryAutoConfiguration;
|
||||
import com.alibaba.cloud.nacos.registry.NacosRegistration;
|
||||
import com.alibaba.cloud.nacos.registry.NacosServiceRegistry;
|
||||
import com.alibaba.nacos.api.naming.NamingService;
|
||||
import com.netflix.appinfo.ApplicationInfoManager;
|
||||
import com.netflix.appinfo.DataCenterInfo;
|
||||
import com.netflix.appinfo.HealthCheckHandler;
|
||||
import com.netflix.appinfo.InstanceInfo;
|
||||
import lombok.SneakyThrows;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
@@ -33,24 +25,19 @@ import org.springframework.boot.web.reactive.context.ReactiveWebServerApplicatio
|
||||
import org.springframework.cloud.client.ConditionalOnDiscoveryEnabled;
|
||||
import org.springframework.cloud.client.serviceregistry.Registration;
|
||||
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
|
||||
import org.springframework.cloud.commons.util.InetUtils;
|
||||
import org.springframework.cloud.commons.util.InetUtilsProperties;
|
||||
import org.springframework.cloud.loadbalancer.support.SimpleObjectProvider;
|
||||
import org.springframework.cloud.netflix.eureka.*;
|
||||
import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaRegistration;
|
||||
import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaServiceRegistry;
|
||||
import org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import we.config.SystemConfig;
|
||||
import we.service_registry.nacos.FizzNacosDiscoveryProperties;
|
||||
import we.util.ReflectionUtils;
|
||||
import we.service_registry.eureka.FizzEurekaHelper;
|
||||
import we.service_registry.eureka.FizzEurekaProperties;
|
||||
import we.service_registry.eureka.FizzEurekaServiceRegistration;
|
||||
import we.service_registry.nacos.FizzNacosHelper;
|
||||
import we.service_registry.nacos.FizzNacosProperties;
|
||||
import we.service_registry.nacos.FizzNacosServiceRegistration;
|
||||
|
||||
import javax.annotation.PreDestroy;
|
||||
import javax.annotation.Resource;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* @author hongqiaowei
|
||||
@@ -60,98 +47,79 @@ import java.util.Properties;
|
||||
@ConditionalOnDiscoveryEnabled
|
||||
@ConditionalOnProperty(name = SystemConfig.FIZZ_API_PAIRING_CLIENT_ENABLE, havingValue = "true")
|
||||
@AutoConfigureAfter({EurekaClientAutoConfiguration.class, NacosDiscoveryAutoConfiguration.class})
|
||||
public class ApiPairingServiceRegistration<R extends Registration> implements ApplicationListener<FizzApiPairingWebServerInitializedEvent> {
|
||||
public class ApiPairingServiceRegistration implements ApplicationListener<FizzApiPairingWebServerInitializedEvent> {
|
||||
|
||||
private ServiceRegistry serviceRegistry;
|
||||
|
||||
private EurekaRegistration eurekaRegistration;
|
||||
|
||||
private CloudEurekaClient eurekaClient;
|
||||
|
||||
private final InetUtils inetUtils = new InetUtils(new InetUtilsProperties());
|
||||
private Registration registration;
|
||||
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public void onApplicationEvent(FizzApiPairingWebServerInitializedEvent event) {
|
||||
|
||||
ReactiveWebServerApplicationContext applicationContext = event.getApplicationContext();
|
||||
// ConfigurableEnvironment env = applicationContext.getEnvironment();
|
||||
ConfigurableEnvironment env = applicationContext.getEnvironment();
|
||||
|
||||
String prefix = SystemConfig.FIZZ_API_PAIRING_CLIENT_PREFIX + ".service-registration";
|
||||
String type = env.getProperty(prefix + ".type");
|
||||
|
||||
/*FizzNacosDiscoveryProperties nacosDiscoveryProperties = new FizzNacosDiscoveryProperties();
|
||||
nacosDiscoveryProperties.setInetUtils(inetUtils);
|
||||
nacosDiscoveryProperties.setService("xxyyzz");
|
||||
nacosDiscoveryProperties.setIp("");
|
||||
nacosDiscoveryProperties.setPort(8601);
|
||||
nacosDiscoveryProperties.setGroup("DEFAULT_GROUP");
|
||||
nacosDiscoveryProperties.setClusterName("DEFAULT");
|
||||
nacosDiscoveryProperties.setNamespace("");
|
||||
nacosDiscoveryProperties.setSecretKey("");
|
||||
nacosDiscoveryProperties.setAccessKey("");
|
||||
nacosDiscoveryProperties.setUsername("");
|
||||
nacosDiscoveryProperties.setPassword("");
|
||||
nacosDiscoveryProperties.setEndpoint("");
|
||||
nacosDiscoveryProperties.setLogName("");
|
||||
nacosDiscoveryProperties.setNamingLoadCacheAtStart("false");
|
||||
if ("eureka".equals(type)) {
|
||||
String application = env.getProperty(prefix + ".application");
|
||||
String ipAddress = env.getProperty(prefix + ".ip-address");
|
||||
String port = env.getProperty(prefix + ".port");
|
||||
String preferIpAddress = env.getProperty(prefix + ".prefer-ip-address", "true");
|
||||
String serviceUrl = env.getProperty(prefix + ".service-url");
|
||||
|
||||
nacosDiscoveryProperties.setServerAddr(":8848");
|
||||
FizzEurekaProperties fizzEurekaProperties = new FizzEurekaProperties().applicationContext(applicationContext)
|
||||
.appName(application)
|
||||
.ipAddress(ipAddress)
|
||||
.nonSecurePort(Integer.parseInt(port))
|
||||
.preferIpAddress(Boolean.parseBoolean(preferIpAddress))
|
||||
.serviceUrl(serviceUrl);
|
||||
FizzEurekaServiceRegistration fizzEurekaServiceRegistration = FizzEurekaHelper.getServiceRegistration(fizzEurekaProperties);
|
||||
|
||||
Properties props = nacosDiscoveryProperties.getNacosProperties();
|
||||
props.put("enabled", true);
|
||||
props.put("server-addr", nacosDiscoveryProperties.getServerAddr());
|
||||
props.put("com.alibaba.nacos.naming.log.filename", "");
|
||||
serviceRegistry = fizzEurekaServiceRegistration.serviceRegistry;
|
||||
registration = fizzEurekaServiceRegistration.registration;
|
||||
}
|
||||
|
||||
nacosDiscoveryProperties.init();
|
||||
if ("nacos".equals(type)) {
|
||||
String application = env.getProperty(prefix + ".application");
|
||||
String ipAddress = env.getProperty(prefix + ".ip-address");
|
||||
String port = env.getProperty(prefix + ".port");
|
||||
String serviceUrl = env.getProperty(prefix + ".service-url");
|
||||
String namespace = env.getProperty(prefix + ".namespace", "");
|
||||
String group = env.getProperty(prefix + ".group", "DEFAULT_GROUP");
|
||||
String clusterName = env.getProperty(prefix + ".clusterName", "DEFAULT");
|
||||
|
||||
serviceRegistry = new NacosServiceRegistry(nacosDiscoveryProperties);
|
||||
NacosServiceManager nacosServiceManager = new NacosServiceManager();
|
||||
ReflectionUtils.set(serviceRegistry, "nacosServiceManager", nacosServiceManager);
|
||||
FizzNacosProperties fizzNacosProperties = new FizzNacosProperties();
|
||||
fizzNacosProperties.setApplicationContext(applicationContext);
|
||||
fizzNacosProperties.setId(application + ':' + serviceUrl);
|
||||
fizzNacosProperties.setService(application);
|
||||
fizzNacosProperties.setIp(ipAddress);
|
||||
fizzNacosProperties.setPort(Integer.parseInt(port));
|
||||
fizzNacosProperties.setNamespace(namespace.equals("") ? null : namespace);
|
||||
fizzNacosProperties.setGroup(group);
|
||||
fizzNacosProperties.setClusterName(clusterName);
|
||||
fizzNacosProperties.setNamespace("");
|
||||
fizzNacosProperties.setSecretKey("");
|
||||
fizzNacosProperties.setAccessKey("");
|
||||
fizzNacosProperties.setUsername("");
|
||||
fizzNacosProperties.setPassword("");
|
||||
fizzNacosProperties.setEndpoint("");
|
||||
fizzNacosProperties.setLogName("");
|
||||
fizzNacosProperties.setNamingLoadCacheAtStart("false");
|
||||
fizzNacosProperties.setServerAddr(serviceUrl);
|
||||
|
||||
NacosRegistration nacosRegistration = new NacosRegistration(null, nacosDiscoveryProperties, applicationContext);
|
||||
serviceRegistry.register(nacosRegistration);
|
||||
FizzNacosServiceRegistration fizzNacosServiceRegistration = FizzNacosHelper.getServiceRegistration(fizzNacosProperties);
|
||||
serviceRegistry = fizzNacosServiceRegistration.serviceRegistry;
|
||||
registration = fizzNacosServiceRegistration.registration;
|
||||
}
|
||||
|
||||
NamingService namingService = nacosServiceManager.getNamingService(props);*/
|
||||
|
||||
|
||||
|
||||
// eureka
|
||||
/*EurekaInstanceConfigBean eurekaInstanceConfig = new EurekaInstanceConfigBean(inetUtils);
|
||||
String app = "xxyyzz";
|
||||
eurekaInstanceConfig.setAppname(app);
|
||||
eurekaInstanceConfig.setVirtualHostName(app);
|
||||
eurekaInstanceConfig.setIpAddress("x.x.x.x");
|
||||
eurekaInstanceConfig.setNonSecurePort(8601);
|
||||
eurekaInstanceConfig.setInstanceId(eurekaInstanceConfig.getIpAddress() + ':' + app + ':' + eurekaInstanceConfig.getNonSecurePort());
|
||||
eurekaInstanceConfig.setPreferIpAddress(true);
|
||||
eurekaInstanceConfig.setSecurePortEnabled(false);
|
||||
eurekaInstanceConfig.setHealthCheckUrl("http://x.x.x.x:8601/actuator/info");
|
||||
eurekaInstanceConfig.setDataCenterInfo(new DataCenterInfo() {
|
||||
@Override
|
||||
public DataCenterInfo.Name getName() {
|
||||
return Name.MyOwn;
|
||||
}
|
||||
});
|
||||
|
||||
InstanceInfo instanceInfo = new InstanceInfoFactory().create(eurekaInstanceConfig);
|
||||
|
||||
ApplicationInfoManager applicationInfoManager = new ApplicationInfoManager(eurekaInstanceConfig, instanceInfo);
|
||||
|
||||
EurekaClientConfigBean eurekaClientConfig = new EurekaClientConfigBean();
|
||||
eurekaClientConfig.setRegion("default");
|
||||
Map<String, String> serviceUrlMap = new HashMap<>();
|
||||
serviceUrlMap.put(EurekaClientConfigBean.DEFAULT_ZONE, "http://x.x.x.x:6600/eureka/");
|
||||
eurekaClientConfig.setServiceUrl(serviceUrlMap);
|
||||
|
||||
eurekaClient = new CloudEurekaClient(applicationInfoManager, eurekaClientConfig, null, applicationContext);
|
||||
|
||||
SimpleObjectProvider<HealthCheckHandler> healthCheckHandler = new SimpleObjectProvider<>(null);
|
||||
eurekaRegistration = EurekaRegistration.builder(eurekaInstanceConfig).with(applicationInfoManager).with(healthCheckHandler).with(eurekaClient).build();
|
||||
|
||||
serviceRegistry = new EurekaServiceRegistry();
|
||||
serviceRegistry.register(eurekaRegistration);*/
|
||||
serviceRegistry.register(registration);
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void stop() {
|
||||
serviceRegistry.deregister(registration);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,6 +56,7 @@ public class SystemConfig {
|
||||
public static final String FIZZ_TIMESTAMP = "fizz-ts";
|
||||
|
||||
public static final String FIZZ_API_PAIRING_SERVER_ENABLE = "fizz.api.pairing.server.enable";
|
||||
public static final String FIZZ_API_PAIRING_CLIENT_PREFIX = "fizz.api.pairing.client";
|
||||
public static final String FIZZ_API_PAIRING_CLIENT_ENABLE = "fizz.api.pairing.client.enable";
|
||||
|
||||
private String gatewayPrefix = DEFAULT_GATEWAY_PREFIX;
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
package we.service_registry.eureka;
|
||||
|
||||
import org.springframework.cloud.commons.util.InetUtils;
|
||||
import org.springframework.cloud.commons.util.InetUtilsProperties;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public abstract class EurekaHelper {
|
||||
|
||||
private static final InetUtils inetUtils = new InetUtils(new InetUtilsProperties());
|
||||
|
||||
public static FizzEurekaServiceRegistration get(FizzEurekaProperties fizzEurekaProperties) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Map<String, FizzEurekaServiceRegistration> get(List<FizzEurekaProperties> fizzEurekaPropertiesList) {
|
||||
Map<String, FizzEurekaServiceRegistration> result = new HashMap<>();
|
||||
for (FizzEurekaProperties properties : fizzEurekaPropertiesList) {
|
||||
FizzEurekaServiceRegistration fizzEurekaServiceRegistration = get(properties);
|
||||
result.put(properties.id, fizzEurekaServiceRegistration);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* Copyright (C) 2020 the original author or authors.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package we.service_registry.eureka;
|
||||
|
||||
import com.netflix.appinfo.ApplicationInfoManager;
|
||||
import com.netflix.appinfo.HealthCheckHandler;
|
||||
import com.netflix.appinfo.InstanceInfo;
|
||||
import org.springframework.cloud.commons.util.InetUtils;
|
||||
import org.springframework.cloud.loadbalancer.support.SimpleObjectProvider;
|
||||
import org.springframework.cloud.netflix.eureka.CloudEurekaClient;
|
||||
import org.springframework.cloud.netflix.eureka.EurekaClientConfigBean;
|
||||
import org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean;
|
||||
import org.springframework.cloud.netflix.eureka.InstanceInfoFactory;
|
||||
import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaRegistration;
|
||||
import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaServiceRegistry;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author hongqiaowei
|
||||
*/
|
||||
|
||||
public abstract class FizzEurekaHelper {
|
||||
|
||||
public static FizzEurekaServiceRegistration getServiceRegistration(FizzEurekaProperties fizzEurekaProperties) {
|
||||
|
||||
InetUtils inetUtils = fizzEurekaProperties.applicationContext.getBean(InetUtils.class);
|
||||
EurekaInstanceConfigBean eurekaInstanceConfig = new EurekaInstanceConfigBean(inetUtils);
|
||||
eurekaInstanceConfig.setAppname(fizzEurekaProperties.appName);
|
||||
eurekaInstanceConfig.setVirtualHostName(fizzEurekaProperties.getVirtualHostName());
|
||||
eurekaInstanceConfig.setIpAddress(fizzEurekaProperties.ipAddress);
|
||||
eurekaInstanceConfig.setNonSecurePort(fizzEurekaProperties.nonSecurePort);
|
||||
eurekaInstanceConfig.setInstanceId(fizzEurekaProperties.getInstanceId());
|
||||
eurekaInstanceConfig.setPreferIpAddress(fizzEurekaProperties.preferIpAddress);
|
||||
eurekaInstanceConfig.setSecurePortEnabled(fizzEurekaProperties.securePortEnabled);
|
||||
String healthCheckUrl = fizzEurekaProperties.getHealthCheckUrl();
|
||||
if (healthCheckUrl != null) {
|
||||
eurekaInstanceConfig.setHealthCheckUrl(healthCheckUrl);
|
||||
}
|
||||
eurekaInstanceConfig.setDataCenterInfo(fizzEurekaProperties.dataCenterInfo);
|
||||
|
||||
InstanceInfo instanceInfo = new InstanceInfoFactory().create(eurekaInstanceConfig);
|
||||
|
||||
ApplicationInfoManager applicationInfoManager = new ApplicationInfoManager(eurekaInstanceConfig, instanceInfo);
|
||||
|
||||
EurekaClientConfigBean eurekaClientConfig = new EurekaClientConfigBean();
|
||||
eurekaClientConfig.setRegion(fizzEurekaProperties.region);
|
||||
Map<String, String> serviceUrlMap = new HashMap<>();
|
||||
serviceUrlMap.put(fizzEurekaProperties.zone, fizzEurekaProperties.serviceUrl);
|
||||
eurekaClientConfig.setServiceUrl(serviceUrlMap);
|
||||
|
||||
CloudEurekaClient eurekaClient = new CloudEurekaClient(applicationInfoManager, eurekaClientConfig, null, fizzEurekaProperties.applicationContext);
|
||||
|
||||
SimpleObjectProvider<HealthCheckHandler> healthCheckHandler = new SimpleObjectProvider<>(null);
|
||||
EurekaRegistration eurekaRegistration = EurekaRegistration.builder(eurekaInstanceConfig).with(applicationInfoManager).with(healthCheckHandler).with(eurekaClient).build();
|
||||
EurekaServiceRegistry serviceRegistry = new EurekaServiceRegistry();
|
||||
return new FizzEurekaServiceRegistration(fizzEurekaProperties.getId(), eurekaRegistration, serviceRegistry, eurekaClient);
|
||||
}
|
||||
|
||||
public static Map<String, FizzEurekaServiceRegistration> getServiceRegistration(List<FizzEurekaProperties> fizzEurekaPropertiesList) {
|
||||
Map<String, FizzEurekaServiceRegistration> result = new HashMap<>();
|
||||
for (FizzEurekaProperties properties : fizzEurekaPropertiesList) {
|
||||
FizzEurekaServiceRegistration fizzEurekaServiceRegistration = getServiceRegistration(properties);
|
||||
result.put(properties.getId(), fizzEurekaServiceRegistration);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -1,28 +1,52 @@
|
||||
/*
|
||||
* Copyright (C) 2020 the original author or authors.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package we.service_registry.eureka;
|
||||
|
||||
import com.netflix.appinfo.DataCenterInfo;
|
||||
import com.netflix.appinfo.MyDataCenterInfo;
|
||||
import org.springframework.cloud.netflix.eureka.EurekaClientConfigBean;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
|
||||
/**
|
||||
* @author hongqiaowei
|
||||
*/
|
||||
|
||||
public class FizzEurekaProperties {
|
||||
|
||||
public String id;
|
||||
public ConfigurableApplicationContext applicationContext;
|
||||
|
||||
private String id;
|
||||
|
||||
public String appName;
|
||||
|
||||
public String virtualHostName;
|
||||
private String virtualHostName;
|
||||
|
||||
public String ipAddress;
|
||||
|
||||
public int nonSecurePort = 80;
|
||||
|
||||
public String instanceId;
|
||||
private String instanceId;
|
||||
|
||||
public boolean preferIpAddress = true;
|
||||
|
||||
public boolean securePortEnabled = false;
|
||||
|
||||
public String healthCheckUrl;
|
||||
private String healthCheckUrl;
|
||||
|
||||
public DataCenterInfo dataCenterInfo = new MyDataCenterInfo(
|
||||
DataCenterInfo.Name.MyOwn);
|
||||
@@ -35,11 +59,23 @@ public class FizzEurekaProperties {
|
||||
|
||||
public int securePort = 443;
|
||||
|
||||
public FizzEurekaProperties applicationContext(ConfigurableApplicationContext applicationContext) {
|
||||
this.applicationContext = applicationContext;
|
||||
return this;
|
||||
}
|
||||
|
||||
public FizzEurekaProperties id(String id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
if (id == null) {
|
||||
id = appName + ':' + serviceUrl;
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
public FizzEurekaProperties appName(String appName) {
|
||||
this.appName = appName;
|
||||
return this;
|
||||
@@ -50,6 +86,13 @@ public class FizzEurekaProperties {
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getVirtualHostName() {
|
||||
if (virtualHostName == null) {
|
||||
virtualHostName = appName;
|
||||
}
|
||||
return virtualHostName;
|
||||
}
|
||||
|
||||
public FizzEurekaProperties ipAddress(String ipAddress) {
|
||||
this.ipAddress = ipAddress;
|
||||
return this;
|
||||
@@ -65,6 +108,13 @@ public class FizzEurekaProperties {
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getInstanceId() {
|
||||
if (instanceId == null) {
|
||||
instanceId = ipAddress + ':' + appName + ':' + nonSecurePort;
|
||||
}
|
||||
return instanceId;
|
||||
}
|
||||
|
||||
public FizzEurekaProperties preferIpAddress(boolean preferIpAddress) {
|
||||
this.preferIpAddress = preferIpAddress;
|
||||
return this;
|
||||
@@ -80,6 +130,13 @@ public class FizzEurekaProperties {
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getHealthCheckUrl() {
|
||||
/*if (healthCheckUrl == null) {
|
||||
healthCheckUrl = "http://" + ipAddress + ':' + nonSecurePort + "/actuator/info";
|
||||
}*/
|
||||
return healthCheckUrl;
|
||||
}
|
||||
|
||||
public FizzEurekaProperties region(String region) {
|
||||
this.region = region;
|
||||
return this;
|
||||
|
||||
@@ -1,9 +1,30 @@
|
||||
/*
|
||||
* Copyright (C) 2020 the original author or authors.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package we.service_registry.eureka;
|
||||
|
||||
import org.springframework.cloud.netflix.eureka.CloudEurekaClient;
|
||||
import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaRegistration;
|
||||
import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaServiceRegistry;
|
||||
|
||||
/**
|
||||
* @author hongqiaowei
|
||||
*/
|
||||
|
||||
public class FizzEurekaServiceRegistration {
|
||||
|
||||
public String id;
|
||||
@@ -13,4 +34,11 @@ public class FizzEurekaServiceRegistration {
|
||||
public EurekaServiceRegistry serviceRegistry;
|
||||
|
||||
public CloudEurekaClient client;
|
||||
|
||||
public FizzEurekaServiceRegistration(String id, EurekaRegistration registration, EurekaServiceRegistry serviceRegistry, CloudEurekaClient client) {
|
||||
this.id = id;
|
||||
this.registration = registration;
|
||||
this.serviceRegistry = serviceRegistry;
|
||||
this.client = client;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright (C) 2020 the original author or authors.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package we.service_registry.nacos;
|
||||
|
||||
import com.alibaba.cloud.nacos.NacosServiceManager;
|
||||
import com.alibaba.cloud.nacos.registry.NacosRegistration;
|
||||
import com.alibaba.cloud.nacos.registry.NacosServiceRegistry;
|
||||
import com.alibaba.nacos.api.naming.NamingService;
|
||||
import we.util.ReflectionUtils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author hongqiaowei
|
||||
*/
|
||||
|
||||
public abstract class FizzNacosHelper {
|
||||
|
||||
public static FizzNacosServiceRegistration getServiceRegistration(FizzNacosProperties fizzNacosProperties) {
|
||||
fizzNacosProperties.init();
|
||||
NacosServiceRegistry nacosServiceRegistry = new NacosServiceRegistry(fizzNacosProperties);
|
||||
NacosServiceManager nacosServiceManager = new NacosServiceManager();
|
||||
ReflectionUtils.set(nacosServiceRegistry, "nacosServiceManager", nacosServiceManager);
|
||||
NacosRegistration nacosRegistration = new NacosRegistration(null, fizzNacosProperties, fizzNacosProperties.getApplicationContext());
|
||||
NamingService namingService = nacosServiceManager.getNamingService(fizzNacosProperties.getNacosProperties());
|
||||
return new FizzNacosServiceRegistration(fizzNacosProperties.getId(), nacosRegistration, nacosServiceRegistry, namingService);
|
||||
}
|
||||
|
||||
public static Map<String, FizzNacosServiceRegistration> getServiceRegistration(List<FizzNacosProperties> fizzNacosPropertiesList) {
|
||||
Map<String, FizzNacosServiceRegistration> result = new HashMap<>();
|
||||
for (FizzNacosProperties properties : fizzNacosPropertiesList) {
|
||||
FizzNacosServiceRegistration fizzNacosServiceRegistration = getServiceRegistration(properties);
|
||||
result.put(properties.getId(), fizzNacosServiceRegistration);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -1,20 +1,47 @@
|
||||
/*
|
||||
* Copyright (C) 2020 the original author or authors.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package we.service_registry.nacos;
|
||||
|
||||
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
|
||||
import com.alibaba.nacos.api.naming.PreservedMetadataKeys;
|
||||
import com.alibaba.nacos.client.naming.utils.UtilAndComs;
|
||||
import org.springframework.cloud.commons.util.InetUtils;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.util.StringUtils;
|
||||
import we.util.JacksonUtils;
|
||||
|
||||
import java.net.Inet4Address;
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.SocketException;
|
||||
import java.util.*;
|
||||
|
||||
import static com.alibaba.nacos.api.PropertyKeyConst.*;
|
||||
|
||||
public class FizzNacosDiscoveryProperties extends NacosDiscoveryProperties {
|
||||
/**
|
||||
* @author hongqiaowei
|
||||
*/
|
||||
|
||||
public class FizzNacosProperties extends NacosDiscoveryProperties {
|
||||
|
||||
private ConfigurableApplicationContext applicationContext;
|
||||
|
||||
private String id;
|
||||
|
||||
private String serverAddr;
|
||||
|
||||
@@ -68,10 +95,14 @@ public class FizzNacosDiscoveryProperties extends NacosDiscoveryProperties {
|
||||
|
||||
private boolean failFast = true;
|
||||
|
||||
private InetUtils inetUtils;
|
||||
private boolean init = false;
|
||||
|
||||
public void init() throws Exception {
|
||||
metadata.put(PreservedMetadataKeys.REGISTER_SOURCE, "FIZZ-API-PAIRING");
|
||||
public void init() {
|
||||
if (init) {
|
||||
return;
|
||||
}
|
||||
|
||||
metadata.put(PreservedMetadataKeys.REGISTER_SOURCE, "SPRING_CLOUD");
|
||||
if (secure) {
|
||||
metadata.put("secure", "true");
|
||||
}
|
||||
@@ -86,13 +117,13 @@ public class FizzNacosDiscoveryProperties extends NacosDiscoveryProperties {
|
||||
|
||||
if (StringUtils.isEmpty(ip)) {
|
||||
if (StringUtils.isEmpty(networkInterface)) {
|
||||
ip = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
|
||||
ip = applicationContext.getBean(InetUtils.class).findFirstNonLoopbackHostInfo().getIpAddress();
|
||||
} else {
|
||||
NetworkInterface netInterface = NetworkInterface
|
||||
.getByName(networkInterface);
|
||||
if (null == netInterface) {
|
||||
throw new IllegalArgumentException(
|
||||
"no such interface " + networkInterface);
|
||||
NetworkInterface netInterface = null;
|
||||
try {
|
||||
netInterface = NetworkInterface.getByName(networkInterface);
|
||||
} catch (SocketException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
Enumeration<InetAddress> inetAddress = netInterface.getInetAddresses();
|
||||
while (inetAddress.hasMoreElements()) {
|
||||
@@ -109,6 +140,24 @@ public class FizzNacosDiscoveryProperties extends NacosDiscoveryProperties {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
init = true;
|
||||
}
|
||||
|
||||
public void setApplicationContext(ConfigurableApplicationContext applicationContext) {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
public ConfigurableApplicationContext getApplicationContext() {
|
||||
return applicationContext;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getEndpoint() {
|
||||
@@ -135,10 +184,6 @@ public class FizzNacosDiscoveryProperties extends NacosDiscoveryProperties {
|
||||
this.logName = logName;
|
||||
}
|
||||
|
||||
public void setInetUtils(InetUtils inetUtils) {
|
||||
this.inetUtils = inetUtils;
|
||||
}
|
||||
|
||||
public float getWeight() {
|
||||
return weight;
|
||||
}
|
||||
@@ -331,7 +376,7 @@ public class FizzNacosDiscoveryProperties extends NacosDiscoveryProperties {
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
FizzNacosDiscoveryProperties that = (FizzNacosDiscoveryProperties) o;
|
||||
FizzNacosProperties that = (FizzNacosProperties) o;
|
||||
return Objects.equals(serverAddr, that.serverAddr)
|
||||
&& Objects.equals(username, that.username)
|
||||
&& Objects.equals(password, that.password)
|
||||
@@ -387,6 +432,10 @@ public class FizzNacosDiscoveryProperties extends NacosDiscoveryProperties {
|
||||
properties.put(CLUSTER_NAME, clusterName);
|
||||
properties.put(NAMING_LOAD_CACHE_AT_START, namingLoadCacheAtStart);
|
||||
|
||||
properties.put("enabled", true);
|
||||
properties.put("server-addr", this.getServerAddr());
|
||||
properties.put("com.alibaba.nacos.naming.log.filename", "");
|
||||
|
||||
return properties;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright (C) 2020 the original author or authors.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package we.service_registry.nacos;
|
||||
|
||||
import com.alibaba.cloud.nacos.registry.NacosRegistration;
|
||||
import com.alibaba.cloud.nacos.registry.NacosServiceRegistry;
|
||||
import com.alibaba.nacos.api.naming.NamingService;
|
||||
|
||||
/**
|
||||
* @author hongqiaowei
|
||||
*/
|
||||
|
||||
public class FizzNacosServiceRegistration {
|
||||
|
||||
public String id;
|
||||
|
||||
public NacosRegistration registration;
|
||||
|
||||
public NacosServiceRegistry serviceRegistry;
|
||||
|
||||
public NamingService namingService;
|
||||
|
||||
public FizzNacosServiceRegistration(String id, NacosRegistration registration, NacosServiceRegistry serviceRegistry, NamingService namingService) {
|
||||
this.id = id;
|
||||
this.registration = registration;
|
||||
this.serviceRegistry = serviceRegistry;
|
||||
this.namingService = namingService;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user