4、Spring Cloud-负载均衡 Ribbon
4.1、RestTemplate 简介
4.2、Ribbon 简介

4.3、使用RestTemplate和Ribbon来消费

ProviderService.java
package com.cr.eurekaclient.service;
import org.springframework.stereotype.Service; @Service
public class ProviderService { public String port(){
return "";
}
}
PortController.java
package com.cr.eurekaclient.controller; import com.cr.eurekaclient.service.ProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class PortController { @Autowired
ProviderService providerService; @GetMapping("/port")
public String getPort(){
return providerService.port();
}
}
配置文件:
server.port=
spring.application.name=CLINET
#应用起名字spring.application.name=provider
#注册服务时使用服务的ip地址
eureka.instance.prefer-ip-address=true
#服务中心地址
eureka.client.service-url.defaultZone=http://localhost:8762/eureka/
访问:

去注册中心可以看到此时的服务已经在注册中心注册:

这里的Application将会是消费者中引用的地址!!!
服务的消费者:


RibbonConfig.java
@Configuration
public class RibbonConfig { @Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
}
RibbonController.java
@RestController
public class RibbonController {
@Autowired
RibbonService ribbonService; @GetMapping("/port")
public String port(){
return ribbonService.port();
}
}
@Service
public class RibbonService { @Autowired
RestTemplate restTemplate; public String port(){
return restTemplate.getForObject("http://CLINET/port",String.class);
}
}
http://CLINET/port中的CLIENT为注册中心中的Application中的值
@EnableEurekaClient
@SpringBootApplication
public class EurekaRibbonClientApplication { public static void main(String[] args) {
SpringApplication.run(EurekaRibbonClientApplication.class, args);
}
}
配置文件:
spring.application.name=eureka-ribbon-client
server.port=
eureka.client.service-url.defaultZone=http://localhost:8762/eureka/


@LoadBalanced开启负载均衡功能
测试实现:

同时也将8090换成8089,使用maven进行打包

在cmd中运行两个项目:

此时8089、8090端口都可以进行访问


http://localhost:8088/port页面进行刷新请求


4.4、loadBalancerClient 简介
在工程中进行测试:

RibbonController.java
package com.cr.eurekaribbonclient.controller; import com.cr.eurekaribbonclient.service.RibbonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class RibbonController {
@Autowired
RibbonService ribbonService; @GetMapping("/port")
public String port(){
return ribbonService.port();
} @Autowired
private LoadBalancerClient loadBalancerClient;
@GetMapping("/testRibbon")
public String testRibbon(){
ServiceInstance instance = loadBalancerClient.choose("CLINET");
return instance.getHost()+ ":" + instance.getPort();
}
}


spring.application.name=eureka-ribbon-client
server.port=
eureka.client.service-url.defaultZone=http://localhost:8762/eureka/
stores.ribbon.listOfServers:example.com,google.com
ribbon.eureka.enabled=false
package com.cr.eurekaribbonclient.controller; import com.cr.eurekaribbonclient.service.RibbonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class RibbonController {
@Autowired
RibbonService ribbonService; @GetMapping("/port")
public String port(){
return ribbonService.port();
} @Autowired
private LoadBalancerClient loadBalancerClient;
@GetMapping("/testRibbon")
public String testRibbon(){
ServiceInstance instance = loadBalancerClient.choose("CLINET");
return instance.getHost()+ ":" + instance.getPort();
} //进一步说明
@GetMapping("/hi")
public String hi(){
ServiceInstance instance = loadBalancerClient.choose("stores");
return instance.getHost()+ ":" + instance.getPort();
}
}

