最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring Cloud Feign實例講解學(xué)習(xí)

 更新時間:2018年02月26日 10:09:39   作者:牛麥康納  
這篇文章主要介紹了Spring Cloud Feign實例講解學(xué)習(xí),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

前面博文搭建了一個Eureka+Ribbon+Hystrix的框架,雖然可以基本滿足服務(wù)之間的調(diào)用,但是代碼看起來實在丑陋,每次客戶端都要寫一個restTemplate,為了讓調(diào)用更美觀,可讀性更強(qiáng),現(xiàn)在我們開始學(xué)習(xí)使用Feign。

Feign包含了Ribbon和Hystrix,這個在實戰(zhàn)中才慢慢體會到它的意義,所謂的包含并不是Feign的jar包包含有Ribbon和Hystrix的jar包這種物理上的包含,而是Feign的功能包含了其他兩者的功能這種邏輯上的包含。簡言之:Feign能干Ribbon和Hystrix的事情,但是要用Ribbon和Hystrix自帶的注解必須要引入相應(yīng)的jar包才可以。

案例一:

Eureka注冊中心:https://github.com/yejingtao/forblog/tree/master/demo-eureka-register

服務(wù)提供方:https://github.com/yejingtao/forblog/tree/master/demo-feign-freeservice

服務(wù)調(diào)用方:https://github.com/yejingtao/forblog/tree/master/demo-feign-freeconsumer

服務(wù)提供方就是個簡單的EurekaClient端+web應(yīng)用,提供以下方法

@RestController 
@RequestMapping("/feign-service") 
public class HelloServiceContorller { 
  private Logger logger = LoggerFactory.getLogger(this.getClass());    
  private void sleep(String methodName) { 
    int sleepMinTime = new Random().nextInt(3000); 
    logger.info("helloService "+methodName+" sleepMinTime: "+sleepMinTime); 
    try { 
      Thread.sleep(sleepMinTime); 
    } catch (InterruptedException e) { 
      e.printStackTrace(); 
    } 
  } 
   
  @RequestMapping(value="/serviceGet",method=RequestMethod.GET) 
  public String helloService(@RequestParam String name) { 
    sleep("get"); 
    return "HelloServiceImpl name :"+name; 
  } 
   
  @RequestMapping(value="/serviceHead", method=RequestMethod.HEAD) 
  public String helloService(@RequestHeader String name, 
      @RequestHeader String password) { 
    sleep("header"); 
    return "helloServiceHead name :"+name +" password:"+password; 
  } 
   
  @RequestMapping(value="/servicePost", method=RequestMethod.POST) 
  public String helloService(@RequestBody UserDemo userDemo) { 
    sleep("post"); 
    return userDemo.toString(); 
  } 
} 

需要注意的以下注解不可以省略。

@RequestParam:Annotation which indicates that amethod parameter should be bound to a web request parameter

@RequestBody:Annotation indicating a methodparameter should be bound to the body of the web request.

@RequestHeader:Annotation which indicates that amethod parameter should be bound to a web request header.

如果缺少了以上注解,服務(wù)運(yùn)行起來以后雖然不會報錯,但是獲取不到入?yún)ⅰ?/p>

服務(wù)調(diào)用方項目:

<dependency> 
      <groupId>org.springframework.cloud</groupId> 
      <artifactId>spring-cloud-starter-feign</artifactId> 
    </dependency> 

這里只依賴了Feign,沒有依賴Ribbon和Hystrix。

application.yml:

server: 
 port: 9051 
 
spring: 
 application: 
  name: demo-feign-freeconsumer 
   
eureka: 
 client: 
  serviceUrl: 
   defaultZone: http://peer1:1111/eureka/,http://peer2:1112/eureka/ 
feign: 
 hystrix: 
  enabled: true 
 
#Ribbon 超時時間設(shè)置 
#ribbon: 
# ConnectTimeout: 500 
# ReadTimeout: 3000

hystrix這個配置坑了我好久我用的Spring Cloud是Dalston版本SR1,比網(wǎng)上其他材料的版本要新,因為在新版本中Feign對Hystrix的支持默認(rèn)是關(guān)閉的,所以要通過配置手動打開feign.hystrix.enabled=true,這樣服務(wù)降級等功能才有效果。

Application啟動程序

@SpringBootApplication 
@EnableEurekaClient 
@EnableFeignClients 
public class DemoFeignApplication {    
  public static void main(String[] args) { 
    SpringApplication.run(DemoFeignApplication.class, args); 
  } 
}

注意這里還有個坑,我這里用的是@SpringBootApplication+@EnableEurekaClient,而不是用的@SpringCloudApplication,因為后者包含了@EnableCircuitBreaker,而@EnableCircuitBreaker又是屬于Hystrix包里的內(nèi)容,我的pom里并沒有引入Hystrix。所以這一點(diǎn)Spring Cloud做的還是有不足的地方,直接用@SpringCloudApplication編譯不會報錯,但是啟動不了。當(dāng)然這里的主角還是@EnableFeignClients這個注解。

