"SpringCloud microservice combat" personal reading notes

Chapter 1 Basic Knowledge

1. What is a microservice:

Microservices is a design style on system architecture;

Its main purpose is to split an originally independent system into multiple small services;

Communication and collaboration between services through HTTP-based RESTful API;

 

2. Problems with microservices:

# Operation's new challenges

In the microservices architecture, the number of processes that O & M personnel need to maintain will greatly increase.

#Interface consistency

Although we split the service, the business logic dependency will not be eliminated,

It's just that the code dependency in a monolithic application has become the communication dependency between services.

#Distributed complexity

Need to consider: network delay, distributed transactions, asynchronous messages, etc .;

 

3. Nine characteristics of microservice architecture:

Service componentization;

Organize teams by business;

Be a "product" attitude;

Smart endpoints and dumb pipes;

Decentralized governance;

Decentralized management data;

Infrastructure automation;

Fault-tolerant design;

Evolutionary design;

 

4. Introduction to Spring Cloud:

SpringCloud is a microservice architecture development tool based on SpringBoot.

 

Chapter 2 Spring Boot

SpringBoot starts

The purpose of Spring Boot is not to rewrite Spring or to replace Spring, but to design a large number of automated configurations to simplify Spring's original boilerplate configuration, so that developers can quickly build applications;

In addition to solving configuration problems, SpringBoot also uses a series of Starter POMs to define, so that when we integrate various functions, we do not need to maintain those complex dependencies in the Maven pom.xml file, but through a similar modular Starter Referenced by module definition, making dependency management work easier;

SpringBoot's Starter POMs adopt the spring-boot-starter- * naming method, * represents a special application function module

SpringBoot packaging method

SpringBoot packages Web applications in the form of jar by default, not in the form of war;

In addition to being better integrated into Docker, SpringBoot itself supports embedded Tomcat and other containers. Therefore, the application built by SpringBoot does not need to install Tomcat, package the application into war, and then deploy to Tomcat. Such complex construction and deployment work, just put the SpringBoot application into a jar package, and run it directly through the java -jar command to start a Standardized Web application;

Implement RESTful API

The implementation code of creating a RESTful API in SpringBoot is the same as the SpringMVC application, except that it does not need to do a lot of configuration like SpringMVC, but can directly write the Controller content.

SpringBoot configuration file

1.application.properties

The configuration directory of SpringBoot is: src / main / resources

The default configuration file of SpringBoot is: src / mian / resources / application.properties

The configuration content of the SpringBoot application can be concentrated in this file. According to the different starter modules we introduce, you can define here: container port number, database connection information, log level and other configuration information

 

2.YAML

SpringBoot's configuration file also supports YAML files that are now widely recommended .

YAML emphasizes data as the center rather than markup language as the center.

YAML is expressed in the form of outline-like indentation. The configuration information uses a stepped indentation method, and its structure is clearer and easier to read. At the same time, the number of characters in the configuration content is also significantly reduced.

 

3. Custom parameters

We can also define some custom attributes we need in the configuration file.

Then in the application, you can use @Value annotation to load these custom parameters

 

4. Multi-environment configuration

application-dev.properties development

application-test.properties test

application-prod.properties production

Monitoring and management

spring-boot-starter-actuator

The introduction of this module can automatically provide a series of endpoints for monitoring applications built by Spring Boot.

 

Chapter 3 SpringCloud Eureka

About: Microservices governance

Mainly responsible for completing the service governance function in the microservice architecture;

Service governance can be said to be the most core and basic module in the microservice architecture. It is mainly used to realize the automatic registration and discovery of each microservice instance;

Service registration: In the framework of microservice governance, a registration center is usually constructed, and each microservice unit registers its own service with the registration center;

Service discovery: The call between services is no longer realized by specifying a specific instance address, but by initiating a request call to the service name; the caller needs to consult the service registration center for services and obtain a list of all service instances to achieve Access to specific service instances;

Use Eureka to implement service registration and discovery;

Eureka includes: server components and client components ;

Eureka server : also known as: service registry , like other service registry, supports high availability configuration;

Eureka client : mainly handles the registration and discovery of services;

                           The client service is embedded in the code of the client application through annotations and parameter configuration. When the application is running, the Eureka client registers its own service with the registry and periodically sends heartbeats to update its service lease. . At the same time, it can also query the currently registered service information from the server and cache them locally and refresh the service status periodically;

Build a service registration center

1. Create a SpringBoot project named: eureka-server;

2. Introduce dependency in pom.xml: spring-cloud-eureka-server ;

3. Use @EnableEurekaServer annotation to start a service registration center to provide dialogue to other applications;

    Note: Under the default settings, the service registration center will also try to register itself as a client , so we need to register its client registration behavior: in application.properties: eureka.client.register-with- eureka = false;

Registered service provider

After completing the construction of the service registration center, next we try to add an existing springboot application to Eureka's service governance system.

1. Add dependency in pom.xml: spring-cloud-starter-eureka ;

