前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java学习笔记—总所周知的微服务架构SpringCloud中Feign

Java学习笔记—总所周知的微服务架构SpringCloud中Feign

作者头像
慕容千语
发布2019-06-13 12:18:58
4450
发布2019-06-13 12:18:58
举报
文章被收录于专栏:慕容千语的架构笔记
Feign简介

Feign 是一个声明web服务客户端,这便得编写web服务客户端更容易,使用Feign 创建一个接口并对它进行注解,它具有可插拔的注解支持包括Feign注解与JAX-RS注解,Feign还支持可插拔的编码器与解码器,Spring Cloud 增加了对 Spring MVC的注解,Spring Web 默认使用了HttpMessageConverters, Spring Cloud 集成 Ribbon 和 Eureka 提供的负载均衡的HTTP客户端 Feign.

声明式REST客户端:Feign

先要启动eureka_register_service工程(注册中心)和biz-service-0工程(服务生产者)

创建一个maven工程eureka_feign_client

pom.xml

org.springframework.boot

spring-boot-starter-parent

1.4.3.RELEASE

UTF-8

1.8

org.springframework.cloud

spring-cloud-starter-feign

org.springframework.cloud

spring-cloud-starter-eureka

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

org.springframework.cloud

spring-cloud-dependencies

Brixton.SR5

pom

import

在应用主类中通过@EnableFeignClients注解开启Feign功能

启动文件FeignApplication.java

@SpringBootApplication

@EnableDiscoveryClient

@EnableFeignClients

public class FeignApplication {

public static void main(String[] args) {

SpringApplication.run(FeignApplication.class, args);

}

}

定义服务接口类UserClient.java

使用@FeignClient("biz-service-0")注解来绑定该接口对应biz-service-0服务

@FeignClient("biz-service-0")

public interface UserClient {

@RequestMapping(method = RequestMethod.GET, value = "/getuser")

public User getuserinfo();

@RequestMapping(method = RequestMethod.GET, value = "/getuser")

public String getuserinfostr();

@RequestMapping(method = RequestMethod.GET, value = "/info")

public String info();

}

在web层中调用上面定义的UserController,具体如下

@RestController

public class UserController {

@Autowired

UserClient userClient;

@RequestMapping(value = "/getuserinfo", method = RequestMethod.GET)

public User getuserinfo() {

return userClient.getuserinfo();

}

@RequestMapping(value = "/getuserinfostr", method = RequestMethod.GET)

public String getuserinfostr() {

return userClient.getuserinfostr();

}

@RequestMapping(value = "/info", method = RequestMethod.GET)

public String info() {

return userClient.info();

}

}

application.properties配置变量

spring.application.name=feign-consumer

server.port=8004

eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

访问 http://127.0.0.1:8004/getuserinfo

总结:

其实通过Feign封装了HTTP调用服务方法,使得客户端像调用本地方法那样直接调用方法,类似Dubbo中暴露远程服务的方式,区别在于Dubbo是基于私有二进制协议,而Feign本质上还是个HTTP客户端

可能遇到的问题

启动抛出: Attribute 'value' in annotation [org.springframework.cloud.netflix.feign.FeignClient] must be declared as an @AliasFor [serviceId], not [name].

解决:把spring-cloud-dependencies version升级到Brixton.SR5(netflix-core版本升到1.1.1就好了)

最后

结合当前互联网公司的技术需求及主流技术,我整理了一套系统的架构技术体系。不少公司很重视高并发高可用的技术,特别是一线互联网公司,分布式、JVM、spring源码分析、微服务等知识点已经是面试的必考题。这些东西可能你们平时在工作中接触过,但是缺少全面系统的学习,希望对正在面试的朋友或是遭遇技术瓶颈的程序员们提供一点思路。

1、开源框架解析专题:站在巨人肩膀,收获不一样的视野。

开源框架

2、架构筑基专题:深入内核、直击故障、拒绝懵圈。

架构筑基

3、微服务架构专题:你还不知道微服务,怎么涨薪。

微服务架构

4、高性能架构专题:成为互联网架构师,你要的都在这里。

高性能架构

5、团队协作开发专题:让你团队开发效率提高十倍。

团队协作开发

6、B2C商城项目实战:撸起袖子干实事,项目经验那点事。

B2C商城实战

7、并发编程

并发编程

8、设计模式

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019.01.11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Feign简介
  • 声明式REST客户端:Feign
  • 总结:
  • 可能遇到的问题
  • 最后
相关产品与服务
Prowork 团队协同
ProWork 团队协同(以下简称 ProWork )是便捷高效的协同平台,为团队中的不同角色提供支持。团队成员可以通过日历、清单来规划每⽇的工作,同时管理者也可以通过统计报表随时掌握团队状况。ProWork 摒弃了僵化的流程,通过灵活轻量的任务管理体系,满足不同团队的实际情况,目前 ProWork 所有功能均可免费使用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档