负载均衡
负载均衡也是 Nginx常用的一个功能,当一台服务器的单位时间内的访问量越大时,服务器压力就越大,大到超过自身承受能力时,服务器就会崩溃。为了避免服务器崩溃,让用户有更好的体验,我们通过负载均衡的方式来分担服务器压力。我们可以建立很多很多服务器,组成一个服务器集群,当用户访问网站时,先访问一个中间服务器,在让这个中间服务器在服务器集群中选择一个压力较小的服务器,然后将该访问请求引入该服务器。如此以来,用户的每次访问,都会保证服务器集群中的每个服务器压力趋于平衡,分担了服务器压力,避免了服务器崩溃的情况。负载均衡配置一般都需要同时配置反向代理,通过反向代理跳转到负载均衡。
下面我们就来用Nginx+Zuul实现负载均衡功能。
1.首先下载nginx-1.15.0并解压后,进入conf文件夹并修改nginx.conf配置文件
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream webapp{
server 127.0.0.1:81;
server 127.0.0.1:82;
}
server {
listen 80;
server_name zy.wang.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root html;
#index index.html index.htm;
proxy_pass http://webapp;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
2.在本地hosts文件中指定本地域名、路径: C:\Windows\System32\drivers\etc
127.0.0.1 zy.wang.com
3.创建springCloud-zuul项目
application.yml:
spring:
application:
name: service-zuul
server:
port: 80
eureka:
client:
serviceUrl:
defaultZone: http://admin:123456@localhost:9500/eureka,http://admin:123456@localhost:9600/eureka
zuul:
routes:
api-a:
path: /api-member/**
serviceId: member-service
api-b:
path: /api-order/**
serviceId: order-service
pom.xml:
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.zy.wang</groupId>
<artifactId>springcloud2.0-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>springcloud2.0-zuul</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
</project>
启动类:
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class AppGateWay {
public static void main(String[] args) {
SpringApplication.run(AppGateWay.class, args);
}
@Bean
public TokenFilter accessUserNameFilter() {
return new TokenFilter();
}
}
网关过滤器:
@Component
public class TokenFilter extends ZuulFilter {
private static Logger log = LoggerFactory.getLogger(TokenFilter.class);
@Value("${server.port}")
private String port;
@Override
public String filterType() {
//前置过滤器
//pre:可以在请求被路由之前调用
//route:在路由请求时候被调用
//post:在route和error过滤器之后被调用
//error:处理请求时发生错误时被调用
return "pre";
}
@Override
public int filterOrder() {
//优先级,数字越大,优先级越低
return 0;
}
public boolean shouldFilter() {
//是否执行该过滤器,true代表需要过滤
return true;
}
public Object run() {
System.out.println("port="+port);
//这里return的值没有意义,zuul框架没有使用该返回值
return null;
}
}
Eureka和其他微服务代码这里就不详细介绍了,主要说明Zuul网关。
4.双击nginx.exe启动Nginx
5.启动Eureka后再启动zuul两次分别为81和82端口,在浏览器中多次输入下图地址,注:createBySuccessCodeMessage 换成自己的请求地址
在控制台中可以看到分别输出81和82的端口(轮询),至此已实现Nginx负载均衡。