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 ...
随机推荐
- HBase电子书
HBase 不睡觉书 https://pan.baidu.com/s/1d4u7pPAu_B3sW5w9x1ARdA HBase2018年年度总结 https://pan.baidu.com/s/1 ...
- 自定义chromium浏览器
自定义chromium浏览器 来源 https://chaopeng.me/blog/2018/08/17/how-to-develop-full-homebrew-browser.html 最近有 ...
- 「POJ-3608」Bridge Across Islands (旋转卡壳--求两凸包距离)
题目链接 POJ-3608 Bridge Across Islands 题意 依次按逆时针方向给出凸包,在两个凸包小岛之间造桥,求最小距离. 题解 旋转卡壳的应用之一:求两凸包的最近距离. 找到凸包 ...
- Android undefined intent constructor错误?
本文选自StackOverflow(简称:SOF)精选问答汇总系列文章之一,本系列文章将为读者分享国外最优质的精彩问与答,供读者学习和了解国外最新技术.在Android中启动Service时出现&qu ...
- vxlan和vlan数据报文
802.1Q标准的以太网帧格式增加了802.1Q字段,该字段包含了Type.PRI.CFI和VID 4个部分,各个部分的含义如下: ·Type:长度为2 bytes,表示帧类型,802.1Q tag帧 ...
- 使用指针来实现变长数组(VLA)
实现代码: #include <cstdio> #include <cstdlib> void usePtoImplementVLA(int SIZE) { scanf(&qu ...
- JQ动态生成的元素,原事件绑定失效
Old Code: $('code').click(function () { console.log($(this).text()); }); New Code:(.container 是<c ...
- electron入门笔记(三)- 引入bootstrap
源码:https://github.com/sueRimn/electron-bootstrap 当引入jQuery和bootstrap文件时,会报错,原因是:electron 的 Renderer ...
- Django(十六)Form组件扩展
http://www.cnblogs.com/wupeiqi/articles/6144178.html Form组件 - form表单(验证:保留上次内容) - - Ajax(验证:无需上次内容) ...
- Python基础语法总结
1.Python标识符 在 Python 里,标识符有字母.数字.下划线组成. 在 Python 中,所有标识符可以包括英文.数字以及下划线(_),但不能以数字开头. Python 中的标识符是区分大 ...