一:Feign简介

Feign 是一种声明式、模板化的 HTTP 客户端,在 Spring Cloud 中使用 Feign,可以做到使用 HTTP请求远程服务时能与调用本地方法一样的编码体验,开发者完全感知不到这是远程方法,更感知不到这是个 HTTP 请求。

Feign 的灵感来源于 Retrofit、JAXRS-2.0 和 WebSocket,它使得 Java HTTP 客户端编写更方便,旨在通过最少的资源和代码来实现和 HTTP API 的连接。

二:Feign使用步骤

在客户端集成Feign即可

第一步:pom依赖

  <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

由于可能特别多的模块需要调用其他模块的项目,那么就需要进行统一依赖,减少重复依赖:

所以将这个依赖添加至父工程里面

启动类添加注解开启远程调用:

package com.cxy;

import com.netflix.loadbalancer.BestAvailableRule;
import com.netflix.loadbalancer.IRule;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; /***
* @ClassName: PersonApplication
* @Description:
* @Auther:
* @Date: 2019/1/2816:30
* @version : V1.0
*/
@SpringBootApplication
@EnableEurekaClient //开启注解,注册服务
@MapperScan("com.cxy")
@EnableFeignClients
public class UserApplication {
public static void main(String[] args) { SpringApplication.run(UserApplication.class,args);
}
@Bean
@LoadBalanced //使用负载均衡器Ribbon
public RestTemplate getRestTemplate(){
return new RestTemplate();
} /*@Bean
public IRule myRule(){
//return new RoundRobinRule();//轮询
// return new RetryRule();//重试
return new BestAvailableRule();
}*/
}
@EnableFeignClients,这个注解

远程调用方法首先需要写一个接口,可以当作是service:
package com.cxy.service;

import com.cxy.dataObject.PersonDo;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Component
@FeignClient("cxy-person-service")
public interface IPersonService {
@RequestMapping("/person/{id}")
PersonDo getPersonDoById(@PathVariable("id") Integer id);
}

contrller调用

    @Autowired
private IPersonService personService;
  **/
@RequestMapping(value = "/get/{id}",method = RequestMethod.GET)
public PersonDo selectPsersonById(@PathVariable Integer id){
PersonDo personDo =personService.getPersonDoById(id);
return personDo; }

启动访问:

直接访问结果:

此处已经成功了

springcloud系列五 feign远程调用服务的更多相关文章

  1. SpringCloud(二) - Eureka注册中心,feign远程调用,hystrix降级和熔断

    1.项目模块介绍 2. 父项目 主要依赖 spring-cloud 的 版本控制 <properties> <!-- springCloud 版本 --> <scd.ve ...

  2. 架构设计:远程调用服务架构设计及zookeeper技术详解(下篇)

    一.下篇开头的废话 终于开写下篇了,这也是我写远程调用框架的第三篇文章,前两篇都被博客园作为[编辑推荐]的文章,很兴奋哦,嘿嘿~~~~,本人是个很臭美的人,一定得要截图为证: 今天是2014年的第一天 ...

  3. 架构设计:一种远程调用服务的设计构思(zookeeper的一种应用实践)

    在深入学习zookeeper我想先给大家介绍一个和zookeeper相关的应用实例,我把这个实例命名为远程调用服务.通过对这种应用实例的描述,我们会对zookeeper应用场景会有深入的了解. 远程调 ...

  4. 使用AIDL远程调用服务中的方法

    AIDL:android interface define language(接口定义语言) 作用:方便远程调用其他服务中的方法 注意:安卓四大组件都要在清单文件注册 aidl创建图: AIDL的全称 ...

  5. zookeeper系列之七—从远程调用认识zookeeper

    http://www.csdn.net/article/2014-01-02/2817944-zookeeper 在Hadoop的学习过程中,Zookeeper是让很多初学者困惑的技术,远程调用服务是 ...

  6. 如何从零开始实现一个soa远程调用服务基础组件

    说起soa远程调用基础组件,最著名的莫过于淘宝的dubbo了,目前很多的大型互联网公司都有一套自己的远程服务调用分布式框架,或者是使用开源的(例如dubbo),或者是自己基于某种协议(例如hessia ...

  7. spring boot / cloud (八) 使用RestTemplate来构建远程调用服务

    spring boot / cloud (八) 使用RestTemplate来构建远程调用服务 前言 上周因家里突发急事,请假一周,故博客没有正常更新 RestTemplate介绍: RestTemp ...

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

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

  9. Springcloud踩坑记---使用feignclient远程调用服务404

    公司项目进行微服务改造,由之前的dubbo改用SpringCloud,微服务之间通过FeignClient进行调用,今天在测试的时候,eureka注册中心有相应的服务,但feignclient就是无法 ...

随机推荐

  1. oracle——存储过程分页

    1.包头: CREATE OR REPLACE PACKAGE BAWQ_PROC_PAGE IS -- BAWQ_PROC_PAGE 是包头名 TYPE T_CURSOR IS REF CURSOR ...

  2. c:if标签数据回显判断是否选中

    <form action="/brand/list.do" method="post" style="padding-top:5px;" ...

  3. sea.js的模块化开发

    为什么使用sea.js? Sea.js 追求简单.自然的代码书写和组织方式,具有以下核心特性: 简单友好的模块定义规范:Sea.js 遵循 CMD 规范,可以像Node.js 一般书写模块代码. 自然 ...

  4. Javascript面向对象(二):构造函数的继承

    这个系列的第一部分,主要介绍了如何"封装"数据和方法,以及如何从原型对象生成实例. 今天要介绍的是,对象之间的"继承"的五种方法. 比如,现在有一个" ...

  5. mysql case

    1.table CREATE TABLE `lee`(`id` INT(10) NOT NULL auto_increment,`name` varchar(20) DEFAULT null,`bir ...

  6. Bind和Eval的不同用法 (转)

    今天在用DataList的模板列的时候习惯性地像在03中那样去给模板列的绑定字段加个处理函数: < asp:Label ID = " Label1 " runat = &qu ...

  7. ORACLE体系结构一 (实例(instance))--ORACLE_SID

    数据库实例(也称为服务器Server)就是用来访问一个数据库文件集的一个存储结构及后台进程的集合.它使一个单独的数据库可以被多个实例访问(也就是ORACLE并行服务器-- OPS).实例在操作系统中用 ...

  8. [codeforces126B]Password

    解题关键:KMP算法中NEXT数组的理解. #include<bits/stdc++.h> #define maxn 1000006 using namespace std; typede ...

  9. 【摘自张宴的"实战:Nginx"】nginx配置

    user nobody;worker_processes 2; #error_log logs/error.log;error_log logs/error.log notice;#error_log ...

  10. Arduino 002 --- 在Ubuntu(Linux) 中搭建Arduino开发环境

    在Ubuntu/Linux 中搭建Arduino开发环境 我的Ubuntu系统:Ubuntu 14.04.10 TLS 32位 需要安装的Arduino的版本:Arduino 1.6.11(最新版本) ...