forked from apolloconfig/apollo
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added nacos service discovery support (apolloconfig#3447)
- Loading branch information
Showing
11 changed files
with
233 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
apollo-adminservice/src/main/resources/application-nacosDiscovery.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
eureka.client.enabled=false | ||
spring.cloud.discovery.enabled=false | ||
#nacos enabled | ||
nacos.discovery.register.enabled=true | ||
nacos.discovery.auto-register=true | ||
nacos.discovery.register.service-name=apollo-adminservice |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...e/src/main/java/com/ctrip/framework/apollo/metaservice/service/NacosDiscoveryService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.ctrip.framework.apollo.metaservice.service; | ||
|
||
import com.alibaba.nacos.api.annotation.NacosInjected; | ||
import com.alibaba.nacos.api.exception.NacosException; | ||
import com.alibaba.nacos.api.naming.NamingService; | ||
import com.alibaba.nacos.api.naming.pojo.Instance; | ||
import com.ctrip.framework.apollo.core.dto.ServiceDTO; | ||
import com.google.common.collect.Lists; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
/** | ||
* @author : kl | ||
* Service discovery nacos implementation | ||
**/ | ||
@Service | ||
@Profile({"nacos-discovery"}) | ||
public class NacosDiscoveryService implements DiscoveryService { | ||
|
||
private final static Logger logger = LoggerFactory.getLogger(NacosDiscoveryService.class); | ||
|
||
private NamingService namingService; | ||
|
||
@NacosInjected | ||
public void setNamingService(NamingService namingService) { | ||
this.namingService = namingService; | ||
} | ||
|
||
@Override | ||
public List<ServiceDTO> getServiceInstances(String serviceId) { | ||
try { | ||
List<Instance> instances = namingService.selectInstances(serviceId,true); | ||
List<ServiceDTO> serviceDTOList = Lists.newLinkedList(); | ||
instances.forEach(instance -> { | ||
ServiceDTO serviceDTO = this.toServiceDTO(instance, serviceId); | ||
serviceDTOList.add(serviceDTO); | ||
}); | ||
return serviceDTOList; | ||
} catch (NacosException ex) { | ||
logger.error(ex.getMessage(),ex); | ||
} | ||
return Collections.emptyList(); | ||
} | ||
|
||
private ServiceDTO toServiceDTO(Instance instance, String appName) { | ||
ServiceDTO service = new ServiceDTO(); | ||
service.setAppName(appName); | ||
service.setInstanceId(instance.getInstanceId()); | ||
String homePageUrl = "http://" + instance.getIp() + ":" + instance.getPort() + "/"; | ||
service.setHomepageUrl(homePageUrl); | ||
return service; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
apollo-configservice/src/main/resources/application-nacos-discovery.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
apollo.eureka.server.enabled=false | ||
eureka.client.enabled=false | ||
spring.cloud.discovery.enabled=false | ||
#nacos enabled | ||
nacos.discovery.register.enabled=true | ||
nacos.discovery.auto-register=true | ||
nacos.discovery.register.service-name=apollo-configservice |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...c/test/java/com/ctrip/framework/apollo/metaservice/service/NacosDiscoveryServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.ctrip.framework.apollo.metaservice.service; | ||
|
||
import com.alibaba.nacos.api.naming.NamingService; | ||
import com.alibaba.nacos.api.naming.pojo.Instance; | ||
import com.ctrip.framework.apollo.core.dto.ServiceDTO; | ||
import com.google.common.collect.Lists; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
import java.util.List; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
/** | ||
* @author kl (http://kailing.pub) | ||
* @since 2020/12/21 | ||
*/ | ||
@RunWith(MockitoJUnitRunner.class) | ||
public class NacosDiscoveryServiceTest { | ||
|
||
private NacosDiscoveryService nacosDiscoveryService; | ||
|
||
@Mock | ||
private NamingService nacosNamingService; | ||
|
||
private String someServiceId; | ||
|
||
|
||
@Before | ||
public void setUp() throws Exception { | ||
nacosDiscoveryService = new NacosDiscoveryService(); | ||
nacosDiscoveryService.setNamingService(nacosNamingService); | ||
someServiceId = "someServiceId"; | ||
} | ||
|
||
@Test | ||
public void testGetServiceInstancesWithEmptyInstances() throws Exception { | ||
assertTrue(nacosNamingService.selectInstances(someServiceId, true).isEmpty()); | ||
} | ||
|
||
|
||
@Test | ||
public void testGetServiceInstancesWithInvalidServiceId() { | ||
assertTrue(nacosDiscoveryService.getServiceInstances(someServiceId).isEmpty()); | ||
} | ||
|
||
@Test | ||
public void testGetServiceInstances() throws Exception { | ||
String someIp = "1.2.3.4"; | ||
int somePort = 8080; | ||
String someInstanceId = "someInstanceId"; | ||
Instance someServiceInstance = mockServiceInstance(someInstanceId, someIp, somePort); | ||
|
||
when(nacosNamingService.selectInstances(someServiceId, true)).thenReturn( | ||
Lists.newArrayList(someServiceInstance)); | ||
|
||
List<ServiceDTO> serviceDTOList = nacosDiscoveryService.getServiceInstances(someServiceId); | ||
ServiceDTO serviceDTO = serviceDTOList.get(0); | ||
assertEquals(1, serviceDTOList.size()); | ||
assertEquals(someServiceId, serviceDTO.getAppName()); | ||
assertEquals("http://1.2.3.4:8080/", serviceDTO.getHomepageUrl()); | ||
|
||
} | ||
|
||
private Instance mockServiceInstance(String instanceId, String ip, int port) { | ||
Instance serviceInstance = mock(Instance.class); | ||
when(serviceInstance.getInstanceId()).thenReturn(instanceId); | ||
when(serviceInstance.getIp()).thenReturn(ip); | ||
when(serviceInstance.getPort()).thenReturn(port); | ||
|
||
return serviceInstance; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters