SpringCloud系列聲明式服務調用Netflix Feign

      網友投稿 823 2022-05-30

      在上一章的學習中,我們知道了微服務的基本概念,知道怎么基于Ribbon+restTemplate的方式實現服務調用,接著上篇博客,我們學習怎么基于Feign實現服務調用,請先學習上篇博客,然后再學習本篇博客

      Feign是一個聲明式的web service客戶端,它使得編寫web service客戶端更為容易。創建接口,為接口添加注解,即可使用Feign。Feign可以使用Feign注解或者JAX-RS注解,還支持熱插拔的編碼器和解碼器。

      環境準備:

      JDK 1.8

      SpringBoot2.2.3

      SpringCloud(Hoxton.SR6)

      Maven 3.2+

      開發工具

      IntelliJ IDEA

      smartGit

      創建一個SpringBoot Initialize項目,詳情可以參考我之前博客:SpringBoot系列之快速創建項目教程

      port: 8083 spring: application: name: feign-service-consumer eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ healthcheck: enabled: false instance: status-page-url-path: http://localhost:8761/actuator/info health-check-url-path: http://localhost:8761/actuator//health prefer-ip-address: true instance-id: feign-service-consumer8083

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      @FeignClient指定服務名稱,@RequestMapping指定要調用的接口

      package com.example.springcloud.client.service; import com.example.springcloud.client.bean.User; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; /** *

       * UserService * 
      * *
       * @author mazq * 修改記錄 * 修改后版本: 修改人: 修改日期: 2020/07/28 13:09 修改內容: * 
      */ @FeignClient(value = "EUREKA-SERVICE-PROVIDER") @Service public interface UserService { @RequestMapping(value = "/api/users/{username}",method = RequestMethod.GET) User findGithubUser(@PathVariable("username")String username); }

      1

      2

      3

      4

      5

      6

      SpringCloud系列之聲明式服務調用Netflix Feign

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      24

      25

      26

      27

      28

      加上@EnableEurekaClient,@EnableFeignClients,寫個接口進行測試

      package com.example.springcloud.client; import com.example.springcloud.client.bean.User; import com.example.springcloud.client.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.scheduling.annotation.Async; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @EnableEurekaClient @EnableFeignClients @RestController public class SpringcloudFeignClientApplication { @Autowired UserService userService; public static void main(String[] args) { SpringApplication.run(SpringcloudFeignClientApplication.class, args); } @GetMapping("/findUser/{username}") public User index(@PathVariable("username")String username){ return userService.findGithubUser(username); } }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      24

      25

      26

      27

      28

      29

      30

      31

      32

      33

      要運行eureka服務端,eureka服務提供者,代碼請參考上一章博客

      補充:IDEA中多實例運行方法

      step1:如圖,不要加上勾選

      step2:指定不同的server端口和實例id,如圖:

      服務注冊,可以看到兩個實例

      ok,啟動eureka server(服務注冊中心),eureka服務提供者端,和feign服務消費者端

      http://localhost:8083/findUser/mojombo

      附錄:

      ok,本博客參考官方教程進行實踐,僅僅作為入門的學習參考資料,詳情可以參考Spring Cloud官方文檔https://cloud.spring.io/spring-cloud-static/spring-cloud-openfeign/2.2.0.RELEASE/reference/html/

      代碼例子下載:code download

      優質學習資料參考:

      方志鵬大佬系列Spring Cloud博客:https://www.fangzhipeng.com/spring-cloud.html

      使用Spring Cloud與Docker實戰微服務:https://eacdy.gitbooks.io/spring-cloud-book/content/

      程序員DD大佬系列Spring Cloud博客:http://blog.didispace.com/spring-cloud-learning/

      Spring Spring Cloud 網絡

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      上一篇:dmhsq-vue3-vite-electron Version0.0.1
      下一篇:敏捷無敵之Gitlab CI架構流程詳解
      相關文章
      亚洲一区免费在线观看| 亚洲成aⅴ人片在线影院八| 亚洲熟妇少妇任你躁在线观看| 老司机亚洲精品影院无码| 亚洲成在人天堂一区二区| 亚洲高清专区日韩精品| 亚洲国产精品特色大片观看完整版| 亚洲人色婷婷成人网站在线观看 | 国产精品亚洲高清一区二区 | 亚洲精品成人久久| 亚洲色偷偷av男人的天堂| 在线观看亚洲人成网站| 亚洲精选在线观看| 久久亚洲AV成人无码| 亚洲福利一区二区| 亚洲一区二区三区深夜天堂| 亚洲国产精品久久网午夜| 亚洲国产日韩在线成人蜜芽| tom影院亚洲国产一区二区| 中文字幕在线日亚洲9| 亚洲欧美日韩中文二区| 国产亚洲精品91| 久久久青草青青国产亚洲免观 | 国产精品亚洲四区在线观看| 亚洲欧美综合精品成人导航| 久久精品国产亚洲av天美18| 亚洲高清国产拍精品青青草原| 亚洲中文字幕无码专区| 国产∨亚洲V天堂无码久久久 | 国产亚洲蜜芽精品久久| 亚洲国产综合久久天堂| 亚洲欧洲成人精品香蕉网| 亚洲AV成人无码久久精品老人| 久久国产亚洲高清观看| 日本亚洲精品色婷婷在线影院| 亚洲AV无码成人网站在线观看| 亚洲国产综合人成综合网站| 亚洲αv在线精品糸列| 亚洲欧洲精品一区二区三区| 亚洲另类无码一区二区三区| 亚洲免费视频一区二区三区|