一、ServiceComb 的概述

ServiceComb作为Apache开源组织下的一款微服务框架,其前身为华为云的微服务引擎CSE(Cloud Service Engine) 云服务。

解决方案级,多语言、多通信协议、标准服务契约、事务最终一致性开源开放,拥抱SpringBootSpringCloudServiceMesh 等主流生态,低门槛准入,业务侵入度低,架构松耦合。

ServiceComb的优势:国内政府部门有的会选择华为云,使用ServiceComb开发可以快速优雅的部署到华为云的的pass平台上。至于ServiceComb支持RPC调用,REST难道不香吗?CSE注册中心中的服务契约提供的类似swagger和接口测试功能,倒是挺好用的。

缺点:由于ServiceComb的特性,遇到bug的可能百度很难解决。在SpringCloud F版后,SpringCloud的功能更强大了,普及度方面SpingCloud也更多人使用。

二、服务中心 CSE 的介绍

在这里插入图片描述
CSE的架构和Eureka很像,经过使用后,可以发现CSE的自我保护机制比Eureka还要严重。

下载安装CSE的安装包:apache-servicecomb-service-center-1.3.0-windows-amd64.tar.gz,解压文件目录如下:
在这里插入图片描述

三、Restful 方式开发

完整代码参考:https://github.com/hucheng1997/servicecomb

①添加依赖

父工程依赖:(这里选择1.x版本的springboot2.x版本会遇到意料之外的错误!)

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.12.RELEASE</version>
    <relativePath/>
</parent>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.apache.servicecomb</groupId>
            <artifactId>java-chassis-dependencies</artifactId>
            <version>1.3.0</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

providerconsumer工程的依赖如下:

<!--做参数校验的-->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.servicecomb</groupId>
    <artifactId>spring-boot-starter-provider</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

②添加配置文件

这里配置文件只能为microservice.yaml,这是ServiceComb的硬性规定:

APPLICATION_ID: hucheng #一般填公司的名称
service_description:
  name: servicecomb-provider #微服务名
  version: 0.0.1 #版本
  environment: development #环境
  properties:
    allowCrossApp: false #不允许跨App访问服务
  rest:
    address: 0.0.0.0:81 #服务端口
  service:
    registry:
      address: http://127.0.0.1:30100 #注册中心的位置

③主启动上添加注解@EnableServiceComb

④Provider代码及说明
在这里插入图片描述
⑤Consumer调用代码

使用RestTemplate远程调用服务
在这里插入图片描述
⑥测试

使用注册中心中的服务契约进行测试
在这里插入图片描述

四、RPC 方式开发

@RestSchema注解换成@RpcSchema即可

五、ServiceComb 服务治理方案

负载均衡

使用了ServiceComb框架的微服务即自带了负载均衡功能,无需任何配置。

限流策略

添加依赖:

<dependency>
      <groupId>org.apache.servicecomb</groupId>
      <artifactId>handler-flowcontrol-qps</artifactId>
  </dependency>

添加配置:

servicecomb:
  flowcontrol:
    Provider:
      qps:
        limit:
          gateway: 100 #限流每秒QPS次数的限制

熔断

添加依赖:

<dependency>
    <groupId>org.apache.servicecomb</groupId>
    <artifactId>handler-bizkeeper</artifactId>
</dependency>

添加配置:

servicecomb:
   circuitBreaker: 
   	provider: 
   	shicifang-qa:
requestVolumeThreshold: 1
  fallbackpolicy:
    provider:
      policy: returnnull #容错策略
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