核心客戶端代碼

@FeignClient(name="demo-feign-freeservice",fallback=DemoFeignFallback.class) 
public interface DemoFeignService{ 
    
  @RequestMapping(value="/feign-service/serviceGet",method=RequestMethod.GET) 
  String helloService(@RequestParam("name") String name); 
   
  @RequestMapping(value="/feign-service/serviceHead", method=RequestMethod.HEAD) 
  String helloService(@RequestHeader("name") String name, 
      @RequestHeader("password") String password); 
   
  @RequestMapping(value="/feign-service/servicePost", method=RequestMethod.POST) 
  String helloService(@RequestBody UserDemo userDemo);  
} 

@FeignClient注解定義了該接口是一個Feign客戶端,name指定了注冊到Eureka上的服務(wù)名,fallback是服務(wù)降級后的接口實現(xiàn)類。

@RequestMapping里指定了請求的相對url和http請求方式,與服務(wù)端一一對應(yīng)。入?yún)⒗锏腀RequestParam、

@RequestBody、@RequestHeader注解比起服務(wù)端多了value屬性,這里不能省略,需要顯式的告知Feign客戶端參數(shù)要如何對應(yīng)。

降級服務(wù)代碼:

@Component 
public class DemoFeignFallback implements DemoFeignService{ 
  @Override 
  public String helloService(String name) { 
    return "get error"; 
  } 
 
  @Override 
  public String helloService(String name,String password) { 
    return "head error"; 
  } 
   
  @Override 
  public String helloService(UserDemo userDemo) { 
    return "post error"; 
  } 
} 

發(fā)現(xiàn)這里的入?yún)⒗镂夜室馊サ袅薂RequestParam、@RequestBody、@RequestHeader注解,因為這幾個注解本質(zhì)上的意義就在于Feign在做微服務(wù)調(diào)用的時候?qū)ttp傳遞參數(shù)用的,但服務(wù)降級根本不會做http請求了,所以此處可以省略。

Controller代碼:

@RestController 
public class DemoFeignController {    
  @Autowired 
  private DemoFeignService demoFeignService;    
  @RequestMapping(value="/test", method=RequestMethod.GET) 
  public String demoServiceTest() { 
    StringBuffer sb = new StringBuffer(); 
    sb.append(demoFeignService.helloService("yuanyuan")); 
    sb.append("\n"); 
    sb.append(demoFeignService.helloService("yjt","xixihaha")); 
    sb.append("\n"); 
    sb.append(demoFeignService.helloService(new UserDemo("yejingtao","123456"))); 
    return sb.toString();      
  } 
} 

我們來看效果:

我們服務(wù)都沒超時,3個方法全部正常,但是head請求沒有拿到返回值,這個是因為head方式http請求的特性決定的,head不返回response的body體,一般用來做連通性測試來用。

再看一組:

運(yùn)氣不好head和post請求方法處理時間超過了2000ms,服務(wù)降級,實現(xiàn)被fallback處理類取代。

在案例一中我們總有種感覺,服務(wù)提供方和服務(wù)調(diào)用方存在重復(fù)的代碼,是否可以進(jìn)行優(yōu)化?請看案例二。

案例二:

Eureka注冊中心:https://github.com/yejingtao/forblog/tree/master/demo-eureka-register

接口API:https://github.com/yejingtao/forblog/tree/master/demo-feign-serviceapi

服務(wù)提供方:https://github.com/yejingtao/forblog/tree/master/demo-feign-serviceimpl

服務(wù)調(diào)用方:https://github.com/yejingtao/forblog/tree/master/demo-feign-apiconsumer

案例二最大的變動是將服務(wù)能力單獨(dú)寫到一個API的project中,調(diào)用方和提供方pom都依賴這個API。

API:

public interface HelloService {    
  @RequestMapping(value="/feign-service/serviceGet",method=RequestMethod.GET) 
  String helloService(@RequestParam("name") String name); 
   
  @RequestMapping(value="/feign-service/serviceHead", method=RequestMethod.HEAD) 
  String helloService(@RequestHeader("name") String name, 
      @RequestHeader("password") String password); 
   
  @RequestMapping(value="/feign-service/servicePost", method=RequestMethod.POST) 
  String helloService(@RequestBody UserDemo userDemo);    
} 

服務(wù)提供方:

@RestController 
public class HelloServiceContorller implements HelloService{    
  private Logger logger = LoggerFactory.getLogger(this.getClass());    
  private void sleep(String methodName) { 
    int sleepMinTime = new Random().nextInt(3000); 
    logger.info("helloService "+methodName+" sleepMinTime: "+sleepMinTime); 
    try { 
      Thread.sleep(sleepMinTime); 
    } catch (InterruptedException e) { 
      e.printStackTrace(); 
    } 
  } 
   
  @Override 
  public String helloService(@RequestParam("name") String name) { 
    sleep("get"); 
    return "HelloServiceImpl name :"+name; 
  } 
   
