Follow me on CSE series 5: How Spring Cloud applications connect to CSE

This chapter uses a practical case to illustrate how Spring Cloud applications can quickly access CSE with a few configuration modifications.

Original Spring Cloud application download address:

https://github.com/huawei-microservice-demo/SpringCloudIntegration/tree/master/springcloud-sample

The Spring Cloud application provides 3 projects:

  • eureka-server provides registration discovery capabilities.
  • The springcloud-provider service provider, which provides a REST interface named HelloService.
  • The springcloud-consumer service consumer also provides a REST interface named HelloService, which implements calling the springcloud-provider's REST interface through Feign.

The transformed application has the following capabilities and changes:

  • Use the service center provided by CSE as a registration discovery service;
  • Using the configuration center provided by CSE as a dynamic configuration service, public configuration can be managed through the configuration center;
  • The other logic of the business does not change, nor does the way of writing code. Developers can still write business code according to the original development habits.

Access steps

CSE provides a very simple access method for Spring Cloud applications. Developers only need to modify the dependencies and a small amount of configuration to enable the service center and configuration center client connection function, and register the Spring Cloud application as a CSE microservice to the service center and use the dynamic configuration capability.

  1. Modify dependencies.

    Replace the dependency on earuka in Spring Cloud with the dependency on CSE.

    Eureka Dependency: Developers generally use spring-cloud-starter-eureka. spring-cloud-starter-eureka-server is used as a registration service and does not need to be used after replacing the service center.

    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
    
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-eureka-server</artifactId>
    </dependency>

    Modified dependencies:

    <dependency>
      <groupId>com.huawei.paas.cse</groupId>
      <artifactId>cse-solution-spring-cloud</artifactId>
      <version>2.3.19</version>
    </dependency>

  2. Consumer uses ribbon.

    If the Ribbon component is used in the Spring Cloud application code, its default implementation is through Eureka, and the following configuration needs to be added to application.yml:

    helloprovider:
      ribbon:
        NIWSServerListClassName: org.apache.servicecomb.springboot.starter.discovery.ServiceCombServerList

    After the above steps, all the rectification of Spring Cloud application access to CSE is completed. Developers can package applications as container images and deploy them on public clouds.

Supplementary Instructions

  • It is recommended that developers introduce dependencyManagement of dependencies in pom.xml to better manage the third-party components used and prevent conflicts.

    dependencyManagement不会往程序里面增加依赖关系,但是可以帮助开发者更好的管理依赖关系,对于解决三方软件冲突非常有用。详细原理描述可以参考“使用maven管理复杂依赖关系的技巧"。

    <dependencyManagement>
      <dependencies>
        <dependency>
          <groupId>com.huawei.paas.cse</groupId>
          <artifactId>cse-dependency</artifactId>
          <version>2.3.19</version>
          <type>pom</type>
          <scope>import</scope>
        </dependency>
      </dependencies>
    </dependencyManagement>
  • 将应用制作为镜像,部署到公有云,部署平台会对应用增加一些认证关系的配置,以完成对于应用的安全认证,这些过程是由部署平台自动完成的。CSE的服务中心和配置中心通过api gateway开放了REST接口,支持开发者在公网环境使用其服务,这样给开发者的线下开发带来大量的便利。为了线下使用CSE的服务中心和配置中心,开发者需要在application.yml中增加认证信息,认证信息包含AS/SK,可以从公有云帐号的"我的凭证”获取。
    cse:
      credentials:
        accessKey: your access key
        secretKey: your secret key
        akskCustomCipher: default

    有些开发者需要通过代理服务器访问公有云,也可以通过设置代理来实现:

    cse:
      proxy:
        enable: true
        host: your proxy server
        port: your proxy server port
        username: user name
        passwd: password for proxy

    CSE的配置是分层次的,按照优先级顺序是:

    yaml配置文件 < 环境变量 < System Property < 配置中心。

    如果开发者不希望将密码信息写入配置文件,也可以通过环境变量或者System Property的方式设置这些配置信息。比如:

    java -Dcse.credentials.accessKey=$ACCESS_KEY Application.jar。

CSE以华为自身实践为基础,历经亿级消费者业务场景的考验,专注帮助企业解决云化转型及上云前后的分布式架构改造难题,针对不同企业的业务场景,提供多语言、多架构的解决方案,致力于帮助企业搭建自身的分布式系统,更加敏捷的应对数字化转型带来的挑战。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325828913&siteId=291194637