diff --git a/_posts/cn/2017-11-27-dubbo-to-servicecomb.md b/_posts/cn/2017-11-27-dubbo-to-servicecomb.md new file mode 100644 index 0000000..2d93932 --- /dev/null +++ b/_posts/cn/2017-11-27-dubbo-to-servicecomb.md @@ -0,0 +1,319 @@ +--- +title: "Dubbo to ServiceComb 迁移指南" +lang: cn +ref: dubbo_to_servicecomb +permalink: /cn/docs/dubbo_to_servicecomb/ +excerpt: "Dubbo to ServiceComb 迁移指南" +last_modified_at: 2017-11-27T11:08:00+08:00 +author: Sean Yin +redirect_from: + - /theme-setup/ +--- + +Dubbo和Java chassis底层都使用了Spring的依赖注入和bean管理系统,所以使用Dubbo的服务迁移到ServiceComb工作量较小, +主要改动在依赖和配置方面。 + +本文示例代码可在[Java chassis](https://github.com/ServiceComb/ServiceComb-Java-Chassis/tree/master/samples)中获取。 + +## 服务提供方 +### 替换依赖 +将对dubbo的依赖替换为对Java chassis的依赖 + +Dubbo +```xml + + com.alibaba + dubbo + ${dubbo.version} + +``` + +Java chassis +```xml + + + + io.servicecomb + java-chassis-dependencies + ${java.chassis.version} + pom + import + + + + + + + io.servicecomb + transport-highway + + + io.servicecomb + transport-rest-vertx + + + io.servicecomb + provider-pojo + + +``` + +### 替换服务接口注释 +以 `@Service` 注释方式暴露的服务接口,用 `@RpcSchema` 替代 + +Dubbo +```java +import com.alibaba.dubbo.config.annotation.Service; + +@Service +public class SomeServiceImpl implements SomeService { + // ... +} +``` + +Java chassis +```java +@RpcSchema(schemaId = "someServiceEndpoint") +public class SomeServiceImpl implements SomeService { + // ... +} +``` + +### 修改提供方启动入口 + +Dubbo +```java +public class ProviderMain +{ + + public static void main(String[] args) throws Exception + { + @SuppressWarnings( + { "resource", "unused" }) + ApplicationContext context = new ClassPathXmlApplicationContext("conf/applicationContext.xml"); + System.out.println("Dubbo provider started successfully..."); + + System.in.read(); + } +} +``` + +Java chassis +```java +public class ProviderMain +{ + + public static void main(String[] args) throws Exception + { + Log4jUtils.init(); + BeanUtils.init(); + + System.out.println("ServiceComb provider started successfully..."); + } +} +``` + +### 修改服务提供方配置 +Dubbo服务通过一个XML文件同时配置了服务接口、服务注册中心地址、服务监听端口等所有内容。 +```xml + + + + + + + + + + + + + + + + + +``` + +Java chassis也同样提供了以XML文件暴露服务接口的方式,但其他服务配置则通过 `microservice.yaml` 文件提供。 +```xml + + + + + + +``` + +```yaml +APPLICATION_ID: my-application +service_description: + name: service-provider + version: 0.0.1 +cse: + service: + registry: + address: http://127.0.0.1:30100 # 服务注册中心地址 + rest: + address: 0.0.0.0:8080 # 服务REST端口 +``` + +## 服务消费方 +### 替换依赖 +将对Dubbo的依赖替换为对Java chassis的依赖 + +Dubbo +```xml + + com.alibaba + dubbo + ${dubbo.version} + +``` + +Java chassis +```xml + + + + io.servicecomb + java-chassis-dependencies + ${java.chassis.version} + pom + import + + + + + + + io.servicecomb + transport-highway + + + io.servicecomb + transport-rest-vertx + + + io.servicecomb + provider-pojo + + +``` + +### 修改消费方启动入口 +Dubbo +```java +public class ConsumerMain +{ + + public static void main(String[] args) throws Exception + { + @SuppressWarnings( + { "resource", "unused" }) + ApplicationContext context = new ClassPathXmlApplicationContext("conf/applicationContext.xml"); + System.out.println("Dubbo Consumer started successfully..."); + + System.in.read(); + } +} +``` + +Java chassis +```java +public class ConsumerMain +{ + + public static void main(String[] args) throws Exception + { + Log4jUtils.init(); + BeanUtils.init(); + + System.out.println("ServiceComb Consumer started successfully..."); + } +} +``` + +### 服务异步调用 +Dubbo +```java + SomeService someService = (SomeService) context.getBean("someServiceRef"); + String hello = someService.sayHello("world"); + Future future = RpcContext.getContext().getFuture(); + return future.get(); +``` + +Java chassis +```java + ApplicationContext context = BeanUtils.getContext(); + SomeService someService = (SomeService) context.getBean("someServiceRef"); + + CompletableFuture future = CompletableFuture.supplyAsync(() -> someService.sayHello("world")); + return future.get(); +``` + +### 修改服务提供方配置 +Dubbo服务通过一个XML文件同时配置了远程服务提供方接口、服务注册中心地址、服务监听端口等所有内容。 +```xml + + + + + + + + + + + + +``` + +Java chassis也同样提供了以XML文件引用远程服务提供方接口的方式,但其他服务配置则通过 `microservice.yaml` 文件提供。 +```xml + + + + + + + + +``` + +```yaml +APPLICATION_ID: my-application +service_description: + name: service-consumer + version: 0.0.1 +cse: + service: + registry: + address: http://127.0.0.1:30100 +```