openresty 负载均衡
负载均衡
轮询
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://web-server;
root /usr/share/nginx/html;
index index.html index.htm;
}
}
upstream web-server {
server 192.168.57.10:8080;
server 192.168.57.11:8080;
}
加权轮询
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://web-server;
root /usr/share/nginx/html;
index index.html index.htm;
}
}
upstream web-server {
server 192.168.57.10:8080 weight=1;
server 192.168.57.11:8080 weight=2;
}
最少连接数
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://web-server;
root /usr/share/nginx/html;
index index.html index.htm;
}
}
upstream web-server {
least_conn;
server 192.168.57.10:8080;
server 192.168.57.11:8080;
}
最短响应时间:需安装nginx-upstream-fair
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://web-server;
root /usr/share/nginx/html;
index index.html index.htm;
}
}
upstream web-server {
fair;
server 192.168.57.10:8080;
server 192.168.57.11:8080;
}
sticky:根据cookie将请求分布到后端服务器上,同一cookie尽可能发送到同一后端服务器
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://web-server;
root /usr/share/nginx/html;
index index.html index.htm;
}
}
upstream web-server {
sticky;
server 192.168.57.10:8080;
server 192.168.57.11:8080;
}
hash:根据指定的key进行hash,如:$request_uri、$uri等
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://web-server;
root /usr/share/nginx/html;
index index.html index.htm;
}
}
upstream web-server {
hash $request_uri;
server 192.168.57.10:8080;
server 192.168.57.11:8080;
}
ip hash:根据请求的ip地址hash
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://web-server;
root /usr/share/nginx/html;
index index.html index.htm;
}
}
upstream web-server {
ip_hash;
server 192.168.57.10:8080;
server 192.168.57.11:8080;
}
应用 1
HelloController
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "hello 瓜田李下";
}
}
Dockerfile
from java:8
workdir /usr/local/jar
copy hello.jar app.jar
expose 8080
entrypoint ["java", "-jar", "app.jar"]
edit configuration ==> docker
配置完成后,启动应用
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.7.1)
2022-07-02 04:46:23.750 INFO 1 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication v0.0.1-SNAPSHOT using Java 1.8.0_111 on ea15715ee141 with PID 1 (/usr/local/jar/app.jar started by root in /usr/local/jar)
2022-07-02 04:46:23.758 INFO 1 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to 1 default profile: "default"
2022-07-02 04:46:25.959 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2022-07-02 04:46:25.991 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-07-02 04:46:25.992 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.64]
2022-07-02 04:46:26.180 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-07-02 04:46:26.180 INFO 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2258 ms
2022-07-02 04:46:27.086 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2022-07-02 04:46:27.109 INFO 1 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 4.334 seconds (JVM running for 5.342)
应用 2
HelloController
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "hello 瓜田李下 2";
}
}
Dockerfile
from java:8
workdir /usr/local/jar
copy hello.jar app.jar
expose 8080
entrypoint ["java", "-jar", "app.jar"]
edit configuration ==> docker
配置完成后,启动应用
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.7.1)
2022-07-02 04:47:21.717 INFO 1 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication v0.0.1-SNAPSHOT using Java 1.8.0_111 on e27c691f3622 with PID 1 (/usr/local/jar/app.jar started by root in /usr/local/jar)
2022-07-02 04:47:21.729 INFO 1 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to 1 default profile: "default"
2022-07-02 04:47:24.071 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2022-07-02 04:47:24.098 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-07-02 04:47:24.099 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.64]
2022-07-02 04:47:24.277 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-07-02 04:47:24.278 INFO 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2402 ms
2022-07-02 04:47:25.292 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2022-07-02 04:47:25.312 INFO 1 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 4.587 seconds (JVM running for 5.603)
openresty 配置
default.conf
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://web-server;
root /usr/share/nginx/html;
index index.html index.htm;
}
}
upstream web-server {
server 172.18.0.3:8080;
server 172.18.0.4:8080;
}
创建容器
docker run -it -d --net fixed --ip 172.18.0.2 -p 9000:80 \
-v /Users/huli/lua/openresty/conf4/default.conf:/etc/nginx/conf.d/default.conf \
--name openresty4 lihu12344/openresty
使用测试
huli@hudeMacBook-Pro ~ % curl localhost:9000/hello
hello 瓜田李下 huli@hudeMacBook-Pro ~ % curl localhost:9000/hello
hello 瓜田李下 huli@hudeMacBook-Pro ~ % curl localhost:9000/hello
hello 瓜田李下 2 huli@hudeMacBook-Pro ~ % curl localhost:9000/hello
hello 瓜田李下 huli@hudeMacBook-Pro ~ % curl localhost:9000/hello
hello 瓜田李下 2 huli@hudeMacBook-Pro ~ % curl localhost:9000/hello
hello 瓜田李下 huli@hudeMacBook-Pro ~ % curl localhost:9000/hello
hello 瓜田李下 2