久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網
      最全最豐富的資訊網站

      SpringCloud?Feign超詳細講解

      Feign是Netflix公司開發(fā)的一個聲明式的REST調用客戶端; Ribbon負載均衡、 Hystrⅸ服務熔斷是我們Spring Cloud中進行微服務開發(fā)非?;A的組件,下面一起來看一下,希望對大家有幫助。

      SpringCloud?Feign超詳細講解

      程序員必備接口測試調試工具:立即使用
      Apipost = Postman + Swagger + Mock + Jmeter
      Api設計、調試、文檔、自動化測試工具
      后端、前端、測試,同時在線協(xié)作,內容實時同步

      推薦學習:《java視頻教程》

      一、什么是Feign

      Feign是聲明式Web Service客戶端,它讓微服務之間的調用變得更簡單,類似controller調用service。SpringCloud集成了Ribbon和Eureka,可以使用Feigin提供負載均衡的http客戶端。Feign是通過接口和注釋來實現負載均衡的。

      二、Feign能干什么

      (摘抄自狂神說JAVA)

      Feign能干什么?

      Feign旨在使編寫Java Http客戶端變得更容易

      前面在使用Ribbon + RestTemplate時,利用RestTemplate對Http請求的封裝處理,形成了一套模板化的調用方法。但是在實際開發(fā)中,由于對服務依賴的調用可能不止一處,往往一個接口會被多處調用,所以通常都會針對每個微服務自行封裝一個客戶端類來包裝這些依賴服務的調用。所以,Feign在此基礎上做了進一步的封裝,由他來幫助我們定義和實現依賴服務接口的定義,在Feign的實現下,我們只需要創(chuàng)建一個接口并使用注解的方式來配置它 (類似以前Dao接口上標注Mapper注解,現在是一個微服務接口上面標注一個Feign注解),即可完成對服務提供方的接口綁定,簡化了使用Spring Cloud Ribbon 時,自動封裝服務調用客戶端的開發(fā)量。

      Feign默認集成了Ribbon

      利用Ribbon維護了MicroServiceCloud-Dept的服務列表信息,并且通過輪詢實現了客戶端的負載均衡,而與Ribbon不同的是,通過Feign只需要定義服務綁定接口且以聲明式的方法,優(yōu)雅而簡單的實現了服務調用。

      三、Feign的使用步驟

      1、新建一個module

      SpringCloud?Feign超詳細講解

      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>
      登錄后復制

      3、配置applicatin.yaml

      server:
      port: 801

      eureka:
      client:
      register-with-eureka: false #不向eureka注冊自己
      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     /*配置負載均衡實現RestTemplate*/     /*IRule*/     /*RoundRobinRule 輪詢 */     /*RandomRule 隨機*/     /*AvailabilityFilteringRule 優(yōu)先過濾掉跳閘、訪問故障的服務,對剩下的進行輪詢 */     public RestTemplate getRestTemplate() {         return new RestTemplate();     } }
      登錄后復制

      登錄后復制

      5、配置Controller類

      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     /*配置負載均衡實現RestTemplate*/     /*IRule*/     /*RoundRobinRule 輪詢 */     /*RandomRule 隨機*/     /*AvailabilityFilteringRule 優(yōu)先過濾掉跳閘、訪問故障的服務,對剩下的進行輪詢 */     public RestTemplate getRestTemplate() {         return new RestTemplate();     } }
      登錄后復制

      登錄后復制

      6、配置啟動類

      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);     } }
      登錄后復制

      7、改動API

      1)引入Feign依賴

      SpringCloud?Feign超詳細講解

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

      2)配置Service

      SpringCloud?Feign超詳細講解

      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); }
      登錄后復制

      3)注意

      SpringCloud?Feign超詳細講解

      服務名字要寫對GetMapper中的內容要和提供者一致,否則報錯(找了一下午)

      下面是提供者的內容

      SpringCloud?Feign超詳細講解

      四、結果

      這樣即可獲取到數據,而且負載平衡的默認算法,仍然是輪詢!

      SpringCloud?Feign超詳細講解

      推薦學習:《java視頻教程》

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