4、Spring Cloud-负载均衡 Ribbon的更多相关文章
- spring Cloud负载均衡Ribbon
Ribbon饥饿加载 默认情况下Ribbon是懒加载的.当服务起动好之后,第一次请求是非常慢的,第二次之后就快很多. 解决方式:开启饥饿加载 ribbon: eager-load: enabled: ...
- Spring Cloud 负载均衡初体验
目录 服务搭建 1.注册中心--Eureka Server 2.服务提供方--Service Provider 3.服务消费方--Service Consumer 服务消费 Feign 与断路器 Hy ...
- Spring Cloud负载均衡:使用zuul作服务器端负载均衡
1.目的: 本文简述Spring Cloud负载均衡之服务器负载均衡模式,使用组件为zuul. zuul作为Spring Cloud中的网关组件,负责路由转发.身份验证.请求过滤等等功能,那么我们可以 ...
- Spring Cloud负载均衡:使用Feign作客户端负载均衡
有了一篇服务端负载均衡后,再来一篇客户端负载均衡,客户端负载均衡很简单,无需在zuul中做多余配置(本示例不引入zuul),只需要在客户端进行Feign引入和配置即可. 准备工作很简单,实现客户端负载 ...
- Spring Cloud 客服端负载均衡 Ribbon
一.简介 Spring Cloud Ribbon 是一个基于Http和TCP的客服端负载均衡工具,它是基于Netflix Ribbon实现的.它不像服务注册中心.配置中心.API网关那样独立部署, ...
- Spring Cloud(Dalston.SR5)--Ribbon 中间层负载均衡
Spring Cloud 集成了 Ribbon 并结合 Eureka 可以实现客户端的负载均衡,使用 @LoadBalanced 修饰的 RestTemplate 类拥有了负载均衡功能,在 Sprin ...
- Spring Cloud:使用Ribbon实现负载均衡详解(下)
在上一篇文章(Spring Cloud:使用Ribbon实现负载均衡详解(上))中,我对 Ribbon 做了一个介绍,Ribbon 可以实现直接通过服务名称对服务进行访问.这一篇文章我详细分析一下如何 ...
- Spring Cloud第四篇 | 客户端负载均衡Ribbon
本文是Spring Cloud专栏的第四篇文章,了解前三篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Cl ...
- 2.【Spring Cloud Alibaba】实现负载均衡-Ribbon
负载均衡的两种方式 如何实现负载均衡 目前已经实现让A总能找到B,如何实现负载均衡 负载均衡的两种方式 服务器端负载均衡 客户端负载均衡 使用Ribbo实现负载均衡 Ribbon是什么 ==Netfl ...
- SpringCloud学习系列之二 ----- 服务消费者(Feign)和负载均衡(Ribbon)使用详解
前言 本篇主要介绍的是SpringCloud中的服务消费者(Feign)和负载均衡(Ribbon)功能的实现以及使用Feign结合Ribbon实现负载均衡. SpringCloud Feign Fei ...
随机推荐
- 如鹏网学习笔记(八)CSS
CSS 一.CSS简介 1,CSS (Cascading Style Sheets) 级联样式表 ,是一种计算机语言,用来控制HTML内容的显示效果 2,CSS预先定义了众多的和显示效果有关的样式属性 ...
- easyui 带参数的datagride
<table id="tt" style="width:100%;height:355px" url="../aowei/Handler/Han ...
- GPUImage使用
GPUImage项目下载地址:https://github.com/BradLarson/GPUImage.git 下载项目时如果下载不下来可以直接check一份(之前下载了好多次都是下载失败,最后没 ...
- array(1) { [0]=> int(5) }和array(1) { [0]=> string(1) "5" }
php array数组: $arrayValue = array(5); $arrayValue = array('5'); 的不同之处 一个是整型一个是字符串型 array(1) { [0]=> ...
- 15、IO (转换流、缓冲流)
转换流概述 * A: 转换流概述 * a: 转换流概述 * OutputStreamWriter 是字符流通向字节流的桥梁:可使用指定的字符编码表,将要写入流中的字符编码成字节 * 将字符串按照指定的 ...
- Golang的 signal
在实际项目中我们可能有下面的需求: 1.修改了配置文件后,希望在不重启进程的情况下重新加载配置文件: 2.当用 Ctrl + C 强制关闭应用后,做一些必要的处理: 这时候就需要通过信号传递来进行处理 ...
- PAT 1074. Reversing Linked List
#include <cstdio> #include <cstdlib> #include <iostream> #include <unordered_ma ...
- BZOJ3529: [Sdoi2014]数表(莫比乌斯反演 树状数组)
题意 题目链接 Sol 首先不考虑\(a\)的限制 我们要求的是 \[\sum_{i = 1}^n \sum_{j = 1}^m \sigma(gcd(i, j))\] 用常规的套路可以化到这个形式 ...
- 理解webpack4.splitChunks之cacheGroups
cacheGroups其实是splitChunks里面最核心的配置,一开始我还认为cacheGroups是可有可无的,这是完全错误的,splitChunks就是根据cacheGroups去拆分模块的, ...
- 浏览器根对象window之值为数值的属性
1. number属性 1.1 length length 属性返回在当前窗口中frames的数量(包括IFRAMES). 该属性值与window.frames.length属性值相等. 1.2 in ...