第一篇讲了服务的注册,这篇来说说服务的调用,服务与服务的通讯是基于http restful,springcloud的服务调用是通过ribbon方式的,客户端的负载均衡。

Talk is cheap.Show me your code.

上一篇,建立了一个eureka server, 一个eureka client,client的注册名是service-hi,用来提供服务。下面来看看如何消费服务:

一、启动服务。

  1、启动eureka server工程。

  2、启动eureka client工程,端口号8762.

  3、修改eureka client工程的application.yml,将端口号改为8763.启动。

  这样就相当于启动了一个小的服务集群,service-hi集群。当访问http://localhost:8761时会看到

注册了2个服务,一个端口号8762,一个8763.下面构建ribbon工程消费服务。

二、建一个服务消费者

  ctrl+n创建一个maven工程,取名叫eureka-service-ribbon,打jar包。

  pom.xml如下:  

  1. <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">
  2. <modelVersion>4.0.0</modelVersion>
  3. <groupId>com.sun</groupId>
  4. <artifactId>eureka-service-ribbon</artifactId>
  5. <version>0.0.1-SNAPSHOT</version>
  6. <packaging>jar</packaging>
  7.  
  8. <name>eureka-service-ribbon</name>
  9. <description>Demo project for Spring Boot</description>
  10.  
  11. <parent>
  12. <groupId>com.sun</groupId>
  13. <artifactId>springcloud-parent</artifactId>
  14. <version>0.0.1-SNAPSHOT</version>
  15. </parent>
  16.  
  17. <dependencies>
  18. <dependency>
  19. <groupId>org.springframework.cloud</groupId>
  20. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-web</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.cloud</groupId>
  28. <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
  29. </dependency>
  30. </dependencies>
  31. </project>

  将eureka-service-ribbon注册到服务中心(eureka-server)。application.yml如下:

  1. eureka:
  2. client:
  3. serviceUrl:
  4. defaultZone: http://localhost:8761/eureka/
  5. server:
  6. port: 8764
  7. spring:
  8. application:
  9. name: service-ribbon

在eureka-service-ribbon启动类中,通过@EnableEurekaClient向服务中心注册;并且向程序的ioc注入一个bean: restTemplate;通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能。

启动类代码如下:

  1. package com.sun;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.cloud.client.loadbalancer.LoadBalanced;
  6. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.web.client.RestTemplate;
  9.  
  10. @SpringBootApplication
  11. @EnableEurekaClient
  12. public class ServiceRibbonApplication {
  13.  
  14. public static void main(String[] args) {
  15. SpringApplication.run( ServiceRibbonApplication.class, args );
  16. }
  17.  
  18. @Bean
  19. @LoadBalanced
  20. RestTemplate restTemplate() {
  21. return new RestTemplate();
  22. }
  23.  
  24. }

这里再提醒一下,启动类需要放到包的根下才会自动扫描。

写一个测试类HelloService,通过之前注入ioc容器的restTemplate来消费service-hi服务的“/hi”接口,在这里我们直接用的服务名替代了具体的url地址,

在ribbon中它会根据服务名来选择具体的服务实例,根据服务实例在请求的时候会用具体的url替换掉服务名,代码如下:

  1. package com.sun;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Service;
  5. import org.springframework.web.client.RestTemplate;
  6.  
  7. @Service
  8. public class HelloService {
  9.  
  10. @Autowired
  11. RestTemplate restTemplate;
  12.  
  13. public String hiService(String name) {
  14. return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
  15. }
  16.  
  17. }

写一个controller,在controller中用调用HelloService 的方法,代码如下:

  1. package com.sun;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.RequestParam;
  6. import org.springframework.web.bind.annotation.RestController;
  7.  
  8. @RestController
  9. public class HelloControler {
  10.  
  11. @Autowired
  12. HelloService helloService;
  13.  
  14. @GetMapping(value = "/hi")
  15. public String hi(@RequestParam String name) {
  16. return helloService.hiService( name );
  17. }
  18. }

在浏览器上多次访问http://localhost:8764/hi?name=sun,浏览器交替显示:

hi sun ,i am from port:8762

hi sun ,i am from port:8763

说明访问controller时,RestTemplate调用了服务,做了负载均衡,访问了不同的端口的服务实例。

  • 一个服务注册中心,eureka server,端口为8761
  • service-hi工程跑了两个实例,端口分别为8762,8763,分别向服务注册中心注册
  • sercvice-ribbon端口为8764,向服务注册中心注册
  • 当sercvice-ribbon通过restTemplate调用service-hi的hi接口时,因为用ribbon进行了负载均衡,会轮流的调用service-hi:8762和8763 两个端口的hi接口;

参考博客:https://blog.csdn.net/forezp/article/details/81040946