  @Override 
  public String helloService(@RequestHeader("name") String name, 
      @RequestHeader("password") String password) { 
    sleep("header"); 
    return "helloServiceHead name :"+name +" password:"+password; 
  } 
   
  @Override 
  public String helloService(@RequestBody UserDemo userDemo) { 
    sleep("post"); 
    return userDemo.toString(); 
  }       
} 

服務(wù)調(diào)用方:

@FeignClient(name="demo-feign-serviceimpl", fallback=FeignServiceFallback.class) 
public interface FeignService extends HelloService{  
} 

其它代碼基本不變,效果也一樣。

兩種風(fēng)格各有優(yōu)缺點(diǎn):freestyle的更自由,服務(wù)端新增方法不會影響客戶端代碼,缺點(diǎn)是服務(wù)能力不同步服務(wù)能力的變動會引起異常;API格式服務(wù)端客戶端服務(wù)能力同步,但是接口的變動需要修改兩邊的代碼,需要構(gòu)建的時候就要考慮清楚。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java ArrayList.add 的實現(xiàn)方法

    Java ArrayList.add 的實現(xiàn)方法

    這篇文章主要介紹了Java ArrayList.add 的實現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11
  • Java IO流 文件的編碼實例代碼

    Java IO流 文件的編碼實例代碼

    本文通過實例代碼給大家介紹了java io流文件編碼的方法,非常不錯,具有參考借鑒價值,需要的朋友參考下吧
    2017-05-05
  • 解決Spring Boot 在localhost域奇怪的404問題(Mac book pro)

    解決Spring Boot 在localhost域奇怪的404問題(Mac book pro)

    這篇文章主要介紹了解決Spring Boot 在localhost域奇怪的404問題(Mac book pro),需要的朋友可以參考下
    2017-09-09
  • JUC之CountdownLatch使用詳解

    JUC之CountdownLatch使用詳解

    這篇文章主要介紹了JUC之CountdownLatch使用詳解,CountdownLatch 用來進(jìn)行線程同步協(xié)作,等待所有線程完成倒計時,
    其中構(gòu)造參數(shù)用來初始化等待計數(shù)值,await() 用來等待計數(shù)歸零,countDown() 用來讓計數(shù)減一,需要的朋友可以參考下
    2023-12-12
  • IDEA JavaWeb項目啟動運(yùn)行后出現(xiàn)404錯誤的解決方法

    IDEA JavaWeb項目啟動運(yùn)行后出現(xiàn)404錯誤的解決方法

    這篇文章主要介紹了IDEA JavaWeb項目啟動運(yùn)行后出現(xiàn)404錯誤的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • Hibernate對數(shù)據(jù)庫刪除、查找、更新操作實例代碼

    Hibernate對數(shù)據(jù)庫刪除、查找、更新操作實例代碼

    本篇文章主要介紹了Hibernate對數(shù)據(jù)庫刪除、查找、更新操作實例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • SpringBoot + 微信公眾號JSAPI支付功能的實現(xiàn)

    SpringBoot + 微信公眾號JSAPI支付功能的實現(xiàn)

    這篇文章主要介紹了SpringBoot + 微信公眾號JSAPI支付功能的實現(xiàn),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-03-03
  • 在Mybatis @Select注解中實現(xiàn)拼寫動態(tài)sql

    在Mybatis @Select注解中實現(xiàn)拼寫動態(tài)sql

    這篇文章主要介紹了在Mybatis @Select注解中實現(xiàn)拼寫動態(tài)sql,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • Java集合List的使用詳細(xì)解析

    Java集合List的使用詳細(xì)解析

    這篇文章主要介紹了Java集合List的使用詳細(xì)解析,List集合類中元素有序、且可重復(fù),集合中的每個元素都有其對應(yīng)的順序索引,鑒于Java中數(shù)組用來存儲數(shù)據(jù)的局限性,我們通常使用java.util.List替代數(shù)組,需要的朋友可以參考下
    2023-11-11
  • 一文詳解Mybatis-plus的介紹與使用

    一文詳解Mybatis-plus的介紹與使用

    Mybatis-Plus?是?MyBatis?的一個增強(qiáng)工具,專門針對于傳統(tǒng)MyBatis開發(fā)中sql需要手動進(jìn)行映射配置繁瑣缺點(diǎn)的一款框架技術(shù)。本文將為大家詳細(xì)講講Mybatis-plus的介紹與使用,感興趣的可以了解一下
    2022-07-07

最新評論

寿光市| 新邵县| 辰溪县| 临沧市| 长寿区| 太湖县| 泰宁县| 麻栗坡县| 吉安市| 临泽县| 吉木乃县| 浠水县| 宁强县| 洛浦县| 微山县| 平果县| 博湖县| 芷江| 文安县| 黄平县| 土默特左旗| 特克斯县| 万年县| 蓬溪县| 鄯善县| 隆化县| 大方县| 新河县| 酒泉市| 车险| 平湖市| 邵东县| 遵义县| 攀枝花市| 册亨县| 盐津县| 牡丹江市| 天峻县| 务川| 聊城市| 攀枝花市|