SpringCloud筆記五:Feign

什麼是Feign?

Feign的做用也是負載均衡,不過Feign是隻須要建立一個接口,而後加上註解的聲明式Web服務客戶端web

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

有了Ribbon我還要Feign幹嗎?

上一篇文章說了,Ribbon很強大,甚至能夠自定義負載均衡的算法。那爲何還會有Feign這個負載均衡的東西呢?spring

緣由是:Ribbon對微服務的調用是這樣的編程

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

    @Autowired
    private RestTemplate restTemplate;

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

新建consumer-feign

咱們原有的consumer80項目,使用的是Ribbon+RestTemplate的模式,如今咱們新建一個consumer項目,起名爲consumer-feign-80,新建完成以後,Maven的pom文件裏,把consumer-80複製過來以外還須要添加Feign的引用app

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

yml文件也複製過來就能夠負載均衡

新建一個Controller,爲Feign而生處理請求,以下maven

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這個接口,這個等下介紹,接口式編程嘛ide

主方法咱們要添加兩個和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的時候發現了一家傻逼網站,叫什麼碼農教程,你教你媽呢?我剛發的文章就抄襲了,還他媽原文,恬不知恥,傻逼

相關文章
相關標籤/搜索