Spring Cloud之Zuul网关集群
程序员文章站
2022-06-13 21:31:53
...
Spring Cloud之Zuul网关集群
Nginx+Zuul 一主一备 或者 轮训多个
在微服务中,所有服务请求都会统一到Zuul网关上。
Nginx 配置:
#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 backServer{
server 192.168.8.159:81;
server 192.168.8.159:82;
}
server {
listen 80;
server_name www.toov5.com;
location / {
### 指定上游服务器负载均衡服务器
proxy_pass http://backServer/;
index index.html index.htm;
}
}
}
网关:
yml:
###注册 中心
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8100/eureka/
server: ##api网关端口号
port: 81
###网关名称
spring: ##网关服务名称
application:
name: service-zuul
###网关名称
cloud:
config:
####读取后缀
profile: dev
####读取config-server注册地址
discovery:
service-id: confi
### 配置网关反向代理
zuul:
routes:
api-member: ##随便写的
### 以 /api-member/访问转发到会员服务 通过别名找
path: /api-member/**
serviceId: app-toov5-member ##别名 如果集群的话 默认整合了ribbon 实现轮训 负载均衡
api-order: ##随便写的
### 以 /api-order/访问转发到订单服务
path: /api-order/**
serviceId: app-toov5-order ##别名
启动类:
package com.toov5;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy //开启网关代理
public class AppGateway {
public static void main(String[] args) {
SpringApplication.run(AppGateway.class, args);
}
// //zuul配置使用config实现实时更新
// @RefreshScope
// @ConfigurationProperties("zuul")
// public ZuulProperties zuulProperties() {
// return new ZuulProperties();
// }
}
访问:
启动两个网关 81和82
下一篇: 阿里运营七大黄金定律
推荐阅读
-
spring-cloud入门之eureka-server(服务发现)
-
spring-cloud入门之spring-cloud-config(配置中心)
-
spring-cloud入门之eureka-client(服务注册)
-
Spring Cloud Gateway网关XSS过滤Filter
-
Spring Cloud Zuul的重试配置详解
-
SpringCloud之服务注册与发现Spring Cloud Eureka实例代码
-
SpringCloud之消息总线Spring Cloud Bus实例代码
-
spring cloud-zuul的Filter使用详解
-
详解spring cloud构建微服务架构的网关(API GateWay)
-
Spring Cloud之服务监控turbine的示例