Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Brief introduction of Spring Cloud micro-service architecture

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Recently, I gained a lot from reading Zhou Li's "Spring Cloud and Docker". I took some time to summarize the contents of the book. It is convenient for you to quickly understand what are the main functions of Spring Cloud,Spring Cloud and Spring Cloud service governance. The content of the following blog is from this book.

What is a microservice?

The microservice architecture style is a method of developing a single application into a set of small services, each running in its own process, and the communication between services uses a lightweight communication mechanism (usually using HTTP resource API). These services are built around business capabilities and can be deployed independently through a fully automated deployment mechanism. These services share the smallest centralized management, and services can be developed in different languages and use different data storage technologies.

The microservice architecture should have the following characteristics:

Each microservice can run independently in its own process.

A series of independent micro-services work together to build the whole system.

Each service is developed for an independent business, and a micro-service only focuses on a specific function, such as order management, user management and so on.

Micro services communicate with each other through some lightweight communication mechanisms, such as invocation through RESTful API.

Different languages and data storage technologies can be used

Fully automatic deployment mechanism

Advantages of microservice architecture

Easy to develop and maintain

A microservice will only focus on a specific business function, so it has a clear business and less code.

A single microservice starts faster

The amount of code for a single microservice is small, so it starts faster.

Local modifications are easy to deploy

As long as a single application is modified, the entire application has to be redeployed, and microservices solve this problem.

The technology stack is not limited.

In the micro-service architecture, the technology stack can be reasonably selected according to the characteristics of the project business and the team.

Scale on demand

Fine-grained expansion can be achieved according to the requirements.

Challenges to micro-service architecture

High requirements for operation and maintenance

More services means more investment in operation and maintenance.

The inherent complexity of distribution

Using a micro-service architecture is a distributed system. For a distributed system, system fault tolerance, network delay, distributed transactions and so on will bring great challenges.

High cost of interface adjustment

Microservices communicate with each other through interfaces. If you modify the API of a microservice, all microservices that use this interface may need to be adjusted.

Repetitive work

Many services may use the same function, but this feature is not decomposed into a micro-service, at this time, each service may develop this feature, resulting in code duplication.

Overall architecture diagram of microservices

The service is registered with discovery

When each service starts, it registers its own network address and other information with the service discovery component, which stores this information.

The service consumer can query the network address of the service provider from the service discovery component and use that address to invoke the interface of the service provider.

Each microservice communicates with the service discovery component using a mechanism such as a heartbeat. If the service discovery component is unable to communicate with a service instance for a long time, it will log out of the instance.

When the micro-service network address changes (such as the increase or decrease of instances or the change of IP ports, etc.), it will be re-registered with the service discovery component. In this way, the service consumer does not have to manually modify the network address of the provider.

Fegion implements declarative REST invocation

REST (Representational State Transfer), translated into Chinese as "representational state transfer" (also "representative state transfer"). It is a term coined by Dr. Roy Thomas Fielding in an academic paper while studying at the University of California, Irvine in 2000. REST defines a set of architectural principles that you can use, including how clients written in different languages handle and transmit resource state through HTTP. Feign is a declarative WebService client. Using Feign makes it easier to write WebService clients by defining an interface and then adding annotations to the interface, as well as supporting JAX-RS standard annotations. Feign also supports pluggable encoders and × ×. SpringCloud encapsulates Feign to support SpringMVC standard annotations and HttpMessageConverters. Feign can be used in combination with Eureka and Ribbon to support load balancing.

Using Hystrix to realize fault-tolerant processing of micro-service

Hystrix is an open source delay and fault-tolerant library by Netflix, which is used to isolate access to remote systems, services or third-party libraries to prevent cascading failures, thereby improving the availability and fault tolerance of the system. Hystrix mainly implements delay and fault tolerance through the following points.

Parcel request: invocation logic for dependency using HystrixCommand, with each command executed in a separate thread. This uses the "command mode" in design mode.

Tripping mechanism: when the error rate of a service exceeds a certain threshold, Hystrix can automatically or manually trip and stop requesting the service for a period of time. Resource isolation: Hystrix maintains a small pool of threads (or semaphores) for each dependency. If the thread pool is full, requests to the dependency are rejected immediately instead of waiting in queue, speeding up failure determination. Monitoring: Hystrix can monitor running metrics and configuration changes in near real time, such as successes, failures, timeouts, and rejected requests. Fallback mechanism: executes fallback logic when a request fails, times out, is rejected, or when a circuit breaker is turned on. Fallback logic can be provided by the developer itself, such as returning a default value. Self-repair: after the circuit breaker is open for a period of time, it will automatically enter the "half-open" state.

If there is no fault tolerance, "basic service failure" leads to "cascade failure", resulting in an avalanche effect on the right.

Using Zuul to build a micro-service gateway

Zuul is Netflix's open source micro-service gateway, which can be used with components such as Eureka,Ribbon,Hystrix. At the core of Zuul is a series of filters that perform the following functions.

Authentication and Security dynamic routing stress Test load Distribution static response handling Multi-area elasticity

Most of the functionality of Zuul is achieved through filters, as shown in the figure.

Unified management of micro-service configuration using Spring CloudConfig

Spring Cloud Config supports storing configuration files in Git, SVN and locally. Versioning is well supported by using Git or SVN repositories. The default configuration of Spring is to use Git repositories. It has the following advantages:

Centralized management configuration: an application system that uses a micro-service architecture may contain hundreds of micro-services, so several management configurations are necessary.

Different environments have different configurations: for example, data source configurations are different in different environments (development, testing, pre-release, production, etc.).

It can be dynamically adjusted during operation: for example, the data source connection pool size or circuit breaker threshold can be dynamically adjusted according to the load of each micro-service, and the micro-service can not be stopped when the configuration is adjusted.

The configuration can be updated automatically after modification. If the configuration content changes, the microservice can update the configuration automatically.

Spring Cloud service governance

Registry monitoring

Fault-tolerant processing monitoring

Micro-service health check

Trace of micro-service call

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report