请求例子
http://127.0.0.1:3000/gateway-api/stm-web/xxx
http://127.0.0.1:3000/gateway-api/prod-api/xxx
当nginx监测到/gateway-api时,将请求转发到node-gateway(node的网关中)
node-gateway,根据配置规则去请求java的微服务
1、创建应用
一、提供开放管理平台,用户在此平台中注册用户,联系管理员激活
二、激活账号以后,用户登录、
2.1 创建应用,输入应用名称,创建应用,平台记录下appId,appKey,appSecret
const random = (rangeStr) => {
const range = rangeStr || 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz'
const radix = range.length;
const t = range[0 | Math.random() * radix]
return t
}
const randomStr = () => {
const str = 'abcdefghijklmnopqrstuvwyz'
return random(str)
}
// A more compact, but less performant, RFC4122v4 solution:
const uuid = function (str) {
const first = randomStr()
const br = typeof str === 'string' ? str : '-';
const t1 = br;
const t2 = br;
const t3 = br;
const t4 = br;
return `${first}xxxxxxx${t1}xxxx${t2}4xxx${t3}yxxx${t4}xxxxxxxxxxxx`.replace(/[xy]/g, function (c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
};
module.exports = {
uuid,
}
const {uuid} = require("./lib/uuid");
const md5 = require('md5');
/*
* 使用应用名称,生成appId,appKey,appSecret
* appKey 公钥
* appSecret 私钥
*/
const createKeyAndSecret = (appName) => {
const appId = uuid();
const appKey = md5(`${appId}:${appName}`);
const appSecret = uuid('');
return {
appId,
appKey,
appSecret
}
}
const obj = createKeyAndSecret('xs_jczz')
console.log(obj, obj.appId.length, obj.appKey.length, obj.appSecret.length);
/*
{
appId: 'a83ca336-c77a-4564-b7b8-3de56a417b95',
appKey: '017ada9ac92e1d6bdcc03e234ea7d100',
appSecret: 'se0f99fdeddd4726b4238f8144d2a874'
} 36 32 32
*/
2、接口签名
3、网关通过验证,代理转发请求
4、网关功能
接口限流:
同一个接口,在1s内不允许超过10次请求,同一个url接口1s内不允许超过30次,否则限流
服务熔断:
应用服务,需要提供一个健康检查的接口,网关每3分钟检查一次,如果健康检查的接口没有调通,在1分钟内,每5s做一次检查,如果没有恢复,做服务熔断,等待下个周期的健康检查,健康检查通过,继续提供服务