SpringCloud--服务消费者(Feign)
2017-08-10 11:01
731 查看
一、Feign简介
Feign是一个声明式的web服务客户端,它使得写web服务变得更简单。使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,包括Feign 注解和JAX-RS注解。Feign同时支持可插拔的编码器和解码器。spring cloud对Springmvc添加了支持,同时在spring web中次用相同的HttpMessageConverter。当我们使用feign的时候,spring cloud 整和了Ribbon和eureka去提供负载均衡。
简而言之:
feign采用的是接口加注解
feign 整合了ribbon
二、准备工作
继续用上一节的工程: 启动eureka-server,端口为8761; 启动service-hi 两次,端口分别为8762 、8773.
三、创建一个feign的服务
创建一个spring-boot工程,取名为:serice-feign,它的pom文件为:<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.forezp</groupId> <artifactId>service-feign</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>service-feign</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <depend 4000 encies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.RC1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
向服务注册中心注册它自己,这时service-feign既是服务提供者,也是服务消费者,配置文件application.yml
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ server: port: 8765 spring: application: name: service-feign1
2
3
4
5
6
7
8
9
10
1
2
3
4
5
6
7
8
9
10
在程序的入口类,需要通过注解@EnableFeignClients来开启feign:
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class ServiceFeignApplication { public static void main(String[] args) { SpringApplication.run(ServiceFeignApplication.class, args); } }1
2
3
4
5
6
7
8
9
10
11
1
2
3
4
5
6
7
8
9
10
11
定义一个feign接口类,通过@ FeignClient(“服务名”),来指定调用哪个服务:
/** * Created by fangzhipeng on 2017/4/6. */ @FeignClient(value = "service-hi") public interface SchedualServiceHi { @RequestMapping(value = "/hi",method = RequestMethod.GET) String sayHiFromClientOne(@RequestParam(value = "name") String name); }1
2
3
4
5
6
7
8
9
10
11
12
1
2
3
4
5
6
7
8
9
10
11
12
在web层的controllrt:
@RestController public class HiController { @Autowired SchedualServiceHi schedualServiceHi; @RequestMapping(value = "/hi",method = RequestMethod.GET) public String sayHi(@RequestParam String name){ return schedualServiceHi.sayHiFromClientOne(name); } }1
2
3
4
5
6
7
8
9
10
11
12
1
2
3
4
5
6
7
8
9
10
11
12
访问http://localhost:8765/hi?name=forezp,浏览器交替显示:
hi forezp,i am from port:8762
hi forezp,i am from port:8763
四、更改feign的配置
在声明feignclient的时候,不仅要指定服务名,同时需要制定服务配置类:@FeignClient(name = "stores", configuration = FooConfiguration.class) public interface StoreClient { //.. }1
2
3
4
5
1
2
3
4
5
重写配置,需要加@Configuration注解,并重写下面的两个bean,栗子:
@Configuration public class FooConfiguration { @Bean public Contract feignContractg() { return new feign.Contract.Default(); } @Bean public BasicAuthRequestInterceptor basicAuthRequestInterceptor() { return new BasicAuthRequestInterceptor("user", "password"); } }
相关文章推荐
- Spring Cloud学习:02服务消费者(Ribbon&Feign)
- Spring Cloud(四)服务提供者 Eureka + 服务消费者 Feign
- Spring Cloud(服务的消费者 Feign)
- SpringCloud: 服务消费者(Feign)
- SpringCloud入门3:服务消费者(Feign)
- SpringCloud教程 | 四.服务消费者(Feign)
- Spring Cloud(四)服务提供者 Eureka + 服务消费者 Feign
- SpringCloud之服务消费者Feign(三)
- 玩转SpringCloud(F版本) 二.服务消费者(2)feign
- SpringCloud之服务消费者Feign(三)
- SpringCloud 学习 | 第四篇: 服务消费者 feign
- [bigdata-108] spring-cloud-04 分布式服务的feign消费者
- 白话SpringCloud | 第四章:服务消费者(RestTemple+Ribbon+Feign)
- SpringCloud教程 | 第三篇: 服务消费者(Feign)
- springcloud实战之6 服务消费者(Feign)
- 一起来学SpringCloud之 - 服务消费者(Feign-上)
- 一起来学SpringCloud之 - 服务消费者(Feign-下)
- SpringCloud入门教学|第三篇:服务消费者(Feign)
- SpringCloud | 服务消费者 ribbon (四)
- SpringCloud教程 | 第二篇: 服务消费者(rest+ribbon)(Finchley版本)