用户工具

站点工具


code:java:spring-cloud-commons

Spring Cloud Commons

Spring Cloud Commons 就两个package,一个 client, 一个 commons, commons 包下是 HttpClient 的封装和几个 utils 工具类。

从 spring cloud 的定位看出来,主要是一些客户端调用的封装。client 包下主要分以下几个模块:

  1. circuitbreaker
  2. discovery
  3. loadbalancer
  4. serviceregistry

综合起来看就是分布式服务的注册、发现,调用时的负载均衡以及熔断。

再详细理解一下每个功能模块的设计:

1. 服务注册

先来看看类图,包含了Spring Cloud 对服务的抽象:

ServiceInstanceString getServiceId();String getHost();int getPort();boolean isSecure();URI getUri();Map<String, String> getMetadata();String getScheme();RegistrationServiceRegistryvoid register(Registration registration);void deregister(Registration registration);void close();void setStatus(Registration registration, String status);String getStatus(R registration);AutoServiceRegistrationPropertiesAutoServiceRegistrationAbstractAutoServiceRegistrationboolean autoStartu;int order;AtomicInteger port;ServiceRegistry<R> serviceRegistry;AutoServiceRegistrationProperties propertiesServiceRegistryEndpointServiceRegistry serviceRegistry;Registration registration;void setStatus(String status);String getStatus();

也就是每一个应用节点都有一个 ServiceRegistry 用于管理服务注册信息,服务注册信息是 Registration.

2. 服务发现

一样的看类图:

ServiceInstanceSimpleServiceInstanceDiscoveryClientdescription():StringgetInstances(serviceId): List<ServiceInstance>getServices(): List<String>SimpleDiscoveryPropertiesMap<String, List<SimpleServiceInstance>> instances;SimpleDiscoveryClientSimpleDiscoveryProperties simpleDiscoveryProperties;CompositeDiscoveryClientList<DiscoveryClient> discoveryClient;List<DiscoveryClient> getDiscoveryClients();DiscoveryHealthIndicatorString getName();Health health();DiscoveryClientHealthIndicatorPropertiesDiscoveryClientHealthIndicator

核心就是 DiscoveryClient 提供从客户端获取服务实例的能力。

3. 负载均衡

ServiceInstanceChooserServiceInstance choose(String serviceId);LoadBalancerRequestT apply(ServiceInstance instance)LoadBalancerClient<T> T execute(String serviceId, LoadBalancerRequest<T> request);<T> T execute(String serviceId, ServiceInstance serviceInstance, LoadBalancerRequest<T> request);URI reconstructURI(ServiceInstance instance, URI original);LoadBalancedRetryContextHttpRequest request;ServiceInstance serviceInstanceLoadBalancedRetryPolicyboolean canRetrySameServer(LoadBalancedRetryContext context)boolean canRetryNextServer(LoadBalancedRetryContext context)void close(LoadBalancedRetryContext context)void registerThrowable(LoadBalancedRetryContext context, Throwable throwable)boolean retryableStatusCode(int statusCode)LoadBalancedRetryFactoryLoadBalancedRetryPolicy createRetryPolicy(String service, ServiceInstanceChooser serviceInstanceChooser)RetryListener[] createRetryListeners(String service)BackOffPolicy createBackOffPolicy(String service)LoadBalancerRequestTransformerHttpRequest transformRequest(HttpRequest request, ServiceInstance instance)

code/java/spring-cloud-commons.txt · 最后更改: 2019/06/23 15:28 由 qiyi