SpringCloud study notes (XI, SpringCloud summary)

SpringCloud Config:

1, dynamic refresh configuration: job by calling Set <String> keys = contextRefresher.refresh ( ) code can be.

2, how to implement custom health check: Rewrite AbstractHealthIndicator doHealthCheck method on it.

See: https://www.cnblogs.com/bzfsdr/p/11589902.html

SpringCloud Netflix Eureka:

1, the service discovery: service discovery two modes, the main difference is that the main logic determines who it .

  • Client Mode: When calling other services First registration center to get the list of services , and a local cache , then according to the client service call its own load balancing strategy .
  • Server mode: When calling other services directly initiate a request to the registry , registration service call center according to its own load balancing policy , and the client does not need to maintain service discovery logic.

2, service discovery comparison between two models:

  • Client Mode: as Eureka.
    • Due to a list of services will, so when calling the service will reduce the first call link in the local cache; but each client needs to maintain its own list of services.
    • High availability; because there is a local cache, so even if the registry is down will not affect the normal use of the room service.
    • When the offline client brief call failed appears on the server.
  • Server mode: as Zookeeper.
    • Simple to use, maintenance-free client service list.
    • Availability depends on the registry, if a registry fails then all of the services are not available; at the same time store and recall all of the work completed by the registry, so the registry load is too high.
    • No off-line perception of the client on the server.

3, Eureka availability:

We all know that Eureka is divided into server and client , so when building a highly available Eureka both of which require high availability .

a, the server:

High availability server actually is to make the client less possible failure of getting the list of services , so we just need to start two or more Server Eureka , let them copy each other a list of services can be.

1 server.port=9091
2 eureka.client.service-url.defaultZone=http://localhost:9092/eureka
1 server.port=9092
2 eureka.client.service-url.defaultZone=http://localhost:9091/eureka

b, client

The client's availability is in as little as failed to obtain the list of services , so we only need to configure multiple registries can .

1 eureka.client.service-url.defaultZone=http://localhost:9091/eureka,http://localhost:9092/eureka

4, Eureka principle:

  • Default 30 seconds, eureka client regularly pull a list of services eureka server configuration.
  • The service offline eureka server will notify the client subscribes to update the list of services eureka, and will also notify the other eureka server.
  • eureka client sends a heartbeat to the default 30 seconds eureka server, and eureka server will not send the heartbeat eureka client offline in 90 seconds, and notifies the eureka client, eureka server.

eureka server protection mechanism: due eureka server may be because of network problems do not receive other services heartbeat, so eureka server does not blind the service offline.

eureka server protection rules: calculate if 85% of the services are not sending heartbeat, if it is to enter the protection mechanism, within this period eureka server would normally accept the registration, tracking for some time, but does not inform the data to eureka client and other eureka server, so it does not manslaughter.

SpringCloud Ribbon:

Re-construct a new style based on the rest of RestTemplate, so call service is able to achieve a load balancing.

1  @Bean
 2  @LoadBalanced
 3  public Residual Template rest template () {
 4      return  new Residual Template ();
5 }

Configuration properties:

1 ## local configuration - individually developed client (eureka- Provider Client)
 2 Eureka-provider.ribbon.listOfServers = localhost: 8072, localhost: 8073

配置格式:<clientName>.<nameSpace>.<propertyName>=<value>

propertyName见com.netflix.client.config.CommonClientConfigKey

SpringCloud Hystrix:

Hystrix is an open source distributed fault-tolerant and delay library , the purpose is to isolate the fault of distributed services . It also provides an elegant degradation and blown implementations, allow the service to fail fast, and fast recovery from failure.

1, Hystrix solve the problem:

  • Achieved by isolating threads and semaphores Limitations of distributed service resources , when there is a service problem does not affect other services.
  • Elegant downgrade mechanism (overtime, lack of resources is downgraded), after downgrading to return to the underpinning data.
  • Optimized fuse mechanism, the failure rate reaches the threshold automatically downgraded, and can be quickly recovered.
  • Provide request merged and request caching .

