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学习(二) 调用服务的更多相关文章

  1. spring cloud (二、服务注册安全demo_eureka)

    spring cloud (一.服务注册demo_eureka) 加强服务的安全性,我们接下来加上访问的账号密码: 首先需要添加对应的依赖 <!--账号密码认证依赖--> <depe ...

  2. spring cloud 学习(4) - hystrix 服务熔断处理

    hystrix 是一个专用于服务熔断处理的开源项目,当依赖的服务方出现故障不可用时,hystrix有一个所谓的断路器,一但打开,就会直接拦截掉对故障服务的调用,从而防止故障进一步扩大(类似中电路中的跳 ...

  3. spring cloud 学习(二)关于 Eureka 的学习笔记

    关于 Eureka 的学习笔记 个人博客地址 : https://zggdczfr.cn/ ,欢迎光临~ 前言 Eureka是Netflix开发的服务发现组件,本身是一个基于REST的服务.Sprin ...

  4. spring cloud学习一--Eureka服务注册与发现

    spring cloud Eureka是基于Netflix Eureka服务发现注册产品的二次封装,它提供了服务注册功能(Service Registry)和服务发现功能(Service Discov ...

  5. Spring Cloud 学习 (二) Ribbon

    负载均衡是指将负载分摊到多个执行单元上,常见的负载均衡有两种方式:一种是独立进程单元,通过负载均衡策略,将请求转发到不同的执行单元上,例如 Ngnix:另一种是将负载均衡逻辑以代码的形式封装到服务消费 ...

  6. Spring Cloud 使用Feign调用服务传递Header中的参数

    1.使用Feign 调用其他微服务,尤其是在多级调用的同时,需要将一些共同的参数传递至下一个服务,如:token.比较方便的做法是放在请求头中,在Feign调用的同时自动将参数放到restTempla ...

  7. spring cloud (四、服务消费者demo_consumer)

    spring cloud (一.服务注册demo_eureka) spring cloud (二.服务注册安全demo_eureka) spring cloud (三.服务提供者demo_provid ...

  8. spring cloud 学习(9) - turbine stream无法在eureka注册的解决办法

    turbine是啥就不多解释了,初次接触的可以移步spring cloud 学习(4) - hystrix 服务熔断处理 拉到最后看一下,turbine stream默认情况下启动成功后,eureka ...

  9. spring cloud 学习之服务消费者(rest+ribbon)

    学习自 http://blog.csdn.net/forezp/article/details/81040946 方志朋的博客 在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于h ...

随机推荐

  1. 2019.01.22 poj2926 Requirements(状态压缩)

    传送门 题意:给一堆五维的点,求最远点对. 思路:跟CF1093G差不多 考虑把正负号状压成一个323232以内的数,然后对于每一类分别求最大最小值再做差更新答案即可. 代码: #include< ...

  2. poj-3177(并查集+双联通分量+Tarjan算法)

    题目链接:传送门 思路: 题目要将使每一对草场之间都有至少两条相互分离的路径,所以转化为(一个有桥的连通图至少加几条边才能变为双联通图?) 先求出所有的桥的个数,同时将不同区块收缩成一个点(利用并查集 ...

  3. (13)How to stay calm when you know you'll be stressed

    https://www.ted.com/talks/daniel_levitin_how_to_stay_calm_when_you_know_you_ll_be_stressed/transcrip ...

  4. Codeforces Round #543 (Div. 2) D 双指针 + 模拟

    https://codeforces.com/contest/1121/problem/D 题意 给你一个m(<=5e5)个数的序列,选择删除某些数,使得剩下的数按每组k个数以此分成n组(n*k ...

  5. FontAwesome 4.7.0 中完整的675个图标样式CSS参考

    FontAwesome 4.7.0 中完整的675个图标样式CSS参考 用法:首先引入CSS文件:<link href="https://maxcdn.bootstrapcdn.com ...

  6. Java 获取最近时间

    public static String getStatetime() throws ParseException{ SimpleDateFormat sdf = new SimpleDateForm ...

  7. tp5,thinkphp5,隐藏index.php,隐藏入口文件

    一.找到/public/.htaccess文件 Apache: <IfModule mod_rewrite.c> Options +FollowSymlinks -Multiviews R ...

  8. vue2.0路由-路由嵌套

    vue一个重要的方面就是路由,下面是自己写的一个路由的例子: 1.引入依赖库就不必再说 2.创建组件 两种写法 第一种:间接 <template id="home"> ...

  9. spring boot和mybatis入门

    [size=x-large]昨天讲了一下spring boot的入门操作相信老手已经明白入门的操作,今天我来讲下我自己的心得,可能与官方有一定差异:希望对大家能有用 一:开门见山首先看工程结构 这里的 ...

  10. canvas画的时钟

    结合几天来学习的canvas的API,终于完成了一个时钟呵呵 html <!doctype html> <html> <head> <meta charset ...