首页
学习
活动
专区
圈层
工具
发布

Feign

作者头像
星哥玩云
发布2022-09-15 14:09:22
发布2022-09-15 14:09:22
6890
举报
文章被收录于专栏:开源部署开源部署

1、认识Feign

1.1、Feign概述

Feign是一个声明式的Web Service客户端,它使编写Web Service客户端变得容易。Spring Cloud为Feign客户端添加了Spring MVC的注解支持,Feign在整合了Ribbon后可以提供负载均衡功能。

1.2、使用Feign调用服务

1.2.1、创建Spring Cloud应用,添加Feign、Eureka Discovery Client和Web依赖
1.2.2、编写配置
代码语言:javascript
复制
spring.application.name=open-feign
server.port=50006
eureka.client.fetch-registry=true
eureka.client.register-with-eureka=false
eureka.client.service-url.defaultZone=http://eureka01:50001/eureka/,http://eureka02:50002//eureka/
1.2.3、添加Feign支持
代码语言:javascript
复制
@SpringBootApplication
@EnableDiscoveryClient //开启客户端发现
@EnableFeignClients //开启Feign支持
public class OpenFeignApplication {

    public static void main(String[] args) {
        SpringApplication.run(OpenFeignApplication.class, args);
    }

}
1.2.4、实现Feign接口
代码语言:javascript
复制
@FeignClient(name = "provider")
public interface FeignClients {
    
    @GetMapping("/hello")
    public String hello();    
}
1.2.5、实现调用服务接口
代码语言:javascript
复制
@RestController
public class HelloController {
    
    @Autowired
    FeignClients feignClients;
    
    @GetMapping("/hello")
    public String index(){
        return feignClients.hello();
    }
}
1.2.6、启动"服务中心"、"服务提供者"和"Feign"工程

访问http://localhost:50006/hello

经验:

  • feignClient接口 有参数在参数必须加@PathVariable(“XXX”)和@RequestParam(“XXX”)
  • feignClient返回值为复杂对象时其类型必须有无参构造函数
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、认识Feign
    • 1.1、Feign概述
    • 1.2、使用Feign调用服务
      • 1.2.1、创建Spring Cloud应用,添加Feign、Eureka Discovery Client和Web依赖
      • 1.2.2、编写配置
      • 1.2.3、添加Feign支持
      • 1.2.4、实现Feign接口
      • 1.2.5、实现调用服务接口
      • 1.2.6、启动"服务中心"、"服务提供者"和"Feign"工程
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档