Ribbon远程调用
Ribbon是客户端的负载均衡机制,它有几种负载均衡机制。默认是轮询,我们也可以自定义规则。通过合理的分配网络请求来减小服务器的压力。项目都是注册到eureka服务器上。通过ribbon去调用其他服务。
项目搭建:

1. 导入依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/>
</parent>
<!-- springcloud依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- eureka客户端依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
2. 写配置文件
# 项目访问路径前缀
server:
context-path: /demo # 设置服务名称,服务会以这个名字注册到eureka服务器上
spring:
application:
name: demo # 设置eureka服务器的注册地址
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
3. 开发服务接口
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController { @RequestMapping("/data")
public String testData(HttpServletRequest req){
String url = req.getRequestURL().toString();
return "提供服务的是: "+url;
}
}
4. 启动主函数,测试。
import java.util.Scanner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
//申明eureka客户端
@EnableEurekaClient
public class Main { public static void main(String[] args) {
//1. 启动main方法,在控制台输入端口号
Scanner scan = new Scanner(System.in);
String port = scan.nextLine();
//项目以输入的端口号启动
new SpringApplicationBuilder(Main.class).properties("server.port="+port).run(args);
//2. 启动两次main方法,分别以8080 8081启动,模拟集群的环境 浏览器访问接口测试下。
}
}

二:搭建Ribbon项目

1. 导入依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/>
</parent>
<!-- springcloud依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- eureka客户端依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- ribbon依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
</dependencies>
2. 配置application.yml文件
# 指定默认端口
server:
port: 8083 # 设置服务名称,服务会以这个名字注册到eureka服务器上
spring:
application:
name: ribbon # 设置eureka服务器的注册地址
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
3. 编写controller
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class Controller { @Autowired
private RestTemplate restTpl; @RequestMapping("/testRibbon")
public String testRibbon() {
String data = restTpl.getForObject("http://demo/demo/data", String.class);
//String data = restTpl.getForObject("http://127.0.0.1:8081/demo/data", String.class);
//User user = restTpl.getForObject("http://demo/demo/user/{id}", User.class,id); return data;
}
}
4. 编写主函数入口, 配置RestTemplate
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
//申明eureka客户端
@EnableEurekaClient
public class RibbonMain { @Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(RibbonMain.class, args);
}
}
测试:我们通过 ribbon来调用其他服务数据,可以看到它默认是走轮询策略。

Ribbon远程调用的更多相关文章
- Feign远程调用
有关微服务中,服务与服务如何通信,我已经给大家介绍了Ribbon远程调用的相关知识,不知道大家有没有发现Ribbon的问题呢? Ribbon的问题 在Ribbon中,如果我们想要发起一个调用,是这样的 ...
- 阶段5 3.微服务项目【学成在线】_day09 课程预览 Eureka Feign_06-Feign远程调用-Ribbon测试
2.1.2 Ribbon测试 Spring Cloud引入Ribbon配合 restTemplate 实现客户端负载均衡.Java中远程调用的技术有很多,如: webservice.socket.rm ...
- SpringCloud使用Nacos服务发现实现远程调用
本文使用SpringCloud结合Nacos服务发现,Feign远程调用做一个简单的Demo. 1 Nacos 关于Nacos之前写了两篇文章关于SpringBoot对它的使用,感兴趣可以查看一下. ...
- springcloud系列五 feign远程调用服务
一:Feign简介 Feign 是一种声明式.模板化的 HTTP 客户端,在 Spring Cloud 中使用 Feign,可以做到使用 HTTP请求远程服务时能与调用本地方法一样的编码体验,开发者完 ...
- 客户端远程调用Feign
客户端远程调用 Feign 什么是Feign? Feign是 Netflix 公司开源的声明式HTTP客户端 Github : Feign 源码 为什么需要Feign? 原代码可读性不高 复杂的URL ...
- 分布式远程调用SpringCloud-Feign的两种具体操作方式(精华)
一 前言 几大RPC框架介绍 1.支持多语言的RPC框架,google的gRPC,Apache(facebook)的Thrift 2.只支持特定语言的RPC框架,例如新浪的Motan 3.支持服务治理 ...
- 2019.12.4 Hystix熔断&Feign进行远程调用&Zuul
0.学习目标 会配置Hystix熔断 会使用Feign进行远程调用 能独立搭建Zuul网关 能编写Zuul的过滤器 1.Hystrix 1.1.简介 Hystrix,英文意思是豪猪,全身是刺,看起来就 ...
- 阶段5 3.微服务项目【学成在线】_day09 课程预览 Eureka Feign_07-Feign远程调用-Feign测试
2.2.1 Feign介绍 Feign是Netflix公司开源的轻量级rest客户端,使用Feign可以非常方便的实现Http 客户端.Spring Cloud引入 Feign并且集成了Ribbon实 ...
- 阶段5 3.微服务项目【学成在线】_day09 课程预览 Eureka Feign_05-Feign远程调用-客户端负载均衡介绍
2 Feign远程调用 在前后端分离架构中,服务层被拆分成了很多的微服务,服务与服务之间难免发生交互,比如:课程发布需要调用 CMS服务生成课程静态化页面,本节研究微服务远程调用所使用的技术. 下图是 ...
随机推荐
- Top 8 Diagrams for Understanding Java
Reference: http://www.programcreek.com/2013/09/top-8-diagrams-for-understanding-java/ A diagram is s ...
- 面试题常考&必考之--js中的对象的浅拷贝和深拷贝(克隆,复制)(下)
这里主要是讲深拷贝: 深拷贝:个人理解就是拷贝所有的层级 1.像对象里再放数组和对象这些叫引用值.开始我们先判断大对象中是否有引用值(数组和小对象), 然后在判断引用值是数组还是对象 2.开始啦: 1 ...
- 寻找的常用webstorm快捷键
1. 必备快捷键 Ctrl+/:注释当前行 Ctrl+Shift+/:当前位置插入注释 Ctrl+Alt+/:块注释,并Focus到首行,写注释说明用的 Ctrl+Shift+A:选择当前标签前后,修 ...
- SSM图片
非关系型数据,redis,mongoDB关系型数据,mysql,oracle 1.springmvc+spring+mybatis1.导入jar2.书写配置xml(applicationContext ...
- Oracle中的rowid rownum
1. rowid和rownum都是虚列 2. rowid是物理地址,用于定位oracle中具体数据的物理存储位置 3. rownum则是sql的输出结果排序,从下面的例子可以看出其中的区别. rowi ...
- 【java工具类】AES加密解密
百度百科一下,AES:高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准.这个标准 ...
- @ResponseBody返回4种数据格式的数据
1.返回一个键值对或者集合 前端JS请求: //返回值为map的形式 $(".name").blur(function(){ $.ajax({ type:"Post&qu ...
- session.flush()与session.clear()的区别
session.flush()和session.clear()就针对session的一级缓存的处理. 简单的说, 1 session.flush()的作用就是将session的缓存中的数据与数据库同步 ...
- view组件
view标签的属性值: hover-class:按下的点击态 属性值:字符串 如果:hover-class="none" 按下就没有点击态 hover-stop-pro ...
- 011-Spring Boot 运行流程分析SpringApplication.run
一.程序入口 1.1.静态方法 //直接调用run方法 ConfigurableApplicationContext context = SpringApplication.run(App.class ...