SpringCloud-Feign接口轉(zhuǎn)換服務(wù)

Feign是通過提供面向接口操作的方式來替代RestTemplate API的Rest操作。

使用Feign

Feign這種技術(shù)應(yīng)用在服務(wù)消費端

  1. 修改pom.xml配置文件,加入Feign的依賴包
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
  1. 由于Fegin是將Rest的操作轉(zhuǎn)換成接口的形式,所以我們需要新建一個接口,并在接口上聲明@FeignClient注解
@FeignClient(value = "DEPT-PROVIDER",configuration = FeignClientConfig.class)
public interface DeptClientService {
    @RequestMapping(method= RequestMethod.GET,value="/dept/get/{id}")
    public Dept get(@PathVariable("id") long id) ;
    @RequestMapping(method=RequestMethod.GET,value="/dept/list")
    public List<Dept> list() ;
    @RequestMapping(method=RequestMethod.POST,value="/dept/add")
    public boolean add(Dept dept) ;
}

@Configuration
public class FeignClientConfig {
    @Bean
    public Logger.Level getFeignLoggerLevel() {
        return feign.Logger.Level.FULL ;
    }
    @Bean
    public BasicAuthRequestInterceptor getBasicAuthRequestInterceptor() {
        return new BasicAuthRequestInterceptor("admin", "admin");
    }
}

其中configuration = FeignClientConfig.class不是必須的,將configuration屬性去除仍然能work。

  1. 將之前的Rest操作的API,替換成面向DeptClientService接口的形式
@RestController
@RequestMapping("/consumer/dept")
public class ConsumerDeptController {

    @Autowired
    private DeptClientService deptClientService;

    @RequestMapping(value = "/get")
    public Dept get(long id) {
        return this.deptClientService.get(id);
    }

    @RequestMapping("/list")
    public List<Dept> list(){
        return this.deptClientService.list();
    }

    @RequestMapping("/add")
    public boolean add(Dept dept){
        return this.add(dept);
    }
/*
    public static final String DEPT_GET_URL = "http://DEPT-PROVIDER/dept/get/";
    public static final String DEPT_LIST_URL = "http://DEPT-PROVIDER/dept/list/";
    public static final String DEPT_ADD_URL = "http://DEPT-PROVIDER/dept/add";

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private HttpHeaders httpHeaders;
    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @RequestMapping(value = "/get")
    public Dept get(long id) {
        ServiceInstance serviceInstance = this.loadBalancerClient.choose("DEPT-PROVIDER") ;
        System.out.println(
                "【*** ServiceInstance ***】host = " + serviceInstance.getHost()
                        + "、port = " + serviceInstance.getPort()
                        + "、serviceId = " + serviceInstance.getServiceId());
        //Dept dept = restTemplate.getForObject(DEPT_GET_URL + id, Dept.class);
        Dept dept = restTemplate.exchange(DEPT_GET_URL+id, HttpMethod.GET,new HttpEntity<Object>(this.httpHeaders),Dept.class).getBody();
        return dept;
    }

    @RequestMapping("/list")
    public List<Dept> list(){
        //List<Dept> deptList = restTemplate.getForObject(DEPT_LIST_URL, List.class);
        List<Dept> deptList = this.restTemplate.exchange(DEPT_LIST_URL,HttpMethod.GET,new HttpEntity<Object>(this.httpHeaders),List.class).getBody();
        return deptList;
    }

    @RequestMapping("/add")
    public boolean add(Dept dept){
        //Boolean flag = restTemplate.postForObject(DEPT_ADD_URL, dept, Boolean.class);
        Boolean flag = this.restTemplate.exchange(DEPT_ADD_URL,HttpMethod.POST,new HttpEntity<Object>(this.httpHeaders),Boolean.class).getBody();
        return flag;
    }*/
}
  1. 在啟動類中加入@EnableFeignClients注解
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages = {"cn.zgc.service"})
public class FeignConsumer_80_StartSpringCloudApplication {
    public static void main(String[] args) {
        SpringApplication.run(FeignConsumer_80_StartSpringCloudApplication.class,args);
    }
}

Feign自帶了負責(zé)均衡特性,所以使用Feign之后可以不用使用Ribbon。

Feign的配置

Feign 最重要的功能就是將 Rest 服務(wù)的信息轉(zhuǎn)換為接口,但是在實際的使用之中也需要考慮到一些配置情況,例如:數(shù)據(jù)壓縮, Rest 的核心本質(zhì)在于: JSON 數(shù)據(jù)傳輸( XML、文本),于是就必須思考一種情況,萬一用戶發(fā)送的數(shù)據(jù)很大呢? 所以這個時候可以考慮修改application.yml 配置文件對傳輸數(shù)據(jù)進行壓縮;

feign:
 compression:
 request:
 mime-types:  # 可以被壓縮的類型
 - text/xml
 - application/xml
 - application/json
 min-request-size: 2048 # 超過2048的字節(jié)進行壓縮

開啟Feign的日志(默認是不開啟的)

logging:
 level:
 cn.zgc.service: DEBUG
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,591評論 19 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,275評論 6 342
  • Spring Web MVC Spring Web MVC 是包含在 Spring 框架中的 Web 框架,建立于...
    Hsinwong閱讀 22,943評論 1 92
  • 來也空空 去也空空 來人世走一遭 福 可享盡榮華富貴 苦 則受盡人間煉獄 到頭來 還不是撒手人寰 自古 成大業(yè)者 ...
    華麒Bearing閱讀 218評論 0 1

友情鏈接更多精彩內(nèi)容