SpringCloud- 第九篇 Feign

时间:2020-07-15 13:11:36   收藏:0   阅读:81
技术图片

1:Feign是什么

Feign是一个声明式的Web服务客户端,使得编写Web服务客户端变得非常容易,只需要创建一个接口,然后在上面添加注解即可。官网:https://github.com/OpenFeign/feign

2:Feign能干什么

3:Feingn特性

4: Feign集成

4.1:加入依赖

<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>

4.2:启动类

需要添加开启feign的注解,示例如下:

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients

4.3:定义接口,示例如下:

@FeignClient(value = "userService")
public interface UserService {
    @RequestMapping(value = "/userServiceProvider", method = RequestMethod.GET)
    String sayHiFromClientOne(@RequestParam(value = "name") String name);
}

4.4:配置文件

基本类似于Ribbon的配置, application.properties示例如下:

eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
server.port=8770
spring.application.name=service-feign

4.5:调用服务的Controller类

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!