An article thoroughly understand Spring Cloud and Dubbo!

Microservice

SOA : Service-oriented architecture, split the service and register it to the enterprise bus to provide unified external services

Microservice : The business system is completely componentized, and the application is split into multiple small applications. These applications are independent and integral from the web UI to the service API.

Features of microservices : Single responsibility, autonomy.

The difference between microservices and SOA : Microservices no longer emphasize the heavy ESB enterprise service bus in the traditional SOA architecture. Microservices completely componentize the business system.

Advantages of microservices :

1. The logic is clear, each service is only responsible for its own part of the business
2. It is convenient to expand, only the current microservices need to be modified, and other modules will not be affected during
deployment 3. High availability, distributed deployment improves the overall application For certain high-traffic services, the nodes can be expanded separately. 4. The technology is heterogeneous. When different teams maintain different services, they can implement them according to their familiar technology stack and will not affect others.

Disadvantages of microservices :

1. Compared with a single application, the complexity is relatively high
2. The maintenance cost is higher, and the application splitting a large number of microservices will also increase the cost of operation and maintenance to a certain extent.
3. Relative to the local call of a single application, microservices Calling each other requires Rest, RPC, etc., which affects performance to a certain extent.

In addition, we also compiled information on spring cloud and dubbo and other Java knowledge points. Friends who need it can click: click this, click this , password: csdn.
Insert picture description here

spring cloud

Spring Cloud Netflix Eureka and service governance:
service discovery and invocation flowchart
Insert picture description here

The three services A, B, and C are all deployed in a cluster environment with multiple instances and need to be registered in the registry. B and C are the service providers of A. A needs to find B through the load balancer, and C completes the service call, load The equalizer obtains service definition information from the registry.

There are three main roles involved in service governance:

  • Registry: Provide registration and discovery of services
  • Service provider: The service provider registers its service with the registration center so that consumers can find
  • Service consumer: Obtain a list of registered services from the registry to complete consumption

Service governance strategy :

Polling mechanism: Consumers go to the registry regularly to pull the latest service list and update the local cache.
Monitoring and notification mechanism: When the registry service list changes, a callback is triggered to notify consumers to update the local cache.

Service governance implementation tools : Zookeeper (Java), Consul (Go), Eureka (Java)

It is very simple to build an Eureka service, introduce spring-cloud-starter-eureka-server dependency, and add @EnableEurekaServer to the startup class. Euraka can form a cluster through mutual registration (eureka.client.serviceUrl.defaultZone: multiple eureka-urls).

The construction of the Eureka client is also very simple. Introduce the dependency spring-cloud-starter-eureka, add the annotation @EnableEurekaClient to the startup class, and add the configuration eureka.client.serviceUrl.defaultZone:eureka-server-url
to the Eureka server in the configuration file. The detailed information of a specific service instance can be accessed through http:eureka-service-url/eureka/apps/appid(servername)

Eureka basic architecture
Insert picture description here
Spring cloud Netflix Ribbon load balancing After
Insert picture description here
all service instances are registered in the registry, Ribbon obtains the service list from the registry to achieve load balancing.

Types of load balancing :

1. Server load balance, add a proxy between the client and the server, access the proxy, and the proxy forwards the request to a microservice node

2. Client load balancing, the load balancer and the client are as a whole, the client first locates the destination node of the visit according to the load balancer when initiating a request, and then initiates the request.

Ribbon is a client-side load balancing solution.

There are also the latest interview questions from Dachang. Those who need it can click: click this, click this , code: csdn.

Insert picture description here

Load balancing algorithm :

1. Static load balancing algorithm: random algorithm, using JDK's own random algorithm or improved weighted random algorithm; polling algorithm: round-robin or weighted round-robin

2. Dynamic load balancing algorithm: the least number of connections algorithm, count the number of times each node is called, the call with fewer calls is preferred; the service call delay algorithm, adjust the call priority according to the average response time of each node; source IP The Hash algorithm calculates the source IPde Hash value to determine which node to call, which can ensure that the same client calls the same service node every time.

Use Ribbon to achieve load balancing :

As a client-side load balancing implementation, Ribbon does not need to be deployed independently, but is directly embedded in the code of the service consumer.

