Search in sources :

Example 1 with NacosDiscoveryProperties

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;
}
Also used : NacosDiscoveryProperties(com.alibaba.cloud.nacos.NacosDiscoveryProperties) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 2 with 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"));
}
Also used : ListView(com.alibaba.nacos.api.naming.pojo.ListView) NacosServiceManager(com.alibaba.cloud.nacos.NacosServiceManager) NamingService(com.alibaba.nacos.api.naming.NamingService) NacosDiscoveryProperties(com.alibaba.cloud.nacos.NacosDiscoveryProperties) Test(org.junit.jupiter.api.Test)

Example 3 with NacosDiscoveryProperties

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");
}
Also used : Instance(com.alibaba.nacos.api.naming.pojo.Instance) HashMap(java.util.HashMap) NamingService(com.alibaba.nacos.api.naming.NamingService) ArrayList(java.util.ArrayList) IClientConfig(com.netflix.client.config.IClientConfig) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) NacosDiscoveryProperties(com.alibaba.cloud.nacos.NacosDiscoveryProperties) NacosMockTest(com.alibaba.cloud.nacos.test.NacosMockTest) Test(org.junit.Test)

Example 4 with NacosDiscoveryProperties

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);
    });
}
Also used : Response(org.springframework.cloud.client.loadbalancer.Response) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) SpringApplicationContext(com.hummer.core.SpringApplicationContext) Mono(reactor.core.publisher.Mono) StringUtils(org.apache.commons.lang3.StringUtils) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) NacosDiscoveryProperties(com.alibaba.cloud.nacos.NacosDiscoveryProperties) Component(org.springframework.stereotype.Component) List(java.util.List) ReactorServiceInstanceLoadBalancer(org.springframework.cloud.loadbalancer.core.ReactorServiceInstanceLoadBalancer) Instance(com.alibaba.nacos.api.naming.pojo.Instance) CollectionUtils(org.springframework.util.CollectionUtils) Request(org.springframework.cloud.client.loadbalancer.Request) ServiceInstance(org.springframework.cloud.client.ServiceInstance) Instance(com.alibaba.nacos.api.naming.pojo.Instance) ServiceInstance(org.springframework.cloud.client.ServiceInstance) NacosDiscoveryProperties(com.alibaba.cloud.nacos.NacosDiscoveryProperties)

Example 5 with NacosDiscoveryProperties

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;
}
Also used : HashMap(java.util.HashMap) NacosDiscoveryProperties(com.alibaba.cloud.nacos.NacosDiscoveryProperties) Primary(org.springframework.context.annotation.Primary) Bean(org.springframework.context.annotation.Bean)

Aggregations

NacosDiscoveryProperties (com.alibaba.cloud.nacos.NacosDiscoveryProperties)9 NamingService (com.alibaba.nacos.api.naming.NamingService)6 Instance (com.alibaba.nacos.api.naming.pojo.Instance)5 NacosMockTest (com.alibaba.cloud.nacos.test.NacosMockTest)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 Test (org.junit.Test)4 IClientConfig (com.netflix.client.config.IClientConfig)3 NacosServiceManager (com.alibaba.cloud.nacos.NacosServiceManager)2 Test (org.junit.jupiter.api.Test)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 ServiceInstance (org.springframework.cloud.client.ServiceInstance)2 Bean (org.springframework.context.annotation.Bean)2 NacosMockTest.serviceInstance (com.alibaba.cloud.nacos.test.NacosMockTest.serviceInstance)1 ListView (com.alibaba.nacos.api.naming.pojo.ListView)1 SpringApplicationContext (com.hummer.core.SpringApplicationContext)1 List (java.util.List)1 Objects (java.util.Objects)1 Collectors (java.util.stream.Collectors)1 StringUtils (org.apache.commons.lang3.StringUtils)1