学习springboot 集成 spring Cloud 之Ribbon负载均衡(四)
本篇内容主要讲解ribbon的介绍和使用,基于上篇 学习springboot 集成 spring Cloud 之服务注册到eureka服务注册中心(三)
一:介绍Ribbon客户端负载均衡
1.提到负载均衡大家一定会想到Nginx对,没错我刚开始学我也寻思是一回事,当使用时候会发现二者还是有一定的区别!
Nginx和ribbon区别
①:Nginx是在服务端去做负载均衡,当一个请求到达服务端,服务端负载均衡服务器,会通过轮询算法,随机算法,
加权重算法,随机算法等等...来进行请求转发到应用服务器。
②:ribbon是在客户端做负载均衡,也就是说他会依赖于服务注册中心,当服务之间的请求到达时候,ribbon会去注册中
心的注册表中拉取全部可用的服务,然后拷贝到本地内存来做负载均衡,常用的算法,轮询(默认),随机,加权重等等
可以自定义去实现负载均衡算法
2.看一下官方Netflix的介绍:
提供云端负载均衡,有多种负载均衡策略可供选择,可配合服务发现和断路器使用。
Ribbon是客户端IPC库,在云中经过了实战测试。它提供以下功能
负载均衡
容错能力
异步和响应模型中的多种协议(HTTP,TCP,UDP)支持
缓存和批处理
二:结合项目来使用
1.开始搭建咱们的项目:在原有的项目基础上再次创建一个订单服务:order-server-8001 依赖和order-server-8000 一样
区别在于yml不同下边代码。
注意一点:spring:
application:
name: order-service 必须和order-server-8000的服务名字一样
server:
port: 8001
spring:
application:
name: order-service
eureka:
client:
service-url:
defaultZone: http://eureka.server7000.com:7000/eureka/,http://eureka.server7001.com:7001/eureka/ #Eureka服务注册中心地址,因为咱们之前做了集群所以填写全部集群的注册中心地址
instance:
prefer-ip-address: true
instance-id: order-service-8001
package com.han.springcloud.orderserver8001;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class OrderServer8001Application {
public static void main(String[] args) {
SpringApplication.run(OrderServer8001Application.class, args);
}
}
运行测试一下:先启动两个注册中心,然后启动两个服务提供者订单服务
ok没问题(爆红那一块没问题是,Eureka的安全保护机制,可以关闭但是不建议关闭)
2.创建一个服务 ribbon-server-9000
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.han.springcloud</groupId>
<artifactId>ribbon-server-9000</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ribbon-server-9000</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.修改application.yml 和 RibbonServer9000Application 启动类
server:
port: 9000
spring:
application:
name: ribbon-consumer
eureka:
instance:
instance-id: ribbon-consumer-9000
prefer-ip-address: true
client:
service-url:
defaultZone: http://eureka.server7000.com:7000/eureka/,http://eureka.server7001.com:7001/eureka/
fetch-registry: true
register-with-eureka: true
package com.han.springcloud.ribbonserver9000;
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.cloud.netflix.ribbon.RibbonClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableEurekaClient
@RibbonClients
public class RibbonServer9000Application {
public static void main(String[] args) {
SpringApplication.run(RibbonServer9000Application.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
4.去order-server-8000 和 order-server-8001 写个接口 供外部 ribbon-server-9000 消费者调用
接口注意内容,可以输出不同但是,Mapping映射地址一定要相同
order-server-8000和order-server-8001 中新建controller包然后创建一个controller 写一个OrderController
package com.han.springcloud.orderserver8000.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/order")
public class OrderController {
@GetMapping("/{id}/get")
public String get(@PathVariable("id") Integer id){
return "【order-server-8000】----->OrderController----->get="+id;
}
}
package com.han.springcloud.orderserver8001.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/order")
public class OrderController {
@GetMapping("/{id}/get")
public String get(@PathVariable("id") Integer id){
return "【order-server-8001】----->OrderController----->get="+id;
}
}
5.ribbon-server-9000项目添加包controller 创建类RibbonConsumerController
package com.han.springcloud.ribbonserver9000.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.Map;
@RestController
@RequestMapping(value = "/ribbon/order",produces = MediaType.APPLICATION_JSON_VALUE)
@RibbonClient(name = "ORDER-SERVICE")
public class RibbonConsumerController {
@Autowired
private RestTemplate restTemplate;
//定义订单服务名称
private static final String ORDER_SERVER_URL = "ORDER-SERVICE";
/*
*ribbon负载均衡(默认轮询算法)获取
* */
@GetMapping("/{id}/get")
public String get(@PathVariable("id") Integer id){
return restTemplate.getForObject("http://"+ORDER_SERVER_URL+"/order/"+id+"/get",String.class);
}
}
6.启动两个注册中心,启动两个order服务,启动ribbon服务
服务启动都没有问题
测试:调用地址:http://localhost:9000/ribbon/order/1/get
刷新查看获取不同内容
依次调用轮询算法:
三:总结
下一篇文章,介绍使用自定义轮询算法。