单点是系统高可用的大敌,单点往往是系统高可用最大的风险和敌人,应该尽量在系统设计的过程中避免单点。方法论上,高可用保证的原则是“集群化”,或者 叫“冗余”:只有一个单点,挂了服务会受影响;如果有冗余备份,挂了还有其他backup能够顶上。
(1) 准备多个GateWay工程
server:
port: 8080 #服务端口
-------------------------------------------
server:
port: 8081 #服务端口
-------------------------------------------
spring:
application:
name: api-gateway #指定服务名
cloud:
gateway:
routes:
- id: product-service
uri: lb://service-product #根据微服务名从注册中心拉取微服务请求路径
predicates:
- Path=/product-service/** #/product-service/product/1 重写为 /product/1 ---> 192.168.1.1:9001/product/1
filters: #配置路由过滤器
- RewritePath=/product-service/(?<segment>.*), /$\{segment} #路径重写的过滤器,yml中$ --> $\
discovery:
locator:
enabled: true #开启根据微服务名自动转发
lower-case-service-id: true #微服务名称小写形式呈现
#配置Eureka
eureka:
client:
register-with-eureka: false #是否将自己注册到注册中心
service-url:
defaultZone: http://localhost:9000/eureka/
registry-fetch-interval-seconds: 10 # 从Eureka拉取服务器配置信息的周期,设置为 10s
instance:
prefer-ip-address: true #使用ip地址注册
(2) 配置ngnix
#配置多台服务器(这里只在一台服务器上的不同端口)
upstream gateway {
server 127.0.0.1:8081;
server 127.0.0.1:8080;
}
#请求转向mysvr 定义的服务器列表
location / {
proxy_pass http://gateway;
}