SpringCloud筆記五:Feign


什么是Feign?

Feign的作用也是負載均衡,不過Feign是只需要創建一個接口,然后加上注解的聲明式Web服務客戶端

而且,Feign集成了Ribbon,默認的負載均衡方式也是輪詢。

有了Ribbon我還要Feign干嘛?

上一篇文章說了,Ribbon很強大,甚至可以自定義負載均衡的算法。那為什么還會有Feign這個負載均衡的東西呢?

原因是:Ribbon對微服務的調用是這樣的

 private static final String REST_URL_PREFIX="http://PROVIDER-DEPT";

    @Autowired
    private RestTemplate restTemplate;

Ribbon通過微服務的服務名和RestTemplate來調用,但是實際開發中會用到接口式編程,例如WebService接口,這個時候Ribbon沒辦法提供接口式的訪問,而Feign可以。所以什么是Feign?接口加注解的Web服務端調用的負載均衡技術。

新建consumer-feign

我們原有的consumer80項目,使用的是Ribbon+RestTemplate的模式,現在我們新建一個consumer項目,起名為consumer-feign-80,新建完成之后,Maven的pom文件里,把consumer-80復制過來之外還需要添加Feign的引用

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>

yml文件也復制過來就可以

新建一個Controller,為Feign而生處理請求,如下

package com.vae.springcloud.controller;

import com.vae.springcloud.entity.DeptEntity;
import com.vae.springcloud.service.DeptClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

public class DeptControllerFeign {

    @Autowired
    private DeptClientService service;

    @PostMapping(value = "/dept/add")
    public boolean add(@RequestBody DeptEntity deptEntity){
        return service.add(deptEntity);
    }

    @GetMapping(value = "/dept/get/{id}")
    public DeptEntity get(@PathVariable("id") Integer id){
        return service.get(id);
    }
    @GetMapping(value = "/dept/list")
    public List<DeptEntity> get() throws  Exception{
        return service.get();
    }
    @GetMapping(value = "/dept/delete/{id}")
    public boolean delete(@PathVariable("id") Integer id){
        return service.delete(id);
    }
    @GetMapping(value = "/dept/update")
    public void update(@RequestBody DeptEntity deptEntity){
        service.update(deptEntity);
    }


}

可以看到,用到了DeptClientService這個接口,這個等下介紹,接口式編程嘛

主方法我們要添加兩個和Feign有關的注解

package com.vae.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages = {"com.vae.springcloud"})
@ComponentScan("com.vae.springcloud")
public class ConsumerFeign80Application {

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

}

@EnableFeignClients(basePackages = {"com.vae.springcloud"})
@ComponentScan("com.vae.springcloud")

就是這兩個注解

修改api項目

我們的Feign是接口式加注解的負載均衡,現在上面我們新建的子項目consumer-feign-80都加了注解了,現在開始寫接口了,在我們的api項目里面寫

引入Maven文件

首先,要在api項目里引入feign的引用

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>

新建feign的接口

package com.vae.springcloud.service;

import com.vae.springcloud.entity.DeptEntity;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import java.util.List;

@FeignClient(value = "provider-dept")
public interface DeptClientService {

    @PostMapping(value = "/dept/add")
    public boolean add(@RequestBody DeptEntity deptEntity);

    @GetMapping(value = "/dept/get/{id}")
    public DeptEntity get(@PathVariable("id") Integer id);

    @GetMapping(value = "/dept/list")
    public List<DeptEntity> get();

    @GetMapping(value = "/dept/delete/{id}")
    public boolean delete(@PathVariable("id") Integer id);

    @GetMapping(value = "/dept/update")
    public void update(@RequestBody DeptEntity deptEntity);

}

啟動項目

啟動eureka集群,再啟動provider集群,再啟動consumer-feign-80客戶端,你可以發現consumer是可以訪問的,默認的還是輪詢的方式。

但是我的項目報了一個錯,如下:

報錯

我加入了Feign的Maven引用之后就是報錯,我暫時無法解決,導致我無法啟動provider項目

報錯如下:

2019-04-16 12:37:18.492 ERROR 12260 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [com.vae.springcloud.api.ApiApplication]; nested exception is java.io.FileNotFoundException: class path resource [org/springframework/boot/autoconfigure/web/ServerPropertiesAutoConfiguration.class] cannot be opened because it does not exist

Caused by: java.io.FileNotFoundException: class path resource [org/springframework/boot/autoconfigure/web/ServerPropertiesAutoConfiguration.class] cannot be opened because it does not exist


我懷疑是我的Maven配置,或者其他某個地方配置的問題。反正我是無法解決,網上目前查不出來原因。

Feign啊,暫且擱置吧

如果有誰會Feign的配置這一塊,希望不吝賜教教教我......

發泄發泄心情,一個段落

我真的是無語了,下面學Hystrix的時候,又遇到了這個問題,網上根本搜不出來,全中國只有我遇到這個問題???
暫時不寫SpringCloud了,不學了,心累,網上根本搜不到,身邊的朋友除了我幾乎沒有學Java的,學的幾個只會SSM........

還有,我Google的時候發現了一家傻逼網站,叫什么碼農教程,你教你媽呢?我剛發的文章就抄襲了,還他媽原文,恬不知恥,傻逼


免責聲明!

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



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