上一篇,讲了SpringClound中的消费者采用Ribbon+Rest来实现,这回我们用组件Feign来实现服务的消费者,Fegin中也是默认集成了Ribbon的;和Eureka结合也能实现负载均衡;

概括来说,Fegin的区别就是基于注解来实现,具备可插拔的特性;

依赖前文说的Eureka,service-hello(一个项目,注册两个实例)

创建Fegin项目;

在Idea里,新建项目,选择Spring initializer.

下面的pom

	<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

  

配置properties文件参数;

#服务端口
server.port=8886 #注册服务中心地址
eureka.client.service-url.defaultZone=http://localhost:8882/eureka/ #注册服务名
spring.application.name=service-feign

  

启动类如下:

@EnableDiscoveryClient
@EnableFeignClients //开启Feign的功能:
@SpringBootApplication
public class SpringCloundEurekaFeginExampleApplication { public static void main(String[] args) {
SpringApplication.run(SpringCloundEurekaFeginExampleApplication.class, args);
}
}

  

然后我们定义一个fegin的接口,在这个接口中我们通过@ FeignClient(“服务名”)来实现消费服务提供者;

//代表改接口用费"service-hello"的服务 提供
@FeignClient(value = "service-hello")
public interface IFeginService { @RequestMapping(value = "/index")
public String index();
}

  

我们再在fegin项目中暴露一个访问接口,controller;

@RestController
public class FeginController { @Autowired
private IFeginService feginService; @RequestMapping("/index")
public String index(){
return feginService.index();
}
}

  

代码基本编写完成,下面我们来启动项目;Eureka,service-hello(两个实例),最后启动service-fegin;

Feign整合了Ribbon和Hystrix,此外,Spring Cloud还对Feign提供了Spring MVC注解的支持,也使得我们在Web中可以使用同一个HttpMessageConverter

总起来说,Feign具有如下特性:

  • 可插拔的注解支持,包括Feign注解和JAX-RS注解;
  • 支持可插拔的HTTP编码器和解码器;
  • 支持Hystrix和它的Fallback;
  • 支持Ribbon的负载均衡;
  • 支持HTTP请求和响应的压缩。

(三)Fegin声明式服务调用的更多相关文章

  1. Spring Cloud 2-Feign 声明式服务调用(三)

    Spring Cloud Feign  1. pom.xml 2. application.yml 3. Application.java 4. Client.java 简化RestTemplate调 ...

  2. Spring Cloud Feign 声明式服务调用

    目录 一.Feign是什么? 二.Feign的快速搭建 三.Feign的几种姿态 参数绑定 继承特性 四.其他配置 Ribbon 配置 Hystrix 配置 一.Feign是什么? ​ 通过对前面Sp ...

  3. spring cloud 系列第4篇 —— feign 声明式服务调用 (F版本)

    源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 一.feign 简介 在上一个用例中,我们使用ribbon+restTem ...

  4. SpringCloud系列-利用Feign实现声明式服务调用

    上一篇文章<手把手带你利用Ribbon实现客户端的负载均衡>介绍了消费者通过Ribbon调用服务实现负载均衡的过程,里面所需要的参数需要在请求的URL中进行拼接,但是参数太多会导致拼接字符 ...

  5. Spring Cloud Feign声明式服务调用(转载)+遇到的问题

    转载:原文 总结: 1.pom添加依赖 2.application中填写正确的eureka配置 3.启动项中增加注解 @EnableFeignClients 4.填写正确的调用接口 通过原文使用Fei ...

  6. SpringCloud 源码系列(6)—— 声明式服务调用 Feign

    SpringCloud 源码系列(1)-- 注册中心 Eureka(上) SpringCloud 源码系列(2)-- 注册中心 Eureka(中) SpringCloud 源码系列(3)-- 注册中心 ...

  7. Spring Cloud Eureka 分布式开发之服务注册中心、负载均衡、声明式服务调用实现

    介绍 本示例主要介绍 Spring Cloud 系列中的 Eureka,使你能快速上手负载均衡.声明式服务.服务注册中心等 Eureka Server Eureka 是 Netflix 的子模块,它是 ...

  8. 声明式服务调用:Spring Cloud Feign

    最近在学习Spring Cloud的知识,现将声明式服务调用:Spring Cloud Feign 的相关知识笔记整理如下.[采用 oneNote格式排版]

  9. SpringCloud之声明式服务调用 Feign(三)

    一 Feign简介 Feign是一种声明式.模板化的HTTP客户端,也是netflix公司组件.使用feign可以在远程调用另外服务的API,如果调用本地API一样.我们知道,阿里巴巴的doubbo采 ...

随机推荐

  1. ORACLE查询闪回

    在Oracle中如果错误地提交了修改操作,然后想查看修改前的值,这时候可以使用查询闪回(query flashback). 查询闪回可以根据根据一个时间值或者系统变更号(SCN)进行. 执行闪回操作, ...

  2. 【HDU 5015】233 Matrix

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5015 [算法] 矩阵乘法 [代码] #include<bits/stdc++.h> u ...

  3. NEUOJ 1702 撩妹全靠魅力值 (三维偏序)

    题目链接:http://acm.neu.edu.cn/hustoj/problem.php?id=1702 题目大意:就是问每个人三个属性同时不低于另外几个人....人不分先后 经典的三维偏序问题 解 ...

  4. CentOS7开启网络配置

    虚拟机在安装时可以开启网络 如果没有开启的话 可以通过以下操作 ip  addr 查看是否开启网络 没有开启的话 cd /etc/sysconfig/network-scripts/ 然后 执行 ls ...

  5. centos + nodejs + egg2.x 开发微信分享功能

    本文章发到掘金上,请移步阅读: https://juejin.im/post/5cf10b02e51d45778f076ccd

  6. Flask Cookie和Session

    1.1.概念 cookie:在网站中,http请求是无状态的.也就是说即使第一次和服务器连接后并且登录成功后,第二次请求服务器依然不能知道当前请求是哪个用户.cookie的出现就是为了解决这个问题,第 ...

  7. Android local socket学习总结

    转载请注明出处: http://blog.csdn.net/xiexh0921/article/details/7513628   ################################## ...

  8. Oracle 新手语法记录

    一.用户 1. 创建用户 语法:create user 用户名 identified by 口令; create user test identified by test; 2. 修改用户 语法:al ...

  9. PHP魔术方法__tostring()篇

    下面是关于__tostring 的运用 header('Content-type:text/html;charset="utf-8"'); /*存在内置方法_tostring()的 ...

  10. Book 动态规划

    虽然之前学过一点点,但是还是不会------现在好好跟着白书1.4节学一下—————— (1)数字三角形 d(i,j) = max(d(i+1,j),d(i+1,j+1)) + a[i][j] hdu ...