5分钟搞定Dubbo应用接入华为云CSE
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,看到如下服务调用关系:
4.2、进入Consumer的输出控制台,能看到输出:Hello world
立即体验:https://console.huaweicloud.com/cse/?region=cn-north-1#/cse/home
了解详情:https://www.huaweicloud.com/product/cse.html
- 点赞
- 收藏
- 关注作者
评论(0)