<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                [TOC] ## **1.Feign的作用** 1. Feign是Netflix公司開源的輕量級Rest(https://github.com/OpenFeign/feign ),使用 Feign 可以非常方便、簡單的實現 Http 客戶端,使用 Feign 只需要定義一個接口,然后在接口上添加注解即可。 2. Spring Cloud 對 Feign 進行了封裝,***Feign 默認集成了 Ribbon 實現了客戶端負載均衡調用。*** * **微服務間的調用就有兩種方式:** 1\. 通過微服務名稱,獲得服務的調用地址 2\. 通過接口+注解,獲得服務的調用 ——Feign (為適應業界其它程序員提出的,還是遵循面向接口編程) 類似于以前Mapper接口上使用@Mapper注解進行標識,而使用Feign就只要在接口上標注@FeignClient注解。 ## **2. Feign使用** Feign是發起調用,所以依然在服務消費端配置 **1. 在消費端pom引入Feign啟動器依賴** ``` <!--feign 依賴--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> ``` **2. 新建 ProductClientService 接口** 新建 ProductClientService 接口,**使用 @FeignClient("服務名稱")** 注解標識,來指定調用哪個服務 比如:在代碼中調用了 MICROSERVICE-PRODUCT 服務的 /product/list 、/product/get/{id}、/product/add 接口,代碼如下: ~~~ @FeignClient(value = "microservice-product") //指定調用的微服務名稱 public interface ProductClientService { @RequestMapping(value = "/product/add", method = RequestMethod.POST) boolean add(@RequestBody Product product); @RequestMapping(value = "/product/get/{id}", method = RequestMethod.GET) Product get(@PathVariable("id") Long id); @RequestMapping(value = "/product/list", method = RequestMethod.GET) List<Product> list(); } ~~~ **3. controller直接利用service調用服務提供端即可** ![](https://img.kancloud.cn/16/47/16473e8c56a97ed1f17f38a2d7168dfc_968x231.png) **4. 修改啟動類** 添加注解 : @EnableFeignClients ~~~ @SpringBootApplication @EnableFeignClients(basePackages = "com.tuna.springcloud.consumer.service") @EnableEurekaClient //標識 是一個Eureka客戶端,測試沒有這個注解也可以 public class ProductConsumer_80 { public static void main(String[] args) { SpringApplication.run(ProductConsumer_80.class, args); } } ~~~ 測試 [http://localhost/consumer/product/get/1](http://localhost/consumer/product/get/1) 可以正常負載 ![](https://img.kancloud.cn/f9/77/f97716578def47ff1ea8c353db4a256f_528x141.png)![](https://img.kancloud.cn/ff/d5/ffd573fd09a929095909d80f9b150112_499x119.png) ## **3. Feign原理** 1. 啟動類添加@EnableFeignClients注解,Spring會掃描標記了@FeignClient注解的接口,并生成此接口的代理 對象 2. @FeignClient("服務名稱 ") 即指定了 product 服務名稱,Feign會從Eureka注冊中心獲取 product 服務列表, 并通過**負載均衡算法進行服務調用.** 3. 在接口方法中使用注解 @RequestMapping(value = "/product/list",method = RequestMethod.GET),指定調 用的url,Feign 會根據url進行遠程調用 ## **4. Feign注意事項** SpringCloud對Feign進行了增強兼容了SpringMVC的注解 ,我們在使用SpringMVC的注解時需要注意: 1. @FeignClient接口方法有基本類型參數在參數必須加@PathVariable("XXX") 或 @RequestParam("XXX") 2. @FeignClient接口方法返回值為復雜對象時,此類型必須有無參構造方法。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看