天天看點

5分鐘搞定Dubbo應用接入華為雲微服務引擎CSE5分鐘搞定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,看到如下服務調用關系:

5分鐘搞定Dubbo應用接入華為雲微服務引擎CSE5分鐘搞定Dubbo應用接入華為雲微服務引擎CSE

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

5分鐘搞定Dubbo應用接入華為雲微服務引擎CSE5分鐘搞定Dubbo應用接入華為雲微服務引擎CSE

繼續閱讀