Implementing microservice architecture learning based on Spring Boot and Spring Cloud!

Author : houxue   2023-04-25 10:43:25 Browse: 752
Category : JavaScript

Abstract: Introduction to Spring Cloud Spring Cloud is a cloud application development tool based on Spring Boot implementation. It provides...

Introduction to Spring Cloud

Spring Cloud is a cloud application development tool based on Spring Boot implementation. It provides a simple development method for configuration management, service discovery, circuit breakers, intelligent routing, micro agents, control buses, global locks, decision contests, distributed sessions, and cluster state management in JVM based cloud application development.

Comparison between Spring Cloud and Dubbo

Speaking of Dubbo, I would like to mention ESB by the way. Currently, CCTV Xinhua News Agency is also using ESB for task scheduling. Here, let's compare Dubbo and ESB first:

ESB (Enterprise Data Bus) generally adopts a centralized forwarding request, suitable for integrating a large number of heterogeneous systems, with a focus on task orchestration. Performance issues can be avoided through heterogeneous methods, and cannot support particularly large concurrency.

Dubbo (Service Registration Management) adopts distributed invocation, where the registration center only records address information and then makes direct calls, which is suitable for concurrent and stressful situations; It focuses on the governance of services, granulating each service, and completing the orchestration of business in the program logic of each sub business system.

Returning to the topic, what are the differences between Spring Cloud and Dubbo? Firstly, let's take a look at the similarities. Both have functions related to distributed service governance, providing service registration, discovery, routing, load balancing, and more. Speaking of this, it seems that Dubbo has so many functions, but Spring Cloud provides a perfect solution for enterprise level distributed cloud applications, which can be combined with Spring Boot and Docker to achieve rapid development. Therefore, Dubbo only has a part of the RPC function of Spring Cloud, and it cannot be said who is good or bad. However, the Dubbo project has now stopped updating, and Taobao has replaced Dubbo with hsf internally. I think this will lead to more people leaning towards Spring Cloud.

From a development perspective, Dubbo is often combined with Spring and Zookeeper, and its implementation only configures service addresses, names, and ports through XML. The code's invasiveness is minimal. Compared to Spring Cloud, its implementation requires class annotations, which is somewhat invasive.

Spring Cloud sub project

Spring Cloud includes multiple sub projects (targeting multiple different open source products involved in distributed systems), such as Spring Cloud Config, Spring Cloud Netflix, Spring Cloud CloudFoundry, Spring Cloud AWS, Spring Cloud Security, Spring Cloud Commons, Spring Cloud Zookeeper, Spring Cloud CLI, and other projects.

Configure Services

Spring Cloud provides Config Server, which has the ability to perform external configuration in distributed system development. Through Config Server, we can centrally store configuration files for all applications. Config Server supports Git or placing configuration files (GitLab) in the file system. Usually, we use different formats to distinguish different configuration files for different applications. In cloud computing environments, it is customary to use YAML configuration, and the general location of the configuration files is placed in the config directory under the class path. The configuration file rules are: application name + profile.yml:

Profile

Service Discovery

Spring Cloud implements service discovery through Eureka in Netflix OSS, and the main purpose of service discovery is to enable each service to communicate with each other. Eureka Server is the registration center for microservices. When it comes to Spring Cloud Netflix, it is one of the sub projects of Spring Cloud, mainly providing modules such as service discovery (Eureka), circuit breaker (Hystrix), intelligent path management (Zuul), client load balancing (Ribbon), etc.

Spring Cloud provides Eureka server (@ EnableEurekaServer) and client (@ EnableEurekaClient) through annotations.

Routing Gateway

The main purpose of a routing gateway is to have only one interface for all microservices. We only need to access one gateway address, and the gateway can proxy all requests to different services. Spring Cloud is implemented through Zuul and supports automatic routing mapping to services registered on Eureka Server. Spring Cloud provides an annotation @ EnableZuulProxy to enable routing proxies.

Load Balancing

Spring Cloud provides Ribbon and Feign as client load balancing. In Spring Cloud, simply inject a RestTemplate object directly using the ribbon, which has been configured for load balancing; To use Feign, you only need to define an annotation with an @ FeignClient annotation interface, and then use the @ RequestMapping annotation to map remote REST services on the method. This method also has a good load balancing configuration.

Circuit Breaker

Circuit Breakers are mainly designed to solve the problem of calling backup methods to replace failed methods when a method call fails, achieving the function of fault tolerance/preventing cascading errors. Spring Cloud uses @ Enable Circuit Breaker to enable circuit breaker support, and @ HystrixCommand's fallbackMethod to specify a fallback method. (@HystrixCommand(fallbackMethod="fallbackOper"))

Spring Cloud also provides a console to monitor the operation of circuit breakers, which is enabled through @ EnableHystrixDashboard annotation.

Spring Cloud Dependency Library

Here, I will list some commonly used dependency packages. In order to better understand the areas responsible for each dependency, I will use a similar diagram to show the functions of each block as before:

Function

The commonly used dependency packages are as follows:

About JHipster Extension

JHipster ->The technology stack for SpringBoot, AngularJS, and Spring ecosystem integration is the best practice for SpringBoot, the best practice for Netflix technology stack by SpringCloud, the best practice for J2EE Cloud microservices, the best practice for Docker, Kubernetes, and AngularJS1/2, and the most ideal tool for full stack development and agile development.

    Sign in for comments!
Comment list (0)

Powered by TorCMS (https://github.com/bukun/TorCMS).