use of com.alibaba.cloud.nacos.NacosDiscoveryProperties in project rpc by makui12315.
the class RpcServerDiscoveryAutoConfiguration method nacosDiscoveryProperties.
@Bean
@ConditionalOnMissingBean
public NacosDiscoveryProperties nacosDiscoveryProperties(RpcServerProperties properties) {
NacosDiscoveryProperties nacosDiscoveryProperties = new NacosDiscoveryProperties();
final Map<String, String> metadata = nacosDiscoveryProperties.getMetadata();
metadata.put("rpcServerPort", properties.getPort().toString());
metadata.put("rpcServerId", properties.getNodeId().toString());
return nacosDiscoveryProperties;
}
use of com.alibaba.cloud.nacos.NacosDiscoveryProperties in project spring-cloud-alibaba by alibaba.
the class NacosServiceDiscoveryTest method testGetServices.
@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);
NacosServiceManager nacosServiceManager = mock(NacosServiceManager.class);
NamingService namingService = mock(NamingService.class);
when(nacosServiceManager.getNamingService(nacosDiscoveryProperties.getNacosProperties())).thenReturn(namingService);
when(nacosDiscoveryProperties.getGroup()).thenReturn("DEFAULT");
when(namingService.getServicesOfServer(eq(1), eq(Integer.MAX_VALUE), eq("DEFAULT"))).thenReturn(nacosServices);
NacosServiceDiscovery serviceDiscovery = new NacosServiceDiscovery(nacosDiscoveryProperties, nacosServiceManager);
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"));
}
use of com.alibaba.cloud.nacos.NacosDiscoveryProperties in project spring-cloud-alibaba by alibaba.
the class NacosServerListTests method testUpdateServers.
@Test
public void testUpdateServers() throws Exception {
ArrayList<Instance> instances = new ArrayList<>();
HashMap<String, String> map = new HashMap<>();
map.put("instanceNum", "1");
instances.add(NacosMockTest.serviceInstance("test-service", true, map));
NacosDiscoveryProperties nacosDiscoveryProperties = mock(NacosDiscoveryProperties.class);
NamingService namingService = mock(NamingService.class);
when(nacosDiscoveryProperties.namingServiceInstance()).thenReturn(namingService);
when(nacosDiscoveryProperties.getGroup()).thenReturn("DEFAULT");
when(namingService.selectInstances(eq("test-service"), eq("DEFAULT"), eq(true))).thenReturn(instances.stream().filter(Instance::isHealthy).collect(Collectors.toList()));
IClientConfig clientConfig = mock(IClientConfig.class);
when(clientConfig.getClientName()).thenReturn("test-service");
NacosServerList serverList = new NacosServerList(nacosDiscoveryProperties);
serverList.initWithNiwsConfig(clientConfig);
List<NacosServer> servers = serverList.getUpdatedListOfServers();
assertThat(servers).hasSize(1);
assertThat(servers.get(0).getInstance().isHealthy()).isEqualTo(true);
assertThat(servers.get(0).getInstance().getMetadata().get("instanceNum")).isEqualTo("1");
}
use of com.alibaba.cloud.nacos.NacosDiscoveryProperties in project hummer-framework by hummer-team.
the class HostByRandomWeightLoadBalancer method choose.
@Override
protected Mono<Instance> choose(Request request, String serviceId, List<Instance> instances) {
return Mono.fromCallable(() -> {
List<Instance> instancesToChoose = instances;
NacosDiscoveryProperties discoveryProperties = SpringApplicationContext.getBean(NacosDiscoveryProperties.class);
if (StringUtils.isNotBlank(discoveryProperties.getClusterName())) {
List<Instance> sameClusterInstances = instances.stream().filter(instance -> Objects.equals(discoveryProperties.getClusterName(), instance.getClusterName())).collect(Collectors.toList());
if (!CollectionUtils.isEmpty(sameClusterInstances)) {
instancesToChoose = sameClusterInstances;
} else {
LOGGER.warn("A cross-cluster call occurs,name = {}, clusterName = {}, instance = {}", serviceId, discoveryProperties.getClusterName(), instances);
}
}
// notice: 2.2.7-RELEASE ExtendBalancer.getHostByRandomWeight2(instancesToChoose)
return Balancer.getHostByRandomWeight(instancesToChoose);
});
}
use of com.alibaba.cloud.nacos.NacosDiscoveryProperties in project HOJ by HimitZH.
the class NacosConfig method nacosProperties.
/**
* 用于改变程序自动获取的本机ip
*/
@Bean
@Primary
public NacosDiscoveryProperties nacosProperties() {
NacosDiscoveryProperties nacosDiscoveryProperties = new NacosDiscoveryProperties();
// 此处我只改了ip,其他参数可以根据自己的需求改变
nacosDiscoveryProperties.setIp(IpUtils.getServiceIp());
HashMap<String, String> meta = new HashMap<>();
int max = cpuNum * 2 + 1;
if (maxTaskNum != -1) {
max = maxTaskNum;
}
meta.put("maxTaskNum", String.valueOf(max));
if (openRemoteJudge) {
max = (cpuNum * 2 + 1) * 2;
if (maxRemoteTaskNum != -1) {
max = maxRemoteTaskNum;
}
meta.put("maxRemoteTaskNum", String.valueOf(max));
}
meta.put("judgeName", name);
nacosDiscoveryProperties.setMetadata(meta);
if (!ip.equals("-1")) {
nacosDiscoveryProperties.setIp(ip);
}
nacosDiscoveryProperties.setPort(port);
nacosDiscoveryProperties.setService("hoj-judge-server");
return nacosDiscoveryProperties;
}
Aggregations