1.Feign概述

在上一篇的HelloService这个类中,我们有这样一行代码:

return restTemplate.getForObject("http://hello-service/hello",String.class);

对于代码有一定洁癖的你来说,一定感觉到了,这个url应该是可以被配置的。既然说到配置,那我们首先想到的就是使用java注解的方式。Feign就是这样一个注解框架,它也是netflix为我们提供的,方便我们整合ribbon和hystrix(后面会学习)。

使用Feign,我们能很方便的通过编写接口并插入注解,来定义和代理HTTP请求。Feign主要具备如下特性:

支持Feign注解;

支持Ribbon负载均衡;

支持HTTP编码器和解码器;

提供了熔断器Hystrix;

2.Feign引入

让我们修改ribbon这个项目,使之支持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/maven-v4_0_0.xsd">

<parent>

<artifactId>springcloud.parent</artifactId>

<groupId>com.zuikc</groupId>

<version>1.0-SNAPSHOT</version>

</parent>

<modelVersion>4.0.0</modelVersion>

<packaging>war</packaging>

<name>ribbon</name>

<artifactId>ribbon</artifactId>

<dependencyManagement>

<dependencies>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-dependencies</artifactId>

<version>Greenwich.RELEASE</version>

<type>pom</type>

<scope>import</scope>

</dependency>

</dependencies>

</dependencyManagement>

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-eureka</artifactId>

<version>1.4.6.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-feign</artifactId>

<version>1.4.6.RELEASE</version>

</dependency>

</dependencies>

</project>

注意pom中的粗体部分。引入Feign依赖,会自动引入Hystrix依赖。

appcation.yml并不需要变动。

3.@EnableFeignClients

修改ServiceRibbonApplication,加入注解@EnableFeignClients,这一步很重要,说明项目对于feign的支持,

package com.zuikc;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;

import org.springframework.cloud.openfeign.EnableFeignClients;

import org.springframework.context.annotation.Bean;

import org.springframework.web.client.RestTemplate;

/**

* @ClassName ServiceRibbonApplication

* @Description 我们提供咨询和培训服务,关于本文有任何困惑,请关注并联系“码农星球”

* @Author 码农星球

**/

@SpringBootApplication

@EnableDiscoveryClient

@EnableFeignClients

public class ServiceRibbonApplication {

public static void main(String[] args) {

SpringApplication.run(ServiceRibbonApplication.class, args);

}

@Bean

@LoadBalanced

RestTemplate restTemplate() {

return new RestTemplate();

}

}

4.服务接口

创建一个接口,

package com.zuikc;

import org.springframework.cloud.openfeign.FeignClient;

import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient(value = "hello-service")

public interface HelloService {

//服务中方法的映射路径

@RequestMapping("/hello")

String hello();

}

这个接口就比较重要了。

注解@FeignClient说明了,我们需要去eureka服务上去调用“hello-service”这个服务。

注解@RequestMapping指的是我们需要调用的路径。

5.控制器

现在我们还需要最后一步,就是创建一个控制器来接受请求,然后让robbin去分发,

package com.zuikc;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**

* @ClassName ConsumerController

* @Description 我们提供咨询和培训服务,关于本文有任何困惑,请关注并联系“码农星球”

* @Author 码农星球

**/

@RestController

public class HelloController {

@Autowired

HelloService helloService;

@RequestMapping("/hello")

public String helloConsumer(){

return helloService.hello();

}

}

当一切处理完成,让我们启动这个项目,我们仍旧看到ribbon的这个服务,

然后,http://localhost:9291/hello吧,可以看到LB非常的成功。

现在,假设其中一个服务提供者因为某种原因(比如异常、断网)停掉了,看看结果会是怎么样的。为了模拟这个现象,让我们关闭provider2,发现我们无论怎么刷新上面的url,LB永远分配到provider1。这也是分布式系统容错能力更强的一种保证!

感谢关注“码农星球”。本文版权属于“码农星球”。我们提供咨询和培训服务,关于本文有任何困惑,请关注并联系我们。

SpringCloud无废话入门03:Feign声明式服务调用的更多相关文章

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

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

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

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

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

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

  4. SpringCloud实战-Feign声明式服务调用

    在前面的文章中可以发现当我们通过RestTemplate调用其它服务的API时,所需要的参数须在请求的URL中进行拼接,如果参数少的话或许我们还可以忍受,一旦有多个参数的话,这时拼接请求字符串就会效率 ...

  5. Feign声明式服务调用

    Feign是一种声明式.模板化的HTTP客户端(仅在Application Client中使用).声明式调用是指,就像调用本地方法一样调用远程方法,无需感知操作远程http请求. Spring Clo ...

  6. 笔记:Spring Cloud Feign 声明式服务调用

    在实际开发中,对于服务依赖的调用可能不止一处,往往一个接口会被多处调用,所以我们通常会针对各个微服务自行封装一些客户端类来包装这些依赖服务的调用,Spring Cloud Feign 在此基础上做了进 ...

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

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

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

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

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

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

随机推荐

  1. 【转】WPF中的窗口的生命周期

    原文地址:http://www.cnblogs.com/Jennifer/articles/1997763.html WPF中的窗口的生命周期 WPF中所有窗口的基类型都是System.Windows ...

  2. Codeforces 1139E Maximize Mex 二分图匹配

    Maximize Mex 离线之后把删数变成加数, 然后一边跑匈牙利一遍算答案. #include<bits/stdc++.h> #define LL long long #define ...

  3. BZOJ2209 [Jsoi2011]括号序列 splay

    原文链接http://www.cnblogs.com/zhouzhendong/p/8093556.html 题目传送门 - BZOJ2209 题解 我太弱了,调出这题感觉都要吐了. 题解懒得写了. ...

  4. hive中使用union出现异常数据

    select * from tbl where id=2 union select * from tbl where id =1 如果hive使用union这么查询的时候,我们会发现数据变乱了. 解决 ...

  5. 爬虫之 案列1补充(pipelines优化)

    1. 先打开settings.py文件将 'ITEM_PIPELINES'启动(取消注释即可) 2. spider代码 # -*- coding: utf-8 -*- import scrapy im ...

  6. Working out(DP)

    题目描述: 题意: 有n*m个格子, 走过一个格子可以得到相应的分数. A 从(1,1)沿 下 或 右 走到(n,m) B 从(n,1)沿 上 或 右 走到(1,m) 两人路径有且只能有一个格子重合( ...

  7. Sunscreen POJ - 3614(贪心)

    To avoid unsightly burns while tanning, each of the C (1 ≤ C ≤ 2500) cows must cover her hide with s ...

  8. 大数据小白系列——HDFS(3)

    这里是大数据小白系列,这是本系列的第三篇,介绍HDFS中NameNode选举,JournalNode等概念. 上一期我们说到了为解决NameNode(下称NN)单点失败问题,HDFS中使用了双NN的机 ...

  9. 页面中去除浮动 clear:both

    今天写代码发现一个很奇怪的问题,发现上面的div加浮动(不管是否包含div)以后对下面div的浮动有所影响,通过去除浮动,搞定: 只需要在受影响的div中的样式中,加入clear:both即可

  10. XamarinAndroid组件教程RecylerView自定义适配器动画

    XamarinAndroid组件教程RecylerView自定义适配器动画 如果RecyclerViewAnimators.Adapters命名空间中没有所需要的适配器动画,开发者可以自定义动画.此时 ...