SpringCloud教程- 服务消费者(rest+ribbon)(SpringCloud版本Finchley)
文章目录
- 一、ribbon介绍
- 二、启动两个实例
- 三、建一个服务消费者(ribbon)
- 四、 Ribbon调用总结
代码地址:github-spring-cloud地址
前言:在上一篇文章,讲了服务的注册和发现。在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http
restful的。Spring
cloud有两种服务调用方式,一种是ribbon+restTemplate,另一种是feign。在这一篇文章首先讲解下ribbon+rest。
一、ribbon介绍
官方文档简介
Ribbon is a client side load balancer which gives you a lot of control over the behaviour of HTTP and TCP clients. Feign already uses Ribbon, so if you are using @FeignClient then this section also applies.
Ribbon是客户端负载平衡器,它使您可以对HTTP和TCP客户端的行为进行大量控制。
Feign已经使用了Ribbon,因此如果您使用@FeignClient,则本节也适用。
ribbon是一个负载均衡客户端,可以很好的控制htt和tcp的一些行为。Feign默认集成了ribbon。
二、启动两个实例
这篇文章基于上一篇文章启动eureka-server 工程;启动service-hello工程,它的端口为8081;将service-hello的配置文件的端口改为8083,并启动:service-hello在eureka-server注册了2个实例,这就相当于一个小的集群。
三、建一个服务消费者(ribbon)
新建一个工程server-ribbon,引入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-learn</artifactId><groupId>com.sl.learn.cloud</groupId><version>1.0-SNAPSHOT</version><relativePath>..</relativePath></parent><modelVersion>4.0.0</modelVersion><groupId>com.sl.learn.cloud</groupId><artifactId>server-ribbon</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-ribbon</artifactId></dependency></dependencies></project>
修改配置文件
server:port: 8082spring:application:name: server-ribboneureka:client:serviceUrl:defaultZone: http://localhost:8080/eureka/management:endpoints:web:exposure:include: '*'endpoint:health:show-details: alwaysshutdown:enabled: true
在工程的启动类中,通过@EnableDiscoveryClient向服务中心注册;注入一个bean: restTemplate;并通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能。
package com.sl.cloud.learn;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;/*** @author shuliangzhao* @Title: ServerRibbonApplication* @ProjectName spring-cloud-learn* @Description: TODO* @date 2019/11/24 15:59*/
@SpringBootApplication
@EnableDiscoveryClient
public class ServerRibbonApplication {public static void main(String[] args) {SpringApplication.run( ServerRibbonApplication.class, args );}@Bean@LoadBalancedRestTemplate restTemplate() {`在这里插入代码片`return new RestTemplate();}}
写一个测试类HelloService
@Service
public class HelloService {@Autowiredprivate RestTemplate restTemplate;public String helloService(String name) {return restTemplate.getForObject("http://EUREKA-HELLO/hi?name="+name,String.class);}
}
写一个controller调用类
@RestController
public class HelloController {@Autowiredprivate HelloService helloService;@GetMapping(value = "/hi")public String helloService(String name) {return helloService.helloService(name);}}
在浏览器上多次访问http://localhost:8082/hi?name=zhansan,浏览器交替显示
hi zhansan ,my port is:8083
hi zhansan ,my port is:8081
四、 Ribbon调用总结
服务注册中心,eureka server,端口为8080
eureka-hello工程跑了两个实例,端口分别为8081,8983,分别向服务注册中心注册
sercvice-ribbon端口为8082,向服务注册中心注册
当sercvice-ribbon通过restTemplate调用eureka-hello的hi接口时,因为用ribbon进行了负载均衡,会轮流的调用eureka-hello:8081,8983 两个端口的hi接口;
SpringCloud教程- 服务消费者(rest+ribbon)(SpringCloud版本Finchley)相关推荐
- SpringCloud教程- 服务消费者(Feign)(SpringCloud版本Finchley)
文章目录 一.Feign简介 二. 环境准备 三.创建基于Feign服务 定义启动类 pom文件配置 配置文件application.yml 定义一个feign接口 定义一个controller 前言 ...
- SpringCloud |第二篇: 服务消费者(Ribbon)
2019独角兽企业重金招聘Python工程师标准>>> 一.Ribbon简介 Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法,将Netflix的中 ...
- SpringCloud教程- 断路器(Hystrix)(SpringCloud版本Finchley)
文章目录 一.断路器简介(Hystrix) 二.在ribbon中使用断路器(Hystrix) 代码地址:github-spring-cloud地址 前言:在微服务架构中,根据业务来拆分成一个个的服务, ...
- Spring Cloud | 第二篇:服务消费者(Ribbon)
本文的参考资料: spring cloud ribbon文档 方志明博客 Spring Cloud构建微服务架构(二)服务消费者 一.Ribbon简介 Ribbon是一个基于HTTP和TCP客户端的负 ...
- Spring Cloud 服务消费者 rest+ribbon (二)
Ribbon简介 Spring Cloud Ribbon是一个基于HTTP和TCP的客户端负载均衡工具,它基于Netflix Ribbon实现.通过Spring Cloud的封装,可以让我们 ...
- Spring Cloud第二篇:服务消费者RestTemplate+Ribbon
在上一篇文章,讲了服务的注册和发现.在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的.Spring cloud有两种服务调用方式,一种是ribbon+r ...
- SpringCloud教程-服务的注册与发现Eureka(SpringCloud版本Finchley)
文章目录 SpringCloud简介 创建注册中心(基于Eureka) 创建maven工程 创建maven子工程 eureka-server服务端pom文件 eureka-server服务端配置文件 ...
- SpringCloud教程- 服务链路追踪(Spring Cloud Sleuth)(SpringCloud版本Greenwich.SR4)
文章目录 一.Sleuth简介 二.为何使用Sleuth 三.构建工程 server-zipkin zipkin-serivce-hi zipkin-server-hello 四. 启动工程演示 代码 ...
- SpringCloud教程-分布式配置中心Config (SpringCloud版本Greenwich.SR4)
文章目录 Config(分布式配置中心)简介 创建服务端ConfigServer 创建客户端config-client 代码地址: github-spring-cloud地址 Config(分布式配置 ...
最新文章
- NET使用了UpdatePanel后如何弹出对话框!
- Android中如何使用命令行查看内嵌数据库SQLite3
- 龙格库塔法基本C程序
- 解析邻居的耳朵音乐地址(单页下载)
- mysql查询索引相关信息查询
- 8635 气球(组合数)
- 数据库笔记10:创建与管理视图
- SQL Server 全文索引创建
- python画柱状图-Python:Matplotlib 画曲线和柱状图(Code)
- 《如果我不曾见过太阳》
- 思购臻选模式,秒杀的底层逻辑—微三云贺心悦
- srand和rand详细讲解
- ECharts绘制饼图
- shell编程之iptables
- 广东民办大学计算机专业,二本考生:12所民办大学的计算机专业实力不错,报考难度较低...
- mysql输出当前是第几周使用week和weekofyear的区别
- GD32 CAN波特率计算问题
- 回文数函数的粗浅理解
- HTML5生日祝福蛋糕页面(生日蛋糕树) HTML+CSS+JS 求婚 html生日快乐祝福代码网页 520情人节告白代码 程序员表白源码 抖音3D旋转相册 js烟花代码 css爱心
- Docker:windows7系统环境下安装docker:Manifest extraction failed: 找不到Windows运行时类型Windows.Data.Json.JsonObject
热门文章
- c语言万年历闹钟程序,c语言编写的万年历 有平年闰年 有闹钟功能.docx
- 英语语法---不定式短语详解
- mxnet基础到提高(53)-ndarray与numpy之间转换
- 【知识图谱】关于知识图谱,我们接下来该研究什么?斯坦福教授们给出了答案...
- 【强化学习】多臂老虎机——E_greedy、UCB、Gradient Bandit 算法 代码实现
- Trapper: Transformer模型都在此!
- 网易智慧企业亮相TOP 100 Summit,以创新和匠心探索行业前沿
- Android Camera的进化史
- 0401互联网新闻 | 企业微信新版发布;阿里巴巴发布“AI谣言粉碎机”
- 异步执行和多线程编程的关系