update NacosAutoServiceRegistration, update pom.xml, and format code

pull/24/head
flystar32 7 years ago
parent 50ffcef735
commit 879e689390

@ -34,7 +34,6 @@
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<!--spring boot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
@ -62,13 +61,12 @@
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>provided</scope>
<optional>true</optional>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>

@ -63,8 +63,10 @@ public class NacosDiscoveryAutoConfiguration {
@Bean
@ConditionalOnBean(AutoServiceRegistrationProperties.class)
public NacosAutoServiceRegistration nacosAutoServiceRegistration(
NacosServiceRegistry registry, AutoServiceRegistrationProperties properties,
NacosServiceRegistry registry,
AutoServiceRegistrationProperties autoServiceRegistrationProperties,
NacosRegistration registration) {
return new NacosAutoServiceRegistration(registry, properties, registration);
return new NacosAutoServiceRegistration(registry,
autoServiceRegistrationProperties, registration);
}
}

@ -35,7 +35,8 @@ import com.alibaba.nacos.api.naming.pojo.ListView;
*/
public class NacosDiscoveryClient implements DiscoveryClient {
private static final Logger LOGGER = LoggerFactory.getLogger(NacosDiscoveryClient.class);
private static final Logger LOGGER = LoggerFactory
.getLogger(NacosDiscoveryClient.class);
public static final String DESCRIPTION = "Spring Cloud Nacos Discovery Client";
@Autowired
@ -85,7 +86,7 @@ public class NacosDiscoveryClient implements DiscoveryClient {
public List<ServiceInstance> getInstances(String serviceId) {
try {
NamingService namingService = nacosRegistration.getNacosNamingService();
List<Instance> instances = namingService.selectInstances(serviceId,true);
List<Instance> instances = namingService.selectInstances(serviceId, true);
return hostToServiceInstanceList(instances, serviceId);
}
catch (Exception e) {
@ -94,25 +95,26 @@ public class NacosDiscoveryClient implements DiscoveryClient {
}
}
private static ServiceInstance hostToServiceInstance(Instance instance, String serviceId) {
private static ServiceInstance hostToServiceInstance(Instance instance,
String serviceId) {
NacosServiceInstance nacosServiceInstance = new NacosServiceInstance();
nacosServiceInstance.setHost(instance.getIp());
nacosServiceInstance.setPort(instance.getPort());
nacosServiceInstance.setServiceId(serviceId);
Map<String, String> metadata = new HashMap<String, String>();
metadata.put("instanceId", instance.getInstanceId());
metadata.put("weight", instance.getWeight()+"");
metadata.put("healthy", instance.isHealthy()+"");
metadata.put("cluster", instance.getCluster()+"");
metadata.put("weight", instance.getWeight() + "");
metadata.put("healthy", instance.isHealthy() + "");
metadata.put("cluster", instance.getCluster() + "");
metadata.putAll(instance.getMetadata());
nacosServiceInstance.setMetadata(metadata);
return nacosServiceInstance;
}
private static List<ServiceInstance> hostToServiceInstanceList(List<Instance> instances,
String serviceId) {
private static List<ServiceInstance> hostToServiceInstanceList(
List<Instance> instances, String serviceId) {
List<ServiceInstance> result = new ArrayList<ServiceInstance>(instances.size());
for (Instance instance: instances) {
for (Instance instance : instances) {
result.add(hostToServiceInstance(instance, serviceId));
}
return result;
@ -123,9 +125,11 @@ public class NacosDiscoveryClient implements DiscoveryClient {
try {
NamingService namingService = nacosRegistration.getNacosNamingService();
ListView<String> services = namingService.getServicesOfServer(1, Integer.MAX_VALUE);
ListView<String> services = namingService.getServicesOfServer(1,
Integer.MAX_VALUE);
return services.getData();
}catch( Exception e){
}
catch (Exception e) {
LOGGER.error("get service name from nacos server fail,", e);
return Collections.emptyList();
}

@ -16,7 +16,6 @@
package org.springframework.cloud.alibaba.nacos;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ -49,7 +48,8 @@ public class NacosDiscoveryProperties {
private String serverAddr;
/**
* the domain name of a service, through which the server address can be dynamically obtained.
* the domain name of a service, through which the server address can be dynamically
* obtained.
*/
private String endpoint;
@ -91,8 +91,8 @@ public class NacosDiscoveryProperties {
private boolean registerEnabled = true;
/**
* The ip address your want to register for your service instance, needn't to set it if
* the auto detect ip works well
* The ip address your want to register for your service instance, needn't to set it
* if the auto detect ip works well.
*/
private String ip;
@ -123,7 +123,6 @@ public class NacosDiscoveryProperties {
private String secretKey;
@Autowired
@JsonIgnore
private InetUtils inetUtils;
@PostConstruct
@ -190,10 +189,6 @@ public class NacosDiscoveryProperties {
this.logName = logName;
}
public InetUtils getInetUtils() {
return inetUtils;
}
public void setInetUtils(InetUtils inetUtils) {
this.inetUtils = inetUtils;
}
@ -296,47 +291,45 @@ public class NacosDiscoveryProperties {
@Override
public String toString() {
return "NacosDiscoveryProperties{" +
"serverAddr='" + serverAddr + '\'' +
", endpoint='" + endpoint + '\'' +
", namespace='" + namespace + '\'' +
", logName='" + logName + '\'' +
", service='" + service + '\'' +
", weight=" + weight +
", clusterName='" + clusterName + '\'' +
", metadata=" + metadata +
", registerEnabled=" + registerEnabled +
", ip='" + ip + '\'' +
", networkInterface='" + networkInterface + '\'' +
", port=" + port +
", secure=" + secure +
", accessKey='" + accessKey + '\'' +
", secretKey='" + secretKey + '\'' +
'}';
}
public void overrideFromEnv(Environment env){
if(StringUtils.isEmpty(this.getServerAddr())) {
this.setServerAddr(env.resolvePlaceholders("${spring.cloud.nacos.discovery.server-addr:}"));
return "NacosDiscoveryProperties{" + "serverAddr='" + serverAddr + '\''
+ ", endpoint='" + endpoint + '\'' + ", namespace='" + namespace + '\''
+ ", logName='" + logName + '\'' + ", service='" + service + '\''
+ ", weight=" + weight + ", clusterName='" + clusterName + '\''
+ ", metadata=" + metadata + ", registerEnabled=" + registerEnabled
+ ", ip='" + ip + '\'' + ", networkInterface='" + networkInterface + '\''
+ ", port=" + port + ", secure=" + secure + ", accessKey='" + accessKey
+ '\'' + ", secretKey='" + secretKey + '\'' + '}';
}
public void overrideFromEnv(Environment env) {
if (StringUtils.isEmpty(this.getServerAddr())) {
this.setServerAddr(env
.resolvePlaceholders("${spring.cloud.nacos.discovery.server-addr:}"));
}
if(StringUtils.isEmpty(this.getNamespace())) {
this.setNamespace(env.resolvePlaceholders("${spring.cloud.nacos.discovery.namespace:}"));
if (StringUtils.isEmpty(this.getNamespace())) {
this.setNamespace(env
.resolvePlaceholders("${spring.cloud.nacos.discovery.namespace:}"));
}
if(StringUtils.isEmpty(this.getAccessKey())) {
this.setAccessKey(env.resolvePlaceholders("${spring.cloud.nacos.discovery.access-key:}"));
if (StringUtils.isEmpty(this.getAccessKey())) {
this.setAccessKey(env
.resolvePlaceholders("${spring.cloud.nacos.discovery.access-key:}"));
}
if(StringUtils.isEmpty(this.getSecretKey())) {
this.setSecretKey(env.resolvePlaceholders("${spring.cloud.nacos.discovery.secret-key:}"));
if (StringUtils.isEmpty(this.getSecretKey())) {
this.setSecretKey(env
.resolvePlaceholders("${spring.cloud.nacos.discovery.secret-key:}"));
}
if(StringUtils.isEmpty(this.getLogName())) {
this.setLogName(env.resolvePlaceholders("${spring.cloud.nacos.discovery.log-name:}"));
if (StringUtils.isEmpty(this.getLogName())) {
this.setLogName(
env.resolvePlaceholders("${spring.cloud.nacos.discovery.log-name:}"));
}
if(StringUtils.isEmpty(this.getClusterName())) {
this.setClusterName(env.resolvePlaceholders("${spring.cloud.nacos.discovery.clusterName-name:}"));
if (StringUtils.isEmpty(this.getClusterName())) {
this.setClusterName(env.resolvePlaceholders(
"${spring.cloud.nacos.discovery.clusterName-name:}"));
}
if(StringUtils.isEmpty(this.getEndpoint())) {
this.setEndpoint(env.resolvePlaceholders("${spring.cloud.nacos.discovery.endpoint:}"));
if (StringUtils.isEmpty(this.getEndpoint())) {
this.setEndpoint(
env.resolvePlaceholders("${spring.cloud.nacos.discovery.endpoint:}"));
}
}

@ -30,7 +30,6 @@ import org.springframework.context.annotation.Configuration;
@ConditionalOnClass(name = "org.springframework.boot.actuate.endpoint.AbstractEndpoint")
public class NacosDiscoveryEndpointAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public NacosDiscoveryEndpoint nacosDiscoveryEndpoint() {

@ -19,12 +19,11 @@ package org.springframework.cloud.alibaba.nacos.registry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;
import org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration;
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties;
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
import org.springframework.context.ApplicationContext;
import org.springframework.context.event.EventListener;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* @author xiaojing
@ -37,20 +36,26 @@ public class NacosAutoServiceRegistration
@Autowired
private NacosRegistration registration;
@Autowired
private ApplicationContext context;
public NacosAutoServiceRegistration(
ServiceRegistry<NacosRegistration> serviceRegistry,
AutoServiceRegistrationProperties properties,
AutoServiceRegistrationProperties autoServiceRegistrationProperties,
NacosRegistration registration) {
super(serviceRegistry, properties);
super(serviceRegistry, autoServiceRegistrationProperties);
this.registration = registration;
}
@Deprecated
public void setPort(int port) {
getPort().set(port);
}
@Override
protected NacosRegistration getRegistration() {
return registration;
if (this.registration.getPort() < 0 && this.getPort().get() > 0) {
this.registration.setPort(this.getPort().get());
}
Assert.isTrue(this.registration.getPort() > 0, "service.port has not been set");
return this.registration;
}
@Override
@ -58,6 +63,27 @@ public class NacosAutoServiceRegistration
return null;
}
@Override
protected void register() {
if (!this.registration.getNacosDiscoveryProperties().isRegisterEnabled()) {
LOGGER.debug("Registration disabled.");
return;
}
if (this.registration.getPort() < 0) {
this.registration.setPort(getPort().get());
}
super.register();
}
@Override
protected void registerManagement() {
if (!this.registration.getNacosDiscoveryProperties().isRegisterEnabled()) {
return;
}
super.registerManagement();
}
@Override
protected int getConfiguredPort() {
return this.getPort().get();
@ -70,34 +96,19 @@ public class NacosAutoServiceRegistration
@Override
protected Object getConfiguration() {
return null;
return this.registration.getNacosDiscoveryProperties();
}
@Override
protected boolean isEnabled() {
return true;
return this.registration.getNacosDiscoveryProperties().isRegisterEnabled();
}
/**
* Register the local service with the {@link ServiceRegistry}
*/
@Override
protected void register() {
this.registration.setPort(this.getPort().get());
this.getServiceRegistry().register(getRegistration());
}
@Override
@EventListener(EmbeddedServletContainerInitializedEvent.class)
public void onApplicationEvent(EmbeddedServletContainerInitializedEvent event) {
if (context.equals(event.getApplicationContext())) {
int localPort = event.getEmbeddedServletContainer().getPort();
if (this.getPort().get() == 0) {
LOGGER.info("Updating port to " + localPort);
this.getPort().compareAndSet(0, localPort);
start();
}
}
@SuppressWarnings("deprecation")
protected String getAppName() {
String appName = registration.getNacosDiscoveryProperties().getService();
return StringUtils.isEmpty(appName) ? super.getAppName() : appName;
}
}

@ -34,6 +34,7 @@ import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.client.naming.utils.UtilAndComs;
import static com.alibaba.nacos.api.PropertyKeyConst.*;
/**
* @author xiaojing
*/
@ -54,11 +55,12 @@ public class NacosRegistration implements Registration, ServiceInstance {
Properties properties = new Properties();
properties.put(SERVER_ADDR, nacosDiscoveryProperties.getServerAddr());
properties.put(NAMESPACE, nacosDiscoveryProperties.getNamespace());
properties.put(UtilAndComs.NACOS_NAMING_LOG_NAME, nacosDiscoveryProperties.getLogName());
properties.put(UtilAndComs.NACOS_NAMING_LOG_NAME,
nacosDiscoveryProperties.getLogName());
properties.put(ENDPOINT, nacosDiscoveryProperties.getEndpoint());
properties.put(ACCESS_KEY,nacosDiscoveryProperties.getAccessKey());
properties.put(SECRET_KEY,nacosDiscoveryProperties.getSecretKey());
properties.put(CLUSTER_NAME,nacosDiscoveryProperties.getClusterName());
properties.put(ACCESS_KEY, nacosDiscoveryProperties.getAccessKey());
properties.put(SECRET_KEY, nacosDiscoveryProperties.getSecretKey());
properties.put(CLUSTER_NAME, nacosDiscoveryProperties.getClusterName());
try {
nacosNamingService = NacosFactory.createNamingService(properties);
}
@ -128,15 +130,14 @@ public class NacosRegistration implements Registration, ServiceInstance {
}
public void setNacosDiscoveryProperties(
NacosDiscoveryProperties nacosDiscoveryProperties) {
NacosDiscoveryProperties nacosDiscoveryProperties) {
this.nacosDiscoveryProperties = nacosDiscoveryProperties;
}
@Override
public String toString() {
return "NacosRegistration{" +
"nacosDiscoveryProperties=" + nacosDiscoveryProperties +
", nacosNamingService=" + nacosNamingService +
'}';
return "NacosRegistration{" + "nacosDiscoveryProperties="
+ nacosDiscoveryProperties + ", nacosNamingService=" + nacosNamingService
+ '}';
}
}

@ -36,7 +36,8 @@ public class NacosServerList extends AbstractServerList<NacosServer> {
private String serviceId;
public NacosServerList(){}
public NacosServerList() {
}
public NacosServerList(String serviceId) {
this.serviceId = serviceId;
@ -54,11 +55,14 @@ public class NacosServerList extends AbstractServerList<NacosServer> {
private List<NacosServer> getServers() {
try {
List<Instance> instances = registration.getNacosNamingService().selectInstances(serviceId, true);
List<Instance> instances = registration.getNacosNamingService()
.selectInstances(serviceId, true);
return instancesToServerList(instances);
}
catch (Exception e) {
throw new IllegalStateException("Can not get service instances from nacos, serviceId=" + serviceId, e);
throw new IllegalStateException(
"Can not get service instances from nacos, serviceId=" + serviceId,
e);
}
}

Loading…
Cancel
Save