1.4 对接微服务应用
1.4.4 Dubbo 接入 CSE
本章节介绍Dubbo如何接入CSE,使得Dubbo能够对接CSE,并且方便的使用CSE提供 的最常用的功能。在使用微服务引擎功能章节,会给出具体的开发指导。
本章节介绍的开发方法,可以在Dubbo ServiceComb Samples项目中找到对应的代 码,供您在开发过程中参考。
Dubbo未提供网关服务,在例子中使用了Spring Cloud Gateway作为网关,Spring Cloud Gateway接入CSE的说明请参考Spring Cloud接入CSE章节。
前提条件
● 已基于Dubbo开发好了微服务应用。
Dubbo微服务框架下的微服务开发,请参考https://dubbo.apache.org/zh/。
● Dubbo ServiceComb版本为1.3.6及以上版本,并且使用Spring Boot作为开发底 座。
● 本文假设您的项目使用了maven管理打包,您熟悉maven的依赖管理机制,能够 正确的修改“pom.xml”文件中的dependency management和dependency。
操作步骤
步骤1 (可选)在项目的“pom.xml”文件中引入依赖。
如果您的项目中,已经包含了如下依赖,则不需要做任何处理。
如果您的项目中使用了其他注册发现库,比如zookeeper、nacos等,可以删除这些依 赖,这个步骤不是必须的。
<dependency>
<groupId>com.huaweicloud.dubbo-servicecomb</groupId>
<artifactId>dubbo-servicecomb-solution-spring-boot</artifactId>
</dependency>
推荐使用Maven Dependency Management管理项目依赖的三方软件,在项目的
“pom.xml”文件中引入:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-bom</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.huaweicloud.dubbo-servicecomb</groupId>
<artifactId>dubbo-servicecomb-bom</artifactId>
<version>${dubbo-servicecomb.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
步骤2 在启动类引入Dubbo ServiceComb的Bean。
下面的示例代码中,"classpath*:spring/dubbo-servicecomb.xml"是Dubbo ServiceComb的Bean信息,增加这个信息才会加载Dubbo ServiceComb的功能。
@SpringBootApplication
@ImportResource({provider.xml", "classpath*:spring/dubbo-servicecomb.xml"})
public class ProviderApplication {
public static void main(String[] args) throws Exception { try {
步骤3 配置微服务信息。
1. 在“dubbo.properties”增加微服务描述信息。如果项目中没有
“dubbo.properties”,则创建一个新的文件。使用Spring Boot集成Dubbo,也 可以将配置信息写到“application.yaml”中。为了符合Dubbo的使用习惯,本文
# 服务名称。默认为 defaultMicroserviceName dubbo.servicecomb.service.name=price-provider
# 版本。默认为 1.0.0.0
dubbo.servicecomb.service.version=1.0.0
# 环境。默认为空。可选值:development,testing,acceptance,production
# dubbo.servicecomb.service.environment=production
# project。 默认为default
# dubbo.servicecomb.service.project=
#### 实例配置信息 ####
# 实例初始状态。可选值:UP,DOWN,STARTING,OUTOFSERVICE
# dubbo.servicecomb.instance.initialStatus=UP
#### 服务中心配置信息 ####
# dubbo.servicecomb.registry.address=https://cse.cn-south-1.myhuaweicloud.com dubbo.servicecomb.registry.address=http://127.0.0.1:30100
#### 配置中心配置信息 ####
# dubbo.servicecomb.config.address=https://cse.cn-south-1.myhuaweicloud.com dubbo.servicecomb.config.address=http://127.0.0.1:30113
dubbo.servicecomb.config.fileSource=provider.yaml
#### SSL 配置信息 ####
# dubbo.servicecomb.ssl.enabled=true dubbo.servicecomb.ssl.enabled=true
2. 修改dubbo registry为CSE的服务中心。这个配置项一般在spring的配置文件中,
比如“spring/dubbo-provider.xml”文件中。
<dubbo:registry address="sc://127.0.0.1:30100"/>
这个配置项的地址信息不会使用,只使用了协议名称sc。
3. (可选)如果使用基于流量的微服务治理的重试功能,需要修改dubbo cluster配 置。如果无此配置项,请新增。详细请参考使用服务治理。
<dubbo:consumer cluster="dubbo-servicecomb"></dubbo:consumer>
步骤4 (可选)配置AK/SK。
“dubbo.properties”和“application.yaml”中均可以使用place holder来使用环境变 量。以“application.yaml”为例,服务中心的地址和配置中心的地址使用环境变量,
那么采用ServiceStage部署的时候,环境变量的实际值会覆盖配置文件里面的缺省值,
减少了重新编译打包的步骤。
PAAS_CSE_SC_ENDPOINT: http://127.0.0.1:30100 PAAS_CSE_CC_ENDPOINT: http://127.0.0.1:30113 dubbo:
servicecomb:
registry:
address: ${PAAS_CSE_SC_ENDPOINT}
config:
address: ${PAAS_CSE_CC_ENDPOINT}
----结束