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 11 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
4 changes: 4 additions & 0 deletions apollo-adminservice/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
klboke marked this conversation as resolved.
Show resolved Hide resolved
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-discovery-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
spring.datasource.url = ${spring_datasource_url}
spring.datasource.username = ${spring_datasource_username}
spring.datasource.password = ${spring_datasource_password}

#nacos discovery
#nacos.discovery.server-addr=127.0.0.1:8848
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
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.serviceName=apollo-adminservice
klboke marked this conversation as resolved.
Show resolved Hide resolved
nacos.discovery.register.metadata.apollo.serviceName=apollo-adminservice
4 changes: 4 additions & 0 deletions apollo-configservice/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
</exclusions>
</dependency>
<!-- end of eureka -->
<dependency>
klboke marked this conversation as resolved.
Show resolved Hide resolved
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-discovery-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
spring.datasource.url = ${spring_datasource_url}
spring.datasource.username = ${spring_datasource_username}
spring.datasource.password = ${spring_datasource_password}

#nacos discovery
#nacos.discovery.server-addr=127.0.0.1:8848
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 kubernetes,nacos discovery service, there is no eureka home page, so we need to add a default one
klboke marked this conversation as resolved.
Show resolved Hide resolved
*/
@Profile({"kubernetes"})
@Profile({"kubernetes","nacosDiscovery"})
klboke marked this conversation as resolved.
Show resolved Hide resolved
klboke marked this conversation as resolved.
Show resolved Hide resolved
@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 @@ -29,9 +29,7 @@ public List<ServiceDTO> getMetaService() {
}

@RequestMapping("/config")
public List<ServiceDTO> getConfigService(
klboke marked this conversation as resolved.
Show resolved Hide resolved
@RequestParam(value = "appId", defaultValue = "") String appId,
@RequestParam(value = "ip", required = false) String clientIp) {
public List<ServiceDTO> getConfigService() {
return discoveryService.getServiceInstances(ServiceNameConsts.APOLLO_CONFIGSERVICE);
}

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","nacosDiscovery"})
klboke marked this conversation as resolved.
Show resolved Hide resolved
public class DefaultDiscoveryService implements DiscoveryService {

private final EurekaClient eurekaClient;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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.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({"nacosDiscovery"})
klboke marked this conversation as resolved.
Show resolved Hide resolved
public class NacosDiscoveryService implements DiscoveryService {
klboke marked this conversation as resolved.
Show resolved Hide resolved

private static final String SERVICE_NAME_METADATA_KEY = "apollo.serviceName";

@NacosInjected
private NamingService namingService;

@Override
public List<ServiceDTO> getServiceInstances(String serviceId) {
try {
List<Instance> instances = namingService.getAllInstances(serviceId);
List<ServiceDTO> serviceDTOList = Lists.newLinkedList();
instances.forEach(instance -> {
ServiceDTO serviceDTO = this.toServiceDTO(instance);
serviceDTOList.add(serviceDTO);
});
return serviceDTOList;
} catch (NacosException e) {
e.printStackTrace();
klboke marked this conversation as resolved.
Show resolved Hide resolved
}
return Collections.emptyList();
}

private ServiceDTO toServiceDTO(Instance instance) {
ServiceDTO service = new ServiceDTO();
String appName = instance.getMetadata().get(SERVICE_NAME_METADATA_KEY);
klboke marked this conversation as resolved.
Show resolved Hide resolved
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,8 @@
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.serviceName=apollo-configservice
nacos.discovery.register.metadata.apollo.serviceName=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
Expand Up @@ -42,7 +42,7 @@ public void testGetConfigService() {
when(discoveryService.getServiceInstances(ServiceNameConsts.APOLLO_CONFIGSERVICE))
.thenReturn(someServices);

assertEquals(someServices, serviceController.getConfigService(someAppId, someClientIp));
assertEquals(someServices, serviceController.getConfigService());
}

@Test
Expand Down
16 changes: 16 additions & 0 deletions docs/zh/deployment/distributed-deployment-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,22 @@ pro.meta=http://apollo.xxx.com

>注2: meta service地址也可以填入IP,0.11.0版本之前只支持填入一个IP。从0.11.0版本开始支持填入以逗号分隔的多个地址([PR #1214](https://github.com/ctripcorp/apollo/pull/1214)),如`http://1.1.1.1:8080,http://2.2.2.2:8080`,不过生产环境还是建议使用域名(走slb),因为机器扩容、缩容等都可能导致IP列表的变化。

###### 2.2.1.1.2.5 启用外部nacos服务注册中心替换内置eureka
klboke marked this conversation as resolved.
Show resolved Hide resolved
启用外部的nacos服务注册中心分两步走,configService和adminService的操作一样,如下:
- 1、修改VM options,激活nacosDiscovery的profile,如:
```shell
-Dapollo_profile=github,auth,nacosDiscovery
klboke marked this conversation as resolved.
Show resolved Hide resolved
```
- 2、前往application-github.properties配置nacos连接地址,如:
klboke marked this conversation as resolved.
Show resolved Hide resolved
```properties
#nacos discovery
nacos.discovery.server-addr=127.0.0.1:8848
```
也可采用VM options的方式配置,如:
```
-Dnacos.discovery.server-addr=127.0.0.1:8848
```

#### 2.2.1.2 通过源码构建

##### 2.2.1.2.1 配置数据库连接信息
Expand Down
6 changes: 6 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.version>0.2.7</nacos.discovery.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 @@ -261,6 +262,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-discovery-spring-boot-starter</artifactId>
<version>${nacos.discovery.version}</version>
</dependency>
<!--third party -->
<dependency>
<groupId>org.slf4j</groupId>
Expand Down