spring cloud学习(二) 调用服务
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.1</version>
</parent>
<modelVersion>4.0.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: 8910
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 {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
}
测试用的controller
/**
* @author fengzp
* @date 17/5/9
* @email fengzp@gzyitop.com
* @company 广州易站通计算机科技有限公司
*/
@RestController
public class TestController {
@Autowired
RestTemplate restTemplate;
@RequestMapping("/hi")
public String hi(@RequestParam String id){
return restTemplate.getForObject("http://service-a/hi?id="+id, String.class);
}
}
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.1</version>
</parent>
<modelVersion>4.0.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: 8911
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 {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
}
这里新建一个ServiceAFeignClient来调用service-a服务
@Component
@FeignClient(value = "service-a") //这里的name对应调用服务的spring.applicatoin.name
public interface ServiceAFeignClient {
@RequestMapping(value = "/hi")
String hi(@RequestParam("id") String id);
}
Controller
@RestController
public class TestController {
@Autowired
ServiceAFeignClient serviceAFeignClient;
@RequestMapping("/hi")
public String hi(@RequestParam String id){
return serviceAFeignClient.hi(id);
}
}
2.2
运行后的结果应该是和ribbon的相同。
个人感觉使用feign比较舒服,代码比较简洁。
spring cloud学习(二) 调用服务的更多相关文章
- spring cloud (二、服务注册安全demo_eureka)
spring cloud (一.服务注册demo_eureka) 加强服务的安全性,我们接下来加上访问的账号密码: 首先需要添加对应的依赖 <!--账号密码认证依赖--> <depe ...
- spring cloud 学习(4) - hystrix 服务熔断处理
hystrix 是一个专用于服务熔断处理的开源项目,当依赖的服务方出现故障不可用时,hystrix有一个所谓的断路器,一但打开,就会直接拦截掉对故障服务的调用,从而防止故障进一步扩大(类似中电路中的跳 ...
- spring cloud 学习(二)关于 Eureka 的学习笔记
关于 Eureka 的学习笔记 个人博客地址 : https://zggdczfr.cn/ ,欢迎光临~ 前言 Eureka是Netflix开发的服务发现组件,本身是一个基于REST的服务.Sprin ...
- spring cloud学习一--Eureka服务注册与发现
spring cloud Eureka是基于Netflix Eureka服务发现注册产品的二次封装,它提供了服务注册功能(Service Registry)和服务发现功能(Service Discov ...
- Spring Cloud 学习 (二) Ribbon
负载均衡是指将负载分摊到多个执行单元上,常见的负载均衡有两种方式:一种是独立进程单元,通过负载均衡策略,将请求转发到不同的执行单元上,例如 Ngnix:另一种是将负载均衡逻辑以代码的形式封装到服务消费 ...
- Spring Cloud 使用Feign调用服务传递Header中的参数
1.使用Feign 调用其他微服务,尤其是在多级调用的同时,需要将一些共同的参数传递至下一个服务,如:token.比较方便的做法是放在请求头中,在Feign调用的同时自动将参数放到restTempla ...
- spring cloud (四、服务消费者demo_consumer)
spring cloud (一.服务注册demo_eureka) spring cloud (二.服务注册安全demo_eureka) spring cloud (三.服务提供者demo_provid ...
- spring cloud 学习(9) - turbine stream无法在eureka注册的解决办法
turbine是啥就不多解释了,初次接触的可以移步spring cloud 学习(4) - hystrix 服务熔断处理 拉到最后看一下,turbine stream默认情况下启动成功后,eureka ...
- spring cloud 学习之服务消费者(rest+ribbon)
学习自 http://blog.csdn.net/forezp/article/details/81040946 方志朋的博客 在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于h ...
随机推荐
- 使用 kbmmw 的ORM开发纯REST数据库访问服务
运行环境: WIN 10 X64 delphi 10.2.2 kbmmw 5.05.11 Firefox 58.0.2 今天使用最新的kbmmw 版本做一个基于ORM的纯数据库访问的REST 服务器 ...
- windows下mongodb安装与使用图文教程(整理)
一.首先安装mongodb 1.下载地址:http://www.mongodb.org/downloads 2.解压缩到自己想要安装的目录,比如d:\mongodb 3.创建文件夹d:\mongodb ...
- Educational Codeforces Round 51 F. The Shortest Statement(lca+最短路)
https://codeforces.com/contest/1051/problem/F 题意 给一个带权联通无向图,n个点,m条边,q个询问,询问两点之间的最短路 其中 m-n<=20,1& ...
- openstack之镜像管理
概览 [root@cc07 fast-pulsar]# glance help | grep image [--os-image-url OS_IMAGE_URL] [--os-image-api-v ...
- etf基金和lof基金区别
①,含义不同.etf即交易指数开放基金,是跟踪某一指数的可以在交易所上市的开放式基金.lof基金是上市向开放基金,是中国首创的一种基金类型,也是etf基金的中国化.②,申购赎回的场所不同.etf和lo ...
- python,函数
numpy.tile():参考https://www.jianshu.com/p/4b74a367833c numpy.argsort:argsort()里面传入参数只有数组时,返回的是数组值从小到大 ...
- maven使用中遇到的问题
一>手动将jar包安装到仓库的命令示例: 首先:编写命令:mvn install:install-file -Dfile=D:\lucene-highlighter-4.10.2.jar -Dg ...
- chmod用法
以下是chmod的详细用法:chmod命令用于改变linux系统文件或目录的访问权限.用它控制文件或目录的访问权限.该命令有两种用法.一种是包含字母和操作符表达式的文字设定法:另一种是包含数字的数字设 ...
- 学以致用十二-----YouCompeteMe巨坑
接上一篇,通过这几天的不断尝试,发现一个无法解决的问题.至于我安装成功的那台,我至今不知道是安装了哪一步导致成功的. 首先,我在.vimrc里加上了 Plugin 'Valloric/YouComp ...
- AngularJS实战之filter的使用一
一.格式化数字为货币格式. <div>{{money|currency:"$"}}</div> <div>{{money|currency:&q ...