5分钟搞定Dubbo应用接入华为云CSE

举报
无微不至 发表于 2018/06/12 16:33:01 2018/06/12
【摘要】 Dubbo和CSE底层都使用了Spring的依赖注入和bean管理系统,所以使用Dubbo的服务迁移到华为云微服务引擎CSE的工作量较小, 主要改动在依赖和配置方面。本示例的完整代码已放在GitHub上,其中目录dubbo-demo是原始的Dubbo DEMO,目录dubbo-demo-servicecomb是改造后的可直接运行于华为云CSE的DEMO。

限时免费体验华为云微服务


5分钟搞定Dubbo应用接入华为云CSE

Dubbo和CSE底层都使用了Spring的依赖注入和bean管理系统,所以使用Dubbo的服务迁移到华为云微服务引擎CSE的工作量较小, 主要改动在依赖和配置方面。

本示例的完整代码已放在GitHub上,其中目录dubbo-demo是原始的Dubbo DEMO,目录dubbo-demo-servicecomb是改造后的可直接运行于华为云CSE的DEMO。

1、管理依赖:/dubbo-demo/pom.xml

在主项目pom里的dependencyManagement中增加如下配置来管理CSE包依赖,子项目就不需要指定CSE版本号。

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>com.huawei.paas.cse</groupId>
        <artifactId>cse-dependency</artifactId>
        <version>2.3.9</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

注意,maven的settings需添加cse的mirror才能正常下载到cse的相关依赖包:

    <mirror>
      <id>nexus-cse</id>
      <mirrorOf>*</mirrorOf>
      <name>cse nexus</name>
      <url>http://maven.huaweicse.com/nexus/content/groups/public</url>    </mirror>

2、服务提供方:dubbo-demo-provider

2.1、替换依赖:/dubbo-demo-provider/pom.xml

将对Dubbo的依赖替换为对CSE的依赖pom.xml

Dubbo

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.7</version>
        </dependency>

CSE

        <dependency>            <groupId>com.huawei.paas.cse</groupId>
            <artifactId>cse-solution-service-engine</artifactId>        </dependency>

2.2、通过添加标签的方式发布服务接口:DemoServiceImpl

Dubbo

public class DemoServiceImpl implements DemoService {
    ......
}

CSE

@RpcSchema(schemaId = "providerSchema")public class DemoServiceImpl implements DemoService {
    ......

}

2.3、修改配置:/dubbo-demo-provider/src/main/resources

删除:/dubbo-demo-provider/src/main/resources/META-INF/spring/dubbo-demo-provider.xml

新增:/dubbo-demo-provider/src/main/resources/META-INF/spring/demo.bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:cse="http://www.huawei.com/schema/paas/cse/pojo"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans classpath:org/springframework/beans/factory/xml/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

      <context:component-scan base-package="com.alibaba.dubbo.demo" />
</beans>

新增:/dubbo-demo-provider/src/main/resources/microservice.yaml

APPLICATION_ID: dubbo_servicecomb
service_description:
  name: provider
  version: 0.0.1
cse:
  service:
    registry:
      address: https://cse.cn-north-1.myhuaweicloud.com:443
  rest:
    address: 0.0.0.0:8082
  credentials:
    accessKey: 替换为华为云IAM账号AK(如何获取AK/SK)
    secretKey: 替换为华为云IAM账号SK
    akskCustomCipher: default
    project: cn-north-1


2.4、修改提供方启动入口

Dubbo

public class Provider {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-provider.xml"});
        context.start();
        System.in.read(); // 按任意键退出
    }
}

CSE

public class Provider {
    public static void main(String[] args) throws Exception {
        Log4jUtils.init();
        BeanUtils.init();    }
}

3、服务消费方:dubbo-demo-consumer

3.1、替换依赖:/dubbo-demo-consumer/pom.xml

将对Dubbo的依赖替换为对CSE的依赖

Dubbo

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.7</version>
        </dependency>

CSE

		<dependency>			<groupId>com.huawei.paas.cse</groupId>
			<artifactId>cse-solution-service-engine</artifactId>		</dependency>

3.2、修改消费方启动入口

Dubbo

public class Consumer {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-consumer.xml"});
        context.start();

        DemoService demoService = (DemoService) context.getBean("demoService"); 
        String hello = demoService.sayHello("world"); 

        System.out.println(hello); 
    }
}

CSE


import org.springframework.stereotype.Component;

@Component

public class Consumer {
    @RpcReference(microserviceName="provider", schemaId="providerSchema")
    private static DemoService demoService;

    public static void main(String[] args) throws Exception {
        Log4jUtils.init();
        BeanUtils.init();

        String hello = demoService.sayHello("world"); 

        System.out.println(hello); 
    }
}


3.3、修改配置:/dubbo-demo-comsumer/src/main/resources

删除:/dubbo-demo-consumer/src/main/resources/META-INF/spring/dubbo-demo-consumer.xml

新增:/dubbo-demo-consumer/src/main/resources/META-INF/spring/demo.bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:cse="http://www.huawei.com/schema/paas/cse/pojo"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans classpath:org/springframework/beans/factory/xml/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

      <context:component-scan base-package="com.alibaba.dubbo.demo" />
</beans>

新增:/dubbo-demo-consumer/src/main/resources/microservice.yaml

APPLICATION_ID: dubbo_servicecomb
service_description:
  name: consumer
  version: 0.0.1
cse:
  service:
    registry:
      address: https://cse.cn-north-1.myhuaweicloud.com:443
  rest:
    address: 0.0.0.0:8084
  credentials:
    accessKey: 替换为华为云IAM账号AK(如何获取AK/SK)    secretKey: 替换为华为云IAM账号SK
    akskCustomCipher: default
    project: cn-north-1


4、验证:先启动Provider,再启动Consumer

4.1、进入华为云微服务引擎CSE控制台:微服务管理>服务治理>dubbo_servicecomb,看到如下服务调用关系:

1.png

4.2、进入Consumer的输出控制台,能看到输出:Hello world

2.png



立即体验:https://console.huaweicloud.com/cse/?region=cn-north-1#/cse/home

了解详情:https://www.huaweicloud.com/product/cse.html

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。