There are two ways to use:
1. Use @LoadBalanced and RestTemplate
2. Use @RibbonClient annotation

Use :
Introduce dependency on spring-boot-starter-ribbon, create RestTemplate through @LoadBalanced annotation, and then call http://serverName (service producer's service name)/api (calling interface) through RestTemplate, which is actually Ribbon follow-up serverName obtains the corresponding Ip and port information list from the registry, obtains the current service node that should be accessed according to the load balancing algorithm, and replaces the url for access.

Spring Cloud Netflix Hystrix service fault tolerance

Hystrix provides three types of fault-tolerant processing :

1. Isolation (thread isolation and semaphore isolation). Thread isolation is equivalent to maintaining multiple thread pools.
2. Fuse maintains a counter of request failures. When the number of abnormal calls of a request reaches a threshold, the fuse is triggered to open, and it will reach a certain value after opening. Time, enter the half-open state, allow some requests to enter, if all succeed, or if the success reaches a certain percentage, close the fuse.
3. The fallback service fallback refers to not throwing an exception directly after the call fails but using another code to handle it

Spring Cloud Netflix Zuul Api Gateway

Decoupling service calls: Service consumers only need to pay attention to the gateway and not the provider of the service itself.
Optimize Api: generate different responses for different callers, such as users querying pc to query all data at once and App paging query.
Simplify the calling process: you can integrate some interfaces that need to be combined with multiple microservices.
Service routing: provide unified service routing for client access.
Security control: unified authentication authority processing.
Access control: control client access times and frequency Load balancing: provide front load balancing function

Zuul uses the internal ZuulFilter chain for processing

Spring Cloud Config Configuration Center

Provide unified configuration information management

Two ways to achieve :

  • Based on the local file system, the file name must be consistent with the name of each service;
  • Based on the Central Warehouse (GIT) implementation

Dubbo

Dubbo+Zookeeper

The interface is abstracted and defined and published to the warehouse. The service provider relies on the api module, implements the interface, and adds @Service to the zk. Service consumers only need to reference the Api module and use the @Reference reference. Both service providers and service consumers need to rely on the related dependencies of dubbo and Zookeeper, and both need to be registered on Zookeeper.

Dubbo is a Java service platform framework and SOA governance solution developed by Alibaba. Its main functions include: high-performance NIO communication and multi-protocol integration, service dynamic addressing and routing, soft load balancing and fault tolerance, dependency analysis, degradation, etc.

Note that the @Service annotation registered by Dubbo is provided by Dubbo, and consumers use @Reference

RPC: Remote procedure call, which enables consumer services to call methods provided by remote services like local methods.
The overall process is: determine the remote service address (ip, port, etc.), establish a connection (Tcp), call the method and serialize the parameter information and transmit it to the remote service through tcp. After the remote service receives the local call, the result is returned to the consumer.

The basic principle of Dubbo (personal guess has not been authenticated yet): Identify @Reference annotations, generate proxy classes, locate the list of remotely accessed services from the registry or local cache, determine the remote caller according to the load algorithm, use Netty to establish a TCP connection with the remote service, The message body that encapsulates the call information is sent to the remote service. The remote service parses the message body to complete the local call and returns the call result. The call may be asynchronous, and the client generates a unique request id.

The difference between SpringCloud and Dubbo

1. SpringCloud uses Rest api communication, Dubbo uses RPC communication, the performance of calls between services will be better
2. There are many springcloud related components, and it has its own registry gateway, etc., which is easy to integrate. Dubbo needs to integrate it by itself.
3. Dubbo is relatively easier to use.

At last

Provide free Java architecture learning materials, learning technology content includes: Spring, Dubbo, MyBatis, RPC, source code analysis, high concurrency, high performance, distributed, performance optimization, microservice advanced architecture development, etc.

Friends in need can click: click this! Click this! , Code: csdn.

There are also Java core knowledge points + a full set of architect learning materials and videos + first-line interview books + interview resume templates can be obtained + Ali Meituan Netease Tencent Xiaomi Iqiyi Kuaishou Bilibili interview questions + Spring source code collection + Java architecture Practical e-book + 2020 latest interview questions from major manufacturers.
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/HarderXin/article/details/109387397