1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        SpringCloud微服務(wù)框架Hystrix服務(wù)熔斷

        共 8912字,需瀏覽 18分鐘

         ·

        2021-04-07 10:48

        Springcloud框架的Hystrix負(fù)載均衡

        相關(guān)概念

        Hystrix是一個(gè)用于處理分布式系統(tǒng)的延遲和容錯(cuò)的開(kāi)源庫(kù),在分布式系統(tǒng)里,許多依賴不可避免的會(huì)調(diào)用失敗,比如超時(shí)、異常等,Hystrix能夠保證在一個(gè)依賴出問(wèn)題的情況下,不會(huì)導(dǎo)致整體服務(wù)失敗,避免級(jí)聯(lián)故障,以提高分布式系統(tǒng)的彈性?!皵嗦菲鳌北旧硎且环N開(kāi)關(guān)裝置,當(dāng)某個(gè)服務(wù)單元發(fā)生故障之后,通過(guò)斷路器的故障監(jiān)控(類(lèi)似熔斷保險(xiǎn)絲),向調(diào)用方返回一個(gè)符合預(yù)期的、可處理的備選響應(yīng)(FallBack),而不是長(zhǎng)時(shí)間的等待或者拋出調(diào)用方無(wú)法處理的異常,這樣就保證了服務(wù)調(diào)用方的線程不會(huì)被長(zhǎng)時(shí)間、不必要地占用,從而避免了故障在分布式系統(tǒng)中的蔓延,乃至雪崩。

        Hystrix 解決了什么問(wèn)題

        服務(wù)降級(jí),服務(wù)熔斷,服務(wù)限流,接近實(shí)時(shí)的監(jiān)控

        服務(wù)熔斷

        概念

        服務(wù)熔斷 熔斷機(jī)制是應(yīng)對(duì)雪崩效應(yīng)的一種微服務(wù)鏈路保護(hù)機(jī)制。當(dāng)扇出鏈路的某個(gè)微服務(wù)不可用或者響應(yīng)時(shí)間太長(zhǎng)時(shí),會(huì)進(jìn)行服務(wù)的降級(jí),進(jìn)而熔斷該節(jié)點(diǎn)微服務(wù)的調(diào)用,快速返回"錯(cuò)誤"的響應(yīng)信息。當(dāng)檢測(cè)到該節(jié)點(diǎn)微服務(wù)調(diào)用響應(yīng)正常后恢復(fù)調(diào)用鏈路。在SpringCloud框架里熔斷機(jī)制通過(guò)Hystrix實(shí)現(xiàn)。Hystrix會(huì)監(jiān)控微服務(wù)間調(diào)用的狀況,當(dāng)失敗的調(diào)用到一定閾值,缺省是5秒內(nèi)20次調(diào)用失敗就會(huì)啟動(dòng)熔斷機(jī)制。熔斷機(jī)制的注解是@HystrixCommand。

        新建microservicecloud-provider-dept-hystrix-8001

        添加相關(guān)的pom依賴

        ????<dependencies>
        ????????<dependency>
        ????????????<groupId>com.yang</groupId>
        ????????????<artifactId>microservicecloud-api</artifactId>
        ????????????<version>1.0-SNAPSHOT</version>
        ????????</dependency>

        ????????<!--??hystrix?-->
        ????????<dependency>
        ????????????<groupId>org.springframework.cloud</groupId>
        ????????????<artifactId>spring-cloud-starter-hystrix</artifactId>
        ????????</dependency>

        ????????<!--?將微服務(wù)provider側(cè)注冊(cè)進(jìn)eureka?-->
        ????????<dependency>
        ????????????<groupId>org.springframework.cloud</groupId>
        ????????????<artifactId>spring-cloud-starter-eureka</artifactId>
        ????????</dependency>
        ????????<dependency>
        ????????????<groupId>org.springframework.cloud</groupId>
        ????????????<artifactId>spring-cloud-starter-config</artifactId>
        ????????</dependency>
        ????????<!--?actuator監(jiān)控信息完善?-->
        ????????<dependency>
        ????????????<groupId>org.springframework.boot</groupId>
        ????????????<artifactId>spring-boot-starter-actuator</artifactId>
        ????????</dependency>
        ????????<dependency>
        ????????????<groupId>junit</groupId>
        ????????????<artifactId>junit</artifactId>
        ????????</dependency>
        ????????<dependency>
        ????????????<groupId>mysql</groupId>
        ????????????<artifactId>mysql-connector-java</artifactId>
        ????????</dependency>
        ????????<dependency>
        ????????????<groupId>com.alibaba</groupId>
        ????????????<artifactId>druid</artifactId>
        ????????</dependency>
        ????????<dependency>
        ????????????<groupId>ch.qos.logback</groupId>
        ????????????<artifactId>logback-core</artifactId>
        ????????</dependency>
        ????????<dependency>
        ????????????<groupId>org.mybatis.spring.boot</groupId>
        ????????????<artifactId>mybatis-spring-boot-starter</artifactId>
        ????????</dependency>
        ????????<dependency>
        ????????????<groupId>org.springframework.boot</groupId>
        ????????????<artifactId>spring-boot-starter-jetty</artifactId>
        ????????</dependency>
        ????????<dependency>
        ????????????<groupId>org.springframework.boot</groupId>
        ????????????<artifactId>spring-boot-starter-web</artifactId>
        ????????</dependency>
        ????????<dependency>
        ????????????<groupId>org.springframework.boot</groupId>
        ????????????<artifactId>spring-boot-starter-test</artifactId>
        ????????</dependency>
        ????????<!--?修改后立即生效,熱部署?-->
        ????????<dependency>
        ????????????<groupId>org.springframework</groupId>
        ????????????<artifactId>springloaded</artifactId>
        ????????</dependency>
        ????????<dependency>
        ????????????<groupId>org.springframework.boot</groupId>
        ????????????<artifactId>spring-boot-devtools</artifactId>
        ????????</dependency>
        ????</dependencies>

        創(chuàng)建application.yml

        server:
        ??port:?8001
        ??
        mybatis:
        ??config-location:?classpath:mybatis/mybatis.cfg.xml????????#?mybatis配置文件所在路徑
        ??type-aliases-package:?com.yang.entity????#?所有Entity別名類(lèi)所在包
        ??mapper-locations:
        ??-?classpath:mybatis/mapper/**/*.xml???????????????????????#?mapper映射文件
        ????
        spring:
        ???application:
        ????name:?microservicecloud-dept?
        ???datasource:
        ????type:?com.alibaba.druid.pool.DruidDataSource????????????#?當(dāng)前數(shù)據(jù)源操作類(lèi)型
        ????driver-class-name:?org.gjt.mm.mysql.Driver??????????????#?mysql驅(qū)動(dòng)包
        ????url:?jdbc:mysql://localhost:3306/cloudDB01??????????????#?數(shù)據(jù)庫(kù)名稱
        ????username:?root
        ????password:?root
        ????dbcp2:
        ??????min-idle:?5???????????????????????????????????????????#?數(shù)據(jù)庫(kù)連接池的最小維持連接數(shù)
        ??????initial-size:?5???????????????????????????????????????#?初始化連接數(shù)
        ??????max-total:?5??????????????????????????????????????????#?最大連接數(shù)
        ??????max-wait-millis:?200??????????????????????????????????#?等待連接獲取的最大超時(shí)時(shí)間

        eureka:
        ??client:?#客戶端注冊(cè)進(jìn)eureka服務(wù)列表內(nèi)
        ????service-url:?
        #??????defaultZone:?http://localhost:7001/eureka
        ???????defaultZone:?http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
        ??instance:
        ????instance-id:?microservicecloud-dept8001-hystrix???#自定義服務(wù)名稱信息
        ????prefer-ip-address:?true?????#訪問(wèn)路徑可以顯示IP地址?????
        ?
        info:?
        ??app.name:?atguigu-microservicecloud
        ??company.name:?www.atguigu.com
        ??build.artifactId:?$project.artifactId$
        ??build.version:?$project.version$

        創(chuàng)建mybatis.cfg.xml配置文件

        <?xml?version="1.0"?encoding="UTF-8"??>
        <!DOCTYPE?configuration
        ??PUBLIC?"-//mybatis.org//DTD?Config?3.0//EN"
        ??"http://mybatis.org/dtd/mybatis-3-config.dtd">

        <configuration>
        <settings>
        ??<setting?name="cacheEnabled"?value="true"?/><!--?二級(jí)緩存開(kāi)啟?-->
        ?</settings>
        </configuration>

        創(chuàng)建mapper配置文件

        <?xml?version="1.0"?encoding="UTF-8"??>
        <!DOCTYPE?mapper?PUBLIC?"-//mybatis.org//DTD?Mapper?3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

        <mapper?namespace="com.yang.dao.DeptDao">

        ?<select?id="findById"?resultType="com.yang.entity.Dept"?parameterType="Long">
        ??select?deptno,dname,db_source?from?dept?where?deptno=#{deptno};
        ?</select>
        ?<select?id="findAll"?resultType="com.yang.entity.Dept">
        ??select?deptno,dname,db_source?from?dept;
        ?</select>
        ?<insert?id="addDept"?parameterType="com.yang.entity.Dept">
        ??INSERT?INTO?dept(dname,db_source)?VALUES(#{dname},DATABASE());
        ?</insert>
        </mapper>

        創(chuàng)建控制類(lèi)controlelr

        package?com.yang.controler;



        import?com.yang.entity.Dept;
        import?com.yang.service.DeptService;
        import?org.springframework.beans.factory.annotation.Autowired;
        import?org.springframework.web.bind.annotation.PathVariable;
        import?org.springframework.web.bind.annotation.RequestMapping;
        import?org.springframework.web.bind.annotation.RequestMethod;
        import?org.springframework.web.bind.annotation.RestController;


        import?com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

        @RestController
        public?class?DeptController
        {
        ????@Autowired
        ????private?DeptService?service?=?null;

        ????@RequestMapping(value="/dept/get/{id}",method=RequestMethod.GET)
        ????@HystrixCommand(fallbackMethod?=?"processHystrix_Get")
        ????public?Dept?get(@PathVariable("id")?Long?id)
        ????
        {
        ????????Dept?dept?=??this.service.get(id);
        ????????if(null?==?dept)
        ????????{
        ????????????throw?new?RuntimeException("該ID:"+id+"沒(méi)有沒(méi)有對(duì)應(yīng)的信息");
        ????????}
        ????????return?dept;
        ????}

        ????public?Dept?processHystrix_Get(@PathVariable("id")?Long?id)
        ????
        {
        ????????return?new?Dept().setDeptno(id)
        ????????????????.setDname("該ID:"+id+"沒(méi)有沒(méi)有對(duì)應(yīng)的信息,null--@HystrixCommand")
        ????????????????.setDb_source("no?this?database?in?MySQL");
        ????}
        }

        創(chuàng)建dao層

        package?com.yang.dao;

        import?com.yang.entity.Dept;
        import?org.apache.ibatis.annotations.Mapper;

        import?java.util.List;

        @Mapper
        public?interface?DeptDao?{
        ????public?boolean?addDept(Dept?dept);

        ????public?Dept?findById(Long?id);

        ????public?List<Dept>?findAll();

        }

        創(chuàng)建service實(shí)現(xiàn)類(lèi)

        package?com.yang.service.impl;


        import?com.yang.dao.DeptDao;
        import?com.yang.entity.Dept;
        import?com.yang.service.DeptService;
        import?org.springframework.beans.factory.annotation.Autowired;
        import?org.springframework.stereotype.Service;

        import?java.util.List;

        @Service
        public?class?DeptServiceImpl?implements?DeptService?{
        ????@Autowired
        ????private?DeptDao?dao?;

        ????@Override
        ????public?boolean?add(Dept?dept)
        ????
        {
        ????????return?dao.addDept(dept);
        ????}

        ????@Override
        ????public?Dept?get(Long?id)
        ????
        {
        ????????return?dao.findById(id);
        ????}

        ????@Override
        ????public?List<Dept>?list()
        ????
        {
        ????????return?dao.findAll();
        ????}

        }

        創(chuàng)建啟動(dòng)類(lèi)

        package?com.yang;

        import?org.springframework.boot.SpringApplication;
        import?org.springframework.boot.autoconfigure.SpringBootApplication;
        import?org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
        import?org.springframework.cloud.client.discovery.EnableDiscoveryClient;
        import?org.springframework.cloud.netflix.eureka.EnableEurekaClient;

        @SpringBootApplication
        @EnableEurekaClient?//本服務(wù)啟動(dòng)后會(huì)自動(dòng)注冊(cè)進(jìn)eureka服務(wù)中
        @EnableCircuitBreaker//對(duì)hystrixR熔斷機(jī)制的支持
        public?class?DeptProvider8001_Hystrix_App
        {
        ????public?static?void?main(String[]?args)
        ????
        {
        ????????SpringApplication.run(DeptProvider8001_Hystrix_App.class,?args);
        ????}
        }


        ? ?

        如果你覺(jué)得這篇內(nèi)容對(duì)你挺有啟發(fā),我想邀請(qǐng)你幫我三個(gè)小忙:

        • 點(diǎn)個(gè)【在看】,或者分享轉(zhuǎn)發(fā),讓更多的人也能看到這篇內(nèi)容

        • 關(guān)注公眾號(hào)【園碼生活】,不定期分享原創(chuàng)&精品技術(shù)文章。

        歡迎評(píng)論區(qū)留下你的精彩評(píng)論~ ? ? ? ? ?
        ? ? ? ? ?

        覺(jué)得文章不錯(cuò)可以分享到朋友圈讓更多的小伙伴看到哦~

        客官!在看一下唄? ? ? ? ??


        瀏覽 54
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            男女午夜免费视频 | 色婷婷综合久久中文字幕雪峰 | 黄色性爱av| 黄片没毛免费的 | 国产一级婬乱片片AAA毛片 | 娇喘呻吟趴在雪白肉体耸动图 | 免费看大黄片 | 国产精品igao视频网网址男男 | 插进去的网站 | 日本手机在线 |