Commit 3ff83008 by wang

流量限制功能发布

parents aa55afc5 af56849f
......@@ -27,3 +27,4 @@ cmd.exe~
/controller/test.go
/regconfig
/cmd/http/regTest
/cmd/http/redisTest
......@@ -3,8 +3,6 @@ package main
import (
"flag"
"github.com/gin-gonic/gin"
Iconfig "github.com/ichunt2019/ichunt-micro-registry/config"
Iregistry "github.com/ichunt2019/ichunt-micro-registry/registry"
_ "github.com/ichunt2019/ichunt-micro-registry/registry/etcd"
"github.com/micro/go-micro/v2/web"
"golang_open_platform/boot"
......@@ -23,7 +21,6 @@ func main() {
r := routes.InitRouter()
port := config.Get("web.port").String()
//web改成micro 就是grpc,并直接注册到etcd里面
Registry()
service := web.NewService(
web.Name("go.micro.api.http.search"),
web.Handler(r),
......@@ -35,23 +32,4 @@ func main() {
if err := service.Run(); err != nil {
panic(err)
}
}
func Registry(){
nodes := []*Iregistry.Node{
{
IP: "192.168.2.72",//当前服务的ip
Port: 60006,//当前服务的端口
Weight:2,//当前服务的权重
},
}
etcdConfig := Iregistry.EtcdConfig{
Address: []string{"192.168.2.232:2379"},//etcd的节点ip
Username: "",//etcd的节点的用户名
Password: "",//etcd的节点的密码
Path:"/ichuntMicroService/",//网关前缀,目前固定写即可
}
Iconfig.Register("open",etcdConfig,nodes)// 第一个参数:服务名,第二个参数etcd配置,第三个参数当前注册节点信息
}
......@@ -25,6 +25,7 @@ var(
//redis 表key
openWhiteList ="openWhiteList"//白名单
openBlackList ="openBlackList"//白名单
openTokenBusinessList="openTokenBusinessList"//商家列表
openBusinessInterface="openBusinessInterface"//商家接口配置
......@@ -48,9 +49,11 @@ func main() {
}
func generate() {
whiteIp()
businessList()
//whiteIp()
//businessList()
businessInterfaceList()
newWhiteBlack()
newBusinessList()
}
//白名单配置
......@@ -76,7 +79,9 @@ func whiteIp() {
"192.168.2.129",
"19.123.78.115",
"222.92.21.99",
"192.168.2.72",
"::1",
"119.123.79.225",
}
/*whiteList:=[]string{
......@@ -97,10 +102,31 @@ func businessList() {
jdToken,//京东
//baiduToken,//百度
}
fmt.Println("商家列表:key:"+redisKey)
redisStringSet(redisKey,businessList)
}
func newBusinessList() {
config:=map[string]string{
openTokenBusinessList:jdToken,
}
fmt.Println("新商家列表配置:")
redisStringSadd(config)
}
func newWhiteBlack() {
jdWhiteList:=jdToken+"_"+openWhiteList
jdBlackList:=jdToken+"_"+openBlackList
config:=map[string]string{
jdWhiteList:"192.168.2.72 222.92.21.99 192.168.2.129 ::1",
jdBlackList:"::1 ",
}
fmt.Println("新白名单配置:")
redisStringSadd(config)
}
//商家接口配置
func businessInterfaceList() {
......@@ -110,24 +136,26 @@ func businessInterfaceList() {
//获取sku列表(根据class_id查询) 500,10000000
//获取sku列表(full-显示所有字段) 50,10000000
jdToken+"_"+GetSkuListByClass: map[string]interface{}{
"dayMaxNum":6100,
"minMaxNum":2000,
"dayMaxNum":1000000,
"minMaxNum":300,
"totalMaxNum":10000,
},
jdToken+"_"+GetSkuListFull: map[string]interface{}{
"dayMaxNum":6100,
"minMaxNum":2000,
"dayMaxNum":1000000,
"minMaxNum":300,
"totalMaxNum":10000,
},
jdToken+"_"+GetSkuListPrice: map[string]interface{}{
"dayMaxNum":6100,
"minMaxNum":2000,
"dayMaxNum":1000000,
"minMaxNum":300,
"totalMaxNum":10000,
},
jdToken+"_"+GetClassList: map[string]interface{}{
"dayMaxNum":6100,
"minMaxNum":2000,
"dayMaxNum":1000000,
"minMaxNum":300,
"totalMaxNum":10000,
},
/*baiduToken+"_"+getSkuListByClass: map[string]interface{}{
"dayMaxNum":100,
......@@ -139,14 +167,17 @@ func businessInterfaceList() {
redisHashSet(redisKey,config)
}
func redisHashSet(key string,values map[string]interface{}) {
for hashk,valueOne:=range values{
b,_:=json.Marshal(valueOne)
b,_=json.Marshal(string(b))
hsetStr:="Hset "+key+" "+string(hashk)+" "+string(b)
fmt.Println(hsetStr)
//fmt.Println("对应计时的min key\nflowUse_"+hashk+"_min")
//fmt.Println("对应计时的day key\nflowUse_"+hashk+"_day")
fmt.Println("对应计时的min key\nflowUse_"+hashk+"_min")
fmt.Println("对应计时的day key\nflowUse_"+hashk+"_day")
}
}
......@@ -155,5 +186,13 @@ func redisStringSet(key string,sliceString []string) {
b,_=json.Marshal(string(b))
setStr:="set "+key+" "+string(b)
fmt.Println(setStr)
}
func redisStringSadd(config map[string]string) {
for hashk,valueOne:=range config{
SaddStr:="SADD "+hashk+" "+valueOne
fmt.Println(SaddStr)
//fmt.Println("对应计时的min key\nflowUse_"+hashk+"_min")
//fmt.Println("对应计时的day key\nflowUse_"+hashk+"_day")
}
}
\ No newline at end of file
......@@ -8,12 +8,12 @@ import (
)
//过滤
func SkuFilter(remoteData RemoteSkuData,filedStr []string) (data RemoteSkuData) {
remoteData=filterData(remoteData,filedStr)
if(len(remoteData)==0){
func SkuFilter(remoteData RemoteSkuData,filedStr []string) (ArraySkuData) {
arraySku:=filterData(remoteData,filedStr)
if(len(arraySku)==0){
return nil
}
return remoteData
return arraySku
}
/**
......@@ -22,9 +22,13 @@ func SkuFilter(remoteData RemoteSkuData,filedStr []string) (data RemoteSkuData)
@param filterField 需要过滤的字段
@return data 返回新的数据
*/
func filterData(remoteData RemoteSkuData,filterField []string)(data RemoteSkuData){
func filterData(remoteData RemoteSkuData,filterField []string)(ArraySkuData){
//arraySku:=make([]map[string]interface{},0)
arraySkud:=ArraySkuData{}
for skuId,skuInfo:=range remoteData{
newItem:=make(map[string]interface{})
/*if _,ok:=skuInfo.(bool);ok{
delete(remoteData,skuId)
continue
......@@ -41,14 +45,15 @@ func filterData(remoteData RemoteSkuData,filterField []string)(data RemoteSkuDat
//common.PrintStdout().Printf("skuId:%s,字段:%s 不存在",string(skuId),field)
}
}
remoteData[skuId]=newItem
arraySkud=append(arraySkud,newItem)
//remoteData[skuId]=newItem
}else{//有问题的格式
common.PrintStdout().Printf("skuId:%s,是个有问题的格式",string(skuId))
delete(remoteData,skuId)
//delete(remoteData,skuId)
continue
}
}
return remoteData
return arraySkud
}
func filterLadder(ladder interface{}) interface{}{
......
......@@ -10,7 +10,7 @@ type QuerySkuCreq struct {
//根据class获取sku列表 返回参数
type QuerySkuCrsp struct {
Count int `json:"count"`
SkuData map[string]interface{} `json:"sku_data"`
SkuData ArraySkuData `json:"sku_data"`
PageSize int `json:"page_size"`
Page int `json:"page"`
}
......@@ -29,9 +29,12 @@ type QuerySkuReq struct {
//根据goodsId获取sku列表 返回参数
type QuerySkuRsp struct {
SkuData map[string]interface{} `json:"sku_data"`
SkuData ArraySkuData `json:"sku_data"`
}
//============end=============
//商详接口返回的数据 data字段
type RemoteSkuData map[string]interface{}
//
type ArraySkuData []map[string]interface{}
......@@ -16,7 +16,9 @@ const (
//白名单配置813
WHILTREDISEERR1 = 813505//白名单读取redis错误
WHILTREDISEERR2 = 813404//ip不在白名单里面
WHILTREDISEERR5 = 813408//黑名单限制
WHILTREDISEERR3 = 813406//没有设置白名单
WHILTREDISEERR4 = 813407//没有设置黑名单
//锁814
FLOWERR1 = 814505//lua脚本执行出错
......
......@@ -2,7 +2,6 @@ package open
import (
"context"
"fmt"
"github.com/gogf/gf/util/gconv"
"github.com/gomodule/redigo/redis"
"golang_open_platform/pkg/common"
......@@ -93,8 +92,8 @@ func (this *flowmeter) checkout(business *business) error{
minMax:=interfaceConfig.minMaxNum
dayKey,_:=this.getFlowKeyAndEx(INCRTYPEDAY)
minKey,_:=this.getFlowKeyAndEx(INCRTYPEMIN)
fmt.Println(dayKey)
fmt.Println(minKey)
/*fmt.Println(dayKey)
fmt.Println(minKey)*/
script:=redis.NewScript(2,incrLua)
//脚本执行5秒算它卡死,用script kill杀死此次脚本
context,cancel:=context.WithTimeout(context.Background(),time.Second*1)
......
......@@ -2,9 +2,11 @@ package open
import (
"github.com/gin-gonic/gin"
"github.com/gomodule/redigo/redis"
"github.com/tidwall/gjson"
"golang_open_platform/pkg/common"
"golang_open_platform/pkg/e"
"golang_open_platform/pkg/gredis"
"strconv"
)
......@@ -17,6 +19,36 @@ func NewopenValidate(business *business) *openValidate{
return &openValidate{business:business,dao:&Dao{}}
}
//ip限制验证(白名单黑名单)
func (this *openValidate) ipValidate(ctx *gin.Context,token string) error {
redisCon := gredis.Conn("search_w")
defer redisCon.Close()
ip:=ctx.ClientIP()
openWhiteList:=token+"_openWhiteList"//白名单
openBlackList:=token+"_openBlackList"//黑名单
boolInt,err:=redis.Int(redisCon.Do("SISMEMBER",openBlackList,ip))
if(err!=nil && err!=redis.ErrNil){
common.PrintStdout().Printf("读取黑名单redis 出错")
return e.NewApiError("service err",WHILTREDISEERR5)
}
if(boolInt==1){
common.PrintStdout().Printf("黑名单限制:"+ctx.ClientIP())
return e.NewApiError("ip黑名单限制",WHILTREDISEERR5)
}
boolInt,err=redis.Int(redisCon.Do("SISMEMBER",openWhiteList,ip))
if(err!=nil && err!=redis.ErrNil){
common.PrintStdout().Printf("读取白名单redis 出错")
return e.NewApiError("service err",WHILTREDISEERR1)
}
if(boolInt!=1){
common.PrintStdout().Printf("ip不在白名单:"+ctx.ClientIP())
return e.NewApiError("ip白名单限制",WHILTREDISEERR2)
}
return nil
}
//白名单验证
func (this *openValidate) whiteValidate(ctx *gin.Context)error {
......@@ -42,21 +74,21 @@ func (this *openValidate) whiteValidate(ctx *gin.Context)error {
根据token到 商家表查询,如果存在,token就存在
*/
func (this *openValidate) exitValidate(token string) error{
businessList,err:=this.dao.getBusinessList()
if(err!=nil){
return err
}
business:=gjson.Parse(businessList)
if (businessList == "" || business.IsArray()!=true || len(business.Array())<=0){
common.PrintStdout().Printf(strconv.Itoa(WHILTREDISEERR3)+":token 没有设置")
redisCon := gredis.Conn("search_w")
defer redisCon.Close()
key:="openTokenBusinessList"
return e.NewApiError("white err",WHILTREDISEERR3)
}
if(InArrayGjson(token,business.Array())){
return nil
boolInt,err:=redis.Int(redisCon.Do("SISMEMBER",key,token))
if(err!=nil && err!=redis.ErrNil){
common.PrintStdout().Printf("读取供应商列表出错 出错")
return e.NewApiError("service err",BUSINESSERR1)
}
if(boolInt!=1){
common.PrintStdout().Printf(strconv.Itoa(BUSINESSERR2)+":token 无效:"+token)
return e.NewApiError("token invalid",BUSINESSERR2)
}
return nil
}
/**
......@@ -74,7 +106,7 @@ func (this *openValidate) permissionValidate(interfaceName string) error{
//check验证
func (this *openValidate) Check(ctx *gin.Context,interfaceName string) error {
err:=this.whiteValidate(ctx)
err:=this.ipValidate(ctx,this.business.token)
if(err!=nil){
return err
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment