新聞中心
這篇文章主要介紹“微服務(wù)架構(gòu)的高并發(fā)問(wèn)題有哪些”,在日常操作中,相信很多人在微服務(wù)架構(gòu)的高并發(fā)問(wèn)題有哪些問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”微服務(wù)架構(gòu)的高并發(fā)問(wèn)題有哪些”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
創(chuàng)新互聯(lián)建站從2013年開(kāi)始,是專(zhuān)業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站制作、網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元葫蘆島做網(wǎng)站,已為上家服務(wù),為葫蘆島各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話(huà):028-86922220
1 準(zhǔn)備環(huán)境
1.1 準(zhǔn)備商品微服務(wù)和訂單微服務(wù)
其中商品微服務(wù)的findById()方法設(shè)置休眠2秒,用來(lái)模擬網(wǎng)絡(luò)波動(dòng)等情況:
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 "新增成功"; } @GetMapping(value = "/findById/{id}") public Product findById(@PathVariable(value = "id") Long id) { try { //休眠2秒,用來(lái)模擬 網(wǎng)絡(luò)波動(dòng)等情況 Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } Product product = productService.findById(id); product.setProductName("訪(fǎng)問(wèn)的地址是:" + ip + ":" + port); return product; } }
設(shè)置訂單微服務(wù)的Tomcat的最大線(xiàn)程數(shù)是10:
server: port: 9002 # 微服務(wù)的端口號(hào) tomcat: max-threads: 10 # 最大線(xiàn)程數(shù)是10 spring: application: name: service-order # 微服務(wù)的名稱(chēng) 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 # 配置Eureka eureka: instance: # 實(shí)例的名稱(chēng) instance-id: service-order:9002 # 顯示IP信息 prefer-ip-address: true lease-renewal-interval-in-seconds: 5 # 發(fā)送心跳續(xù)約間隔(默認(rèn)30秒) lease-expiration-duration-in-seconds: 10 # Eureka Client發(fā)送心跳給Eureka Server端后,續(xù)約到期時(shí)間(默認(rèn)90秒) 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的重試機(jī)制 service-product: ribbon: # 修改ribbon的負(fù)載均衡策略 服務(wù)名 - ribbon - NFLoadBalancerRuleClassName :負(fù)載均衡策略 # NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule # 修改ribbon的負(fù)載均衡策略為權(quán)重策略 # Ribbon的重試機(jī)制參數(shù) ConnectTimeout: 250 # Ribbon的連接超時(shí)時(shí)間 ReadTimeout: 1000 # Ribbon的數(shù)據(jù)讀取超時(shí)時(shí)間 OkToRetryOnAllOperations: true # 是否對(duì)所有操作都進(jìn)行重試 MaxAutoRetriesNextServer: 50 # 切換實(shí)例的重試次數(shù) MaxAutoRetries: 1 # 對(duì)當(dāng)前實(shí)例的重試次數(shù) # 微服務(wù)info內(nèi)容詳細(xì)信息 info: app.name: xxx company.name: xxx build.artifactId: $project.artifactId$ build.version: $project.version$ # 開(kāi)啟日志debug logging: level: root: info
訂單微服務(wù)中的SpringConfig.java
package com.sunxiaping.order.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class SpringConfig { @Bean // @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } }
訂單微服務(wù)的OrderController.java
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 "商品查詢(xún)到了"; } }
2 使用Jmeter測(cè)試接口
使用JMeter性能測(cè)試工具以50個(gè)線(xiàn)程每個(gè)線(xiàn)程循環(huán)50次測(cè)試:http://localhost:9002/order/buy/1接口,然后通過(guò)瀏覽器調(diào)用http://localhost:9002/order/findOrder接口,發(fā)現(xiàn)特別慢。
3 系統(tǒng)負(fù)載過(guò)高存在的問(wèn)題
3.1 問(wèn)題分析
在微服務(wù)架構(gòu)中,我們將業(yè)務(wù)拆成一個(gè)個(gè)的服務(wù),服務(wù)和服務(wù)之間可以相互調(diào)用,由于網(wǎng)絡(luò)原因或者自身的原因,服務(wù)并不能保證100%可用,如果單個(gè)服務(wù)出現(xiàn)問(wèn)題,調(diào)用這個(gè)服務(wù)就會(huì)出現(xiàn)網(wǎng)絡(luò)延遲,此時(shí)如果有大量的網(wǎng)絡(luò)請(qǐng)求涌入,會(huì)形成任務(wù)累計(jì),導(dǎo)致服務(wù)癱瘓。
換句話(huà)說(shuō),Tomcat等容器會(huì)以線(xiàn)程池的方式對(duì)所有的請(qǐng)求進(jìn)行統(tǒng)一的管理,如果某個(gè)方法可能存著耗時(shí)問(wèn)題,隨著外面積壓的請(qǐng)求越來(lái)越多,勢(shì)必會(huì)造成系統(tǒng)的崩潰、癱瘓等。
為了不影響其他接口的正常訪(fǎng)問(wèn):對(duì)多個(gè)服務(wù)之間進(jìn)行隔離。
服務(wù)隔離的方式:
:two:信號(hào)量隔離(計(jì)數(shù)器,就是對(duì)某個(gè)方法進(jìn)行設(shè)置閾值,如果超過(guò)了閾值,直接報(bào)錯(cuò))。
:one:線(xiàn)程池隔離。
4 線(xiàn)程池隔離的方式處理積壓?jiǎn)栴}
4.1 在訂單微服務(wù)中引入相關(guān)jar包的Maven坐標(biāo)
com.netflix.hystrix hystrix-metrics-event-stream 1.5.12 com.netflix.hystrix hystrix-javanica 1.5.12
4.2 配置線(xiàn)程池
配置HystrixCommand接口的實(shí)現(xiàn)類(lèi),在實(shí)現(xiàn)類(lèi)中可以對(duì)線(xiàn)程池進(jìn)行配置:
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() { // 服務(wù)分組 HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory.asKey("order_product"); // 服務(wù)標(biāo)識(shí) HystrixCommandKey commandKey = HystrixCommandKey.Factory.asKey("product"); // 線(xiàn)程池名稱(chēng) HystrixThreadPoolKey threadPoolKey = HystrixThreadPoolKey.Factory.asKey("order_product_pool"); /** * 線(xiàn)程池配置 * withCoreSize : 線(xiàn)程池大小為10 * withKeepAliveTimeMinutes: 線(xiàn)程存活時(shí)間15秒 * withQueueSizeRejectionThreshold :隊(duì)列等待的閾值為100,超過(guò)100執(zhí)行拒絕策略 */ HystrixThreadPoolProperties.Setter threadPoolProperties = HystrixThreadPoolProperties.Setter().withCoreSize(50) .withKeepAliveTimeMinutes(15).withQueueSizeRejectionThreshold(100); // 命令屬性配置Hystrix 開(kāi)啟超時(shí) HystrixCommandProperties.Setter commandProperties = HystrixCommandProperties.Setter() // 采用線(xiàn)程池方式實(shí)現(xiàn)服務(wù)隔離 .withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.THREAD) // 禁止 .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("http://localhost:9001/product/findById/" + id, Product.class); } /** * 服務(wù)降級(jí) * * @return */ @Override protected Product getFallback() { Product product = new Product(); product.setProductName("不好意思,出錯(cuò)了"); return product; } }
4.3 修改Controller
package 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; /** * 使用OrderCommand調(diào)用遠(yuǎn)程遠(yuǎn)程服務(wù) * * @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 "商品查詢(xún)到了"; } }
到此,關(guān)于“微服務(wù)架構(gòu)的高并發(fā)問(wèn)題有哪些”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
網(wǎng)頁(yè)題目:微服務(wù)架構(gòu)的高并發(fā)問(wèn)題有哪些
本文路徑:http://fisionsoft.com.cn/article/pdhesj.html