Skip to content

Commit

Permalink
fix(registry-etcd,demo-etcd): complete etcd registry function
Browse files Browse the repository at this point in the history
- Complete the registration of etcd by the service consumer, and then the consumer can make API calls to the service provider and pass all test cases.
  • Loading branch information
SweetWuXiaoMei committed Oct 18, 2024
1 parent ee26b4c commit 93e4cc8
Show file tree
Hide file tree
Showing 9 changed files with 197 additions and 180 deletions.
2 changes: 1 addition & 1 deletion demo/demo-etcd/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Notice

This integration tests is designed for Zookeeper registry and configuration. And extra test cases include:
This integration tests is designed for Etcd registry and configuration. And extra test cases include:

* Test cases related to SpringMVC annotations that demo-springmvc can not cover.

10 changes: 5 additions & 5 deletions demo/demo-etcd/consumer/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ servicecomb:
properties:
group: red
registry:
zk:
enabled: true
connectString: 127.0.0.1:2181
etcd:
# enabled: true
connectString: http://127.0.0.1:2379

rest:
address: 0.0.0.0:9092?websocketEnabled=true
server:
websocket-prefix: /ws

highway:
address: 0.0.0.0:7092
# highway:
# address: 0.0.0.0:7092



6 changes: 6 additions & 0 deletions demo/demo-etcd/gateway/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
<groupId>org.apache.servicecomb</groupId>
<artifactId>edge-core</artifactId>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.25.3</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>registry-etcd</artifactId>
Expand Down
4 changes: 2 additions & 2 deletions demo/demo-etcd/gateway/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ servicecomb:
version: 0.0.1
name: gateway
registry:
zk:
etcd:
enabled: true
connectString: 127.0.0.1:2181
connectString: http://127.0.0.1:2379

rest:
address: 0.0.0.0:9090?websocketEnabled=true
Expand Down
22 changes: 13 additions & 9 deletions demo/demo-etcd/provider/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,28 @@ servicecomb:
properties:
group: green
registry:
zk:
connectString: 127.0.0.1:2181
config:
zk:
connectString: 127.0.0.1:2181
instance-tag: config-demo
etcd:
connectString: http://127.0.0.1:2379
# config:
# zk:
# connectString: 127.0.0.1:2181
# instance-tag: config-demo

rest:
address: 0.0.0.0:9094?websocketEnabled=true
server:
websocket-prefix: /ws

highway:
address: 0.0.0.0:7094
# highway:
# address: 0.0.0.0:7094

cors:
enabled: true
origin: "*"
allowCredentials: false
allowedMethod: "*"
maxAge: 3600
maxAge: 3600

key1: 1
key2: 3
key3: 5
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@

@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = TestClientApplication.class)
public class ZookeeperIT {
private static final Logger LOGGER = LoggerFactory.getLogger(ZookeeperIT.class);
public class EtcdIT {
private static final Logger LOGGER = LoggerFactory.getLogger(EtcdIT.class);

@BeforeEach
public void setUp() {
TestMgr.errors().clear();
}

@Test
public void clientGetsNoError() throws Exception {
public void clientGetsNoError() {
try {
TestClientApplication.run();
} catch (Exception e) {
Expand Down
1 change: 1 addition & 0 deletions demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<module>demo-cse-v2</module>
<module>demo-nacos</module>
<module>demo-etcd</module>
<module>demo-zookeeper</module>
</modules>

<dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
package org.apache.servicecomb.registry.etcd;

import com.google.common.collect.Lists;
import io.etcd.jetcd.*;
import io.etcd.jetcd.ByteSequence;
import io.etcd.jetcd.Client;
import io.etcd.jetcd.KeyValue;
import io.etcd.jetcd.Watch;
import io.etcd.jetcd.kv.GetResponse;
import io.etcd.jetcd.options.GetOption;
import io.etcd.jetcd.options.WatchOption;
Expand All @@ -26,15 +29,14 @@
import org.apache.servicecomb.config.BootStrapProperties;
import org.apache.servicecomb.foundation.common.utils.JsonUtils;
import org.apache.servicecomb.registry.api.Discovery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

public class EtcdDiscovery implements Discovery<EtcdDiscoveryInstance> {

Expand All @@ -46,8 +48,6 @@ public class EtcdDiscovery implements Discovery<EtcdDiscoveryInstance> {

private Client client;

private static final Logger LOGGER = LoggerFactory.getLogger(EtcdDiscovery.class);

private InstanceChangedListener<EtcdDiscoveryInstance> instanceChangedListener;

@Autowired
Expand Down Expand Up @@ -76,10 +76,10 @@ public boolean enabled(String application, String serviceName) {
@Override
public List<EtcdDiscoveryInstance> findServiceInstances(String application, String serviceName) {

String prefixPath = basePath + "/" + application;
String prefixPath = basePath + "/" + application + "/" + serviceName;
Watch watchClient = client.getWatchClient();
watchClient.watch(ByteSequence.from(prefixPath, Charset.defaultCharset()),
WatchOption.newBuilder().build(),
WatchOption.builder().build(),
resp -> {
List<KeyValue> keyValueList = resp.getEvents().stream().map(WatchEvent::getKeyValue).toList();
instanceChangedListener.onInstanceChanged(name(), application, serviceName, convertServiceInstanceList(keyValueList));
Expand All @@ -93,7 +93,7 @@ public List<KeyValue> getValuesByPrefix(String prefix) {

CompletableFuture<GetResponse> getFuture = client.getKVClient().get(
ByteSequence.from(prefix, StandardCharsets.UTF_8),
GetOption.newBuilder().withPrefix(ByteSequence.from(prefix, StandardCharsets.UTF_8)).build()
GetOption.builder().withPrefix(ByteSequence.from(prefix, StandardCharsets.UTF_8)).build()
);
GetResponse response = MuteExceptionUtil.builder().withLog("get kv by prefix error")
.executeCompletableFuture(getFuture);
Expand All @@ -114,11 +114,14 @@ private List<EtcdDiscoveryInstance> convertServiceInstanceList(List<KeyValue> ke
return list;
}

// todo
@Override
public List<String> findServices(String application) {

return List.of();
String prefixPath = basePath + "/" + application;
List<KeyValue> endpointKv = getValuesByPrefix(prefixPath);
return endpointKv.stream()
.map(kv -> kv.getKey().toString(StandardCharsets.UTF_8)) // 转换 ByteSequence 为 String
.collect(Collectors.toList());
}

@Override
Expand Down
Loading

0 comments on commit 93e4cc8

Please sign in to comment.