Skip to content

Commit

Permalink
feat(registry-etcd,demo-etcd): implement the basic discovery function…
Browse files Browse the repository at this point in the history
… through etcd service registration, and add a demo to test etcd

- Implement the method of finding all instances of EtcdDiscovery
- Implement the registered instance information of EtcdRegistration to etcd
- Implement silent exception information printing, see MuteExceptionUtil for details
  • Loading branch information
chenzhida committed Oct 14, 2024
1 parent 8abfdff commit ee26b4c
Show file tree
Hide file tree
Showing 56 changed files with 2,121 additions and 332 deletions.
6 changes: 6 additions & 0 deletions demo/demo-etcd/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Notice

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

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

88 changes: 88 additions & 0 deletions demo/demo-etcd/consumer/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You under the Apache License, Version 2.0
~ (the "License"); you may not use this file except in compliance with
~ the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.apache.servicecomb.demo</groupId>
<artifactId>demo-etcd</artifactId>
<version>3.3.0-SNAPSHOT</version>
</parent>

<artifactId>etcd-consumer</artifactId>
<name>Java Chassis::Demo::Etcd::CONSUMER</name>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>java-chassis-spring-boot-starter-standalone</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>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>docker</id>
<build>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.commonjava.maven.plugins</groupId>
<artifactId>directory-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.github.odavid.maven.plugins</groupId>
<artifactId>mixin-maven-plugin</artifactId>
<configuration>
<mixins>
<mixin>
<groupId>org.apache.servicecomb.demo</groupId>
<artifactId>docker-build-config</artifactId>
<version>${project.version}</version>
</mixin>
</mixins>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.servicecomb.samples;

import org.apache.servicecomb.core.CoreConst;
import org.apache.servicecomb.core.annotation.Transport;
import org.apache.servicecomb.provider.pojo.RpcReference;
import org.apache.servicecomb.provider.rest.common.RestSchema;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import io.vertx.core.http.ServerWebSocket;
import io.vertx.core.http.WebSocket;

@RestSchema(schemaId = "ClientWebsocketController")
@RequestMapping(path = "/ws")
public class ClientWebsocketController {
interface ProviderService {
WebSocket websocket();
}

@RpcReference(schemaId = "WebsocketController", microserviceName = "provider")
private ProviderService providerService;

@PostMapping("/websocket")
@Transport(name = CoreConst.WEBSOCKET)
public void websocket(ServerWebSocket serverWebsocket) {
WebSocket providerWebSocket = providerService.websocket();
providerWebSocket.closeHandler(v -> serverWebsocket.close());
providerWebSocket.textMessageHandler(m -> {
System.out.println("send message " + m);
serverWebsocket.writeTextMessage(m);
});
serverWebsocket.textMessageHandler(m -> {
System.out.println("receive message " + m);
providerWebSocket.writeTextMessage(m);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.servicecomb.samples;

import org.apache.servicecomb.provider.pojo.RpcReference;
import org.apache.servicecomb.provider.rest.common.RestSchema;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@RestSchema(schemaId = "ConsumerController")
@RequestMapping(path = "/")
public class ConsumerController {
@RpcReference(schemaId = "ProviderController", microserviceName = "provider")
private ProviderService providerService;

// consumer service which delegate the implementation to provider service.
@GetMapping("/sayHello")
public String sayHello(@RequestParam("name") String name) {
return providerService.sayHello(name);
}

@GetMapping("/getConfig")
public String getConfig(@RequestParam("key") String key) {
return providerService.getConfig(key);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.servicecomb.samples;

import java.util.List;

import org.apache.servicecomb.demo.api.IHeaderParamWithListSchemaSpringMvc;
import org.apache.servicecomb.provider.pojo.RpcReference;
import org.apache.servicecomb.provider.rest.common.RestSchema;

@RestSchema(schemaId = "ConsumerHeaderParamWithListSchema", schemaInterface = IHeaderParamWithListSchemaSpringMvc.class)
public class ConsumerHeaderParamWithListSchema implements IHeaderParamWithListSchemaSpringMvc {
@RpcReference(microserviceName = "provider", schemaId = "HeaderParamWithListSchema")
private IHeaderParamWithListSchemaSpringMvc provider;

@Override
public String headerListDefault(List<String> headerList) {
return provider.headerListDefault(headerList);
}

@Override
public String headerListCSV(List<String> headerList) {
return provider.headerListCSV(headerList);
}

@Override
public String headerListMULTI(List<String> headerList) {
return provider.headerListMULTI(headerList);
}

@Override
public String headerListSSV(List<String> headerList) {
return provider.headerListSSV(headerList);
}

@Override
public String headerListPIPES(List<String> headerList) {
return provider.headerListPIPES(headerList);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.servicecomb.samples;


import org.apache.servicecomb.core.CoreConst;
import org.apache.servicecomb.core.annotation.Transport;
import org.apache.servicecomb.provider.pojo.RpcReference;
import org.apache.servicecomb.provider.rest.common.RestSchema;
import org.reactivestreams.Publisher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@RestSchema(schemaId = "ReactiveStreamController")
@RequestMapping(path = "/")
public class ConsumerReactiveStreamController {
interface ProviderReactiveStreamController {
Publisher<String> sseString();

Publisher<Model> sseModel();
}

@RpcReference(microserviceName = "provider", schemaId = "ReactiveStreamController")
ProviderReactiveStreamController controller;

public static class Model {
private String name;

private int age;

public Model() {

}

public Model(String name, int age) {
this.name = name;
this.age = age;
}

public int getAge() {
return age;
}

public Model setAge(int age) {
this.age = age;
return this;
}

public String getName() {
return name;
}

public Model setName(String name) {
this.name = name;
return this;
}
}

@GetMapping("/sseString")
@Transport(name = CoreConst.RESTFUL)
public Publisher<String> sseString() {
return controller.sseString();
}

@GetMapping("/sseModel")
@Transport(name = CoreConst.RESTFUL)
public Publisher<Model> sseModel() {
return controller.sseModel();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@
* limitations under the License.
*/

package org.apache.servicecomb;
package org.apache.servicecomb.samples;

import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
public class EtcdConsumerApplication {
public static void main(String[] args) throws Exception {
try {
new SpringApplicationBuilder(ConsumerApplication.class).web(WebApplicationType.NONE).run(args);
} catch (Throwable e) {
new SpringApplicationBuilder().web(WebApplicationType.NONE).sources(EtcdConsumerApplication.class).run(args);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.servicecomb.samples;

public interface ProviderService {
String sayHello(String name);

String getConfig(String key);
}
Loading

0 comments on commit ee26b4c

Please sign in to comment.