In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "what are the high concurrency problems of micro-service architecture". In daily operations, I believe many people have doubts about the high concurrency of micro-service architecture. I have consulted all kinds of materials and sorted out simple and useful operation methods. I hope it will be helpful for you to answer the questions of "what are the high concurrency problems of micro-service architecture?" Next, please follow the editor to study!
1 prepare environment 1.1 prepare commodity micro-service and order micro-service
The findById () method of commodity micro-service sets hibernation for 2 seconds to simulate network fluctuations:
Package com.sunxiaping.product.controller;import com.sunxiaping.product.domain.Product;import com.sunxiaping.product.service.ProductService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.*;@RestController@RequestMapping (value = "/ product") public class ProductController {@ Autowired private ProductService productService; @ Value ("${server.port}") private String port @ Value ("${spring.cloud.client.ip-address}") private String ip; @ PostMapping (value = "/ save") public String save (@ RequestBody Product product) {productService.save (product); return "added successfully" } @ GetMapping (value = "/ findById/ {id}") public Product findById (@ PathVariable (value = "id") Long id) {try {/ / dormant for 2 seconds, used to simulate network fluctuations, Thread.sleep (2000);} catch (InterruptedException e) {e.printStackTrace ();} Product product = productService.findById (id) Product.setProductName ("access address is:" + ip + ":" + port); return product;}}
The maximum number of threads for setting up the order micro-service Tomcat is 10:
The port number of server: port: 9002 # is tomcat: max-threads: 10 # the maximum number of threads is 10spring: application: name: service-order # the name of microservice datasource: url: jdbc:mysql://192.168.1.57:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true driver-class-name: com.mysql.cj.jdbc.Driver username: root password: 123456 jpa: Generate-ddl: true show-sql: true open-in-view: true database: mysql jmx: unique-names: true# configuration Eurekaeureka: instance: # instance name instance-id: service-order:9002 # display IP information prefer-ip-address: true lease-renewal-interval-in-seconds: 5 # send heartbeat renewal interval (default 30 seconds) lease-expiration-duration-in-seconds: After 10 # Eureka Client sends heartbeat to Eureka Server end Renewal expiration time (default is 90 seconds) address of client: healthcheck: enabled: true service-url: # Eureka Server # defaultZone: http://localhost:9000/eureka/ defaultZone: http://eureka7001.com:7001/eureka/, Http://eureka7002.com:7002/eureka/# Ribbon retry mechanism service-product: ribbon: # modify ribbon's load balancing policy service name-ribbon-NFLoadBalancerRuleClassName: load balancing policy # NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule # modify ribbon's load balancing policy to weight policy # Ribbon retry mechanism parameter ConnectTimeout: 250 # Ribbon connection timeout ReadTimeout: 1000 # Ribbon Data read timeout OkToRetryOnAllOperations: true # whether to retry all operations MaxAutoRetriesNextServer: 50 # retry number of switching instances MaxAutoRetries: 1 # number of retries on the current instance # Microservice info content details info: app.name: xxx company.name: xxx build.artifactId: $project.artifactId$ build.version: $project.version$# enable log debuglogging: level: root: info
SpringConfig.java in order Micro Service
Package com.sunxiaping.order.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.client.RestTemplate;@Configurationpublic class SpringConfig {@ Bean// @ LoadBalanced public RestTemplate restTemplate () {return new RestTemplate ();}
OrderController.java of order micro-service
Package com.sunxiaping.order.controller;import com.sunxiaping.order.domain.Product;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;@RestController@RequestMapping (value = "/ order") public class OrderController {@ Autowired private RestTemplate restTemplate / * @ param id * @ return * / @ GetMapping (value = "/ buy/ {id}") public Product buy (@ PathVariable (value = "id") Long id) {Product product = restTemplate.getForObject ("http://localhost:9001/product/findById/" + id, Product.class); return product } @ GetMapping (value = "/ findOrder") public String findOrder () {return "goods found";}} 2 use the Jmeter test interface
Use the JMeter performance testing tool to cycle through 50 threads 50 times per thread: the http://localhost:9002/order/buy/1 interface, and then call the http://localhost:9002/order/findOrder interface through the browser, which is found to be particularly slow.
(3) Analysis of the problems existing in the excessive load of the system 3.1.
In the micro-service architecture, we split the business into services, and services and services can be called each other. Due to network reasons or their own reasons, services cannot be guaranteed to be 100% available. If there is a problem with a single service, there will be network delay in calling this service. At this time, if a large number of network requests pour in, tasks will accumulate, resulting in service paralysis.
In other words, containers such as Tomcat will uniformly manage all requests in the way of thread pool. If a method may have time-consuming problems, as the backlog of requests increases, it is bound to cause system crash, paralysis and so on.
In order not to affect the normal access of other interfaces: isolate multiple services.
How the service is isolated:
: two: semaphore isolation (counter, which sets a threshold for a method and reports an error directly if the threshold is exceeded).
: one: thread pool isolation.
4. Thread pool isolation to deal with backlog 4.1 introducing Maven coordinates of related jar packages into the order microservice com.netflix.hystrix hystrix-metrics-event-stream 1.5.12 com.netflix.hystrix hystrix-javanica 1.5.124.2 configure thread pool
Configure the implementation class of the HystrixCommand interface, where you can configure the thread pool:
Package com.sunxiaping.order.command;import com.netflix.hystrix.*;import com.sunxiaping.order.domain.Product;import org.springframework.web.client.RestTemplate;public class OrderCommand extends HystrixCommand {private RestTemplate restTemplate; private Long id; public OrderCommand (RestTemplate restTemplate, Long id) {super (setter ()); this.restTemplate = restTemplate; this.id = id } private static Setter setter () {/ / Service grouping HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory.asKey ("order_product"); / / Service ID HystrixCommandKey commandKey = HystrixCommandKey.Factory.asKey ("product"); / / Thread Pool name HystrixThreadPoolKey threadPoolKey = HystrixThreadPoolKey.Factory.asKey ("order_product_pool") / * Thread pool configuration * withCoreSize: thread pool size is 10 * withKeepAliveTimeMinutes: thread survival time is 15 seconds * withQueueSizeRejectionThreshold: the threshold of queue waiting is 100, and execute reject policy beyond 100 * / HystrixThreadPoolProperties.Setter threadPoolProperties = HystrixThreadPoolProperties.Setter (). WithCoreSize (50) .withKeepAliveTimeMinutes (15). WithQueueSizeRejectionThreshold (100) / / Command attribute configures Hystrix startup timeout HystrixCommandProperties.Setter commandProperties = HystrixCommandProperties.Setter () / / implements service isolation by thread pool. WithExecutionIsolationStrategy (HystrixCommandProperties.ExecutionIsolationStrategy.THREAD) / / disable .withExecutionTimeoutEnabled (false) Return Setter.withGroupKey (groupKey) .andCommandKey (commandKey) .andThreadPoolKey (threadPoolKey) .andThreadPoolPropertiesDefaults (threadPoolProperties) .andCommandPropertiesDefaults (commandProperties);} @ Override protected Product run () throws Exception {System.out.println (Thread.currentThread (). GetName ()); return restTemplate.getForObject ("ThreadPoolPropertiesDefaults (threadPoolProperties), Product.class) } / * * Service downgrade * * @ return * / @ Override protected Product getFallback () {Product product = new Product (); product.setProductName ("Sorry, error"); return product;} 4.3 modify Controllerpackage com.sunxiaping.order.controller;import com.sunxiaping.order.command.OrderCommand;import com.sunxiaping.order.domain.Product Import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;@RestController@RequestMapping (value = "/ order") public class OrderController {@ Autowired private RestTemplate restTemplate / * use OrderCommand to invoke remote remote service * * @ param id * @ return * / @ GetMapping (value = "/ buy/ {id}") public Product buy (@ PathVariable (value = "id") Long id) {return new OrderCommand (restTemplate, id). Execute () } @ GetMapping (value = "/ findOrder") public String findOrder () {System.out.println (Thread.currentThread (). GetName ()); return "goods found";}} at this point, the study on "what are the high concurrency problems of micro-service architecture" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.