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 support for consul service discovery #3575

Merged
merged 16 commits into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
eureka.client.enabled=false
#consul enabled
spring.cloud.consul.enabled=true
spring.cloud.consul.discovery.enabled=true
spring.cloud.consul.service-registry.enabled=true
spring.cloud.consul.discovery.heartbeat.enabled=true
spring.cloud.consul.discovery.instance-id=apollo-adminservice
3 changes: 3 additions & 0 deletions apollo-adminservice/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ spring:
name: apollo-adminservice
profiles:
active: ${apollo_profile}
cloud:
consul:
enabled: false

ctrip:
appid: 100003172
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ spring.jpa.properties.hibernate.show_sql=false
spring.h2.console.enabled = true
spring.h2.console.settings.web-allow-others=true
spring.main.allow-bean-definition-overriding=true
spring.cloud.consul.enabled=false
4 changes: 4 additions & 0 deletions apollo-biz/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- end of eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
Expand Down
1 change: 1 addition & 0 deletions apollo-biz/src/test/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.Ph
spring.jpa.properties.hibernate.show_sql=false
spring.h2.console.enabled = true
spring.h2.console.settings.web-allow-others=true
spring.cloud.consul.enabled=false
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
/**
* 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", "nacos-discovery"})
@Profile({"kubernetes", "nacos-discovery", "consul-discovery"})
@RestController
public class HomePageController {
private final DiscoveryService discoveryService;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.ctrip.framework.apollo.metaservice.service;

import com.ctrip.framework.apollo.core.dto.ServiceDTO;
import com.google.common.collect.Lists;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.consul.discovery.ConsulDiscoveryClient;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
* @author : kl
* Service discovery consul implementation
**/
@Service
@Profile({"consul-discovery"})
public class ConsulDiscoveryService implements DiscoveryService {

private final ConsulDiscoveryClient discoveryClient;

public ConsulDiscoveryService(ConsulDiscoveryClient discoveryClient) {
this.discoveryClient = discoveryClient;
}


@Override
public List<ServiceDTO> getServiceInstances(String serviceId) {
List<ServiceInstance> instances = discoveryClient.getInstances(serviceId);
List<ServiceDTO> serviceDTOList = Lists.newLinkedList();
if (!CollectionUtils.isEmpty(instances)) {
instances.forEach(instance -> {
ServiceDTO serviceDTO = this.toServiceDTO(instance, serviceId);
serviceDTOList.add(serviceDTO);
});
}
return serviceDTOList;
}

private ServiceDTO toServiceDTO(ServiceInstance instance, String appName) {
ServiceDTO service = new ServiceDTO();
service.setAppName(appName);
service.setInstanceId(instance.getInstanceId());
String homePageUrl = "http://" + instance.getHost() + ":" + instance.getPort() + "/";
service.setHomepageUrl(homePageUrl);
return service;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* Default discovery service for Eureka
*/
@Service
@ConditionalOnMissingProfile({"kubernetes", "nacos-discovery"})
@ConditionalOnMissingProfile({"kubernetes", "nacos-discovery", "consul-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,9 @@
apollo.eureka.server.enabled=false
eureka.client.enabled=false

#consul enabled
spring.cloud.consul.enabled=true
spring.cloud.consul.discovery.enabled=true
spring.cloud.consul.service-registry.enabled=true
spring.cloud.consul.discovery.heartbeat.enabled=true
spring.cloud.consul.discovery.instance-id=apollo-configservice
5 changes: 3 additions & 2 deletions apollo-configservice/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ spring:
name: apollo-configservice
profiles:
active: ${apollo_profile}

cloud:
consul:
enabled: false
ctrip:
appid: 100003171

Expand Down Expand Up @@ -31,7 +33,6 @@ eureka:
healthcheck:
enabled: true
eurekaServiceUrlPollIntervalSeconds: 60

management:
health:
status:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.ctrip.framework.apollo.metaservice.service;

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 org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.consul.discovery.ConsulDiscoveryClient;

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 2021/3/1
*/
@RunWith(MockitoJUnitRunner.class)
public class ConsulDiscoveryServiceTest {

@Mock
private ConsulDiscoveryClient consulDiscoveryClient;

private ConsulDiscoveryService consulDiscoveryService;

private String someServiceId;

@Before
public void setUp() throws Exception {
consulDiscoveryService = new ConsulDiscoveryService(consulDiscoveryClient);
someServiceId = "someServiceId";
}

@Test
public void testGetServiceInstancesWithNullInstances() {
when(consulDiscoveryClient.getInstances(someServiceId)).thenReturn(null);
assertTrue(consulDiscoveryService.getServiceInstances(someServiceId).isEmpty());
}


@Test
public void testGetServiceInstances() {
String someIp = "1.2.3.4";
int somePort = 8080;
String someInstanceId = "someInstanceId";
ServiceInstance someServiceInstance = mockServiceInstance(someInstanceId, someIp, somePort);

when(consulDiscoveryClient.getInstances(someServiceId)).thenReturn(
Lists.newArrayList(someServiceInstance));

List<ServiceDTO> serviceDTOList = consulDiscoveryService.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 ServiceInstance mockServiceInstance(String instanceId, String ip, int port) {
ServiceInstance serviceInstance = mock(ServiceInstance.class);
when(serviceInstance.getInstanceId()).thenReturn(instanceId);
when(serviceInstance.getHost()).thenReturn(ip);
when(serviceInstance.getPort()).thenReturn(port);

return serviceInstance;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.Ph
spring.h2.console.enabled = true
spring.h2.console.settings.web-allow-others=true
spring.jpa.properties.hibernate.show_sql=false

spring.cloud.consul.enabled=false
spring.main.allow-bean-definition-overriding=true

# for ReleaseMessageScanner test
Expand Down
12 changes: 12 additions & 0 deletions docs/zh/deployment/distributed-deployment-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,18 @@ mvn clean package -Pgithub,nacos-discovery -DskipTests -pl apollo-configservice,
```properties
nacos.discovery.server-addr=127.0.0.1:8848
```
##### 2.2.1.2.8 启用外部Consul服务注册中心替换内置eureka

1. 修改build.sh/build.bat,将config-service和admin-service的maven编译命令更改为
```shell
mvn clean package -Pgithub -DskipTests -pl apollo-configservice,apollo-adminservice -am -Dapollo_profile=github,consul-discovery -Dspring_datasource_url=$apollo_config_db_url -Dspring_datasource_username=$apollo_config_db_username -Dspring_datasource_password=$apollo_config_db_password
```

2. 分别修改apollo-configservice和apollo-adminservice安装包中config目录下的application-github.properties,配置consul服务器地址
```properties
spring.cloud.consul.host=127.0.0.1
spring.cloud.consul.port=8500
```

### 2.2.2 部署Apollo服务端

Expand Down