In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the SpringCloud micro-service architecture upgrade example analysis, the article is very detailed, has a certain reference value, interested friends must read it!
I. background
1.1 Architecture History of Application system
1.2 what is a microservice?
Origin: the concept of microservices originated from an article "Microservices" written by Martin Fowler in March 2014. It is mentioned in this paper that micro-service architecture is an architectural model, which advocates dividing a single application into a group of small services, which coordinate and cooperate with each other to provide users with the ultimate value.
Communication mode: each service runs in its own independent process, and the service and the service use a lightweight communication mechanism to communicate with each other (usually RESTful API based on HTTP).
General definition of micro-service: micro-service is an architectural style, and a large and complex software application consists of one or more micro-services. Each micro-service in the system can be deployed independently, and each micro-service is loosely coupled. Each microservice focuses on accomplishing only one task.
The original complete process service is divided into two or more process services, and there is a calling relationship between them. Compared with the original single process service, it is called "micro service". (micro-service is a comparative concept, not a single concept)
1.3 advantages of microservice architecture
Scalability: when adding business functions, a single application architecture needs to make major adjustments based on the code of the original architecture, while the micro-service architecture only needs to add new micro-service nodes and adjust the associated micro-service nodes. In order to increase the business response capacity, a single architecture needs to expand the overall capacity, while the micro-service architecture only needs to expand the micro-service nodes with insufficient response capacity.
Fault tolerance: in the event of system failure, a single application architecture needs to repair the whole system, which involves code changes and application start and stop, while micro-service architecture only needs code changes and service start and stop for problematic services. Other services can achieve fault tolerance at the application level through mechanisms such as retry and circuit breaker.
Flexible technology selection: under the micro-service architecture, each micro-service node can freely choose the most suitable technology stack according to the different functions required. Even if a single micro-service node is reconstructed, the cost is also very low.
Development, operation and maintenance is more efficient: each micro-service node is a single process, focuses on a single function, and clearly expresses the service boundary through a well-defined interface. Because of its small size and low complexity, each microservice can be completely controlled by a small team or individual, making it easy to maintain high maintainability and development efficiency.
As the most popular micro-service development framework at present, Spring Cloud does not adopt the Spring Cloud framework to realize the micro-service architecture, which has the advantages of micro-service architecture. The correct understanding is to use the Spring Cloud framework to develop micro-service architecture systems, so that the system has the advantages of micro-service architecture (Spring Cloud is like a tool, but also needs to "do" the process).
1.4 what is Spring Boot? What is Spring Cloud?
The Spring Boot framework is a new framework provided by the Pivotal team, which is designed to simplify the initial construction and development process of Spring-based applications.
SpringBoot framework uses a specific way to configure the application system, so that developers no longer need to spend a lot of effort to define templated configuration files.
Spring Cloud is a cloud application development tool based on Spring Boot, which provides a simple development method for configuration management, service registration, service discovery, circuit breaker, intelligent routing, micro-agent, control bus, global lock, decision campaign, distributed session and cluster state management in JVM-based cloud application development.
1.5 the relationship among microservices, Spring Boot and Spring Cloud
Idea: micro-service is a concept of architecture, puts forward the design principles of micro-service, and provides a guiding ideology for specific technology landing from the theory.
Scaffolding: Spring Boot is a set of quick configuration scaffolding that allows you to quickly develop individual micro services based on Spring Boot.
Collection of multiple components: Spring Cloud is a service governance toolkit based on Spring Boot implementation; Spring Boot focuses on a single micro-service individual that is fast and easy to integrate; and Spring Cloud focuses on the global service governance framework.
II. Technical analysis
2.1 Everything is jar, Everything is http
SpringBoot is identified as a SpringBoot application with the @ SpringBootApplication annotation. All applications are compiled, deployed and run through jar packages.
@ SpringBootApplication public class Application {private static final Logger LOGGER = LoggerFactory.getLogger (Application.class); public static void main (String [] args) {SpringApplication.run (Application.class, args); LOGGER.info ("started successfully!") ;}}
Every Spring Boot application can provide http services through embedded web containers, and only need to rely on spring-boot-start-web in the pom file. In principle, the microservice architecture wants each independent node to provide http services.
Org.springframework.bootspring-boot-starter-web
2.2 Spring boot Task task startup and timing tasks
When Spring Boot needs to start a task, all it has to do is to inherit the CommandLineRunner interface to implement its run method.
@ SpringBootApplication public class ClientDataListener implements CommandLineRunner public void run (String... Strings) throws Exception {clientInfoListenerHandler ();}}
When Spring Boot needs to execute scheduled tasks, you only need to add @ Scheduled (cron = "0150 * *?") to the scheduled task method. Annotations (support for standard cron expressions), and add @ EnableScheduling annotations to the service startup class.
@ SpringBootApplication@EnableSchedulingpublic class Application {private static final Logger LOGGER = LoggerFactory.getLogger (Application.class); public static void main (String [] args) {SpringApplication.run (Application.class, args); LOGGER.info ("started successfully!") ;}} / / some class@Scheduled (cron = "0 150 * *?") public void someTimeTask () {* *}
2.3 Spring boot Actuator Monitoring
Actuator is a component provided by spring boot to monitor the application system itself. Spring-boot-starter-actuator can be introduced on the basis of the introduction of spring-boot-start-web.
Org.springframework.bootspring-boot-starter-actuator
2.4 Spring cloud Config configuration Center
When we implement the micro-service architecture, each micro-service node needs its own relevant configuration data items. when there are many nodes, maintenance becomes very difficult, so it is necessary to establish a central configuration service.
Spring Cloud Config is divided into two parts. Spring Cloud Config server is a service process, and Spring Cloud Config File is the location where configuration files are stored.
2.5 Spring cloud Eureka service registry
The concept of service registration appeared long before the micro-service architecture, which splits the original single application node into a large number of micro-service nodes. The invocation relationship between each other will be very complex. Spring Cloud Eureka as a registry, all micro-services can register themselves with Spring Cloud Eureka for unified management and access (unlike Eureka and Zookeeper, OP is chosen in the AOP principle, with more emphasis on the effectiveness of the service)
2.6 Spring cloud Zuul server intelligent routing
When we register all the services with the Eureka (Service Registry), it involves the question of how to invoke them. Spring Cloud Zuul is a server proxy component provided by Spring Cloud, which can be regarded as a gateway. Zuul obtains the available services through Eureka, and through mapping configuration, the client accesses the services that need to be accessed by accessing Zuul. All services are identified by spring.application.name
Different IP addresses, the same spring.application.name is a service cluster. When we add a node with the same spring.application.name, Zuul communicates with Eureka to obtain the information of the new node to realize intelligent routing and increase the response ability of this type of service.
2.7 Spring cloud Ribbon client intelligent routing
Corresponding to the server proxy of Spring Cloud Zuul, Spring Cloud Ribbon provides a client proxy. In the server agent, the client does not need to know which micro-service node provides the service, while the client agent acquires the node that actually provides the service and selects one for service invocation. Ribbon is similar to Zuul in that it communicates with Eureka (Service Registry) to achieve intelligent routing on the client side.
2.8Spring cloud Sleuth distributed tracking
2.9 Spring cloud Zipkin call chain
2.10 Spring cloud Feign http client
Spring Cloud Feign is a declarative, templated http client. Using Spring Cloud Feign to request a remote service is the same as calling a local method, making developers feel that this is a remote method (Feign integrates Ribbon for load balancing).
Mapping remote services to local services
@ FeignClient (name = "rabbitmq-http", url = "${SKYTRAIN_RABBITMQ_HTTP}") public interface TaskService {@ RequestMapping (value = "/ api/queues", method = RequestMethod.GET) public String query (@ RequestHeader ("Authorization") String token);}
Invoke a remote service by invoking a local service
@ Autowired private TaskService taskService; private String queryRabbitmqStringInfo () {byte [] credentials = Base64 .encode Base64 ((rabbitmqHttpUserName + ":" + rabbitmqHttpPassword) .getBytes (StandardCharsets.UTF_8)); String token = "Basic" + new String (credentials, StandardCharsets.UTF_8); return taskService.query (token);}
2.11 Spring cloud Hystrix circuit breaker
III. Micro-service practice
3.1 several micro-service components we have developed-Application Management Center
The application management center can stop, compile, package, deploy and start the complete online operation for each registered micro-service node.
3.2 several micro-service components we developed-zookeeper data query center
Zookeeper data query center obtains zookeeper data information according to zookeeper address, port, and command.
3.3 several micro-service components we have developed-micro-service health testing center
The health detection center periodically checks the status of each micro-service and triggers an alarm when it is found that there is a micro-service status in DOWN or connection timeout.
3.4 several micro-service components we have developed-timed task query center
/ / intercept @ Componentpublic class SkytrainBeanPostProcessor implements BeanPostProcessor in BeanPostProcessor subclass, processing after instantiation of Ordered {* / * Bean * / public Object postProcessAfterInitialization (Object bean, String beanName) throws BeansException {beanPostProcessor.postProcessAfter (bean, beanName); return bean;} *} / / obtain scheduled task notes after interception * * public Object postProcessAfter (Object bean, String beanName) {Class targetClass = AopUtils.getTargetClass (bean) Map annotatedMethods = MethodIntrospector.selectMethods (targetClass, new MethodIntrospector.MetadataLookup () {public Set inspect (Method method) {Set scheduledMethods = AnnotatedElementUtils.getMergedRepeatableAnnotations (method, Scheduled.class, Schedules.class); return (! scheduledMethods.isEmpty ()? ScheduledMethods: null);}}); if (! annotatedMethods.isEmpty ()) {String className = targetClass.getName (); for (Map.Entry entry: annotatedMethods.entrySet ()) {Method method = entry.getKey (); for (Scheduled scheduled: entry.getValue ()) {String key = className + ":" + method.getName (); String value = scheduled.toString (); taskInfos.put (key, value) } return null;} * * / register * public void taskRegister () {String nodeInfo = ipAddress + ":" + serverPort + ":"; try {/ * scheduled task * / Map infos = taskInfos; for (Entry item: infos.entrySet ()) {String taskId = nodeInfo + item.getKey (); String taskParameter = item.getValue () JSONObject info = new JSONObject (); info.put ("taskId", taskId); info.put ("taskParameter", taskParameter); info.put ("applicationName", applicationName); info.put ("taskType", "schedule"); LOGGER.info (info.toString ()); zooKeeperExecutor.createZKNode (SKYTRAIN_TASK_ZKNODE_PREFIX + taskId, info.toString ()) }} catch (Exception ex) {LOGGER.error ("", ex);}} * *
3.5 Classification of microservices
Micro service platform component
Common service component
Basic service component / business service component
3.6 overall micro-service architecture diagram
The above is all the contents of the article "sample Analysis of SpringCloud Microservice Architecture upgrade". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.