天天看點

Feign的簡單使用

Feign是Springcloud元件中的一個輕量級Restful的HTTP服務用戶端,底層Httpclient工具,Feign内置了Ribbon, feignClient已經預設使用了ribbon自帶負載均衡,去調用服務注冊中心的服務。

Feign的使用方式是:使用Feign的注解定義接口,調用這個接口,就可以調用服務注冊中心的服務

依賴

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

啟動類

@SpringBootApplication
@EnableFeignClients
public class SpringcloudConsumer8002Application {

    public static void main(String[] args) {
        SpringApplication.run(SpringcloudConsumer8002Application.class, args);
    }
}
           

接口

@Repository
@FeignClient(value = "goodstype-provider") //goodstype-provider為服務名
public interface FeignInterface {
    @GetMapping("/provider/goodstype/get/{id}")
    Goodstype findById(@PathVariable("id")int id);
}
           

controller

@Autowired
    private FeignInterface feignInterface;

    @GetMapping("/test/{id}")
    public Goodstype addCart(@PathVariable("id") int id){
        return feignInterface.findById(id);
    }
           
feign:
    compression:
      request:
        enabled: true # 開啟請求壓縮
        mime-types: text/html,application/xml,application/json # 設定壓縮的資料類型
        min-request-size: 2048 # 設定觸發壓縮的大小下限
      response:
        enabled: true # 開啟響應壓縮
           

歐克

Feign的簡單使用