Merge pull request #990 from echooymxq/feature-nacos-reactive
Support Nacos reactive service discoverypull/1050/head
commit
c52cbfb92d
@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013-2018 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.alibaba.cloud.nacos.discovery;
|
||||||
|
|
||||||
|
import com.alibaba.cloud.nacos.ConditionalOnNacosDiscoveryEnabled;
|
||||||
|
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
|
||||||
|
import com.alibaba.cloud.nacos.NacosNamingManager;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
|
import org.springframework.cloud.client.ConditionalOnDiscoveryEnabled;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author <a href="mailto:echooy.mxq@gmail.com">echooymxq</a>
|
||||||
|
**/
|
||||||
|
@Configuration
|
||||||
|
@ConditionalOnDiscoveryEnabled
|
||||||
|
@ConditionalOnNacosDiscoveryEnabled
|
||||||
|
public class NacosDiscoveryAutoConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public NacosNamingManager nacosNamingManager() {
|
||||||
|
return new NacosNamingManager();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@ConditionalOnMissingBean
|
||||||
|
public NacosDiscoveryProperties nacosProperties() {
|
||||||
|
return new NacosDiscoveryProperties();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@ConditionalOnMissingBean
|
||||||
|
public NacosServiceDiscovery nacosServiceDiscovery(
|
||||||
|
NacosNamingManager nacosNamingManager,
|
||||||
|
NacosDiscoveryProperties discoveryProperties) {
|
||||||
|
return new NacosServiceDiscovery(nacosNamingManager, discoveryProperties);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,110 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013-2018 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.alibaba.cloud.nacos.discovery;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
|
||||||
|
import com.alibaba.cloud.nacos.NacosNamingManager;
|
||||||
|
import com.alibaba.cloud.nacos.NacosServiceInstance;
|
||||||
|
import com.alibaba.nacos.api.exception.NacosException;
|
||||||
|
import com.alibaba.nacos.api.naming.pojo.Instance;
|
||||||
|
import com.alibaba.nacos.api.naming.pojo.ListView;
|
||||||
|
|
||||||
|
import org.springframework.cloud.client.ServiceInstance;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author <a href="mailto:echooy.mxq@gmail.com">echooymxq</a>
|
||||||
|
**/
|
||||||
|
public class NacosServiceDiscovery {
|
||||||
|
|
||||||
|
private NacosNamingManager nacosNamingManager;
|
||||||
|
|
||||||
|
private NacosDiscoveryProperties discoveryProperties;
|
||||||
|
|
||||||
|
public NacosServiceDiscovery(NacosNamingManager nacosNamingManager,
|
||||||
|
NacosDiscoveryProperties discoveryProperties) {
|
||||||
|
this.nacosNamingManager = nacosNamingManager;
|
||||||
|
this.discoveryProperties = discoveryProperties;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return all instances for the given service.
|
||||||
|
* @param serviceId id of service
|
||||||
|
* @return list of instances
|
||||||
|
* @throws NacosException nacosException
|
||||||
|
*/
|
||||||
|
public List<ServiceInstance> getInstances(String serviceId) throws NacosException {
|
||||||
|
String group = discoveryProperties.getGroup();
|
||||||
|
List<Instance> instances = nacosNamingManager.getNamingService()
|
||||||
|
.selectInstances(serviceId, group, true);
|
||||||
|
return hostToServiceInstanceList(instances, serviceId);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the names of all services.
|
||||||
|
* @return list of service names
|
||||||
|
* @throws NacosException nacosException
|
||||||
|
*/
|
||||||
|
public List<String> getServices() throws NacosException {
|
||||||
|
String group = discoveryProperties.getGroup();
|
||||||
|
ListView<String> services = nacosNamingManager.getNamingService()
|
||||||
|
.getServicesOfServer(1, Integer.MAX_VALUE, group);
|
||||||
|
return services.getData();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<ServiceInstance> hostToServiceInstanceList(
|
||||||
|
List<Instance> instances, String serviceId) {
|
||||||
|
List<ServiceInstance> result = new ArrayList<>(instances.size());
|
||||||
|
for (Instance instance : instances) {
|
||||||
|
ServiceInstance serviceInstance = hostToServiceInstance(instance, serviceId);
|
||||||
|
if (serviceInstance != null) {
|
||||||
|
result.add(serviceInstance);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ServiceInstance hostToServiceInstance(Instance instance,
|
||||||
|
String serviceId) {
|
||||||
|
if (instance == null || !instance.isEnabled() || !instance.isHealthy()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
NacosServiceInstance nacosServiceInstance = new NacosServiceInstance();
|
||||||
|
nacosServiceInstance.setHost(instance.getIp());
|
||||||
|
nacosServiceInstance.setPort(instance.getPort());
|
||||||
|
nacosServiceInstance.setServiceId(serviceId);
|
||||||
|
|
||||||
|
Map<String, String> metadata = new HashMap<>();
|
||||||
|
metadata.put("nacos.instanceId", instance.getInstanceId());
|
||||||
|
metadata.put("nacos.weight", instance.getWeight() + "");
|
||||||
|
metadata.put("nacos.healthy", instance.isHealthy() + "");
|
||||||
|
metadata.put("nacos.cluster", instance.getClusterName() + "");
|
||||||
|
metadata.putAll(instance.getMetadata());
|
||||||
|
nacosServiceInstance.setMetadata(metadata);
|
||||||
|
|
||||||
|
if (metadata.containsKey("secure")) {
|
||||||
|
boolean secure = Boolean.parseBoolean(metadata.get("secure"));
|
||||||
|
nacosServiceInstance.setSecure(secure);
|
||||||
|
}
|
||||||
|
return nacosServiceInstance;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,82 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013-2018 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.alibaba.cloud.nacos.discovery.reactive;
|
||||||
|
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
import com.alibaba.cloud.nacos.discovery.NacosServiceDiscovery;
|
||||||
|
import com.alibaba.nacos.api.exception.NacosException;
|
||||||
|
import org.reactivestreams.Publisher;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import reactor.core.publisher.Flux;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
import org.springframework.cloud.client.ServiceInstance;
|
||||||
|
import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author <a href="mailto:echooy.mxq@gmail.com">echooymxq</a>
|
||||||
|
**/
|
||||||
|
public class NacosReactiveDiscoveryClient implements ReactiveDiscoveryClient {
|
||||||
|
|
||||||
|
private static final Logger log = LoggerFactory
|
||||||
|
.getLogger(NacosReactiveDiscoveryClient.class);
|
||||||
|
|
||||||
|
private NacosServiceDiscovery serviceDiscovery;
|
||||||
|
|
||||||
|
public NacosReactiveDiscoveryClient(NacosServiceDiscovery nacosServiceDiscovery) {
|
||||||
|
this.serviceDiscovery = nacosServiceDiscovery;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String description() {
|
||||||
|
return "Spring Cloud Nacos Reactive Discovery Client";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Flux<ServiceInstance> getInstances(String serviceId) {
|
||||||
|
|
||||||
|
return Mono.justOrEmpty(serviceId).flatMapMany(loadInstancesFromNacos());
|
||||||
|
}
|
||||||
|
|
||||||
|
private Function<String, Publisher<ServiceInstance>> loadInstancesFromNacos() {
|
||||||
|
return serviceId -> {
|
||||||
|
try {
|
||||||
|
return Flux.fromIterable(serviceDiscovery.getInstances(serviceId));
|
||||||
|
}
|
||||||
|
catch (NacosException e) {
|
||||||
|
log.error("get service instance[{}] from nacos error!", serviceId, e);
|
||||||
|
return Flux.empty();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Flux<String> getServices() {
|
||||||
|
return Flux.defer(() -> {
|
||||||
|
try {
|
||||||
|
return Flux.fromIterable(serviceDiscovery.getServices());
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
log.error("get services from nacos server fail,", e);
|
||||||
|
return Flux.empty();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,52 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013-2018 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.alibaba.cloud.nacos.discovery.reactive;
|
||||||
|
|
||||||
|
import com.alibaba.cloud.nacos.ConditionalOnNacosDiscoveryEnabled;
|
||||||
|
import com.alibaba.cloud.nacos.discovery.NacosDiscoveryAutoConfiguration;
|
||||||
|
import com.alibaba.cloud.nacos.discovery.NacosServiceDiscovery;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||||
|
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
|
import org.springframework.cloud.client.ConditionalOnDiscoveryEnabled;
|
||||||
|
import org.springframework.cloud.client.ConditionalOnReactiveDiscoveryEnabled;
|
||||||
|
import org.springframework.cloud.client.ReactiveCommonsClientAutoConfiguration;
|
||||||
|
import org.springframework.cloud.client.discovery.composite.reactive.ReactiveCompositeDiscoveryClientAutoConfiguration;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author <a href="mailto:echooy.mxq@gmail.com">echooymxq</a>
|
||||||
|
**/
|
||||||
|
@Configuration
|
||||||
|
@ConditionalOnDiscoveryEnabled
|
||||||
|
@ConditionalOnReactiveDiscoveryEnabled
|
||||||
|
@ConditionalOnNacosDiscoveryEnabled
|
||||||
|
@AutoConfigureAfter({ NacosDiscoveryAutoConfiguration.class,
|
||||||
|
ReactiveCompositeDiscoveryClientAutoConfiguration.class })
|
||||||
|
@AutoConfigureBefore({ ReactiveCommonsClientAutoConfiguration.class })
|
||||||
|
public class NacosReactiveDiscoveryClientConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@ConditionalOnMissingBean
|
||||||
|
public NacosReactiveDiscoveryClient nacosReactiveDiscoveryClient(
|
||||||
|
NacosServiceDiscovery nacosServiceDiscovery) {
|
||||||
|
return new NacosReactiveDiscoveryClient(nacosServiceDiscovery);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,8 +1,10 @@
|
|||||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||||
com.alibaba.cloud.nacos.NacosDiscoveryAutoConfiguration,\
|
com.alibaba.cloud.nacos.discovery.NacosDiscoveryAutoConfiguration,\
|
||||||
com.alibaba.cloud.nacos.ribbon.RibbonNacosAutoConfiguration,\
|
com.alibaba.cloud.nacos.ribbon.RibbonNacosAutoConfiguration,\
|
||||||
com.alibaba.cloud.nacos.endpoint.NacosDiscoveryEndpointAutoConfiguration,\
|
com.alibaba.cloud.nacos.endpoint.NacosDiscoveryEndpointAutoConfiguration,\
|
||||||
com.alibaba.cloud.nacos.discovery.NacosDiscoveryClientAutoConfiguration,\
|
com.alibaba.cloud.nacos.registry.NacosServiceRegistryAutoConfiguration,\
|
||||||
|
com.alibaba.cloud.nacos.discovery.NacosDiscoveryClientConfiguration,\
|
||||||
|
com.alibaba.cloud.nacos.discovery.reactive.NacosReactiveDiscoveryClientConfiguration,\
|
||||||
com.alibaba.cloud.nacos.discovery.configclient.NacosConfigServerAutoConfiguration
|
com.alibaba.cloud.nacos.discovery.configclient.NacosConfigServerAutoConfiguration
|
||||||
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
|
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
|
||||||
com.alibaba.cloud.nacos.discovery.configclient.NacosDiscoveryClientConfigServiceBootstrapConfiguration
|
com.alibaba.cloud.nacos.discovery.configclient.NacosDiscoveryClientConfigServiceBootstrapConfiguration
|
||||||
|
@ -0,0 +1,47 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013-2018 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.alibaba.cloud.nacos.discovery;
|
||||||
|
|
||||||
|
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
|
||||||
|
import com.alibaba.cloud.nacos.NacosNamingManager;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||||
|
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||||
|
import org.springframework.cloud.commons.util.UtilAutoConfiguration;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author <a href="mailto:echooy.mxq@gmail.com">echooymxq</a>
|
||||||
|
**/
|
||||||
|
public class NacosDiscoveryAutoConfigurationTests {
|
||||||
|
|
||||||
|
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||||
|
.withConfiguration(AutoConfigurations.of(UtilAutoConfiguration.class,
|
||||||
|
NacosDiscoveryAutoConfiguration.class));
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDefaultInitialization() {
|
||||||
|
contextRunner.run(context -> {
|
||||||
|
assertThat(context).hasSingleBean(NacosDiscoveryProperties.class);
|
||||||
|
assertThat(context).hasSingleBean(NacosNamingManager.class);
|
||||||
|
assertThat(context).hasSingleBean(NacosServiceDiscovery.class);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013-2018 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.alibaba.cloud.nacos.discovery;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||||
|
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||||
|
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||||
|
import org.springframework.cloud.commons.util.UtilAutoConfiguration;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author <a href="mailto:echooy.mxq@gmail.com">echooymxq</a>
|
||||||
|
**/
|
||||||
|
public class NacosDiscoveryClientConfigurationTest {
|
||||||
|
|
||||||
|
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||||
|
.withConfiguration(AutoConfigurations.of(UtilAutoConfiguration.class,
|
||||||
|
NacosDiscoveryAutoConfiguration.class,
|
||||||
|
NacosDiscoveryClientConfiguration.class));
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDefaultInitialization() {
|
||||||
|
contextRunner.run(context -> {
|
||||||
|
assertThat(context).hasSingleBean(DiscoveryClient.class);
|
||||||
|
assertThat(context).hasSingleBean(NacosWatch.class);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDiscoveryBlockingDisabled() {
|
||||||
|
contextRunner.withPropertyValues("spring.cloud.discovery.blocking.enabled=false")
|
||||||
|
.run(context -> {
|
||||||
|
assertThat(context).doesNotHaveBean(DiscoveryClient.class);
|
||||||
|
assertThat(context).doesNotHaveBean(NacosWatch.class);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,135 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013-2018 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.alibaba.cloud.nacos.discovery;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
|
||||||
|
import com.alibaba.cloud.nacos.NacosNamingManager;
|
||||||
|
import com.alibaba.nacos.api.exception.NacosException;
|
||||||
|
import com.alibaba.nacos.api.naming.NamingService;
|
||||||
|
import com.alibaba.nacos.api.naming.pojo.Instance;
|
||||||
|
import com.alibaba.nacos.api.naming.pojo.ListView;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import org.springframework.cloud.client.ServiceInstance;
|
||||||
|
|
||||||
|
import static com.alibaba.cloud.nacos.test.NacosMockTest.serviceInstance;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author xiaojing
|
||||||
|
* @author echooymxq
|
||||||
|
**/
|
||||||
|
public class NacosServiceDiscoveryTest {
|
||||||
|
|
||||||
|
private String host = "123.123.123.123";
|
||||||
|
|
||||||
|
private int port = 8888;
|
||||||
|
|
||||||
|
private String serviceName = "test-service";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetInstances() throws NacosException {
|
||||||
|
ArrayList<Instance> instances = new ArrayList<>();
|
||||||
|
|
||||||
|
HashMap<String, String> map = new HashMap<>();
|
||||||
|
map.put("test-key", "test-value");
|
||||||
|
map.put("secure", "true");
|
||||||
|
|
||||||
|
instances.add(serviceInstance(serviceName, true, host, port, map));
|
||||||
|
|
||||||
|
NacosDiscoveryProperties nacosDiscoveryProperties = mock(
|
||||||
|
NacosDiscoveryProperties.class);
|
||||||
|
|
||||||
|
NacosNamingManager nacosNamingManager = mock(NacosNamingManager.class);
|
||||||
|
|
||||||
|
NamingService namingService = mock(NamingService.class);
|
||||||
|
|
||||||
|
when(nacosNamingManager.getNamingService()).thenReturn(namingService);
|
||||||
|
when(nacosDiscoveryProperties.getGroup()).thenReturn("DEFAULT");
|
||||||
|
when(namingService.selectInstances(eq(serviceName), eq("DEFAULT"), eq(true)))
|
||||||
|
.thenReturn(instances);
|
||||||
|
|
||||||
|
NacosServiceDiscovery serviceDiscovery = new NacosServiceDiscovery(
|
||||||
|
nacosNamingManager, nacosDiscoveryProperties);
|
||||||
|
|
||||||
|
List<ServiceInstance> serviceInstances = serviceDiscovery
|
||||||
|
.getInstances(serviceName);
|
||||||
|
|
||||||
|
assertThat(serviceInstances.size()).isEqualTo(1);
|
||||||
|
|
||||||
|
ServiceInstance serviceInstance = serviceInstances.get(0);
|
||||||
|
|
||||||
|
assertThat(serviceInstance.getServiceId()).isEqualTo(serviceName);
|
||||||
|
assertThat(serviceInstance.getHost()).isEqualTo(host);
|
||||||
|
assertThat(serviceInstance.getPort()).isEqualTo(port);
|
||||||
|
assertThat(serviceInstance.isSecure()).isEqualTo(true);
|
||||||
|
assertThat(serviceInstance.getUri().toString())
|
||||||
|
.isEqualTo(getUri(serviceInstance));
|
||||||
|
assertThat(serviceInstance.getMetadata().get("test-key")).isEqualTo("test-value");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetServices() throws NacosException {
|
||||||
|
ListView<String> nacosServices = new ListView<>();
|
||||||
|
|
||||||
|
nacosServices.setData(new LinkedList<>());
|
||||||
|
|
||||||
|
nacosServices.getData().add(serviceName + "1");
|
||||||
|
nacosServices.getData().add(serviceName + "2");
|
||||||
|
nacosServices.getData().add(serviceName + "3");
|
||||||
|
|
||||||
|
NacosDiscoveryProperties nacosDiscoveryProperties = mock(
|
||||||
|
NacosDiscoveryProperties.class);
|
||||||
|
|
||||||
|
NacosNamingManager nacosNamingManager = mock(NacosNamingManager.class);
|
||||||
|
|
||||||
|
NamingService namingService = mock(NamingService.class);
|
||||||
|
|
||||||
|
when(nacosNamingManager.getNamingService()).thenReturn(namingService);
|
||||||
|
when(nacosDiscoveryProperties.getGroup()).thenReturn("DEFAULT");
|
||||||
|
when(namingService.getServicesOfServer(eq(1), eq(Integer.MAX_VALUE),
|
||||||
|
eq("DEFAULT"))).thenReturn(nacosServices);
|
||||||
|
|
||||||
|
NacosServiceDiscovery serviceDiscovery = new NacosServiceDiscovery(
|
||||||
|
nacosNamingManager, nacosDiscoveryProperties);
|
||||||
|
|
||||||
|
List<String> services = serviceDiscovery.getServices();
|
||||||
|
|
||||||
|
assertThat(services.size()).isEqualTo(3);
|
||||||
|
assertThat(services.contains(serviceName + "1"));
|
||||||
|
assertThat(services.contains(serviceName + "2"));
|
||||||
|
assertThat(services.contains(serviceName + "3"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getUri(ServiceInstance instance) {
|
||||||
|
|
||||||
|
if (instance.isSecure()) {
|
||||||
|
return "https://" + host + ":" + port;
|
||||||
|
}
|
||||||
|
|
||||||
|
return "http://" + host + ":" + port;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013-2018 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.alibaba.cloud.nacos.discovery.reactive;
|
||||||
|
|
||||||
|
import com.alibaba.cloud.nacos.discovery.NacosDiscoveryAutoConfiguration;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||||
|
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||||
|
import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient;
|
||||||
|
import org.springframework.cloud.commons.util.UtilAutoConfiguration;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author <a href="mailto:echooy.mxq@gmail.com">echooymxq</a>
|
||||||
|
**/
|
||||||
|
public class NacosReactiveDiscoveryClientConfigurationTests {
|
||||||
|
|
||||||
|
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||||
|
.withConfiguration(AutoConfigurations.of(UtilAutoConfiguration.class,
|
||||||
|
NacosDiscoveryAutoConfiguration.class,
|
||||||
|
NacosReactiveDiscoveryClientConfiguration.class));
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDefaultInitialization() {
|
||||||
|
contextRunner.run(context -> assertThat(context)
|
||||||
|
.hasSingleBean(ReactiveDiscoveryClient.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,74 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013-2018 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.alibaba.cloud.nacos.discovery.reactive;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import com.alibaba.cloud.nacos.discovery.NacosServiceDiscovery;
|
||||||
|
import com.alibaba.nacos.api.exception.NacosException;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.mockito.InjectMocks;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
import reactor.core.publisher.Flux;
|
||||||
|
import reactor.test.StepVerifier;
|
||||||
|
|
||||||
|
import org.springframework.cloud.client.ServiceInstance;
|
||||||
|
|
||||||
|
import static java.util.Collections.singletonList;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author <a href="mailto:echooy.mxq@gmail.com">echooymxq</a>
|
||||||
|
**/
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
class NacosReactiveDiscoveryClientTests {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private NacosServiceDiscovery serviceDiscovery;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private ServiceInstance serviceInstance;
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
|
private NacosReactiveDiscoveryClient client;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetInstances() throws NacosException {
|
||||||
|
|
||||||
|
when(serviceDiscovery.getInstances("reactive-service"))
|
||||||
|
.thenReturn(singletonList(serviceInstance));
|
||||||
|
|
||||||
|
Flux<ServiceInstance> instances = this.client.getInstances("reactive-service");
|
||||||
|
|
||||||
|
StepVerifier.create(instances).expectNextCount(1).expectComplete().verify();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetServices() throws NacosException {
|
||||||
|
|
||||||
|
when(serviceDiscovery.getServices())
|
||||||
|
.thenReturn(Arrays.asList("reactive-service1", "reactive-service2"));
|
||||||
|
|
||||||
|
Flux<String> services = this.client.getServices();
|
||||||
|
|
||||||
|
StepVerifier.create(services).expectNext("reactive-service1", "reactive-service2")
|
||||||
|
.expectComplete().verify();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue