欧美亚洲中文,在线国自产视频,欧洲一区在线观看视频,亚洲综合中文字幕在线观看

      1. <dfn id="rfwes"></dfn>
          <object id="rfwes"></object>
        1. 站長(zhǎng)資訊網(wǎng)
          最全最豐富的資訊網(wǎng)站

          SpringCloud?Feign超詳細(xì)講解

          Feign是Netflix公司開(kāi)發(fā)的一個(gè)聲明式的REST調(diào)用客戶(hù)端; Ribbon負(fù)載均衡、 Hystrⅸ服務(wù)熔斷是我們Spring Cloud中進(jìn)行微服務(wù)開(kāi)發(fā)非?;A(chǔ)的組件,下面一起來(lái)看一下,希望對(duì)大家有幫助。

          SpringCloud?Feign超詳細(xì)講解

          程序員必備接口測(cè)試調(diào)試工具:立即使用
          Apipost = Postman + Swagger + Mock + Jmeter
          Api設(shè)計(jì)、調(diào)試、文檔、自動(dòng)化測(cè)試工具
          后端、前端、測(cè)試,同時(shí)在線協(xié)作,內(nèi)容實(shí)時(shí)同步

          推薦學(xué)習(xí):《java視頻教程》

          一、什么是Feign

          Feign是聲明式Web Service客戶(hù)端,它讓微服務(wù)之間的調(diào)用變得更簡(jiǎn)單,類(lèi)似controller調(diào)用service。SpringCloud集成了Ribbon和Eureka,可以使用Feigin提供負(fù)載均衡的http客戶(hù)端。Feign是通過(guò)接口和注釋來(lái)實(shí)現(xiàn)負(fù)載均衡的。

          二、Feign能干什么

          (摘抄自狂神說(shuō)JAVA)

          Feign能干什么?

          Feign旨在使編寫(xiě)Java Http客戶(hù)端變得更容易

          前面在使用Ribbon + RestTemplate時(shí),利用RestTemplate對(duì)Http請(qǐng)求的封裝處理,形成了一套模板化的調(diào)用方法。但是在實(shí)際開(kāi)發(fā)中,由于對(duì)服務(wù)依賴(lài)的調(diào)用可能不止一處,往往一個(gè)接口會(huì)被多處調(diào)用,所以通常都會(huì)針對(duì)每個(gè)微服務(wù)自行封裝一個(gè)客戶(hù)端類(lèi)來(lái)包裝這些依賴(lài)服務(wù)的調(diào)用。所以,F(xiàn)eign在此基礎(chǔ)上做了進(jìn)一步的封裝,由他來(lái)幫助我們定義和實(shí)現(xiàn)依賴(lài)服務(wù)接口的定義,在Feign的實(shí)現(xiàn)下,我們只需要?jiǎng)?chuàng)建一個(gè)接口并使用注解的方式來(lái)配置它 (類(lèi)似以前Dao接口上標(biāo)注Mapper注解,現(xiàn)在是一個(gè)微服務(wù)接口上面標(biāo)注一個(gè)Feign注解),即可完成對(duì)服務(wù)提供方的接口綁定,簡(jiǎn)化了使用Spring Cloud Ribbon 時(shí),自動(dòng)封裝服務(wù)調(diào)用客戶(hù)端的開(kāi)發(fā)量。

          Feign默認(rèn)集成了Ribbon

          利用Ribbon維護(hù)了MicroServiceCloud-Dept的服務(wù)列表信息,并且通過(guò)輪詢(xún)實(shí)現(xiàn)了客戶(hù)端的負(fù)載均衡,而與Ribbon不同的是,通過(guò)Feign只需要定義服務(wù)綁定接口且以聲明式的方法,優(yōu)雅而簡(jiǎn)單的實(shí)現(xiàn)了服務(wù)調(diào)用。

          三、Feign的使用步驟

          1、新建一個(gè)module

          SpringCloud?Feign超詳細(xì)講解

          2、配置Pom.xml

          <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">     <parent>         <artifactId>springcloud-demo2</artifactId>         <groupId>com.you</groupId>         <version>1.0-SNAPSHOT</version>     </parent>     <modelVersion>4.0.0</modelVersion>     <artifactId>springcloud-eureka-7001</artifactId>     <dependencies>         <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->         <!--Eureka Server-->         <dependency>             <groupId>org.springframework.cloud</groupId>             <artifactId>spring-cloud-starter-eureka-server</artifactId>             <version>1.4.6.RELEASE</version>         </dependency>         <!--熱部署-->         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-devtools</artifactId>         </dependency>     </dependencies> </project>
          登錄后復(fù)制

          3、配置applicatin.yaml

          server:
          port: 801

          eureka:
          client:
          register-with-eureka: false #不向eureka注冊(cè)自己
          service-url:
          defaultZone: http://localhost:7001/eureka/
          ribbon:
          eureka:
          enabled: true

          4、配置configBean

          package com.you.config; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class ConfigBean {     @Bean     @LoadBalanced  //ribbon     /*配置負(fù)載均衡實(shí)現(xiàn)RestTemplate*/     /*IRule*/     /*RoundRobinRule 輪詢(xún) */     /*RandomRule 隨機(jī)*/     /*AvailabilityFilteringRule 優(yōu)先過(guò)濾掉跳閘、訪問(wèn)故障的服務(wù),對(duì)剩下的進(jìn)行輪詢(xún) */     public RestTemplate getRestTemplate() {         return new RestTemplate();     } }
          登錄后復(fù)制

          登錄后復(fù)制

          5、配置Controller類(lèi)

          package com.you.config; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class ConfigBean {     @Bean     @LoadBalanced  //ribbon     /*配置負(fù)載均衡實(shí)現(xiàn)RestTemplate*/     /*IRule*/     /*RoundRobinRule 輪詢(xún) */     /*RandomRule 隨機(jī)*/     /*AvailabilityFilteringRule 優(yōu)先過(guò)濾掉跳閘、訪問(wèn)故障的服務(wù),對(duì)剩下的進(jìn)行輪詢(xún) */     public RestTemplate getRestTemplate() {         return new RestTemplate();     } }
          登錄后復(fù)制

          登錄后復(fù)制

          6、配置啟動(dòng)類(lèi)

          package com.you; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.ribbon.RibbonClient; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @EnableEurekaClient @EnableFeignClients(basePackages = {         "com.you"}) public class FeignDeptConsumer_80 {     public static void main(String[] args) {         SpringApplication.run(FeignDeptConsumer_80.class,args);     } }
          登錄后復(fù)制

          7、改動(dòng)API

          1)引入Feign依賴(lài)

          SpringCloud?Feign超詳細(xì)講解

           <dependency>             <groupId>org.springframework.cloud</groupId>             <artifactId>spring-cloud-starter-feign</artifactId>             <version>1.4.6.RELEASE</version>         </dependency>
          登錄后復(fù)制

          2)配置Service

          SpringCloud?Feign超詳細(xì)講解

          package com.you.service; import com.you.pojo.Dept; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @Component @FeignClient(value = "SPRINGCLOUD-PROVIDER-DEPT") public interface DeptClientService {     @GetMapping("/dept/aDept/{id}")     public Dept getDeptOfId(@PathVariable("id") Long id); }
          登錄后復(fù)制

          3)注意

          SpringCloud?Feign超詳細(xì)講解

          服務(wù)名字要寫(xiě)對(duì)GetMapper中的內(nèi)容要和提供者一致,否則報(bào)錯(cuò)(找了一下午)

          下面是提供者的內(nèi)容

          SpringCloud?Feign超詳細(xì)講解

          四、結(jié)果

          這樣即可獲取到數(shù)據(jù),而且負(fù)載平衡的默認(rèn)算法,仍然是輪詢(xún)!

          SpringCloud?Feign超詳細(xì)講解

          推薦學(xué)習(xí):《java視頻教程》

          贊(0)
          分享到: 更多 (0)
          網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)