Written in front: I am "sailing to the sun". This nickname comes from my name and my desire for knowledge. I love technology, open source and programming. Technology is open source and knowledge is shared
This blog is a little summary and record of your study. If you are interested in Java and algorithms, you can pay attention to my dynamics and we will study together.
With knowledge to change the fate, let our family live a better life, chose this road, I have no retreat, I can only go all out.

1. Learn the preface
1.1 content overview
Learning premise:
- JavaSE
- Database (MySQL + Redis)
- Front end (HTML +CSS + JS)
- Servlet(Tomcat)
- Http
- Mybatis + Spring + SpringMVC
- Springboot
- Dubbo + Zookeeper + distributed infrastructure
- Maven + Git
- Ajax + Json
- ...
How to learn at this stage?
The basic development thinking is similar to the framework model: three-tier architecture + MVC, which later evolved into a standardized framework.
- Spring (lightweight Java open source framework): solve the complexity of enterprise development, IOC and AOP
- Springboot (upgraded version of spring): a new generation of Java EE development standard automatic assembly
- Modular ~ all in one modular development, all in one code has not changed
Core issues in the era of micro services
- There are many services. How can clients access and provide external gateways?
- With so many services, how do services communicate? HTTP or RPC?
- How to manage so many services? Service registration and discovery.
- What if the service hangs up? circuit breaker
Micro service solution selection:
- Spring cloud Netflix (maintenance stopped): one stop solution! It can solve the above four core problems
API gateway: zuul component
Communication: feign -- httpclient -- HTTP communication mode, synchronization, blocking
Service registration and discovery: Eureka
Fuse mechanism: Hystrix
...
- Apache Dubbo Zookeeper: semi automatic! Need to integrate other people's
API gateway: No, find a third-party component (such as integrating zull components) or implement it yourself
Communication: Dubbo is a high-performance RPC communication framework based on Java (performance is stronger than Feign)
Service registration and discovery: Zookeeper
Circuit breaker mechanism: No, need to use Hystrix
- Spring Cloud Alibaba: the latest one-stop solution! It can solve the above four core problems, which is simpler
API gateway:
signal communication:
Service registration and discovery:
Fuse mechanism:
- New concept: Service Grid ~ Server Mesh istio
1.2 common interview questions
1.1 what is micro service?
1.2 how do microservices communicate independently?
1.3 what are the differences between springcloud and Dubbo?
1.4 SpringBoot and SpringCloud, please talk about your understanding of them
1.5 what is service fusing? What is service degradation?
1.6 what are the advantages and disadvantages of microservices? Tell me about the pitfalls you encountered in project development
1.7 what are the micro service technology stacks you know? List one or two
1.8 Eureka and Zookeeper can provide the functions of service registration and discovery. Please tell us the difference between them
2. Overview of microservices
2.1 what is micro service?
What is micro service? Microservice architecture is a popular architecture idea in recent years. It is difficult to sum up its concept.
What exactly is a micro service? We hereby quote Martin Fowler, chief scientist of ThoughtWorks, who put forward a paragraph in 2014:
Original text: https://martinfowler.com/articles/microservices.html
Sinicization: https://www.cnblogs.com/liuning8023/p/4493156.html
- At present, there is no unified and standard definition of microservices in the industry.
- But generally speaking, microservice architecture is an architectural mode, or architectural style. Its length divides a single application into a group of small services. Each service runs in its own independent process. Services coordinate and configure with each other to provide final value for users. Services communicate with each other through lightweight communication mechanism (HTTP). Each service is built around specific business, In addition, a unified and centralized service management mechanism should be avoided as far as possible. For a specific service, an appropriate language should be selected according to the business context, and the tool (Maven) should be used to build it. There can be a very lightweight centralized management to coordinate these services. Services can be written in different languages or different data stores.
- Microservice is an architecture mode. It is more reasonable to call microservice architecture, which is to disassemble each function point in a system into small applications and then deploy them separately. At the same time, because there are many small applications, some methods are needed to manage these small applications.
From the perspective of Technology:
- The core of microservicing is to divide the traditional one-stop application into one service according to the business and completely decouple it. Each microservice provides a service with a single business function, and one service does one thing. From a technical point of view, it is a small and independent processing process. The concept of similar process can be started or destroyed independently and has its own independent database.
2.2 microservices and microservice architecture concepts
Microservices
It emphasizes the size of the service. It focuses on a certain point. It is a service application that specifically solves a certain problem / provides landing corresponding services. In a narrow sense, it can be regarded as micro service projects in the IDEA or Moudel.
In the IDEA tool, independent small Moudel developed by Maven is used. It specifically uses a small module developed by SpringBoot. Professional things are done by professional modules, and each module does one thing. The emphasis is on individuals, and each individual completes a specific task or function.
Microservice architecture
A new form of architecture was proposed by Martin Fowler in 2014. Microservice architecture is an architecture mode. Its body length divides a single application into a group of small services. Services coordinate and cooperate with each other to provide final value for users. Each service runs in its own independent process. Lightweight communication mechanisms (such as HTTP) are used between services to collaborate with each other. Each service is built around specific businesses and can be independently deployed to the production environment. In addition, unified and centralized service management mechanisms should be avoided as far as possible. For a specific service, appropriate languages should be selected according to the business context Tools * * (such as Maven) * * build it.
2.3 advantages and disadvantages of microservices
advantage
- Single responsibility principle: each service is cohesive and small enough, and the code is easy to understand, so as to focus on a specified business function or business requirement;
- Development is simple and efficient. A service may be dedicated to only one thing;
- Microservices can be developed independently by a small team, which only needs 2-5 developers;
- Microservices are loosely coupled and functionally meaningful services, which are independent in both development and deployment stages;
- Microservices can be developed in different languages;
- It is easy to integrate with third parties. Microservices allow easy and flexible integration and automatic deployment through continuous integration tools, such as jenkins, Hudson and bamboo;
- Microservices are easy to be understood, modified and maintained by a developer, so that small teams can pay more attention to their own work results and reflect their value without cooperation;
- Microservices allow the use and integration of the latest technologies;
- Microservices are just the code of business logic and will not be mixed with HTML, CSS or other interfaces;
- Each microservice has its own storage capacity. It can have its own database or a unified database;
shortcoming
- Developers need to deal with the complexity of distributed systems;
- Multi service operation and maintenance is difficult. With the increase of services, the pressure of operation and maintenance is also increasing;
- System deployment dependency;
- Communication cost between services;
- Data consistency;
- System integration test problems;
- Performance and monitoring issues;
2.4 what are the micro service technology stacks?
Microservice Technology Entry
Landing technical service development, Spring boot, Spring MVC and other service configuration and management
Netfix
Archaius and Ali of the company
Diamond
Registration and discovery of other services
Eureka Consul Zookeeper
And other services call Rest, PRC, gRPC service fuses
Hystrix
, Envoy and other load balancing ribbons, Nginx and other service interface calls (simplified tools for clients to call services) Fegin and other message queues Kafka, RabbitMQ, ActiveMQ and other service configuration centers manage SpringCloudConfig, Chef and other service routes (API gateway) Zuul and other services monitor Zabbix, Nagios, Metrics, specifier and other full link tracking Zipkin, Brave Dapper and other data flow operation development packages spring cloud stream (encapsulating sending and receiving messages with Redis, Rabbit, Kafka, etc.) time message stack spring cloud bus service deployment Docker, OpenStack, Kubernetes, etc
2.5 why spring cloud is chosen as the microservice architecture
- Selection basis
- Overall solution and framework maturity
- Community heat
- Maintainability
- learning curve
- What are the current micro service architectures used by major IT companies?
- Ali: dubbo+HFS
- JD: JFS
- Sina: Motan
- Dangdang: DubboX
Comparison of micro Service Frameworks
Function point / service framework
Netflix/SpringCloudMotangRPCThriftDubbo/DubboX function locates the complete micro service framework RPC framework, but integrates ZK or Consul to realize the basic service registration in the cluster environment. It is found that RPC framework RPC framework service framework supports Rest yes, Ribbon supports a variety of pluggable serial numbers. No, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, Zuul service, dynamic routing, cloud load balancing, Eureka (for middle tier server) Yes (client) no (client) configuration service Netfix Archaius, Spring Cloud Config Server centralized configuration Yes (provided by Zookeeper) no no no no service call chain monitoring Yes (zuul), zuul provides edge services, API gateway no high availability / fault tolerance Yes (server-side Hystrix + client Ribbon) Yes (client-side) no no yes (client-side) typical application case NetflixSinaGoogleFacebook community is highly active, generally high, and maintenance will be restarted after 2017, After five years of interruption, the learning difficulty is medium, low, high and low, and the richness of documents is generally high. Other spring cloud buses have brought more management endpoints to our applications, and support degradation. There are more companies developing and integrating gRPCIDL definition practice within Netflix
3. Introduction to spring cloud
3.1 what is spring cloud?
Spring official website: https://spring.io/
Official description:
Spring Cloud provides tools for developers to quickly build some of the common patterns in distributed systems (e.g. configuration management, service discovery, circuit breakers, intelligent routing, micro-proxy, control bus, one-time tokens, global locks, leadership election, distributed sessions, cluster state). Coordination of distributed systems leads to boiler plate patterns, and using Spring Cloud developers can quickly stand up services and applications that implement those patterns. They will work well in any distributed environment, including the developer's own laptop, bare metal data centres, and managed platforms such as Cloud Foundry.
Translation:
Spring Cloud provides developers with tools to quickly build some common patterns in distributed systems (such as configuration management, service discovery, circuit breaker, intelligent routing, micro agent, control bus, one-time token, global lock, leader election, distributed session, cluster state). The coordination of distributed systems leads to boiler board patterns, and developers using Spring Cloud can quickly establish services and applications to implement these patterns. They work well in any distributed environment, including developers' own laptops, bare metal data centers and managed platforms such as cloud computing.
In short, Spring Cloud is a specification of a microservice framework. Note that it is only a specification, not any specific framework. The favorite approach of java bosses is to formulate their own specifications, and then others implement them based on my specifications. So what's in this specification? It stipulates that it should have the following functions.
Service registration and discovery
load balancing
Service fusing and current limiting
Intelligent routing
Control bus
Link monitoring
...
Just at this time, there is a framework collection that can meet almost all the above requirements, which is Spring Cloud Netflix. Of course, the implementation products of Spring Cloud are not only this one, but also Spring Cloud Alibaba, which is newly launched by Alibaba recently. At present, Spring Cloud Netflix is the mainstream in China.
3.2 relationship between springcloud and SpringBoot
- SpringBoot focuses on the convenient development of individual micro services;
- SpringCloud is a microservice coordination and management framework that focuses on the overall situation. It integrates and manages individual microservices developed by SpringBoot, and provides integrated services between microservices: configuration management, service discovery, circuit breaker, routing, proxy, event stack, global lock, decision-making campaign, distributed session and so on;
- SpringBoot can be used independently of SpringCloud to develop projects, but SpringCloud cannot be separated from SpringBoot, which belongs to dependency;
3.3 Dubbo and SpringCloud technology selection
1. Distributed + service governance Dubbo
The current mature Internet architecture, application service splitting + message middleware
2. Comparison between Dubbo and spring cloud
Take a look at community activity:
dubbo: https://github.com/dubbo
SpringCloud: https://github.com/spring-cloud
Comparison results:
Dubbosprincloud service registry ZookeeperSpring Cloud Netfilx Eureka service calling method RPCREST API service monitoring Dubbo monitorspring boot admin circuit breaker imperfect Spring Cloud Netfilx Hystrix service gateway no Spring Cloud Netfilx Zuul distributed configuration no Spring Cloud Config service tracking no Spring Cloud Sleuth message stack no Spring Cloud Bus data flow no Spring Cloud Stream batch task no Spring Cloud Task
The biggest difference: Spring Cloud abandons Dubbo's RPC communication and adopts HTTP based REST
Strictly speaking, these two methods have their own advantages and disadvantages. Although to some extent, the latter sacrifices the performance of service invocation, it also avoids the problems caused by the above-mentioned native RPC. Moreover, REST is more flexible than RPC. The dependence of service providers and callers only depends on a contract, and there is no strong dependence at the code level. This advantage is more appropriate in the current microservice environment that emphasizes rapid evolution.
Dubbo and spring cloud are somewhat similar to the difference between brand machine and assembly machine
Summary:
The two solve different problem domains: Dubbo is positioned as an RPC framework, while the goal of spring cloud is a one-stop solution under the microservice architecture.
3.4 what can spring cloud do?
- Distributed/versioned configuration
- Service registration and discovery
- Routing
- Service to service calls
- Load balancing configuration
- Circuit Breakers
- Distributed messaging
- ...
3.5 spring cloud Download
Official website: http://projects.spring.io/spring-cloud/
The version number is a little special:
Instead of naming the version number by numerical number, spring cloud uses the name of London underground station, and corresponds to the version chronological order according to the order of alphabet. The reason is that spring cloud is an integration of several modules. Each small module has a different version. It is inappropriate to use that version number alone.
Self study reference books:
- SpringCloud Netflix Chinese document: https://springcloud.cc/spring-cloud-netflix.html
- SpringCloud Chinese API document (official document translation): https://springcloud.cc/spring-cloud-dalston.html
- SpringCloud China Community: http://springcloud.cn/
- SpringCloud Chinese website: https://springcloud.cc
4. Construction of spring cloud rest learning environment: Service Provider
4.1 introduction
- We will use a Dept Department module as a general case of micro services. The Consumer consumer (Client) calls the services provided by the Provider (Server) through REST.
- Review the previous knowledge of Spring, Spring MVC, Mybatis, etc.
- Review of Maven's subcontracting module architecture.
A simple Maven The module structure is as follows: -- app-parent: A parent item(app-parent)Aggregated many subprojects(app-util\app-dao\app-web...) |-- pom.xml | |-- app-core ||---- pom.xml | |-- app-web ||---- pom.xml ......
One parent project has multiple Moudule sub modules, and MicroServiceCloud parent project has three sub modules for the first time
- Microservicecloud API [encapsulated overall entity / interface / public configuration, etc.]
- microservicecloud-consumer-dept-80 [service provider]
- microservicecloud-provider-dept-8001 [serving consumers]
4.2 spring cloud version selection
Large Version Description
SpringBootSpringCloud relationship 1.2 Xangel Version (angel) is compatible with springboot1.0 2x1. 3. Xbrixton Version (Brixton) is compatible with springboot1 3x, also compatible with springboot1 4x1. The 4.x Camden Version (Camden) is compatible with springboot1 4X, also compatible with springboot1 5x1. 5. Xdalston Version (dorston) is compatible with springboot1.0 5x, incompatible with springboot2 0x1. 5.xedgware Version (edgwell) is compatible with springboot1.0 5x, incompatible with springboot2 0x2. The 0.xfinchley Version (Finchley) is compatible with springboot 2 0x, incompatible with springboot1 5x2. 1. Xgreenwich Version (Greenwich)
Actual development version relationship
spring-boot-starter-parentspring-cloud-dependencles
Version number release date version number release date
1.5.2.RELEASE2017-03Dalston.RC12017-x1.5.9.RELEASE2017-11Edgware.RELEASE2017-111.5.16.RELEASE2018-04Edgware.SR52018-101.5.20.RELEASE2018-09Edgware.SR52018-102.0.2.RELEASE2018-05Fomchiey.BULD-SNAPSHOT2018-x2.0.6.RELEASE2018-10Fomchiey-SR22018-102.1.4.RELEASE2019-04Greenwich.SR12019-03
Use the last two
4.3 create parent project
- Create a new parent project springcloud. Remember that packaging is a pom mode
- It is mainly to define POM files and uniformly extract the jar packages shared by subsequent sub modules, which is similar to an abstract parent class
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0modelVersion> <groupId>com.haustgroupId> <artifactId>springcloudartifactId> <version>1.0-SNAPSHOTversion> <modules> <module>springcloud-apimodule> <module>springcloud-provider-dept-8001module> <module>springcloud-consumer-dept-80module> <module>springcloud-eureka-7001module> <module>springcloud-eureka-7002module> <module>springcloud-eureka-7003module> <module>springcloud-provider-dept-8002module> <module>springcloud-provider-dept-8003module> <module>springcloud-consumer-dept-feignmodule> <module>springcloud-provider-dept-hystrix-8001module> <module>springcloud-provider-dept-hystrix-8001module> <module>springcloud-consumer-hystrix-dashboardmodule> <module>springcloud-zuul-9527module> <module>springcloud-config-server-3344module> <module>springcloud-config-client-3355module> <module>springcloud-config-eureka-7001module> <module>springcloud-config-dept-8001module> modules> <packaging>pompackaging> <properties> <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> <maven.compiler.source>1.8maven.compiler.source> <maven.compiler.target>1.8maven.compiler.target> <junit.version>4.12junit.version> <log4j.version>1.2.17log4j.version> <lombok.version>1.18.8lombok.version> properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-dependenciesartifactId> <version>Greenwich.SR1version> <type>pomtype> <scope>importscope> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-dependenciesartifactId> <version>2.1.4.RELEASEversion> <type>pomtype> <scope>importscope> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> <version>5.1.46version> dependency> <dependency> <groupId>com.alibabagroupId> <artifactId>druidartifactId> <version>1.1.10version> dependency> <dependency> <groupId>org.mybatis.spring.bootgroupId> <artifactId>mybatis-spring-boot-starterartifactId> <version>1.3.2version> dependency> <dependency> <groupId>ch.qos.logbackgroupId> <artifactId>logback-coreartifactId> <version>1.2.3version> dependency> <dependency> <groupId>junitgroupId> <artifactId>junitartifactId> <version>${junit.version}version> dependency> <dependency> <groupId>log4jgroupId> <artifactId>log4jartifactId> <version>${log4j.version}version> dependency> <dependency> <groupId>org.projectlombokgroupId> <artifactId>lombokartifactId> <version>${lombok.version}version> dependency> dependencies> dependencyManagement> project>
The parent project is springcloud, under which there are multiple child Mudules. Please refer to the complete code for details
springcloud-consumer-dept-80 access the controller under springcloud-provider-dept-8001 and use REST mode
Such as deptconsumercontroller java
@RestController public class DeptConsumerController { @Autowired private RestTemplate restTemplate; private static final String REST_URL_PREFIX = "http://localhost:8001"; @RequestMapping("/consumer/dept/add") public boolean add(Dept dept){ return restTemplate.postForObject(REST_URL_PREFIX+"/dept/add",dept,Boolean.class); } @RequestMapping("/consumer/dept/get/{id}") public Dept get(@PathVariable("id") Long id){ return restTemplate.getForObject(REST_URL_PREFIX+"/dept/get/"+id,Dept.class); } @RequestMapping("/consumer/dept/list") public List<Dept> list(){ return restTemplate.getForObject(REST_URL_PREFIX+"/dept/list",List.class); } }
Before using resttemplet, you need to put it into the Spring container
ConfigBean.java
@Configuration public class ConfigBean { @Bean public RestTemplate getRestTemplate(){ return new RestTemplate(); } }
The dao interface of springcloud-provider-dept-8001 calls pojo under the springcloud API module. You can use the pom file in springcloud-provider-dept-8001 to import the methods that the springcloud API module depends on:
<dependency> <groupId>com.haustgroupId> <artifactId>springcloud-apiartifactId> <version>1.0-SNAPSHOTversion> dependency>
POM of springcloud-consumer-dept-80 and springcloud-provider-dept-8001 XML is basically the same as the dependency under the parent project. You can directly look at the complete code without adding duplicate notes.
5. Eureka service registration and discovery
5.1 what is Eureka
-
Netflix follows the API principle when it comes to Eureka
-
Eureka is a sub module of Netflix and one of the core modules. Eureka is a REST based service. It is used to locate services to realize service discovery and failover in the cloud middleware layer. Service registration and discovery are very important for microservices. With service registration and discovery, you can access services only by using the service identifier without modifying the service call configuration file. Its function is similar to Dubbo's registration center, such as Zookeeper.
5.2 understanding of Eureka principle
- Eureka basic architecture
-
Spring cloud encapsulates the Eureka module developed by Netflix to realize service registration and discovery (compared with Zookeeper)
-
Eureka adopts C-S architecture design. Eureka server is the server of service registration function. It is the service registration center
-
Other microservices in the system use Eureka's client to connect to Eureka server and maintain heartbeat connection. In this way, the maintenance personnel of the system can monitor the normal operation of each micro service in the system through EurekaServer. Some other modules of spring cloud (such as Zuul) can find other micro services in the system through EurekaServer and execute relevant logic
-
- Compare with Dubbo architecture
-
Eureka consists of two components: Eureka Server and Eureka Client
-
Eureka Server provides service registration. After each node is started, it will be registered in Eureka Server. In this way, the service registry in Eureka Server will store the information of all class service nodes, and the information of service nodes can be seen intuitively in the interface
-
Eureka Client is a Java client, which is used to simplify the interaction of Eureka Server. The client also has a built-in load balancer using polling load algorithm. After the application starts, a heartbeat will be sent to EurekaServer (the default cycle is 30 seconds). If Eureka Server does not receive the heartbeat of a node in multiple heartbeat cycles, Eureka Server will remove the service node from the service registry (the default cycle is 90s)
-
Three roles
- Eureka Server: provides service registration and discovery
- Service Provider: the service producer registers its own services in Eureka so that the service consumer can find them
- Service Consumer: the Service Consumer. Get the list of registered services from Eureka to find the consumer services
5.3 construction steps
1. eureka-server
- Establishment of springcloud-eureka-7001 module
- pom.xml configuration
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>springcloudartifactId> <groupId>com.haustgroupId> <version>1.0-SNAPSHOTversion> parent> <modelVersion>4.0.0modelVersion> <artifactId>springcloud-eureka-7001artifactId> <dependencies> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-eureka-serverartifactId> <version>1.4.6.RELEASEversion> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-devtoolsartifactId> dependency> dependencies> project>
- application.yml
server: port: 7001 eureka: instance: hostname: localhost client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
The default port and access path of Eureka in the source code:
- Main startup class
@SpringBootApplication @EnableEurekaServer public class EurekaServer_7001 { public static void main(String[] args) { SpringApplication.run( EurekaServer_7001.class,args); } }
- Access after successful startup http://localhost:7001/ Get the following page
2. eureka-client
Adjust the previously created springlouc-provider-dept-8001
- Import Eureka dependencies
<!--Eureka rely on--> <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka --> > >org.springframework.cloud> >spring-cloud-starter-eureka> >1.4.6.RELEASE> >
- Eureka configuration added in application
# Eureka configuration: configure the service registry address eureka: client: service-url: defaultZone: http://localhost:7001/eureka/
- Add to main startup class @EnableEurekaClient annotation
@SpringBootApplication @EnableEurekaClient public class DeptProvider_8001 { public static void main(String[] args) { SpringApplication.run(DeptProvider_8001.class,args); } }
- First start the 7001 server, then start the 8001 client for testing, and then access the monitoring page http://localhost:7001/ The results are shown in the figure. The service is registered successfully.
5. Modify the default description information on Eureka
eureka: client: service-url: defaultZone: http://localhost:7001/eureka/ instance: instance-id: springcloud-provider-dept-8001
The results are shown in the figure below:
If you stop springcloud-provider-dept-8001 at this time, the monitoring will turn on the protection mechanism after 30s
6. Configure monitoring information about service loading
pom. Adding dependencies to XML
<dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-actuatorartifactId> dependency>
application. Add configuration in YML
info: app.name: haust-springcloud company.name: com.haust
Refresh the monitoring page and click to enter
Jump to a new page and display the following contents:
3. EureKa self-protection mechanism: living is better than dying
In a word, if a micro service is unavailable at a certain time, eureka will not clean it immediately, but will still save the information of the micro service!
- By default, when eureka server does not receive the heartbeat of an instance within a certain period of time, it will delete the instance from the registry (the default is 90 seconds). However, if a large number of instance heartbeats are lost in a short period of time, eureka server's self-protection mechanism will be triggered. For example, during development and testing, microservice instances need to be restarted frequently, However, we rarely restart the eureka server together (because the eureka Registry will not be modified during the development process). When the number of heartbeats received in one minute decreases significantly, the protection mechanism will be triggered. You can see Renews threshold and Renews(last min) in the eureka management interface. When the latter (heartbeat received in the last minute) is less than the former (heartbeat threshold), the protection mechanism will be triggered and a red warning will appear: EMERGENCY!EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY’RE NOT. Renewals are lesser that threshold and hence the instances are not registering expired just to be safe. From the warning, eureka believes that although it cannot receive the heartbeat of the instance, it believes that the instance is still healthy. eureka will protect these instances and will not delete them from the registry.
- The purpose of this protection mechanism is to avoid network connection failure. In case of network failure, the micro service and the registration center cannot communicate normally, but the service itself is healthy and should not be cancelled. If eureka mistakenly deletes the micro service due to network failure, the micro service will not be re registered with eureka server even if the network is restored, because the registration request will be initiated only when the micro service is started, Only heartbeat and service list requests will be sent later. In this way, although the instance is running, it will never be perceived by other services. Therefore, when eureka server loses too many client heartbeats in a short time, it will enter the self-protection mode. In this mode, eureka will protect the information in the registry and will not log off any microservices. When the network fault recovers, eureka will automatically exit the protection mode. The self-protection mode can make the cluster more robust.
- However, in the development and testing phase, we need to restart publishing frequently. If the protection mechanism is triggered, the old service instance is not deleted. At this time, the request may run to the old instance, and the instance has been closed, which will lead to request errors and affect the development and testing. Therefore, in the development and testing phase, we can turn off the self-protection mode by adding the following in the eureka server configuration file to configure You can:
eureka.server.enable-self-preservation=false
For details, please refer to the following blog: protection mechanism
4. Register the micro service and get some messages (which will be used by team development)
springcloud-provider-dept-8001 add method deptcontroller java
@Autowired private DiscoveryClient client; @GetMapping("/dept/discovery") public Object discovery() { List<String> services = client.getServices(); System.out.println("discovery=>services:" + services); List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-DEPT"); for (ServiceInstance instance : instances) { System.out.println( instance.getHost() + "\t" + instance.getPort() + "\t" + instance.getUri() + "\t" + instance.getServiceId() ); } return this.client; }
Add in the main startup class @EnableDiscoveryClient annotation
@SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient public class DeptProvider_8001 { ... }
The results are shown in the figure below:
5.4 Eureka: cluster environment configuration
1. Initialization
New springcloud-eureka-7002 and springcloud-eureka-7003 modules
1. POM XML add dependency (same as springcloud-eureka-7001)
<dependencies> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-eureka-serverartifactId> <version>1.4.6.RELEASEversion> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-devtoolsartifactId> dependency> dependencies>
2.application.yml configuration (same as springcloud-eureka-7001)
server: port: 7003 eureka: instance: hostname: localhost client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
3. Main startup class (same as springcloud-eureka-7001)
@SpringBootApplication @EnableEurekaServer public class EurekaServer_7003 { public static void main(String[] args) { SpringApplication.run(EurekaServer_7003.class,args); } }
2. Cluster members are interrelated
Configure some custom local names, find the local hosts file and open it
At the end of the hosts file, add the local name to be accessed. The default is localhost. This step is used to simulate multi URL access.
Modify application The configuration of YML, as shown in the figure, is springcloud-eureka-7001 configuration, and springcloud-eureka-7002/springcloud-eureka-7003 can also be modified to their corresponding names
Associate springcloud-eureka-7001 with springcloud-eureka-7002 and springcloud-eureka-7003 in the cluster
Complete application under springcloud-eureka-7001 YML is as follows
server: port: 7002 eureka: instance: hostname: eureka7002.com client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/
At the same time, associate springcloud-eureka-7002 with springcloud-eureka-7001 and springcloud-eureka-7003 in the cluster
Complete application under springcloud-eureka-7002 YML is as follows
server: port: 7002 eureka: instance: hostname: eureka7002.com client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/
Spring cloud-eureka-7003 can be configured in the same way
Modify Eureka configuration through the yml configuration file under springcloud-provider-dept-8001: configure the service registry address
eureka: client: service-url: defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/ instance: instance-id: springcloud-provider-dept-8001
In this way, the simulation cluster is set up, and a project can be attached to three servers
5.5 difference between Eureka and Zookeeper
Review CAP principles
- RDBMS (MySQL,Oracle,sqlServer)
- A (Atomicity)
- C (Consistency)
- I (Isolation)
- D (Durability)
- NoSQL (Redis\MongoDB) ===> CAP
- C (Consistency) strong consistency
- A (Availability)
- P (Partition tolerance)
Three in two of CAP: CA, AP and CP. generally, P must be met!
4. The core of cap theory
- A distributed system cannot meet the three requirements of consistency, availability and partition fault tolerance at the same time
- According to the principle of CAP, NoSQL database is divided into three categories: meeting CA principle, meeting CP principle and meeting AP principle
- CA: single point cluster. Systems that meet consistency and availability usually have poor scalability
- CP: a system that meets consistency and partition fault tolerance. Generally, the performance is not particularly high
- AP: a system that meets the requirements of availability and partition fault tolerance, and may generally have lower requirements for consistency
5. As a distributed service registry, where is Eureka better than Zookeeper?
The famous CAP theory points out that A distributed system cannot meet C (consistency), A (availability) and P (fault tolerance) at the same time. Because partition fault tolerance P must be guaranteed in the distributed system, we can only trade off between A and C.
- Zookeeper guarantees CP
- Eureka guarantees AP
Zookeeper guarantees CP and underestimates availability
When querying the service list from the registry, we can tolerate that the registry returns the registration information a few minutes ago, but we can't receive the service and directly down it. In other words, the service registration function requires higher availability than consistency. However, there will be such a situation in zookeeper. When the master node loses contact with other nodes due to network failure, the remaining nodes will conduct leader election again. The problem is that the election leader takes too long, 30-120s, and the whole zookeeper cluster is unavailable during the election, which leads to the paralysis of the registration service during the election. In the cloud deployment environment, the loss of the master node of the zookeeper cluster due to network problems is an event with a high probability. Although the service can be restored eventually, the long election time leads to the long-term unavailability of registration, which is intolerable.
Eureka guarantees the AP and belittles the consistency
Eureka understands this, so it gives priority to ensuring availability in design. Eureka's nodes are equal. The failure of several nodes will not affect the work of normal nodes, and the remaining nodes can still provide registration and query services. When registering with an Eureka client, if it finds that the connection fails, it will automatically switch to other nodes. As long as one Eureka is still there, the availability of the registration service can be maintained, but the information found may not be up-to-date. In addition, Eureka also has a self-protection mechanism. If more than 85% of the nodes do not have a normal heartbeat within 15 minutes, Eureka thinks that there is a network failure between the client and the registry. At this time, the following situations will occur:
- Eureka is no longer removing services that should expire because they haven't received a heartbeat for a long time from the registration list
- Eureka can still accept the registration and query requests of new services, but it will not be synchronized to other nodes (that is, ensure that the current node is still available)
- When the network is stable, the new registration information of the current instance will be synchronized to other nodes
Therefore, Eureka can deal with the loss of contact of some nodes due to network failure without paralyzing the whole registration service like zookeeper.
6. Ribbon: load balancing (client based)
6.1 load balancing and Ribbon
-
What is Ribbon?
- Spring Cloud Ribbon is a set of client-side load balancing tools based on Netflix Ribbon.
- In short, Ribbon is an open source project released by Netflix. Its main function is to provide software load balancing algorithms for clients and connect Netflix's middle tier services together. The Ribbon client component provides a series of complete configuration items, such as connection timeout, Retry, etc. Simply put, it is to list all the configurations behind the LoadBalancer (LB: load balancer for short) in the configuration file. The Ribbon will automatically help you connect these machines based on certain rules (such as simple polling, random connection, etc.). We can also easily use Ribbon to implement custom load balancing algorithm!
What can Ribbon do?
-
LB, load balancer, is an application often used in microservices or distributed clusters.
-
Load balancing simply means that users' requests are evenly distributed to multiple services, so as to achieve the HA (high use) of the system.
-
Common load balancing software include Nginx, Lvs and so on.
-
Both Dubbo and spring cloud provide us with load balancing. The load balancing algorithm of spring cloud can be customized.
-
Simple classification of load balancing:
-
Centralized LB
- That is, an independent LB facility is used between the service provider and the consumer, such as Nginx, which is responsible for forwarding the access request to the service provider through some policy!
-
Enter program LB
- Integrate LB logic into the consumer. The consumer knows which addresses are available from the service registry, and then selects a suitable server from these addresses.
- Ribbon belongs to in-process LB, which is just a class library integrated into the consumer process. The consumer obtains the address of the service provider through it!
-
6.2 integrated Ribbon
springcloud-consumer-dept-80 to POM Add Ribbon and Eureka dependencies to XML
<dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-ribbonartifactId> <version>1.4.6.RELEASEversion> dependency> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-eurekaartifactId> <version>1.4.6.RELEASEversion> dependency>
In application Configure Eureka in YML file
eureka: client: register-with-eureka: false service-url: defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
Main startup class plus @EnableEurekaClient Annotation, open Eureka
@SpringBootApplication @EnableEurekaClient public class DeptConsumer_80 { public static void main(String[] args) { SpringApplication.run(DeptConsumer_80.class, args); } }
Custom Spring configuration class: configbean Java configuration load balancing to implement RestTemplate
@Configuration public class ConfigBean { @LoadBalanced @Bean public RestTemplate getRestTemplate() { return new RestTemplate(); } }
Modify the controller: deptconsumercontroller java
private static final String REST_URL_PREFIX = "http://SPRINGCLOUD-PROVIDER-DEPT";
6.3 load balancing using Ribbon
flow chart:
1. Create two new service providers: springcloud-provider-dept-8002 and springcloud-provider-dept-8003
2. Refer to springcloud-provider-dept-8001 to add POM for the other two clouds in turn XML dependency, mybatis under resourcece and application YML configuration, Java code
3. Start all service tests (determine the number of services to start according to your computer configuration), and access http://eureka7001.com:7002/ View results
Test access http://localhost/consumer/dept/list At this time, the service provider 8003 is accessed randomly
Visit again http://localhost/consumer/dept/list At this time, the random service provider 8001
Each visit http://localhost/consumer/dept/list Random access to a service provider in the cluster is called polling. The polling algorithm can be customized in spring cloud.
How to switch or customize rules?
Configure in the ConfigBean under the springcloud-provider-dept-80 module and switch to different rules
@Configuration public class ConfigBean { @LoadBalanced @Bean public RestTemplate getRestTemplate() { return new RestTemplate(); } @Bean public IRule myRule(){ return new RandomRule(); } }
You can also customize rules and customize a configuration class myRule under myRule package Java, note: this package should not be at the same level as the package where the main startup class is located, but at the same level as the package where the startup class is located:
MyRule.java
@Configuration public class MyRule { @Bean public IRule myRule(){ return new MyRandomRule(); } }
The main startup class enables load balancing and specifies a custom MyRule configuration class
@SpringBootApplication @EnableEurekaClient @RibbonClient(name = "SPRINGCLOUD-PROVIDER-DEPT",configuration = MyRule.class) public class DeptConsumer_80 { public static void main(String[] args) { SpringApplication.run(DeptConsumer_80.class, args); } }
Custom rules (here we refer to the default rule code in the Ribbon and change it slightly): myrandomrule java
public class MyRandomRule extends AbstractLoadBalancerRule { private int total = 0; private int currentIndex = 0; public Server choose(ILoadBalancer lb, Object key) { if (lb == null) { return null; } Server server = null; while (server == null) { if (Thread.interrupted()) { return null; } List<Server> upList = lb.getReachableServers(); List<Server> allList = lb.getAllServers(); int serverCount = allList.size(); if (serverCount == 0) { return null; } if (total < 5) { server = upList.get(currentIndex); total++; } else { total = 0; currentIndex++; if (currentIndex >= upList.size()) { currentIndex = 0; } server = upList.get(currentIndex); } if (server == null) { Thread.yield(); continue; } if (server.isAlive()) { return (server); } server = null; Thread.yield(); } return server; } protected int chooseRandomInt(int serverCount) { return ThreadLocalRandom.current().nextInt(serverCount); } @Override public Server choose(Object key) { return choose(getLoadBalancer(), key); } @Override public void initWithNiwsConfig(IClientConfig clientConfig) { } }
7.Feign: load balancing (based on the server)
7.1 Feign introduction
Feign is a declarative Web Service client, which makes it easier to call between microservices, similar to the controller calling service. Spring cloud integrates Ribbon and Eureka, and can use Feigin to provide a load balanced http client
Just create an interface and add annotations~
Community oriented programming, mainly for everyone. This is the norm for many developers. There are two methods to call microservice access
- Microservice name [ribbon]
- Interface and notes [feign]
What can Feign do?
-Feign aims to make it easier to write Java Http clients
- Previously, when using Ribbon + RestTemplate, the RestTemplate is used to encapsulate the Http request, forming a set of templated calling methods. However, in the actual development, because there may be more than one invocation of service dependencies, and often an interface will be invoked in multiple places, a client class is usually encapsulated for each micro service to wrap the invocation of these dependent services. Therefore, Feign has made further encapsulation on this basis, and he will help us define and implement the definition of dependent service interface. Under the implementation of Feign, we only need to create an interface and configure it in the way of annotation (similar to the Mapper annotation on Dao interface in the past, and now a Feign Annotation on a micro service interface), so as to complete the interface binding to the service provider, It simplifies the development amount of automatically encapsulating service call clients when using Spring Cloud Ribbon.
Feign integrates Ribbon by default
- The Ribbon is used to maintain the service list information of microservicecloud dept, and the load balancing of the client is realized through polling. Different from the Ribbon, Feign only needs to define the service binding interface and realize the service invocation in a declarative way.
7.2 use steps of feign
- Create the springcloud consumer fdept feign module
Copy the POM under the springcloud-consumer-dept-80 module XML, resource, and java code to the springcloud consumer feign module, and add feign dependencies.
<dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-feignartifactId> <version>1.4.6.RELEASEversion> dependency>
Realized through the Ribbon: the original controller: deptconsumercontroller java
package com.haust.springcloud.controller; import com.haust.springcloud.pojo.Dept; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import java.util.List; @RestController public class DeptConsumerController { @Autowired private RestTemplate restTemplate; private static final String REST_URL_PREFIX = "http://SPRINGCLOUD-PROVIDER-DEPT"; @RequestMapping("/consumer/dept/add") public boolean add(Dept dept) { return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add", dept, Boolean.class); } @RequestMapping("/consumer/dept/get/{id}") public Dept get(@PathVariable("id") Long id) { return restTemplate.getForObject(REST_URL_PREFIX + "/dept/get/" + id, Dept.class); } @RequestMapping("/consumer/dept/list") public List<Dept> list() { return restTemplate.getForObject(REST_URL_PREFIX + "/dept/list", List.class); } }
Realized through Feign: controller after transformation: deptconsumercontroller java
@RestController public class DeptConsumerController { @Autowired private DeptClientService deptClientService = null; @RequestMapping("/consumer/dept/add") public boolean add(Dept dept) { return this.deptClientService.addDept(dept); } @RequestMapping("/consumer/dept/get/{id}") public Dept get(@PathVariable("id") Long id) { return this.deptClientService.queryById(id); } @RequestMapping("/consumer/dept/list") public List<Dept> list() { return this.deptClientService.queryAll(); } }
Compared with Ribbon, Feign shows the characteristics of interface oriented programming, and the code looks more refreshing
Main configuration class
@SpringBootApplication @EnableEurekaClient @EnableFeignClients(basePackages = {"com.haust.springcloud"}) @ComponentScan("com.haust.springcloud") public class FeignDeptConsumer_80 { public static void main(String[] args) { SpringApplication.run(FeignDeptConsumer_80.class, args); } }
- Transforming the spring cloud API module
pom. Adding feign dependency to XML
<dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-feignartifactId> <version>1.4.6.RELEASEversion> dependency>
Create a new service package and a new deptclientservice Java interface,
@FeignClient(value = "SPRINGCLOUD-PROVIDER-DEPT") public interface DeptClientService { @GetMapping("/dept/get/{id}") public Dept queryById(@PathVariable("id") Long id); @GetMapping("/dept/list") public Dept queryAll(); @GetMapping("/dept/add") public Dept addDept(Dept dept); }
7.3 how to choose feign and Ribbon?
It depends on personal habits. If you like REST style, use Ribbon; If you like the community version of the interface oriented style, use Feign
8. Hystrix: Service fuse
Problems faced by distributed systems
Applications in complex distributed architecture have dozens of dependencies, and each dependency will inevitably fail at some time!
8.1 service avalanche
When calling between multiple microservices, suppose that microservice A calls microservice B and microservice C, and microservice B and microservice C call other microservices, which is the so-called "fan out". If the call response time of A microservice on the fan out link is too long or unavailable, the call to microservice A will occupy more and more system resources, resulting in system crash, the so-called "avalanche effect".
For high traffic applications, a single back-end dependency may cause all resources on all servers to saturate in tens of seconds. Worse than failure, these applications may also lead to increased delays between services, tight backup queues, threads and other system resources, resulting in more cascading failures of the whole system. These all mean that the failures and delays need to be isolated and managed to facilitate the failure of a single dependency, and the whole application or system cannot be cancelled. (after base note, call or asynchronous call)
We need to get rid of the car
8.2 what is Hystrix?
Hystrix is an open source library used to deal with the delay and fault tolerance of distributed systems. In distributed systems, many dependencies inevitably fail to call, such as timeout, exception, etc. hystrix can ensure that when a dependency fails, it will not lead to the failure of the whole system service, avoid cascading failures, and improve the elasticity of distributed systems.
The circuit breaker itself is a kind of switching device. When a service unit fails, it returns a service expected and processable alternative response (FallBack) to the caller through the fault monitoring of the circuit breaker (similar to fusing a fuse), rather than waiting for a long time or throwing an exception that cannot be handled by the calling method, so as to ensure that the thread of the service caller will not be occupied unnecessarily for a long time, Thus, the spread of faults in the distributed system and even avalanche are avoided.
8.3 what can hystrix do?
- service degradation
- Service fuse
- Service current limiting
- Near real-time monitoring
- ...
When everything is normal, the request flow can be as follows:
When there is a potential in many back-end systems, it can block the entire user request:
With the increase of high-capacity traffic, the potential of a single back-end dependency will cause all resources on all servers to saturate in a few seconds.
Every point in the application that may cause network requests through the network or client library is the source of potential failures. Worse than failure, these applications can also lead to increased latency between services, backing up queues, threads, and other system resources, resulting in more cascading failures across systems.
When each base dependency is wrapped with hystrix, the architecture shown in the above diagram changes similar to the following diagram. Each dependency is isolated from each other, limited to the resources it can fill when the delay occurs, and included in the fallback logic, which determines the response to any type of failure in the dependency:
Official website information: https://github.com/Netflix/Hystrix/wiki
8.4 service fusing
What is service fusing
Circuit breaker mechanism is a microservice link protection mechanism to win avalanche effect.
In the microservice architecture, the data interaction between microservices is completed through remote call. Microservice A calls microservice B and microservice C, and microservice B and microservice C call other microservices. At this time, if the call response time of A microservice on the link is too long or unavailable, the call to microservice A will occupy more and more system resources, resulting in system crash and "avalanche effect".
Service fusing is a microservice link protection mechanism to deal with avalanche effect. For example, in a high-voltage circuit, if the voltage in a certain place is too high, the fuse will blow to protect the circuit. Similarly, in the micro service architecture, the circuit breaker mechanism also plays a similar role. When a microservice of the calling link is unavailable or the response time is too long, the service will be blown. There will be no more microservices called by the node, and the wrong response information will be returned quickly. When it is detected that the microservice call response of the node is normal, the call link is restored.
When a microservice of the fan out link is unavailable or the response time is too long, the service will be degraded, which will fuse the call of the microservice of the node and quickly return the wrong response information. After detecting that the microservice call response of the node is normal, restore the call link. In the spring cloud framework, the fuse mechanism is implemented through hystrix. Hystrix will monitor the status of calls between microservices. When the failed call reaches a certain threshold, the default is 20 calls in 5 seconds, and the circuit breaker mechanism will be started. The notes of the circuit breaker mechanism are: @HystrixCommand .
The service can solve the following problems:
- When the dependent object is unstable, it can achieve the purpose of rapid failure;
- After a fast failure, it can dynamically test whether the dependent object is restored according to a certain algorithm.
Introductory case
Create a new springcloud provider Dept hystrix-8001 module and copy the POM in springcloud provider dept-8001 XML, resource, and Java code to initialize and adjust.
Import hystrix dependency
<dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-hystrixartifactId> <version>1.4.6.RELEASEversion> dependency>
Adjust yml profile
server: port: 8001 mybatis: type-aliases-package: com.haust.springcloud.pojo config-location: classpath:mybatis/mybatis-config.xml mapper-locations: classpath:mybatis/mapper/*.xml spring: application: name: springcloud-provider-dept datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/db01?useUnicode=true&characterEncoding=utf-8 username: root password: root eureka: client: service-url: defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/ instance: instance-id: springcloud-provider-dept-hystrix-8001 prefer-ip-address: true info: app.name: haust-springcloud company.name: com.haust
prefer-ip-address: false:
prefer-ip-address: true:
Modify controller
@RestController public class DeptController { @Autowired private DeptService deptService; @HystrixCommand(fallbackMethod = "hystrixGet") @GetMapping("/dept/get/{id}") public Dept get(@PathVariable("id") Long id){ Dept dept = deptService.queryById(id); if (dept==null){ throw new RuntimeException("this id=>"+id+",The user does not exist or the information cannot be found~"); } return dept; } public Dept hystrixGet(@PathVariable("id") Long id){ return new Dept().setDeptno(id) .setDname("this id=>"+id+",No corresponding information,null---@Hystrix~") .setDb_source("stay MySQL This database does not exist in"); } }
Add supporting annotations for the main startup class @EnableCircuitBreaker
@SpringBootApplication @EnableEurekaClient @EnableCircuitBreaker public class HystrixDeptProvider_8001 { public static void main(String[] args) { SpringApplication.run(HystrixDeptProvider_8001.class,args); } }
Test:
After using fuse, when accessing a nonexistent id, the data displayed on the foreground page is as follows
Instead of using the fused springcloud provider dept-8001 module to access the same address, the following conditions will occur
Therefore, it is necessary to use fuse in order to avoid errors in the whole application or web page due to exceptions or errors in the background of a micro service
8.5 service degradation
What is service degradation
Service degradation means that when the server pressure increases sharply, some services and pages are not processed strategically or handled in a simple way according to the actual business conditions and traffic, so as to release server resources to ensure the normal or efficient operation of core business. To put it bluntly, it is to give system resources to services with high priority as much as possible.
Resources are limited and requests are unlimited. If the service is not degraded during the peak period of concurrency, on the one hand, it will certainly affect the performance of the overall service. If it is serious, it may lead to downtime and unavailability of some important services. Therefore, in the peak period, in order to ensure the availability of core function services, some services must be degraded. For example, when the double 11 event is held, all services unrelated to the transaction are downgraded, such as viewing ant forest, viewing historical orders, etc.
What are the main scenarios for service degradation? When the overall load of the whole microservice architecture exceeds the preset upper threshold or the upcoming traffic is expected to exceed the preset threshold, some unimportant or non urgent services or tasks can be delayed or suspended in order to ensure the normal operation of important or basic services.
The method of degradation can be based on the business, and the service can be delayed. For example, the delay will add points to the user, which will only be put into a cache and executed after the service is stable; Or close the service within the granularity, such as the recommendation of relevant articles.
It can be seen from the above figure that when the traffic of service A increases sharply and the traffic of B and C is less at A certain time, in order to alleviate the pressure of service A, B and C need to temporarily close some service functions to undertake some services of A, so as to share the pressure for A, which is called service degradation.
Issues to be considered in service degradation
- Which services are core services and which are non core services
- Which services can support degradation, which services cannot support degradation, what is the degradation strategy
- In addition to service degradation, is there a more complex business release scenario and what is the strategy?
Automatic degradation classification
- Timeout degradation: it mainly configures the timeout time and timeout retry times and mechanism, and uses asynchronous mechanism to detect the reply
- Degradation of failure times: it mainly refers to some unstable APIs. When the number of failed calls reaches a certain threshold, it will be degraded automatically. Similarly, asynchronous mechanism should be used to detect the reply
- Failure degradation: for example, if the remote service to be called hangs (network failure, DNS failure, http service returns an error status code, rpc service throws an exception), it can be directly degraded. The processing schemes after degradation include: default value (for example, if the inventory service is suspended, return to the default stock), bottom data (for example, if the advertisement is suspended, return some static pages prepared in advance), cache (some cache data temporarily stored before)
- Current limit degradation: when second killing or rush buying some restricted goods, the system may crash due to too much traffic. At this time, the current limit will be used to limit the traffic. When the current limit threshold is reached, the subsequent requests will be degraded; The processing scheme after degradation can be: queue page (divert the user to the queue page and try again later), no goods (directly inform the user that there is no goods), error page (if the activity is too hot, try again later).
Introductory case
Create a new degraded configuration class deptclientservicefallbackfactory in the service package under the springcloud API module java
@Component public class DeptClientServiceFallBackFactory implements FallbackFactory { @Override public DeptClientService create(Throwable cause) { return new DeptClientService() { @Override public Dept queryById(Long id) { return new Dept() .setDeptno(id) .setDname("id=>" + id + "There is no corresponding information. The client provides degraded information. The service has been shut down now") .setDb_source("no data~"); } @Override public List<Dept> queryAll() { return null; } @Override public Boolean addDept(Dept dept) { return false; } }; } }
Specify the degraded configuration class DeptClientServiceFallBackFactory in DeptClientService
@Component @FeignClient(value = "SPRINGCLOUD-PROVIDER-DEPT",fallbackFactory = DeptClientServiceFallBackFactory.class) public interface DeptClientService { @GetMapping("/dept/get/{id}") public Dept queryById(@PathVariable("id") Long id); @GetMapping("/dept/list") public List<Dept> queryAll(); @GetMapping("/dept/add") public Boolean addDept(Dept dept); }
Enable demotion in the springcloud consumer dept feign module
server: port: 80 eureka: client: register-with-eureka: false service-url: defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/ feign: hystrix: enabled: true
8.6 difference between service fusing and degradation
- Service fusing - > server: a service timeout or abnormality causes fusing ~, which is similar to a fuse (Self Fusing)
- Service degradation - > client: considering the overall website request load, when a service is blown or closed, the service will no longer be called. At this time, on the client, we can prepare a FallBackFactory and return a default value (default value). It will lead to the decline of the overall service, but at least it can be used, which is better than hanging up directly.
- The triggering reasons are different. Service fusing is generally caused by a service (downstream service) failure, and service degradation is generally considered from the overall load; The levels of management objectives are different. Fusing is actually a framework level process, and each micro service needs to be (without hierarchy), while degradation generally needs to have hierarchy for the business (for example, degradation generally starts from the most peripheral services)
- The implementation method is different. Service degradation is code intrusive (completed by the controller and / or automatic degradation), and fusing is generally called self fusing.
Flow restriction: limit concurrent request access. If the threshold is exceeded, it will be rejected;
Degradation: services are prioritized, sacrificing non core services (unavailable) to ensure the stability of core services; Considering the overall load;
Fusing: the dependent downstream service failure triggers fusing to avoid causing the system to crash; Automatic system execution and recovery
8.7 Dashboard flow monitoring (client)
Create a new springcloud consumer hystrix dashboard module
Add dependency
<dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-hystrixartifact >1.4.6.RELEASEversion> dependency> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-hystrix-dashboardartifactId> <version>1.4.6.RELEASEversion> dependency> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-ribbonartifactI >1.4.6.RELEASEversion> dependency> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-eurekaartifactI >1.4.6.RELEASEversion> dependency> <dependency> <groupId>com.haustgroupId> <artifactId>springcloud-apiartifactId> <version>1.0-SNAPSHOTversion> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-devtoolsartifactId> dependency>
Main startup class
@SpringBootApplication @EnableHystrixDashboard public class DeptConsumerDashboard_9001 { public static void main(String[] args) { SpringApplication.run(DeptConsumerDashboard_9001.class,args); } }
Add the following code to the main startup class under the springcloud-provider-dept-8001 module to add monitoring
@SpringBootApplication @EnableEurekaClient @EnableCircuitBreaker public class HystrixDeptProvider_8001 { public static void main(String[] args) { SpringApplication.run(HystrixDeptProvider_8001.class, args); } @Bean public ServletRegistrationBean hystrixMetricsStreamServlet() { ServletRegistrationBean registrationBean = new ServletRegistrationBean(new HystrixMetricsStreamServlet()); registrationBean.addUrlMappings("/actuator/hystrix.stream"); return registrationBean; } }
http://localhost:9001/hystrix
Configuration filling:
visit: http://localhost:8001/dept/get/1
9. Zull routing gateway
summary
- What is zuul?
Zull includes two main functions: Request Routing (for jump) and filtering:
- Routing function: it is responsible for forwarding external requests to specific micro service instances, which is the basis for realizing the unified entrance of external access.
- Filter function: it is responsible for intervening in the request processing process, which is the basis for realizing request verification, service aggregation and other functions.
- Zuul and Eureka integrate, register zuul as an application under Eureka service governance, and obtain messages of other services from Eureka, that is, access to micro services in the future is obtained through zuul jump.
- Note: Zuul service will eventually register with Eureka
- It provides three functions: agent + routing + filtering!
- Official documents: https://github.com/Netflix/zuul/
Introductory case
Create a new springcloud zuul module and import dependencies
<dependencies> <!--Import zuul rely on--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId <version>1.4.6.RELEASE</version> </dependency> <!--Hystrix rely on--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifac <version>1.4.6.RELEASE</version> </dependency> <!--dashboard rely on--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix-dashboar <version>1.4.6.RELEASE</version> </dependency> <!--Ribbon--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifact <version>1.4.6.RELEASE</version> </dependency> <!--Eureka--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifact <version>1.4.6.RELEASE</version> </dependency> <!--Entity class+web--> <dependency> <groupId>com.haust</groupId> <artifactId>springcloud-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--Hot deployment--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies>
application.yml
server: port: 9527 spring: application: name: springcloud-zuul #Microservice name eureka: client: service-url: defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/ instance: #id of the instance instance-id: zuul9527.com prefer-ip-address: true # Display ip info: app.name: haust.springcloud #entry name company.name: haust #corporate name zuul: routes: mydept.serviceId: springcloud-provider-dept mydept.path: /mydept/** ignored-services: "*" # Can no longer use a (*: all) path to access, ignored: ignore, hide all~ prefix: /kuagn # Set the public prefix to hide the original route
Start as follows:
The local host is added as follows:
127.0.0.1 www.sowhat.com
zull 9527 gives us the right:
During work, the project name should be masked. Change spring provider Dept to mydept, and then masked
ignored-services: "spring-provider-dept"
Main startup class
@SpringBootApplication @EnableZuulProxy public class ZuulApplication_9527 { public static void main(String[] args) { SpringApplication.run(ZuulApplication_9527.class,args); } }
For details, please refer to the springcloud Chinese community zuul component: https://www.springcloud.cc/spring-cloud-greenwich.html#_router_and_filter_zuul
10. Spring Cloud Config distributed configuration
Dalston.RELEASE
Spring Cloud Config provides server and client support for external configurations in distributed systems. With Config Server, you can manage external properties of applications in all environments. The conceptual mappings on the client and server are the same as the Spring Environment and PropertySource abstractions, so they fit well with spring applications, but can be used with any application running in any language. As applications go through the deployment process from developers to test and production, you can manage the configuration between these environments and determine that the application has everything you need to run when migrating. The default implementation of the server storage back-end uses git, so it easily supports the configuration environment of label version and can access various tools for managing content. It's easy to add an alternative implementation and insert it using the spring configuration.
summary
Configuration file problem faced by distributed system
Microservice means to split the business in a single application into one sub service. The granularity of each service is relatively small, so there will be a large number of services in the system. Since each service needs the necessary configuration information to run, a set of centralized and dynamic configuration management facilities is essential. spring cloud provides configServer to solve this problem. Each microservice brings an application YML, it's a headache to modify hundreds of configuration files!
What is the spring cloud config distributed configuration center?
spring cloud config provides centralized external support for microservices in the microservice architecture, and the configuration server provides a centralized external configuration for all links of different microservice applications.
spring cloud config is divided into two parts: server and client.
The server is also known as the distributed configuration center. It is an independent micro service application, which is used to connect to the configuration server and provide the client with access interfaces such as obtaining configuration information, encrypting and decrypting information.
The client manages application resources and business-related configuration content through the specified configuration center, and obtains and loads configuration information from the configuration center at startup. The configuration server uses git to store configuration information by default, which is helpful for version management of environment configuration. The GIT client tool can be used to manage and access the configuration content conveniently.
What can the spring cloud config distributed configuration center do?
- Centralized management profile
- Different environments, different configurations, dynamic configuration updates, deployment by environment, such as / dev /test /prod /beta /release
- The configuration is dynamically adjusted during operation. It is no longer necessary to write configuration files on each service deployed machine. The service will uniformly pull and configure its own information from the configuration center
- When the configuration changes, the service does not need to restart, it can sense the change of the configuration and apply the new configuration
- Expose the configuration information in the form of REST interface
Integration of spring cloud config distributed configuration center and GitHub
Because spring cloud config uses git to store configuration files by default (there are other ways, such as self-contained SVN and local files), Git is the most recommended and is accessed in the form of http / https.
Introductory case
Server
Create a new springcloud-config-server-3344 module and import POM XML dependency
<dependencies> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId> dependency> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-config-serverartifactId> <version>2.1.1.RELEASEversion> dependency> dependencies>
Create application. Under resource YML configuration file, Spring Cloud Config server provides configuration for remote clients from git repository (required):
server: port: 3344 spring: application: name: springcloud-config-server cloud: config: server: git: uri: https://gitee.com/cao_shi_peng/springcloud-config.git eureka: client: register-with-eureka: false fetch-registry: false
Main startup class
@EnableConfigServer @SpringBootApplication public class Config_server_3344 { public static void main(String[] args) { SpringApplication.run(Config_server_3344.class,args); } }
Put the newly created application.config in the springcloud config folder of the local git warehouse YML submitted to code cloud warehouse:
The default strategy for locating resources is to clone a git repository (in spring.cloud.config.server.git.uri) and use it to initialize a mini spring application. The Environment of the applet is used to enumerate property sources and publish them through JSON endpoints.
The HTTP service has resources in the following format:
/{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties
The default strategy for locating resources is to clone a git repository (in spring.cloud.config.server.git.uri) and use it to initialize a mini spring application. The Environment of the applet is used to enumerate property sources and publish them through JSON endpoints.
The HTTP service has resources in the following format:
Test access http://localhost:3344/application-dev.yml
Test access http://localhost:3344/application/test/master
Test access http://localhost:3344/master/application-dev.yml
If the test accesses a configuration that does not exist, it will not be displayed, such as: http://localhost:3344/master/application-aaa.yml
client
Put the newly created config client in the springcloud config folder of the local git warehouse YML submitted to code cloud warehouse:
Create a new springcloud-config-client-3355 module and import dependencies
<!--config--> <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-start <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> <version>2.1.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
Create application. Under resources YML and bootstrap YML profile
- bootstrap.yml is a system level configuration
# System level configuration spring: cloud: config: name: config-client # The resource name that needs to be read from git without suffix profile: dev label: master uri: http://localhost:3344
- application.yml is a user level configuration
# User level configuration spring: application: name: springcloud-config-client
Create the configclientcontroller under the controller package Java for testing
@RestController public class ConfigClientController { @Value("${spring.application.name}") private String applicationName; //Get microservice name @Value("${eureka.client.service-url.defaultZone}") private String eurekaServer; //Get Eureka services @Value("${server.port}") private String port; //Get the port number of the server @RequestMapping("/config") public String getConfig(){ return "applicationName:"+applicationName + "eurekaServer:"+eurekaServer + "port:"+port; } }
Main startup class
@SpringBootApplication public class ConfigClient { public static void main(String[] args) { SpringApplication.run(ConfigClient.class,args); } }
Test: start server Config_server_3344 restart the client ConfigClient
visit: http://localhost:8201/config/
Small case
Create config-dept.yml and config eureka.com locally YML and submit to code cloud warehouse
Here, the content of the configuration file is no longer listed, but directly to the code.
Create a new springcloud-config-eureka-7001 module and copy the contents under the original springcloud-eureka-7001 module to this module.
1. Clear the application of this module YML configuration and create a new bootstrap YML connection remote configuration
spring: cloud: config: name: config-eureka # Profile name in warehouse label: master profile: dev uri: http://localhost:3344
In POM Add spring cloud config dependency to XML
<!--config--> <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-config --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> <version>2.1.1.RELEASE</version> </dependency>
3. Main startup
@SpringBootApplication @EnableEurekaServer //The startup class of EnableEurekaServer server can accept others to register~ public class ConfigEurekaServer_7001 { public static void main(String[] args) { SpringApplication.run(ConfigEurekaServer_7001.class,args); } }
4. Test
Step 1: start Config_Server_3344, and visit http://localhost:3344/master/config-eureka-dev.yml test
The above figure shows success. Create a new springcloud-config-dept-8001 module and copy the contents of springcloud-provider-dept-8001
Similarly, import spring cloud config dependency and clear application YML. Create a bootstrap YML configuration file and configure
spring: cloud: config: name: config-dept label: master profile: dev uri: http://localhost:3344
Main startup class
@SpringBootApplication @EnableEurekaClient //Automatically register in Eureka after the service starts! @EnableDiscoveryClient //Service discovery~ @EnableCircuitBreaker // public class ConfigDeptProvider_8001 { public static void main(String[] args) { SpringApplication.run(ConfigDeptProvider_8001.class,args); } //Add a Servlet @Bean public ServletRegistrationBean hystrixMetricsStreamServlet(){ ServletRegistrationBean registrationBean = new ServletRegistrationBean(new HystrixMetricsStreamServlet()); registrationBean.addUrlMappings("/actuator/hystrix.stream"); return registrationBean; } }