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代碼:
        @RestController
        @RequestMapping("/user")
        public class UserController {

            @Autowired
            UserService userService;
            @PostMapping("/findOne")
            @ApiOperation(value="查詢單個用戶",notes="根據id查詢單個用戶信息")
            @ApiImplicitParam(name="userId",value="userId",required=true,paramType="query")
            public ObjectRestResponse<User> findOne(@RequestParam Integer userId){
                ObjectRestResponse<User> result = new ObjectRestResponse<User>();
                User user = userService.selectById(userId);
                result.data(user);
                return result;
            }

        }


總結: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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM