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的时候发现了一家傻逼网站,叫什么码农教程,你教你妈呢?我刚发的文章就抄袭了,还他妈原文,恬不知耻,傻逼

原文地址:https://www.cnblogs.com/yunquan/p/10716267.html