springcloud 服务调用的两种方式
spring-cloud调用服务有两种方式,一种是Ribbon+RestTemplate, 另外一种是Feign。
Ribbon是一个基于HTTP和TCP客户端的负载均衡器,其实feign也使用了ribbon, 只要使用@FeignClient时,ribbon就会自动使用。
一、Ribbon
1.1
新建模块client-a
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/xsd/maven-4.0.0.xsd">
- <parent>
- <artifactId>spring-cloud</artifactId>
- <groupId>com.feng</groupId>
- <version>0.0.</version>
- </parent>
- <modelVersion>4.0.</modelVersion>
<artifactId>client-a</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
</dependencies>
</project>

新建bootstrap.yml

- server:
- port:
- eureka:
- client:
- serviceUrl:
- defaultZone: http://localhost:8010/eureka/
- spring:
- application:
- name: client-a

ClientApplication, 这里我们需要注册一个RestTemplate,并且使用@LoadBalanced开启负载功能

- /**
- * @author fengzp
- * @date 17/5/9
- * @email fengzp@gzyitop.com
- * @company 广州易站通计算机科技有限公司
- */
- @SpringBootApplication
- @EnableDiscoveryClient
- public class ClientApplication {
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> main(String[] args) {
SpringApplication.run(ClientApplication.</span><span style="color: #0000ff;">class</span><span style="color: #000000;">, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate(){
</span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span><span style="color: #000000;"> RestTemplate();
}
}

测试用的controller

- /**
- * @author fengzp
- * @date 17/5/9
- * @email fengzp@gzyitop.com
- * @company 广州易站通计算机科技有限公司
- */
- @RestController
- public class TestController {
@Autowired
RestTemplate restTemplate;
@RequestMapping(</span><span style="color: #800000;">"</span><span style="color: #800000;">/hi</span><span style="color: #800000;">"</span><span style="color: #000000;">)
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String hi(@RequestParam String id){
</span><span style="color: #0000ff;">return</span> restTemplate.getForObject(<span style="color: #800000;">"</span><span style="color: #800000;">http://service-a/hi?id=</span><span style="color: #800000;">"</span>+id, String.<span style="color: #0000ff;">class</span><span style="color: #000000;">);
}
}

1.2
为了测试负载功能,这里要再新建一个模块service-b, 和上一篇的service-a的代码基本相同,只把端口修改了就可以。
把client-a和service-b都启动成功后,打开eureka中心应该看到:
1.3
打开http://localhost:8910/hi?id=123
可以看到服务已经成功调用。
然后刷新页面
看到端口已经改变,说明负载功能成功实现
二、feign
2.1
新建模块client-b
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/xsd/maven-4.0.0.xsd">
- <parent>
- <artifactId>spring-cloud</artifactId>
- <groupId>com.feng</groupId>
- <version>0.0.</version>
- </parent>
- <modelVersion>4.0.</modelVersion>
<artifactId>client-b</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
</dependencies>
</project>

bootstrap.yml

- server:
- port:
- eureka:
- client:
- serviceUrl:
- defaultZone: http://localhost:8010/eureka/
- spring:
- application:
- name: client-b

ClientApplication, 使用@EnableFeignClients开启feiginClient功能

- /**
- * @author fengzp
- * @date 17/5/9
- * @email fengzp@gzyitop.com
- * @company 广州易站通计算机科技有限公司
- */
- @SpringBootApplication
- @EnableDiscoveryClient
- @EnableFeignClients
- public class ClientApplication {
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> main(String[] args) {
SpringApplication.run(ClientApplication.</span><span style="color: #0000ff;">class</span><span style="color: #000000;">, args);
}
}

这里新建一个ServiceAFeignClient来调用service-a服务

- @Component
- @FeignClient(value = "service-a") //这里的name对应调用服务的spring.applicatoin.name
- public interface ServiceAFeignClient {
@RequestMapping(value </span>= <span style="color: #800000;">"</span><span style="color: #800000;">/hi</span><span style="color: #800000;">"</span><span style="color: #000000;">)
String hi(@RequestParam(</span><span style="color: #800000;">"</span><span style="color: #800000;">id</span><span style="color: #800000;">"</span><span style="color: #000000;">) String id);
}

Controller

- @RestController
- public class TestController {
@Autowired
ServiceAFeignClient serviceAFeignClient;
@RequestMapping(</span><span style="color: #800000;">"</span><span style="color: #800000;">/hi</span><span style="color: #800000;">"</span><span style="color: #000000;">)
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String hi(@RequestParam String id){
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> serviceAFeignClient.hi(id);
}
}

2.2
运行后的结果应该是和ribbon的相同。
个人感觉使用feign比较舒服,代码比较简洁。
springcloud 服务调用的两种方式的更多相关文章
- DLL调用的两种方式(IDE:VC6.0,C++)
原文:http://www.cnblogs.com/Pickuper/articles/2050409.html DLL调用有两种方式,一种是静态调用,另外一种是动态调用 (一)静态调用 静态调用是一 ...
- 服务容错保护断路器Hystrix之一:入门示例介绍(springcloud引入Hystrix的两种方式)
限流知识<高可用服务设计之二:Rate limiting 限流与降级> 在微服务架构中,我们将系统拆分成了一个个的服务单元,各单元间通过服务注册与订阅的方式互相依赖.由于每个单元都在不同的 ...
- 使用RestTemplate进行服务调用的几种方式
首先我们在名为MSG的服务中定义一个简单的方法 @RestController public class ServerController { @GetMapping("/msg" ...
- JAVA客户端API调用memcached两种方式
1. memcached client for java客户端API:memcached client for java 引入jar包:java-memcached-2.6.2.jar package ...
- js对象中属性调用.和[] 两种方式的区别
JS 调用属性一般有两种方法——点和中括号的方法. 标准格式是对象.属性(不带双引号),注意一点的是:js对象的属性,key标准是不用加引号的,加也可以,特别的情况必须加,如果key数字啊,表达式啊等 ...
- Linux 服务管理的两种方式service和systemctl
service命令 service命令其实是去/etc/init.d目录下,去执行相关程序 ``` # service命令启动redis脚本 service redis start # 直接启动red ...
- Svn服务启动的两种方式
一.svn服务器启动 cmd命令行启动:vsvnserve -d –r 文档仓库路径 -d 后台执行 -r 版本库的根目录 二.Windows服务自动启动 利用xp.2000 以上的 ...
- Service 服务发现的两种方式-通过案例来理解+服务外部访问类型+selector-label
1.环境变量 在创建一个Pod时,kubelet在该Pod的所有容器中为当前所有Service添加一系列环境变量. 例如,已存在名称为“redis-master”的Service,它对外暴露6379的 ...
- WCF 客户端调用服务操作的两种方法
本节的主要内容:1.通过代理类的方式调用服务操作.2.通过通道的方式调用服务操作.3.代码下载 一.通过代理类的方式调用服务操作(两种方式添加代理类) 1.手动编写代理类,如下: 客户端契约: usi ...
随机推荐
- 使用 dmidecode 查看Linux服务器信息
使用 dmidecode 查看Linux服务器信息 来源 http://www.laozuo.org/6682.html 对于大部分普通服务器用户来说,我们选择VPS.服务器产品的时候比较关心的是产 ...
- 爬虫_微信小程序社区教程(crawlspider)
照着敲了一遍,,, 需要使用"LinkExtrator"和"Rule",这两个东西决定爬虫的走向. 1.allow设置规则的方法:要能够限制在我们想要的url上 ...
- 爬虫_豆瓣电影top250 (正则表达式)
一样的套路,就是多线程还没弄 import requests import re import json headers = 'Mozilla/5.0 (Windows NT 10.0; WOW64) ...
- Android studio preview界面无法预览,报错render problem
1.查看报错信息,如果有报错,该叹号应为红色,点击查看报错,显示为render problem 2.打开res/styles.xml修改为如图,添加Base. 3.再打开preview界面
- 洛谷SP22343 NORMA2 - Norma(分治,前缀和)
洛谷题目传送门 这题推式子恶心..... 考虑分治,每次统计跨过\(mid\)的所有区间的答案和.\(i\)从\(mid-1\)到\(l\)枚举,统计以\(i\)为左端点的所有区间. 我们先维护好\( ...
- 【CF809D】Hitchhiking in the Baltic States(Splay,动态规划)
[CF809D]Hitchhiking in the Baltic States(Splay,动态规划) 题面 CF 洛谷 题解 朴素\(dp\):设\(f[i][j]\)表示当前考虑到第\(i\)个 ...
- table用模板生成的问题
在使用<template></template>存放HTML模板标记时,发现一个烦人的问题,表格不行. <template> <table> <t ...
- 牛客寒假算法基础集训营3B 处女座的比赛资格(用拓扑排序解决DAG中的最短路)
链接:https://ac.nowcoder.com/acm/contest/329/B 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言5242 ...
- 论一个蒟蒻的脑子里可以有多少坑(貌似咕了……目前更新保持在noip阶段)
就是错题整理了,其实也会把一些不该犯的失误整进来. 其实之前一直拖着不想写,直到某次模拟赛,看错了2道题,顺便爆了一道题的int(没错第一个点就会爆)之后爆零了,吓得我赶紧把这篇博客搞出来了..... ...
- SRM 605 div 2 T3
#include <bits/stdc++.h> #define Mo 1000000007 #define MAXN 50 #define MAXK 10 using namespace ...