Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added nacos service discovery support #3447

Merged
merged 23 commits into from
Jan 3, 2021
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
fe75b4d
add tech-support-qq-4.png
klboke May 16, 2019
99bf97a
Update README.md
klboke May 16, 2019
1579f41
Merge remote-tracking branch 'upstream/master'
klboke Nov 11, 2020
9f65eed
Merge remote-tracking branch 'upstream/master'
klboke Dec 9, 2020
d7d3fd9
Enhance the user experience in the scenario of submitting duplicate keys
klboke Dec 11, 2020
7329fab
Merge branch 'master' into master
nobodyiam Dec 12, 2020
5def448
Modify the key-value conflict exception prompt, adjust the code style
klboke Dec 12, 2020
6942564
Merge branch 'master' into master
nobodyiam Dec 12, 2020
0fc1f91
Merge remote-tracking branch 'upstream/master'
klboke Dec 19, 2020
61ad016
Merge remote-tracking branch 'origin/master'
klboke Dec 19, 2020
e12a480
Added nacos service discovery support
klboke Dec 19, 2020
da1b764
add NacosDiscoveryServiceTest.java
klboke Dec 21, 2020
02b65cb
use logger print Nacosxception
klboke Dec 21, 2020
6ecfbe6
Fix UnnecessaryStubbingException thrown by NacosDiscoveryServiceTest
klboke Dec 21, 2020
03f6e35
Nacosdiscovery is disabled by default
klboke Dec 21, 2020
bdc9a5b
Isolate nacos service discovery dependencies through maven profile
klboke Dec 29, 2020
1cfe1e0
Modify the nacos service discovery activation naming style
klboke Jan 2, 2021
3262be4
Modify nacos service discovery usage document
klboke Jan 2, 2021
1a87aea
Update distributed-deployment-guide.md
nobodyiam Jan 3, 2021
0e308df
Update HomePageController.java
nobodyiam Jan 3, 2021
fa710d2
Update DefaultDiscoveryService.java
nobodyiam Jan 3, 2021
4af2656
Update distributed-deployment-guide.md
nobodyiam Jan 3, 2021
da6b767
Merge branch 'master' into nacos
nobodyiam Jan 3, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions apollo-adminservice/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,21 @@
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>nacos-discovery</id>
<dependencies>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-discovery-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
</dependencies>
</profile>
</profiles>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
eureka.client.enabled=false
klboke marked this conversation as resolved.
Show resolved Hide resolved
spring.cloud.discovery.enabled=false
#nacos enabled
nacos.discovery.register.enabled=true
nacos.discovery.auto-register=true
nacos.discovery.register.service-name=apollo-adminservice
22 changes: 22 additions & 0 deletions apollo-configservice/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@
</exclusions>
</dependency>
<!-- end of eureka -->
<dependency>
klboke marked this conversation as resolved.
Show resolved Hide resolved
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-api</artifactId>
<version>${nacos-discovery-api.version}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
Expand Down Expand Up @@ -145,4 +150,21 @@
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>nacos-discovery</id>
<dependencies>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-discovery-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
</dependencies>
</profile>
</profiles>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@


/**
* For kubernetes discovery service, there is no eureka home page, so we need to add a default one
* For non-eureka discovery services such as kubernetes and nacos, there is no eureka home page, so we need to add a default one
*/
@Profile({"kubernetes"})
@Profile({"kubernetes","nacos-discovery"})
@RestController
public class KubernetesHomePageController {
public class HomePageController {
private final DiscoveryService discoveryService;

public KubernetesHomePageController(DiscoveryService discoveryService) {
public HomePageController(DiscoveryService discoveryService) {
this.discoveryService = discoveryService;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* Default discovery service for Eureka
*/
@Service
@ConditionalOnMissingProfile({"kubernetes"})
@ConditionalOnMissingProfile({"kubernetes","nacos-discovery"})
public class DefaultDiscoveryService implements DiscoveryService {

private final EurekaClient eurekaClient;
Expand Down
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 {
klboke marked this conversation as resolved.
Show resolved Hide resolved

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;
}
}
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class KubernetesHomePageControllerTest {
public class HomePageControllerTest {

@Mock
private DiscoveryService discoveryService;

private KubernetesHomePageController homePageController;
private HomePageController homePageController;

@Before
public void setUp() throws Exception {
homePageController = new KubernetesHomePageController(discoveryService);
homePageController = new HomePageController(discoveryService);
}

@Test
Expand Down
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;
}

}
14 changes: 13 additions & 1 deletion docs/zh/deployment/distributed-deployment-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,19 @@ META_SERVERS_OPTS="-Ddev_meta=$dev_meta -Dfat_meta=$fat_meta -Duat_meta=$uat_met

位于`apollo-portal/target/`目录下的`apollo-portal-x.x.x-github.zip`

### 2.2.2 部署Apollo服务端
klboke marked this conversation as resolved.
Show resolved Hide resolved
启用外部nacos服务注册中心替换内置eureka
klboke marked this conversation as resolved.
Show resolved Hide resolved

##### 2.2.1.2.7 启用外部nacos服务注册中心替换内置eureka

1.修改build.sh,将maven命令更改为
klboke marked this conversation as resolved.
Show resolved Hide resolved
```shell
mvn clean package -Pgithub,nacos-discovery -DskipTests -pl apollo-configservice,apollo-adminservice -am -Dapollo_profile=github,nacos-discovery -Dspring_datasource_url=$apollo_config_db_url -Dspring_datasource_username=$apollo_config_db_username -Dspring_datasource_password=$apollo_config_db_password

```
2.在config目录下修改application-github.properties,添加nacos服务器地址
klboke marked this conversation as resolved.
Show resolved Hide resolved
```properties
nacos.discovery.server-addr=127.0.0.1:8848
```

#### 2.2.2.1 部署apollo-configservice
将对应环境的`apollo-configservice-x.x.x-github.zip`上传到服务器上,解压后执行scripts/startup.sh即可。如需停止服务,执行scripts/shutdown.sh.
Expand Down
22 changes: 22 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
<javax.activation.version>1.1.1</javax.activation.version>
<javax.mail.version>1.6.2</javax.mail.version>
<javassist.version>3.23.1-GA</javassist.version>
<nacos-discovery-api.version>1.4.0</nacos-discovery-api.version>
<!-- Plugins Version -->
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
Expand Down Expand Up @@ -815,6 +816,27 @@
</dependency>
</dependencies>
</profile>
<profile>
<id>nacos-discovery</id>
<properties>
<nacos.discovery.version>0.2.7</nacos.discovery.version>
<fastjson.version>1.2.75</fastjson.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-discovery-spring-boot-starter</artifactId>
<version>${nacos.discovery.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</profile>
<profile>
<!-- for ctrip development with logging capability -->
<id>ctrip-logging</id>
Expand Down