天天看點

Spring cloud Feign 入門demo

spring Feign (服務與服務調用工具)

    添加Feign依賴:

            <dependency>  

                <groupId>org.springframework.cloud</groupId>  

                <artifactId>spring-cloud-starter-feign</artifactId>  

            </dependency>

    application.properties中添加配置,逾時時間設定和服務提供方的位址和端口号

    #Feign

    ribbon.ConnectTimeout=3000

    ribbon.ReadTimeout=60000

    admin.ribbon.listOfServers=192.16.150.130:8082

    啟動類上要加@EnableFeignClients注解

    注意:如果使用的spring cloud版本比較新,在新版本中Feign對Hystrix的支援是預設關閉的,是以需要通過配置手動打開

          feign.hystrix.enabled=true,這樣服務降級等功能才有效果。

    啟動程式:

        @SpringBootApplication

        @EnableFeignClients

        public class MovCenterBootstrap {

            public static void main(String[] args) {

                SpringApplication.run(MovCenterBootstrap.class, args);

            }

        }

    @EnableFeignClients注解的作用:通過@EnableFeignCleints注解開啟FeignCleint(在啟動項目的時候會檢查啟動類上有沒有@EnableFeignClients注解,如果有該注解,則開啟包掃描,掃描被@FeignClient注解的接口并将這些資訊注入到ioc容器中)

    服務調用方Client代碼:

        @Component

        @FeignClient(url = "http://localhost:8010", name = "user")

        public interface UserClient {

            @RequestMapping(method = RequestMethod.POST,value="/user/findOne",consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)

            Object findOne(@RequestParam("userId") Integer userId);

        @FeignClient注解定義了該接口是一個Feign用戶端,name指定了注冊到Eureka上的服務名。

        @RequestMapping裡指定了請求的相對url和http請求方式,與服務端一一對應。入參裡的@RequestParam、@RequestBody、@RequestHeader注解比起服務端多了value屬性,這裡不能省略,需要顯式的告知Feign用戶端參數要如何對應

    服務調用方Controller代碼:

        @RequestMapping("user")

        @RestController

        public class UserController {

            @Autowired

            private UserClient userClient;

            @PostMapping("findOne")

            @ApiOperation(value="查找單個使用者", notes="根據id查詢單個使用者")

            @ApiImplicitParam(name="userId",value="userId",required=true,paramType="query")

            public Object findOne(@RequestParam("userId") Integer userId){

                Object result = userClient.findOne(userId);

                return result;

    spring cloud Feign 逾時問題:預設的請求時間為1秒,超過這個時間便逾時異常,在配置檔案中添加如下配置可以解決逾時問題,即把逾時的時間設長

    application.yml服務調用方配置:hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 60000

    服務提供方Controller代碼:

        @RequestMapping("/user")

            UserService userService;

            @PostMapping("/findOne")

            @ApiOperation(value="查詢單個使用者",notes="根據id查詢單個使用者資訊")

            public ObjectRestResponse<User> findOne(@RequestParam Integer userId){

                ObjectRestResponse<User> result = new ObjectRestResponse<User>();

                User user = userService.selectById(userId);

                result.data(user);

總結:Feign的源碼實作的過程如下:

  • 首先通過@EnableFeignCleints注解開啟FeignCleint
  • 根據Feign的規則實作接口,并加@FeignCleint注解
  • 程式啟動後,會進行包掃描,掃描所有的@ FeignCleint的注解的類,并将這些資訊注入到ioc容器中。
  • 當接口的方法被調用,通過jdk的代理,來生成具體的RequesTemplate
  • RequesTemplate在生成Request
  • Request交給Client去處理,其中Client可以是HttpUrlConnection、HttpClient也可以是Okhttp
  • 最後Client被封裝到LoadBalanceClient類,這個類結合類Ribbon做到了負載均衡。

原文位址:https://blog.csdn.net/weixin_39702323/article/details/80505908

繼續閱讀