第二篇:服务消费者(RestTemplate+ribbon)的更多相关文章

  1. SpringCloud教程 | 第二篇: 服务消费者(rest+ribbon)

    在上一篇文章,讲了服务的注册和发现.在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的.Spring cloud有两种服务调用方式,一种是ribbon+r ...

  2. 史上最简单的SpringCloud教程 | 第二篇: 服务消费者(rest+ribbon)

    在上一篇文章,讲了服务的注册和发现.在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的.Spring cloud有两种服务调用方式,一种是ribbon+r ...

  3. SpringCloud教程 | 第二篇: 服务消费者(rest+ribbon)(Finchley版本)

    在上一篇文章,讲了服务的注册和发现.在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的.Spring cloud有两种服务调用方式,一种是ribbon+r ...

  4. 史上最简单的SpringCloud教程 | 第二篇: 服务消费者(rest+ribbon)(Finchley版本)

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f2-ribbon/ 本文出自方志朋的博客 在上一篇文章,讲了 ...

  5. 【SpringCloud】第二篇: 服务消费者(rest+ribbon)

    前言: 必需学会SpringBoot基础知识 简介: spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选. ...

  6. 服务消费者(RestTemplate+Ribbon+feign)

    负载均衡 ​ spring cloud 体系中,我们知道服务之间的调用是通过http协议进行调用的.注册中心就是维护这些调用的服务的各个服务列表.在Spring中提供了RestTemplate,用于访 ...

  7. Spring Cloud学习笔记【二】Eureka 服务提供者/服务消费者(ribbon)

    Ribbon 是 Netflix 发布的开源项目,主要功能是为 REST 客户端实现负载均衡.它主要包括六个组件: ServerList,负载均衡使用的服务器列表.这个列表会缓存在负载均衡器中,并定期 ...

  8. springcloud干货之服务消费者(ribbon)

    本章介绍springcloud中的服务消费者 springcloud服务调用方式有两种实现方式: 1,restTemplate+ribbon, 2,feign 本来想一篇讲完,发现篇幅有点长,所以本章 ...

  9. SpringCloud教程 | 第三篇: 服务消费者(Feign)

    上一篇文章,讲述了如何通过RestTemplate+Ribbon去消费服务,这篇文章主要讲述如何通过Feign去消费服务.一.Feign简介 Feign是一个声明式的伪Http客户端,它使得写Http ...

  10. 第三篇: 服务消费者(Feign)

    本文根据https://blog.csdn.net/forezp/article/details/81040965写出,修正了部分瑕疵,在此对那位博主表示感谢. 上一篇文章讲述通过RestTempla ...

随机推荐

  1. scrapy使用MongoDB简单示例

    1.下载安装MongoDBhttps://www.mongodb.com/download-center#community找到合适的版本下载,安装.安装好之后,找到安装目录下D:\Program F ...

  2. day37 异步回调和协程

    异步回调 """ 异步任务使用场景 爬虫 1.从目标站点下载网页数据 本质就是HTML格式字符串 2.用re从字符串中提取出你需要的数据 ""&quo ...

  3. vue 总结

    VUE总结 双花括号{{}} 01.index.hmlt main.js 内存的数据可以更改 v-model 双休数据绑定 代码: <!DOCTYPE html> <html lan ...

  4. POST application/json 适用于传递多层的json

    本来以为自己写了那么多post请求,ajax已经难不住了呢, 结果现实无比的残酷, 后台换成了java,发多层级的json,后台就取不到了, 虽然到最后还是配置正确了,..记录下来,引以为戒, axi ...

  5. 22. pt-sift

    pt-sift /var/lib/pt-stalk/ ======== server01 at 2018_11_23_15_56_46 DEFAULT (1 of 1) ========--disks ...

  6. maven 项目中没有src/test/java文件夹

    项目右键->buildPath configure Build Path->点击选项卡Libraries->选中JRE System Library->点击edit->选 ...

  7. You are using pip version 9.0.1, however version 9.0.3 is available.

    1,pip不能用了,提示:You are using pip version 8.1.1, however version 9.0.1 is available.网上搜索了一箩筐的安装程序,各种安装, ...

  8. java中random的几个方法的使用Math.random()和random().

    random java中我们有时候也需要使用使用random来产生随机数,下面我来简单的介绍下java中random的使用方法 第一种:Math.random() public static doub ...

  9. Python从入门到精通之First!

    Python的种类 Cpython Python的官方版本,使用C语言实现,使用最为广泛,CPython实现会将源文件(py文件)转换成字节码文件(pyc文件),然后运行在Python虚拟机上. Jy ...

  10. 更改h标签的字体粗细

    h1,h2,h3,h4,h5,h6{ font-weight:normal }