溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

微服務(wù)架構(gòu)的高并發(fā)問題有哪些

發(fā)布時(shí)間:2021-10-19 11:18:28 來源:億速云 閱讀:106 作者:iii 欄目:編程語言

這篇文章主要介紹“微服務(wù)架構(gòu)的高并發(fā)問題有哪些”,在日常操作中,相信很多人在微服務(wù)架構(gòu)的高并發(fā)問題有哪些問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對(duì)大家解答”微服務(wù)架構(gòu)的高并發(fā)問題有哪些”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!

1 準(zhǔn)備環(huán)境

1.1 準(zhǔn)備商品微服務(wù)和訂單微服務(wù)

  • 其中商品微服務(wù)的findById()方法設(shè)置休眠2秒,用來模擬網(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秒,用來模擬 網(wǎng)絡(luò)波動(dòng)等情況
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Product product = productService.findById(id);
        product.setProductName("訪問的地址是:" + ip + ":" + port);
        return product;
    }
}
  • 設(shè)置訂單微服務(wù)的Tomcat的最大線程數(shù)是10:

server:
  port: 9002 # 微服務(wù)的端口號(hào)
  tomcat:
    max-threads: 10 # 最大線程數(shù)是10

spring:
  application:
    name: service-order # 微服務(wù)的名稱
  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í)例的名稱
    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$

# 開啟日志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 "商品查詢到了";
    }
}

2 使用Jmeter測(cè)試接口

  • 使用JMeter性能測(cè)試工具以50個(gè)線程每個(gè)線程循環(huán)50次測(cè)試:http://localhost:9002/order/buy/1接口,然后通過瀏覽器調(diào)用http://localhost:9002/order/findOrder接口,發(fā)現(xiàn)特別慢。

微服務(wù)架構(gòu)的高并發(fā)問題有哪些

3 系統(tǒng)負(fù)載過高存在的問題

3.1 問題分析

  • 在微服務(wù)架構(gòu)中,我們將業(yè)務(wù)拆成一個(gè)個(gè)的服務(wù),服務(wù)和服務(wù)之間可以相互調(diào)用,由于網(wǎng)絡(luò)原因或者自身的原因,服務(wù)并不能保證100%可用,如果單個(gè)服務(wù)出現(xiàn)問題,調(diào)用這個(gè)服務(wù)就會(huì)出現(xiàn)網(wǎng)絡(luò)延遲,此時(shí)如果有大量的網(wǎng)絡(luò)請(qǐng)求涌入,會(huì)形成任務(wù)累計(jì),導(dǎo)致服務(wù)癱瘓。

  • 換句話說,Tomcat等容器會(huì)以線程池的方式對(duì)所有的請(qǐng)求進(jìn)行統(tǒng)一的管理,如果某個(gè)方法可能存著耗時(shí)問題,隨著外面積壓的請(qǐng)求越來越多,勢(shì)必會(huì)造成系統(tǒng)的崩潰、癱瘓等。

微服務(wù)架構(gòu)的高并發(fā)問題有哪些

  • 為了不影響其他接口的正常訪問:對(duì)多個(gè)服務(wù)之間進(jìn)行隔離。

  • 服務(wù)隔離的方式:

    微服務(wù)架構(gòu)的高并發(fā)問題有哪些

    • :two:信號(hào)量隔離(計(jì)數(shù)器,就是對(duì)某個(gè)方法進(jìn)行設(shè)置閾值,如果超過了閾值,直接報(bào)錯(cuò))。

    • :one:線程池隔離。

4 線程池隔離的方式處理積壓問題

4.1 在訂單微服務(wù)中引入相關(guān)jar包的Maven坐標(biāo)

<dependency>
    <groupId>com.netflix.hystrix</groupId>
    <artifactId>hystrix-metrics-event-stream</artifactId>
    <version>1.5.12</version>
</dependency>
<dependency>
    <groupId>com.netflix.hystrix</groupId>
    <artifactId>hystrix-javanica</artifactId>
    <version>1.5.12</version>
</dependency>

4.2 配置線程池

  • 配置HystrixCommand接口的實(shí)現(xiàn)類,在實(shí)現(xiàn)類中可以對(duì)線程池進(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<Product> {

    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");
        // 線程池名稱
        HystrixThreadPoolKey threadPoolKey = HystrixThreadPoolKey.Factory.asKey("order_product_pool");
        /**
         * 線程池配置
         *     withCoreSize :  線程池大小為10
         *     withKeepAliveTimeMinutes:  線程存活時(shí)間15秒
         *     withQueueSizeRejectionThreshold  :隊(duì)列等待的閾值為100,超過100執(zhí)行拒絕策略
         */
        HystrixThreadPoolProperties.Setter threadPoolProperties = HystrixThreadPoolProperties.Setter().withCoreSize(50)
                .withKeepAliveTimeMinutes(15).withQueueSizeRejectionThreshold(100);

        // 命令屬性配置Hystrix 開啟超時(shí)
        HystrixCommandProperties.Setter commandProperties = HystrixCommandProperties.Setter()
                // 采用線程池方式實(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 "商品查詢到了";
    }
}

到此,關(guān)于“微服務(wù)架構(gòu)的高并發(fā)問題有哪些”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI