服务间调用--feign跟ribbon
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
application.yml内容:
spring:
application:
name: sms-module
info:
name: author:wzy,class:develop,tel:17301394307 server:
port: 9001 eureka:
client:
registerWithEureka: true
fetchRegistry: true
serverUrl:
defaultZone: http://127.0.0.1:8761/eureka
instance:
#心跳间隔
leaseRenewalIntervalInSeconds: 10
@RequestMapping("/sms")
@RestController
public class SmsController {
@Autowired
MessageService msgService; @RequestMapping("/getPerson")
public PersonVo getPerson(String phoneNumber){
PersonVo person = new PersonVo();
person.setName("张三");
person.setAge(20);
person.setSex('男');
person.setNativePlace("山东菏泽");
person.setIdentityCardId("37132519900809244x");
person.setPhoneNumber("13100001111"); System.out.println("sms收到的phoneNumber : "+phoneNumber);
return person;
}
}
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
spring:
application:
name: feign-demo
info:
name: cloud feign-demo
server:
port: 8080 eureka:
client:
register-with-eureka: true
fetch-registry: true
serviceUrl:
defaultZone: http://127.0.0.1:8761/eureka/ feign:
hystrix:
enable: false
//暂无熔断,因此无fallback回调
@FeignClient(name = "sms-module") //name对应的是服务名称
public interface FeignSmsClient { //注意,这里是接口
@RequestMapping(value = "/sms/getPerson") //类似于controller中的路径映射,但这不是controller,而是请求的发起端
PersonVo queryPerson(@RequestParam("phoneNumber") String phoneNumber); //普通的参数,如果是uri中的参数,应该用@PathParam,post请求还有@Body模板
}
@RequestMapping("/demo")
@RestController
public class TestClientController {
@Autowired
FeignSmsClient feignClient; @RequestMapping("/test2")
public PersonVo myGetPerson2(){
PersonVo personVo = null;
personVo = feignClient.queryPerson("15611273879");
return personVo;
}
}

sms-module: #服务名
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule #负载策略配置
public class MyBalance {
@Bean
public IRule ribbonRule(){
System.out.println("------------------ribbon Rule -------------");
return new RandomRule();
}
}
@RibbonClients(value = {
@RibbonClient(name="sms-module",configuration = MyBalance.class),
@RibbonClient(name="consumer-module",configuration = MyBalance2.class)
})
@RequestMapping("/demo")
@RestController
public class TestClientController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private LoadBalancerClient loadBalancerClient; @RequestMapping("/test")
public PersonVo myGetPerson(){
ServiceInstance instance = loadBalancerClient.choose("sms-module");
System.out.println("本次执行的实例是:"+instance.getHost()+":"+instance.getPort()); PersonVo personVo = null;
personVo = restTemplate.getForObject("http://sms-module/sms/getPerson?phoneNumber=17301394307",PersonVo.class); //注意这里http://后跟的是服务名,而不是实例的ip+port
return personVo;
} @Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}

服务间调用--feign跟ribbon的更多相关文章
- spring cloud服务间调用feign
参考文章:Spring Cloud Feign设计原理 1.feign是spring cloud服务间相互调用的组件,声明式.模板化的HTTP客户端.类似的HttpURLConnection.Apac ...
- SpringCloud初体验:三、Feign 服务间调用(FeignClient)、负载均衡(Ribbon)、容错/降级处理(Hystrix)
FeignOpenFeign Feign是一种声明式.模板化的HTTP客户端. 看了解释过后,可以理解为他是一种 客户端 配置实现的策略,它实现 服务间调用(FeignClient).负载均衡(Rib ...
- ②SpringCloud 实战:引入Feign组件,完善服务间调用
这是SpringCloud实战系列中第二篇文章,了解前面第一篇文章更有助于更好理解本文内容: ①SpringCloud 实战:引入Eureka组件,完善服务治理 简介 Feign 是一个声明式的 RE ...
- 小D课堂 - 新版本微服务springcloud+Docker教程_4-01 常用的服务间调用方式讲解
笔记 第四章 服务消费者ribbon和feign实战和注册中心高可用 1.常用的服务间调用方式讲解 简介:讲解常用的服务间的调用方式 RPC: 远程过程调用,像调用本地 ...
- 小D课堂 - 新版本微服务springcloud+Docker教程_4-04 高级篇幅之服务间调用之负载均衡策略调整实战
笔记 4.高级篇幅之服务间调用之负载均衡策略调整实战 简介:实战调整默认负载均衡策略实战 自定义负载均衡策略:http://cloud.spring.io/spring-cloud-stati ...
- SpringCloud微服务服务间调用之OpenFeign介绍
开发微服务,免不了需要服务间调用.Spring Cloud框架提供了RestTemplate和FeignClient两个方式完成服务间调用,本文简要介绍如何使用OpenFeign完成服务间调用. Op ...
- Asp.Net Core使用SignalR进行服务间调用
网上查询过很多关于ASP.NET core使用SignalR的简单例子,但是大部分都是简易聊天功能,今天心血来潮就搞了个使用SignalR进行服务间调用的简单DEMO. 至于SignalR是什么我就不 ...
- SpringCloud实现服务间调用(RestTemplate方式)
上一篇文章<SpringCloud搭建注册中心与服务注册>介绍了注册中心的搭建和服务的注册,本文将介绍下服务消费者调用服务提供者的过程. 本文目录 一.服务调用流程二.服务提供者三.服务消 ...
- 基于gin的golang web开发:服务间调用
微服务开发中服务间调用的主流方式有两种HTTP.RPC,HTTP相对来说比较简单.本文将使用 Resty 包来实现基于HTTP的微服务调用. Resty简介 Resty 是一个简单的HTTP和REST ...
随机推荐
- [转]关于截取字符串substr和substring两者的区别
subString(start,stop) substr(start,length) substr和substring两个都是截取字符串的. 两者有相同点,如果只是写一个参数,两者的作用都是一样的:就 ...
- Net.Core导入EXCel文件里的数据
1.前台的表单: <form enctype="multipart/form-data" method="post" id="inportFil ...
- shell脚本实现自动保留最近n次备份记录
项目中出现的问题 某天上午服务器出现卡顿特别严重,页面加载速度奇慢,并且某些页面刷新出现404的问题,就连服务器的tab命令的自动提示都出现了问题,楼主费了九牛二虎之力,根据服务器排查发现,服务器数据 ...
- c语言参考书籍
很惭愧没能把c++学的很好,毕竟离开始工作只有2年时间,对自己要求不要过高,慢慢来吧.话说知道自己的不足,以后要更加抓紧了!fighting~ 现在计划着把c语言给学习一下了,当然这次指的是深入地学习 ...
- P2723 丑数 Humble Numbers
题意:给你k个质数,定义丑数集合为k个质数随机(1--k)个相乘得到的数 求第n小的丑数 暴力...貌似不太可行,(把所有大量丑数求出来,sort QAQ) 可以想到,对于第i个丑数f[i],它一 ...
- js 遍历tree的一个例子(全遍历),更复杂的功能
更复杂的功能 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...
- js 联动下拉菜单
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- C语言中的常用函数_持续更新
isspace函数: 背景:之前遇到scanf()输入时会把换行符留在输入队列的情况,如果下次要用到getchar(),但是会导致其先返回这个我们不需要的换行符:从而导致不希望出现的行为: 说明:检查 ...
- 互联网开发-web文件上传性能问题
1. 问题描述 文件大小 部署环境 平均上传速度 5M 外网 28s-36s 5M 公司局域内网 秒传,很快 2. 问题分析 在网上搜索“测速网”测试了一下公司外网的带宽情况: 上传带宽 = 1.04 ...
- HMAC算法加密
/** * HMAC算法加密 * @param message 待加密信息 * @param key 密钥 * @return */ public static String HmacSHA256(b ...