注:本文写的是Gateway
的负载均衡及集群的搭建,Gateway
的使用不在本文中介绍。
负载均衡,英文名称为Load Balance,其含义就是指将负载(工作任务)进行平衡、分摊到多个操作单元上进行运行,例如FTP服务器、Web服务器、企业核心应用服务器和其它主要任务服务器等,从而协同完成工作任务。
准备
这里不在详细说明Gateway
的创建。最简单的方式就是使用IDEA
的Spring Initializr
创建,只需勾选几下就可以了。
<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.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.2.11.RELEASEversion>
<relativePath/>
parent>
<groupId>cn.yanghuisengroupId>
<artifactId>gateway-1artifactId>
<version>0.0.1-SNAPSHOTversion>
<name>gateway-1name>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
<spring-cloud-alibaba.version>2.2.1.RELEASEspring-cloud-alibaba.version>
<spring-cloud.version>Hoxton.SR9spring-cloud.version>
properties>
<dependencies>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-gatewayartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
<exclusions>
<exclusion>
<groupId>org.junit.vintagegroupId>
<artifactId>junit-vintage-engineartifactId>
exclusion>
exclusions>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${spring-cloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>${spring-cloud-alibaba.version}version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
Gateway:网关,本文章中用到的重要依赖
Nacos:注册中心,Gateway结合Nacos实现请求转发
项目创建完毕后就要进行项目的配置了,否则只添加依赖没有任何用
server:
port: 5000
spring:
application:
name: GATEWAY-1
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848,127.0.0.1:8849,127.0.0.1:8850 #Nacos集群地址
gateway:
routes:
- id: nacos-demo
uri: lb://NACOS-DEMO # lb表示当前的注册中心,后面是服务名
predicates:
- Path=/** # 根据请求路径匹配
因为是Gateway
结合Nacos
使用的,而Nacos
自带Ribbon
负载均衡依赖,所以Gateway
默认就开启了负载均衡,默认负载均衡策略就是轮询,如果要修改策略只需要配置一下即可
# 服务名
NACOS-DEMO:
ribbon:
# 指定负载均衡策略类名
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
# Provider服务列表,多个服务之间使用,分隔
# listOfServers: http://localhost:8088,http://localhost:8888
listOfServers
可以去掉,会从注册中心获取服务地址
这样负载均衡就配置好了
如果服务停止运行了,在通过
Gateway
请求转发时,就会报错,这时我们就要进行服务熔断,从而保证请求不会报错
需要结合Hystrix
进行服务熔断,所以需要添加Hystrix
的依赖
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-hystrixartifactId>
dependency>
当服务熔断时Gateway
会把请求转发到这个请求接口上
package cn.yanghuisen.gateway1.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Y
* @date 2020/11/15 16:59
* @desc 服务熔断
*/
@RestController
public class FallBackController {
@RequestMapping("/fallback")
public String fallback(){
return "错误:FALLBACK";
}
}
gateway:
routes:
- id: nacos-demo
uri: lb://NACOS-DEMO # lb表示当前的注册中心,后面是服务名
predicates:
- Path=/** # 根据请求路径匹配
filters:
- name: Hystrix # 服务熔断
args:
name: fallback
fallbackUri: forward:/fallback
fallbackUri
:会把请求转发到/fallback
上
首先停止后端服务,然后访问URLhttp://localhost:5000/demo/test
Gateway
自己是不能搭建集群的,所以需要配合Nginx
实现集群的搭建及负载均衡。集群搭建很简单, Gateway
项目自己不用做改动。
准备:
Nginx下载地址:http://nginx.org/en/download.html
上面说了,因为Gateway
自身不能进行集群的搭建及负载均衡,所以需要在Nginx
上进行负载均衡,转发到多个不同的Gateway
上
修改Nginx
下的conf
下的nginx.conf
文件,配置Nginx
的反向代理,通过Nginx
的反向代理实现负载均衡的配置
# 负载均衡,默认是轮询策略
upstream backserver {
server localhost:5000;
server localhost:5001;
}
server {
# 监听端口
listen 80;
server_name localhost;
# 根目录
location / {
proxy_pass http://backserver;
}
}
启动:Nginx
的启动很简单,只要双击Nginx
下的nginx.exe
启动即可,或者在Nginx下通过命令行start nginx
启动
停止:Nginx下命令行nginx -s stop
停止nginx
到此集群搭建完毕