springcloud系列五 feign远程调用服务
一: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远程调用服务的更多相关文章
- SpringCloud(二) - Eureka注册中心,feign远程调用,hystrix降级和熔断
1.项目模块介绍 2. 父项目 主要依赖 spring-cloud 的 版本控制 <properties> <!-- springCloud 版本 --> <scd.ve ...
- 架构设计:远程调用服务架构设计及zookeeper技术详解(下篇)
一.下篇开头的废话 终于开写下篇了,这也是我写远程调用框架的第三篇文章,前两篇都被博客园作为[编辑推荐]的文章,很兴奋哦,嘿嘿~~~~,本人是个很臭美的人,一定得要截图为证: 今天是2014年的第一天 ...
- 架构设计:一种远程调用服务的设计构思(zookeeper的一种应用实践)
在深入学习zookeeper我想先给大家介绍一个和zookeeper相关的应用实例,我把这个实例命名为远程调用服务.通过对这种应用实例的描述,我们会对zookeeper应用场景会有深入的了解. 远程调 ...
- 使用AIDL远程调用服务中的方法
AIDL:android interface define language(接口定义语言) 作用:方便远程调用其他服务中的方法 注意:安卓四大组件都要在清单文件注册 aidl创建图: AIDL的全称 ...
- zookeeper系列之七—从远程调用认识zookeeper
http://www.csdn.net/article/2014-01-02/2817944-zookeeper 在Hadoop的学习过程中,Zookeeper是让很多初学者困惑的技术,远程调用服务是 ...
- 如何从零开始实现一个soa远程调用服务基础组件
说起soa远程调用基础组件,最著名的莫过于淘宝的dubbo了,目前很多的大型互联网公司都有一套自己的远程服务调用分布式框架,或者是使用开源的(例如dubbo),或者是自己基于某种协议(例如hessia ...
- spring boot / cloud (八) 使用RestTemplate来构建远程调用服务
spring boot / cloud (八) 使用RestTemplate来构建远程调用服务 前言 上周因家里突发急事,请假一周,故博客没有正常更新 RestTemplate介绍: RestTemp ...
- SpringCloud系列-利用Feign实现声明式服务调用
上一篇文章<手把手带你利用Ribbon实现客户端的负载均衡>介绍了消费者通过Ribbon调用服务实现负载均衡的过程,里面所需要的参数需要在请求的URL中进行拼接,但是参数太多会导致拼接字符 ...
- Springcloud踩坑记---使用feignclient远程调用服务404
公司项目进行微服务改造,由之前的dubbo改用SpringCloud,微服务之间通过FeignClient进行调用,今天在测试的时候,eureka注册中心有相应的服务,但feignclient就是无法 ...
随机推荐
- gridcontrol 添加行号
private void gridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndic ...
- Hbase表重命名 表改名
PS:现在我有个表 :test11_new ,我要给他改名 开始: 1.先disable掉表hbase(main):023:0> disable 'test11_new' 0 row(s) i ...
- TCP/IP 笔记 7 Ping
lenovo-myc@lenovomyc-Lenovo-Product:~$ ping www.baidu.com PING www.a.shifen.com (() bytes of data. 这 ...
- Lucene源码解析--Analyzer之Tokenizer
Analyzer包含两个核心组件,Tokenizer以及TokenFilter.两者的区别在于,前者在字符级别处理流,而后者则在词语级别处理流.Tokenizer是Analyzer的第一步,其构造函数 ...
- 1-1+zookeeper简介
zookeeper是中间件,可以为分布式系统提供协调服务.如果没有分布式系统,zookeeper也发挥不了它的优势.
- [patl2-020]功夫传人
解题关键:dfs的简单应用,需要注意类型double与int #include<cstdio> #include<cstring> #include<algorithm& ...
- git在eclipse中的配置 完整版 转载
http://www.cnblogs.com/zhxiaomiao/archive/2013/05/16/3081148.html
- Windows系统 为 Visual Studio软件 搭建 OpenCV2 开发环境
Windows系统 为 Visual Studio软件 搭建 OpenCV2 开发环境 我们的电脑系统:Windows 10 64位 Visual Studio 软件:Visual Studio 20 ...
- C#调用C++类库的几种方式
1. 直接调用C++类库中的公共方法 使用DllImport特性对方法进行调用,比如一个C++类库SampleCppWrapper.dll中的公共方法: extern "C" _ ...
- Hibernate 处理查询 in 字句
from : http://blog.csdn.net/wodestudy/article/details/38200421 在处理hibernate的实体查询,一般可以采用多种方式处理复杂的查询,比 ...