ServiceComb
- 一,概述
ServiceComb是apache组织的一款微服务框架,前身是华为云的微服务引擎CSE。官网:http://servicecomb.apache.org/。设计上,包括编程模型,通信模型,运行模型。官网介绍:
Type | artifact id | Available or NOT | Function |
Programming model | provider-pojo | Yes | Provides the RPC development mode. |
Programming model | provider-jaxrs | Yes | Provides the JAX-RS development mode. |
Programming model | provider-springmvc | Yes | Provides the Spring MVC development mode. |
Communication on model | transport-rest-vertx | Yes | A development framework running over HTTP, it does not depend on Web containers. Applications are packaged as executable .jar files. |
Communication on model | transport-rest-servlet | Yes | A development framework running on Web container. Applications are packaged as WAR files. |
Communication on model | transport-highway | Yes | Provides high-performance private communication protocols for Java communication. |
Running model | handler-loadbalance | Yes | A load balancing module that provides various routing policies and configurations. It is usually used for clients. |
Running model | handler-bizkeeper | Yes | Provides service governance functions, such as isolation, fallbreak, and fault tolerance. |
Running model | handler-tracing | Yes | Provides the capability of developing and managing TCC transactions. |
- 二,开发准备
安装java开发环境,ide等。下载serviceComb注册中心软件。
- 三,服务注册中心
服务中心,用于服务元数据以及服务实例元数据的管理和处理注册、发现。ServiceComb的服务注册中心CSE,cloud service engine,云服务引擎,可以将微服务发布到云平台。
- 四,快速开发脚手架
访问官方网址http://start.servicecomb.io/。配置,下载脚手架工程。
启动服务注册中心。启动demo项目。访问hello路径。
- 五,使用ServiceComb框架开发RESTFUL微服务项目的demo
创建一个site-simple的maven工程serviceCombDemo。
创建三个子模块。serviceInterface,serviceProvider,serviceConsumer。
注意,springboot启动类需要和定义RestSchema的类放在同一个目录下或者springboot启动类放在RestSchema类的父目录下(不可以是父目录的非同目录的子目录下)。比如目录结构为com/s/service/impl
,RestServiceImpl是一个服务提供类,里面定义了@RestSchema(schemaId = "providerDemo")注解,那么启动类可以在com目录,s目录,service目录或者impl目录下,但是不能在com/s/root下,否者向serviceComb注册中心注册会找不到schame。
Demo的Github地址:https://github.com/hellohaha88/serviceCombDemo/tree/master
- 六,使用ServiceComb开发RPC通信方式微服务的demo
创建一个site-simple的maven工程rpcServiceCombDemo。
创建三个子模块。serviceInterface,rpcProvider,rpcConsumer。
Demo的github地址:https://github.com/hellohaha88/serviceCombRpcDemo/tree/master
- 七,ServiceComb微服务治理
ServiceComb的脚手架页面,可选微服务治理依赖。生成demo,可以看到引入的微服务依赖项。
在服务提供者的pom依赖添加:
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>handler-flowcontrol-qps</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>handler-bizkeeper</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>handler-tracing-zipkin</artifactId>
</dependency>
然后,修改服务提供者的yaml文件
APPLICATION_ID: start.servicecomb.io
service_description:
name: provider
version: 0.0.1
servicecomb:
circuitBreaker:
Provider:
HelloServiceComb:
requestVolumeThreshold: 8
fallbackpolicy:
provider:
policy: returnnull
flowcontrol:
Provider:
qps:
limit:
gateway: 1000
handler:
chain:
Provider:
default: qps-flowcontrol-provider,bizkeeper-provider,tracing-provider
rest:
address: 0.0.0.0:9080
service:
registry:
address: http://127.0.0.1:30100
autodiscovery: false
1,负载均衡策略
ServiceComb的负载均衡方案基于Ribbon,可以配置负载均衡策略, 支持的策略有随机、顺序、基于响应时间的权值等方案。
在Edit Configurations里面设置,将服务提供者的启动类的运行模式设置为share,为了便于调试,将启动类设置为允许并行运行。
在提供服务的方法里面,打印服务提供者序号。
启动一个服务实例后,修改服务的端口和打印的服务提供者序号,然后再次启动一个服务。
启动服务消费者。
浏览器访问服务消费者,查看服务提供者打印的服务者序号日志
2,限流策略
限流是微服务框架中常见的系统保障措施。提前预测系统的吞吐量,当请求超过预期的阈值时可以采取一些限制请求流量措施来保障系统的稳定运行,比如延迟处理、拒绝服务等。
ServiceComb基于zuul网关实现限流。
创建gateWay子模块
添加zuul网关依赖
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>spring-cloud-zuul-zipkin</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>spring-cloud-zuul</artifactId>
</dependency>
添加application.yml配置文件
server:
port: 9100
zuul:
routes:
consumer_service:
serviceId: consumer_service
discoveryServer:
ribbon:
eureka:
enabled: false
servicecomb:
tracing:
enabled: true
添加microservice.yaml
APPLICATION_ID: start.servicecomb.io
service_description:
name: gateway
version: 0.0.1
servicecomb:
tracing:
collector:
address: http://127.0.0.1:9411
service:
registry:
address: http://127.0.0.1:30100
编写启动类
@SpringBootApplication
@EnableServiceComb
@EnableZuulProxy
public class ZuulGateApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulGateApplication.class, args);
}
@Bean
public CorsFilter corsFilter(){
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
final CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
config.setMaxAge(18000L);
config.addAllowedMethod("OPTIONS");
config.addAllowedMethod("HEAD");
config.addAllowedMethod("GET");
config.addAllowedMethod("POST");
config.addAllowedMethod("PUT");
config.addAllowedMethod("DELETE");
config.addAllowedMethod("PATCH");
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}
}
在需要限流的子模块的配置文件里设置限流
APPLICATION_ID: start.servicecomb.io
service_description:
name: consumer_service
version: 0.0.1
servicecomb:
circuitBreaker:
Provider:
provider_service:
requestVolumeThreshold: 1
fallbackpolicy:
provider:
policy: returnnull
flowcontrol:
Provider:
qps:
limit:
gateway: 1
handler:
chain:
Provider:
default: qps-flowcontrol-provider,bizkeeper-provider,tracing-provider
rest:
address: 0.0.0.0:9003
service:
registry:
address: http://127.0.0.1:30100
autodiscovery: false
使用jmeter设置zuul网关地址:http://localhost:9100/consumer_service/msgTest/msgStr?msg=abcadsdf这个地址的压测。并且访问zuul网关地址,http://localhost:9100/consumer_service/msgTest/msgStr?msg=abcadsdf。
查看返回429服务拒绝表示限流生效。
Demo的git地址:https://github.com/hellohaha88/serviceCombDemo/tree/master
- 八,熔断机制
CircuitBreaker,熔断。网络中,当下游服务器因访问压力过大而响应延迟过度或者服务失败,上游服务器为了保持系统的可用性,暂时切断对下游服务的调用,就是网络中的熔断机制。
测试熔断效果。
在需要熔断的子模块,一般是操作数据库的模块,添加相应的依赖项。在配置文件添加配置。
APPLICATION_ID: start.servicecomb.io
service_description:
name: provider_service
version: 0.0.1
servicecomb:
circuitBreaker:
Provider:
HelloServiceComb:
requestVolumeThreshold: 1
fallbackpolicy:
provider:
policy: returnnull
flowcontrol:
Provider:
qps:
limit:
gateway: 1000
handler:
chain:
Provider:
default: qps-flowcontrol-provider,bizkeeper-provider,tracing-provider
rest:
address: 0.0.0.0:9081
service:
registry:
address: http://127.0.0.1:30100
autodiscovery: false
启动所有服务,然后关闭数据库应用程序,在浏览器或其他方式继续请求服务,可以看到服务失败。再次启动数据库程序,请求服务,服务请求成功,熔断机制发挥了作用。
Demo的git地址:https://github.com/hellohaha88/serviceCombDemo/tree/master