Skip to content

Commit

Permalink
Merge 6d6c0e2 into 4b69ff6
Browse files Browse the repository at this point in the history
  • Loading branch information
kakulisen committed Nov 27, 2019
2 parents 4b69ff6 + 6d6c0e2 commit 39e2a61
Show file tree
Hide file tree
Showing 11 changed files with 467 additions and 0 deletions.
Binary file added images/toolkit-syncer.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions samples/pom.xml
Expand Up @@ -31,6 +31,7 @@
<module>generate-from-code-sample</module>
<module>generate-from-contract-sample</module>
<module>verify-with-code-sample</module>
<module>with-syncer-sample</module>
</modules>

<build>
Expand Down
87 changes: 87 additions & 0 deletions samples/with-syncer-sample/HelloService/pom.xml
@@ -0,0 +1,87 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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>

<artifactId>HelloService</artifactId>
<version>0.2.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.servicecomb.toolkit</groupId>
<artifactId>toolkit-maven-plugin</artifactId>
<version>0.2.0-SNAPSHOT</version>
<executions>
<execution>
<id>provider</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<sourceType>code</sourceType>
<outputDirectory>../output/servicecomb</outputDirectory>
<service>
<serviceType>provider</serviceType>
<groupId>com.demo</groupId>
<artifactId>servicecomb-provider</artifactId>
</service>
</configuration>
</execution>
<execution>
<id>consumer</id>
<configuration>
<sourceType>code</sourceType>
<outputDirectory>../output/springcloud</outputDirectory>
<service>
<serviceType>consumer</serviceType>
<groupId>com.demo</groupId>
<artifactId>springcloud-consumer</artifactId>
<providerServiceId>servicecomb-provider</providerServiceId>
<microServiceFramework>SpringCloud</microServiceFramework>
</service>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.12.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

</project>
@@ -0,0 +1,30 @@
/*
* 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.toolkit.hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HelloApplication {

public static void main(String[] args) {
SpringApplication.run(HelloApplication.class,args);
}

}
@@ -0,0 +1,31 @@
/*
* 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.toolkit.hello;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

@GetMapping("/hello/{name}")
public String sayHello(@PathVariable("name") String name) {
return "Hello," + name;
}
}
@@ -0,0 +1,19 @@
## ---------------------------------------------------------------------------
## 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.
## ---------------------------------------------------------------------------

server:
port: 8081
122 changes: 122 additions & 0 deletions samples/with-syncer-sample/README_ZH.md
@@ -0,0 +1,122 @@
### Apache ServiceComb Toolkit 结合 Apache ServiceComb Syncer 的实践



#### 场景

[Syncer](https://github.com/apache/servicecomb-service-center/tree/master/syncer)是一个多服务中心的同步工具,专为大型微服务架构设计,用于在网络互通的情况下,不同技术栈服务中心、跨区域的实例同步

结合Apache ServiceComb Toolkit 和Apache ServiceComb Syncer,不同开发者使用自己熟悉的框架进行开发,极大提高了开发效率。

如图1所示,开发者A和开发者B基于同一份OpenAPI规范进行开发,开发者A对于Apache ServiceComb十分熟悉,基于Apache ServiceComb框架开发了Provider应用,提供Restful API接口的实现。而开发者B对于Spring Cloud框架十分熟悉,基于Spring Cloud框架开发了Consumer应用,调用Provider应用的接口,然后进行自己的业务实现。

在通常情况下,他们是无法通过服务注册中心进行相互调用的。这时我们引入了Apache ServiceComb Syncer,这让一切变得简单。开发者A在Provider侧启动一个Syncer进程syncer-a,开发者B在Consumer侧启动一个Syncer进程syncer-b,进行一些简单的配置,它们将会把各自关联的服务注册中心的实例进行相互同步。此时Provider和Consumer就可以通过服务注册中心进行相互通信了。

![toolkit-syncer](https://github.com/kakulisen/picture_repo/blob/master/pratice/toolkit-syncer.png?raw=true)

#### 环境要求
* windows

* maven 3.3.1 +

* jdk 1.8



#### 实践步骤

* 下载并启动Apache ServiceComb服务中心ServiceCenter

* 下载地址:http://servicecomb.apache.org/cn/release/service-center-downloads/

* 启动

```cmd
service-center.exe
```

* 启动Eureka服务中心

```
cd ./eureka-server
mvn spring-boot:run
```

* 下载并启动Apache ServiceComb Syncer 异构通信工具

* 下载地址:http://servicecomb.apache.org/cn/release/service-center-downloads/

* 启动 ServiceCenter 侧的syncer

```
syncer daemon --node=syncer-sc --sc-addr http://127.0.0.1:30100 --bind-addr ${your_host_ip}:30190 --rpc-addr ${your_host_ip}:30191 --cluster-port=30192 --sc-plugin=servicecenter --join-addr ${your_host_ip}:30180
```

* 启动 Eureka 侧的syncer

```
syncer.exe daemon --node=syncer-eureka --sc-addr http://127.0.0.1:8761/eureka --bind-addr ${your_host_ip}:30180 --rpc-addr ${your_host_ip}:30181 --cluster-port=30182 --sc-plugin=eureka --join-addr ${your_host_ip}:30190
```



* 使用Apache ServiceComb Toolkit生成基于ServiceComb框架的服务提供者servicecomb-provider

```
cd ./HelloService
mvn toolkit:generate@provider
```



* 使用Apache ServiceComb Toolkit生成基于SpringCloud框架的服务消费者springcloud-consumer

```
cd ./HelloService
mvn toolkit:generate@consumer
```



* 验证

* 为生成的服务提供者添加业务代码

服务提供者默认生成目录 output/servicecomb-provider/project/servicecomb-provider

编辑其中的 domain.orgnization.project.sample.api.HelloController 类

添加如下自定义的业务逻辑

```
return new ResponseEntity<String>(String.format("Hello %s", name), HttpStatus.OK);
```

* 为生成的服务消费者添加调用代码

修改消费者springcloud-consumer的启动类Application.java,添加如下代码

```
@Autowired
HelloController helloController;
@Bean
ApplicationRunner applicationRunner() {
return args -> {
String serviceComb = helloController.sayHello("ServiceComb");
System.out.println(serviceComb);
};
}
```

* 调用

```
mvn spring-boot:run
```

在控制台输出的日志最后有如下信息,则调用成功

```
"Hello ServiceComb"
```
72 changes: 72 additions & 0 deletions samples/with-syncer-sample/eureka-server/pom.xml
@@ -0,0 +1,72 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.apache.servicecomb.servicecenter</groupId>
<artifactId>eureka-server</artifactId>
<version>0.2.0-SNAPSHOT</version>
<name>eureka-server</name>
<description>Eureka server</description>

<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

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

</project>

0 comments on commit 39e2a61

Please sign in to comment.