2. Transform the / hello request processing interface, and print the relevant content of the service in the log by injecting the DiscoveryClient object ;

3. Activate the DiscoveryClient implementation in Eureka by adding the @EnableDiscoveryClient annotation in the main class ;

4. In application.properties, name the service by: spring.application.name property , for example: hello-service;

                                             Then pass: eureka.client.serviceUrl.defaultZone property  to specify the address of the service registration center;

Service discovery and consumption

Build service consumers.

Service discovery : completed by Eureka client ;

Service consumption : completed by Ribbon ;

1. Start the service registration center eureka-server and hello-service service implemented before;

2. Create a springboot project to implement service consumers , named: ribbon-consumer ;

    Add dependencies to pom.xml: spring-cloud-starter-eureka, spring-cloud-starter-ribbon;

3. Create the application main class ConsumerApplication and register the application as an Eureka client application via the @EnableDiscoveryClient annotation to obtain service discovery capabilities;

    At the same time, create a SpringBoot instance of RestTemplate in the main class, and enable client load balancing through the @LoadBalanced annotation;

4. Create ConsumerController class and implement / ribbon-consumer interface;

    In this interface, the RestTemplate created above is used to call the / hello interface provided by the HELLO-SERVICE service;

   It can be seen that the address accessed here is the service name HELLO-SERVICE, not a specific address;

5. Configure the location of the Eureka service registration center in application.properties, which needs to be the same as the previous HELLO-SERVICE, otherwise the service cannot be found

6. Start the ribbon-consumer application;

to sum up

Three core elements of the Eureka service governance infrastructure:

   1) Service Registration Center

             Service registration

             Service synchronization

             Service renewal

   2) Service provider

             Get service

             Service call

             Service offline

   3) Service consumers

             Failure rejection

             Self-protection

 

Chapter 4 Spring Cloud Ribbon: Client Load Balancing

SpringCloud Ribbon

SpringCloud Ribbon is a client load balancing tool based on HTTP and TCP;

Can easily convert service-oriented REST template requests into client-side load balancing service calls;

It exists in almost every microservice and infrastructure built by SpringCloud, including Feign is also a tool based on Ribbon implementation;

The biggest difference between client-side load balancing and server-side load balancing is the location where the service list is stored. All client nodes maintain the server list that they want to access, and these service lists come from the service registry, such as Eureka server ; Client load balancing also requires heartbeat to maintain the health of the service list that you want to access, but this step needs to be completed in cooperation with the service registration center;

RestTemplate

The RestTemplate class provided by the spring framework can be used to call rest services in applications. It simplifies the communication with the http service, unifies the RESTful standards, and encapsulates the http link. We only need to pass in the url and return value types. Compared with the HttpClient, RESTClient is a more elegant way to call RESTful services.

RestTemplate implements the service invocation for several different request types and parameter types:

GET request:

     The first: getForEntity ()

     The second kind: getForObject ()

POST request:

     The first: postForEntity ()

     The second kind: postForObject ()

     The third kind: postForLocation ()

PUT request:

     put()

DELETE request:

     delete()

Load balancer

When specifically implementing client load balancing, it is implemented through the ILoadBalancer interface of Ribbon;

The following is the implementation class of the ILoadBalancer interface:

     AbstractLoadBalancer;

     BasedLoadBalancer class is the basic implementation class of Ribbon load balancer, which defines a lot of basic content related to load balancing;

     The DynamicServerListLoadBalancer class inherits from the BasedLoadBalancer class, which is an extension of the basic load balancer;

     

 

Chapter 6 SpringCloud Feign: Declarative Service Invocation

About Feign:

Feign is a declarative service calling component based on Ribbon and Hystrix, a declarative HTTP client;

When we use Ribbon, we usually use its interception of RestTemplate requests to implement interface calls to dependent services, and RestTemplate has implemented encapsulation processing of HTTP requests, forming a set of templated calling methods. Feign further encapsulates on this basis, which helps us define and implement the definition of service-dependent interfaces, which can complete the interface binding to the service provider, simplifying the development of self-encapsulating service call clients when using Ribbon;

Steps to create:

1. Create a SpringBoot project, named: feign-consumer,

    Introduce spring-cloud-starter-eureka and spring-cloud-starter-feign dependencies in pom.xml;

2. Create the application main class ConsumerApplication and enable the support function of SpringCloud Feign through the @EnableFeignClients annotation;

3. Define the HelloService interface, specify the service name to bind the service through the @FeignClient annotation, and then use the SpringMVC annotation to bind the REST interface provided by the service;

4. Create a ConsumerController to implement the call to the Feign client; use @Autowired to directly inject the HelloService instance defined above, and then call;

5. Finally, like consumers implemented by Ribbon, you need to specify the service registry in application.properties and define your own service name as feign-consumer

 

 

 

Published 162 original articles · won 30 · 90,000 views +

Guess you like

Origin blog.csdn.net/ScorpC/article/details/102638434