2, thread isolation: The Tomcat request tasks to be performed inside its own thread, so Tomcat can respond to more requests, then the internal thread after the implementation of the results forwarded to Tomcat.

3, the semaphore isolation: thread spacer similar, except that the amount of signal isolation procedure is an internal limiting.

4, the amount of isolation and signal isolation threads differences:

 

Thread Pool

signal

Thread

And non-scheduled thread the same thread

And scheduling thread is the same thread

Spending

Queuing, scheduling, and other overhead context switching

Radio switching process, low cost

asynchronous

stand by

not support

Concurrent support

Support (determined by the size of the thread pool)

Support (determined by the size of the semaphore)

 

 

 

 

 

 

 

Note : The service must meet fusing time, the number of requests, failure rates three conditions will trigger the circuit breaker.

SpringCloud Feign:

Feign is an open source declarative, template-based HTTP client, it can be more convenient and elegant calling HTTP API; and SpringCloud Feign is an enhancement to the Netflix Feign, to support Ribbon, Eureka.

When using only need to define an interface, the interface coupled scan annotation @EnableFeignClients (basePackages = { "com.xxx.xxx"}) , and the same method as call can be a call interface.

SpringCloud Zuul:

Zuul is open source Netflix gateways, primarily for routing and filtering.

It is also based on the JVM routers and load balancers, so it's rules engine allows the use of any JVM language, such as java, groovy.

1, Zuul role:

  • Journal
  • pressure test
  • Dynamic Routing
  • safety certificate
  • and many more

2, Zuul request lifecycle:

  • pre: process the request before forwarding the request, such as logs, request checking.
  • route: forwards the request to a specific service provider.
  • post: some processing result received after returning to the service provider to do, such as data processing, content transformation, etc. (data desensitization).
  • error: error filter enabled exception occurs when a request.

3, the custom filter Zuul

 1 package com.jdr.maven.sc.integration.zuul.filter;
 2 
 3 import com.netflix.zuul.ZuulFilter;
 4 import com.netflix.zuul.exception.ZuulException;
 5 import org.springframework.beans.factory.annotation.Value;
 6 import org.springframework.cloud.context.config.annotation.RefreshScope;
 7 import org.springframework.stereotype.Component;
 8 
 9 import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_TYPE;
10 
11 /**
12  * @author zhoude
13  * @date 2019/10/20 9:44
14  */
15 @Component
16 @RefreshScope
17 public class LogFilter extends ZuulFilter {
18 
19     @Value("${zuul.log-name}")
20     private String logName;
21 
22     /**
23      * 过滤器类型
24      *
25      * @return pre、route、post、err
26      */
27     @Override
28     public String filterType() {
29         return PRE_TYPE;
30      }
 31 is  
32      / ** 
33       execution order filter *, the smaller the first implementation
 34 is       *
 35       * @return execution sequence
 36       * / 
37 [      @Override
 38 is      public  int filterOrder () {
 39          return 0 ;
 40      }
 41 is  
42 is      / * * 
43       * should the filter (this function can increase the number of logic switches, to achieve dynamic control)
 44 is       *
 45       * @return true-filtering should be performed, the filter should not be performed false-
 46 is       * / 
47      @Override
 48      public  BooleanshouldFilter () {
 49          return  to true ;
 50      }
 51 is  
52 is      / ** 
53 is       * filter specific execution logic
 54 is       *
 55       * @return target
 56 is       * @throws ZuulException performed thrown when an abnormality occurs ZuulException
 57 is       * / 
58      @Override
 59      public Object RUN () throws ZuulException {
 60          System.err.println ( "this is a log" + logName);
 61 is          return  null ;
 62 is      }
 63 is }

SpringCloud Bus:

SpringCloud Bus is a distributed actuator, it can serve as a communication channel between applications, broadcast (configuration change) when the system state changes.

Guess you like

Origin www.cnblogs.com/bzfsdr/p/12064091.html