一、Fegin的概述
Feign是一个声明式的web服务客户端,使得编写web服务客户端变得非常容易,只需要创建一个接口,然后在上面添加注解即可。
二、Feign与Ribbon的区别
1.Ribbon:可以使用微服务名字调用,并且可以自定义算法。
2.Feign:可以通过接口+注解的方式调用微服务,简化了Ribbon使自动封装服务调用客户端的开发量,
并且Feign自带负载均衡配置项
。
在使用Ribbon+RestTemplate时,利用RestTemplate对Http请求的封装处理,形成了一套模板化的调用方法。但在实际开发中,由于对服务依赖的调用可能不止一处,往往一个接口会被多出调用,所以通常都会针对每个微服务自行封装一些客户端类来包装这些依赖服务的调用。所以,Feign
在此基础上做了进一步封装,由他来帮我们定义和实现依赖服务接口的定义。在Feign
的实现下,我们只需要创建一个接口并使用注解的方式来配置它,即可完成对服务提供方的接口绑定,简化了使用springcloud Ribbon时,自动封装服务调用客户端的开发量。
注:Feign****继承了Ribbon,利用Ribbon维护了微服务的服务列表信息,并且通过轮询实现了客户端的负载均衡。而与Ribbon不同的是,通过Fegin只需要定义服务绑定接口且以声明式的方法,优雅而简单的实现了服务调用。
三、关于Fegin需要引入的依赖
1
2
3
4
5
6
7
8 1<!-- feign相关 -->
2<dependencies>
3 <dependency>
4 <groupId>org.springframework.cloud</groupId>
5 <artifactId>spring-cloud-starter-feign</artifactId>
6 </dependency>
7</dependencies>
8
四、在接口上添加注释@FeignClient(value="……")
value的值为微服务的名字
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 1package com.atguigu.springcloud.service;
2
3import com.atguigu.springcloud.entities.Dept;
4import org.springframework.cloud.netflix.feign.FeignClient;
5import org.springframework.web.bind.annotation.PathVariable;
6import org.springframework.web.bind.annotation.RequestBody;
7import org.springframework.web.bind.annotation.RequestMapping;
8import org.springframework.web.bind.annotation.RequestMethod;
9
10import java.util.List;
11
12/**
13 * @Author:
14 * @Despriction:
15 * @Package: com.atguigu.springcloud.service
16 * @Date:Created in 2019/4/29 16:41
17 * @Modify By:
18 */
19@FeignClient(value = "microservicecloud-dept") //microservicecloud-dept为某个微服务的名字
20public interface DeptClientService {
21
22 @RequestMapping(value = "/dept/add",method = RequestMethod.POST)
23 public Boolean add(@RequestBody Dept dept);
24
25 @RequestMapping(value = "/dept/get/{id}",method = RequestMethod.GET)
26 public Dept get(@PathVariable("id") Long id);
27
28 @RequestMapping(value = "/dept/list",method = RequestMethod.GET)
29 public List<Dept> list();
30
31}
32
33
五、在Controller中注入上述接口,然后即可调用该接口的方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 1package com.atguigu.springcloud.controller;
2
3import com.atguigu.springcloud.entities.Dept;
4import com.atguigu.springcloud.service.DeptClientService;
5import org.springframework.beans.factory.annotation.Autowired;
6import org.springframework.cloud.netflix.feign.FeignClient;
7import org.springframework.web.bind.annotation.PathVariable;
8import org.springframework.web.bind.annotation.RequestMapping;
9import org.springframework.web.bind.annotation.RestController;
10
11import java.util.List;
12
13/**
14 * Created with IntelliJ IDEA.
15 * Description:
16 * User:
17 * Date: 2019-04-08
18 * Time: 21:41
19 */
20@RestController
21public class DeptController_Consumer {
22
23 //注入的接口
24 @Autowired
25 private DeptClientService deptClientService;
26
27 @RequestMapping(value = "/consumer/dept/get/{id}")
28 public Dept get(@PathVariable("id") Long id)
29 {
30 return this.deptClientService.get(id);
31 }
32
33 @RequestMapping(value = "/consumer/dept/list")
34 public List<Dept> list()
35 {
36 List<Dept> list = this.deptClientService.list();
37 return list;
38 }
39
40 @RequestMapping(value = "/consumer/dept/add")
41 public Object add(Dept dept)
42 {
43 return this.deptClientService.add(dept);
44 }
45
46
47}
48
49
六、在主启动类上添加相应的注解
1
2
3 1
2@EnableFeignClients(basePackages = "com.atguigu.springcloud")
3
1
2
3 1
2@ComponentScan("com.atguigu.springcloud")
3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 1package com.atguigu.springcloud;
2
3import org.springframework.boot.SpringApplication;
4import org.springframework.boot.autoconfigure.SpringBootApplication;
5import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
6import org.springframework.cloud.netflix.feign.EnableFeignClients;
7import org.springframework.context.annotation.ComponentScan;
8
9/**
10 * Created with IntelliJ IDEA.
11 * Description:
12 * User:
13 * Date: 2019-04-08
14 * Time: 22:18
15 */
16@SpringBootApplication
17@EnableEurekaClient
18@EnableFeignClients(basePackages = "com.atguigu.springcloud") //所在包名,一般都写最外层的包名
19@ComponentScan("com.atguigu.springcloud") //需要扫描的包,一般都写最外层的包名
20public class DeptConsumer80_Feign_App {
21 public static void main(String[] args) {
22 SpringApplication.run(DeptConsumer80_Feign_App.class,args);
23 }
24}
25
26