Skip to content

Commit

Permalink
[SCB-2813]Nacos implementation not properly read endpoints and other …
Browse files Browse the repository at this point in the history
…properties (#3980)
  • Loading branch information
liubao68 authored Oct 20, 2023
1 parent 7f18ee5 commit ed60267
Show file tree
Hide file tree
Showing 18 changed files with 166 additions and 169 deletions.
4 changes: 0 additions & 4 deletions demo/demo-nacos/consumer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@
<groupId>org.apache.servicecomb</groupId>
<artifactId>java-chassis-spring-boot-starter-standalone</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
3 changes: 0 additions & 3 deletions demo/demo-nacos/consumer/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,5 @@ servicecomb:
rest:
address: 0.0.0.0:9092

server:
port: 9092



4 changes: 0 additions & 4 deletions demo/demo-nacos/gateway/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@
<groupId>org.apache.servicecomb</groupId>
<artifactId>edge-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
4 changes: 0 additions & 4 deletions demo/demo-nacos/gateway/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
## See the License for the specific language governing permissions and
## limitations under the License.
## ---------------------------------------------------------------------------
server:
port: 9090

servicecomb:
service:
application: demo-nacos
Expand All @@ -27,7 +24,6 @@ servicecomb:
nacos:
enabled: true
serverAddr: ${PAAS_CSE_NACOS_ENDPOINT:http://127.0.0.1:8848}
version: 0.0.1

rest:
address: 0.0.0.0:9090?sslEnabled=false
Expand Down
4 changes: 4 additions & 0 deletions demo/demo-nacos/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
</dependencies>

<modules>
Expand Down
4 changes: 0 additions & 4 deletions demo/demo-nacos/provider/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@
<groupId>org.apache.servicecomb</groupId>
<artifactId>java-chassis-spring-boot-starter-standalone</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
3 changes: 0 additions & 3 deletions demo/demo-nacos/provider/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,3 @@ servicecomb:
rest:
address: 0.0.0.0:9094

server:
port: 9094

Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ public void testRestTransport() throws Exception {
private void testHelloWorld() {
String result = template
.getForObject(Config.GATEWAY_URL + "/sayHello?name=World", String.class);
TestMgr.check("\"Hello World\"", result);
TestMgr.check("Hello World", result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
## See the License for the specific language governing permissions and
## limitations under the License.
## ---------------------------------------------------------------------------
# spring boot configurations
server:
port: 9097 # should be same with servicecomb.rest.address to use web container

servicecomb:
service:
application: demo-nacos
Expand All @@ -28,8 +24,6 @@ servicecomb:
nacos:
enabled: true
serverAddr: ${PAAS_CSE_NACOS_ENDPOINT:http://127.0.0.1:8848}
serviceName: test-client
version: 0.0.1

rest:
address: 0.0.0.0:9097 # should be same with server.port to use web container
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.servicecomb.registry.nacos;

import org.apache.servicecomb.config.DataCenterProperties;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
Expand All @@ -33,10 +34,12 @@ public NacosDiscoveryProperties nacosDiscoveryProperties() {

@Bean
public NacosRegistration nacosRegistration(
DataCenterProperties dataCenterProperties,
@Qualifier("nacosDiscoveryProperties") NacosDiscoveryProperties nacosDiscoveryProperties,
@Qualifier("nacosDiscovery") NacosDiscovery nacosDiscovery, Environment environment,
Environment environment,
@Qualifier("instancesChangeEventListener") InstancesChangeEventListener instancesChangeEventListener) {
return new NacosRegistration(nacosDiscoveryProperties, nacosDiscovery, environment, instancesChangeEventListener);
return new NacosRegistration(dataCenterProperties, nacosDiscoveryProperties, environment,
instancesChangeEventListener);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,24 @@
package org.apache.servicecomb.registry.nacos;

public class NacosConst {
public static final String PROPERTY_VERSION = "scb-version";

public static final String PROPERTY_ALIAS = "scb-alias";

public static final String PROPERTY_DESCRIPTION = "scb-description";

public static final String PROPERTY_ENDPOINT = "scb-endpoint";

public static final String PROPERTY_REGION = "scb-region";

public static final String PROPERTY_ZONE = "scb-zone";

public static final String PROPERTY_DATACENTER = "scb-datacenter";

public static final String PROPERTY_SCHEMA_PREFIX = "scb-schema-";

public static final String ENDPOINT_PROPERTY_SEPARATOR = ",";

public static final String NACOS_REGISTRY_PREFIX = "servicecomb.registry.nacos";

public static final String SERVER_ADDR = "serverAddr";
Expand All @@ -39,6 +57,4 @@ public class NacosConst {
public static final String NAMING_LOAD_CACHE_AT_START = "namingLoadCacheAtStart";

public static final String NACOS_REGISTRY_NAME = "nacos-registry";

public static final String SCHEMA_PREFIX = "schema_";
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,20 @@ public boolean enabled(String application, String serviceName) {
@Override
public List<NacosDiscoveryInstance> findServiceInstances(String application, String serviceName) {
try {
List<Instance> instances = namingService.selectInstances(serviceName, application, true);
List<Instance> instances = namingService.getAllInstances(serviceName, application, true);
return convertServiceInstanceList(instances, application);
} catch (NacosException e) {
throw new IllegalStateException("updateMicroserviceInstanceStatus process is interrupted.");
}
}

private List<NacosDiscoveryInstance> convertServiceInstanceList(List<Instance> isntances, String application) {
if (CollectionUtils.isEmpty(isntances)) {
private List<NacosDiscoveryInstance> convertServiceInstanceList(List<Instance> instances, String application) {
if (CollectionUtils.isEmpty(instances)) {
return Collections.emptyList();
}
List<NacosDiscoveryInstance> result = new ArrayList<>();
for (Instance instance : isntances) {
result.add(new NacosDiscoveryInstance(instance, nacosDiscoveryProperties, application));
for (Instance instance : instances) {
result.add(new NacosDiscoveryInstance(instance, application, environment));
}
return result;
}
Expand All @@ -96,7 +96,7 @@ public void onInstanceChangedEvent(InstancesChangeEvent event) {

@Override
public void init() {
namingService = NamingServiceManager.buildNamingService(nacosDiscoveryProperties);
namingService = NamingServiceManager.buildNamingService(environment, nacosDiscoveryProperties);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,39 @@

package org.apache.servicecomb.registry.nacos;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.servicecomb.config.BootStrapProperties;
import org.apache.servicecomb.registry.api.AbstractDiscoveryInstance;
import org.apache.servicecomb.registry.api.DataCenterInfo;
import org.apache.servicecomb.registry.api.MicroserviceInstanceStatus;
import org.springframework.util.CollectionUtils;
import org.springframework.core.env.Environment;

import com.alibaba.nacos.api.naming.pojo.Instance;

public class NacosDiscoveryInstance extends AbstractDiscoveryInstance {
private final NacosDiscoveryProperties nacosDiscoveryProperties;

private final Instance instance;

private final Map<String, String> schemas = new HashMap<>();
private final String application;

private final Environment environment;

private final Map<String, String> schemas;

private String application;
private final List<String> endpoints;

public NacosDiscoveryInstance(Instance instance, NacosDiscoveryProperties nacosDiscoveryProperties,
String application) {
public NacosDiscoveryInstance(Instance instance, String application,
Environment environment) {
this.instance = instance;
this.nacosDiscoveryProperties = nacosDiscoveryProperties;
this.environment = environment;
this.application = application;
this.endpoints = readEndpoints(instance);
this.schemas = readSchemas(instance);
}

@Override
Expand All @@ -57,12 +64,12 @@ public String getRegistryName() {

@Override
public String getEnvironment() {
return nacosDiscoveryProperties.getNamespace();
return BootStrapProperties.readServiceEnvironment(environment);
}

@Override
public String getApplication() {
return application;
return this.application;
}

@Override
Expand All @@ -72,25 +79,26 @@ public String getServiceName() {

@Override
public String getAlias() {
return instance.getMetadata().get("alias");
return instance.getMetadata().get(NacosConst.PROPERTY_ALIAS);
}

@Override
public String getVersion() {
return instance.getMetadata().get("version");
return instance.getMetadata().get(NacosConst.PROPERTY_VERSION);
}

@Override
public DataCenterInfo getDataCenterInfo() {
DataCenterInfo dataCenterInfo = new DataCenterInfo();
dataCenterInfo.setRegion(instance.getMetadata().get("region"));
dataCenterInfo.setAvailableZone(instance.getMetadata().get("zone"));
dataCenterInfo.setRegion(instance.getMetadata().get(NacosConst.PROPERTY_REGION));
dataCenterInfo.setAvailableZone(instance.getMetadata().get(NacosConst.PROPERTY_ZONE));
dataCenterInfo.setName(instance.getMetadata().get(NacosConst.PROPERTY_DATACENTER));
return dataCenterInfo;
}

@Override
public String getDescription() {
return instance.getMetadata().get("description");
return instance.getMetadata().get(NacosConst.PROPERTY_DESCRIPTION);
}

@Override
Expand All @@ -100,31 +108,34 @@ public Map<String, String> getProperties() {

@Override
public Map<String, String> getSchemas() {
if (CollectionUtils.isEmpty(schemas)) {
Map<String, String> metaData = instance.getMetadata();
Map<String, String> instanceSchemas = new HashMap<>();
for (Map.Entry<String, String> entry: metaData.entrySet()) {
if (entry.getKey().startsWith(NacosConst.SCHEMA_PREFIX)) {
instanceSchemas.put(entry.getKey().substring(NacosConst.SCHEMA_PREFIX.length()), entry.getValue());
}
return schemas;
}

private static Map<String, String> readSchemas(Instance instance) {
Map<String, String> metaData = instance.getMetadata();
Map<String, String> instanceSchemas = new HashMap<>();
for (Map.Entry<String, String> entry : metaData.entrySet()) {
if (entry.getKey().startsWith(NacosConst.PROPERTY_SCHEMA_PREFIX)) {
instanceSchemas.put(entry.getKey().substring(NacosConst.PROPERTY_SCHEMA_PREFIX.length()),
entry.getValue());
}
schemas.putAll(instanceSchemas);
}
return schemas;
return instanceSchemas;
}

@Override
public List<String> getEndpoints() {
List<String> endpoints = new ArrayList<>();
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("rest://")
.append(instance.getIp())
.append(":")
.append(instance.getPort());
endpoints.add(stringBuilder.toString());
return endpoints;
}

private static List<String> readEndpoints(Instance instance) {
if (StringUtils.isEmpty(instance.getMetadata().get(NacosConst.PROPERTY_ENDPOINT))) {
return Collections.emptyList();
}
return Arrays.asList(instance.getMetadata().get(NacosConst.PROPERTY_ENDPOINT)
.split(NacosConst.ENDPOINT_PROPERTY_SEPARATOR));
}

@Override
public String getInstanceId() {
return instance.getInstanceId();
Expand Down
Loading

0 comments on commit ed60267

Please sign in to comment.