Commit 578ce9ad by mushishixian

fix

parents ced647fb c248f4ac
......@@ -5,7 +5,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/micro/go-micro/v2/web"
"search_server/boot"
_ "search_server/controller"
//_ "search_server/controller"
"search_server/pkg/config"
"search_server/routes"
)
......
......@@ -7,6 +7,8 @@ require (
github.com/gin-contrib/cors v1.3.1
github.com/gin-gonic/gin v1.6.3
github.com/go-ini/ini v1.57.0
github.com/go-sql-driver/mysql v1.5.0
github.com/go-xorm/xorm v0.7.9
github.com/gogo/protobuf v1.3.1 // indirect
github.com/golang/protobuf v1.4.2
github.com/gomodule/redigo v2.0.1-0.20180401191855-9352ab68be13+incompatible
......@@ -33,8 +35,8 @@ require (
github.com/stretchr/testify v1.5.1 // indirect
github.com/syyongx/php2go v0.9.4
github.com/tidwall/gjson v1.6.0
github.com/urfave/cli v1.22.4 // indirect
github.com/uniplaces/carbon v0.1.6
github.com/urfave/cli v1.22.4 // indirect
go.etcd.io/bbolt v1.3.4
go.uber.org/zap v1.14.1 // indirect
golang.org/x/net v0.0.0-20200520182314-0ba52f642ac2 // indirect
......
......@@ -13,42 +13,17 @@ type RedisDatabase struct {
//多数据库配置
func BuildRedisConfgs() (RedisDatabaseMap map[string]RedisDatabase) {
return map[string]RedisDatabase{
"search_read": {
"search_r": {
Host: Get("default_redis_read.host").String(),
Password: Get("default_redis_read.password").String(),
MaxIdle: Get("default_redis_read.max_idle").String(),
MaxActive: Get("default_redis_read.max_active").String(),
},
"search_write": {
"search_w": {
Host: Get("default_redis_write.host").String(),
Password: Get("default_redis_write.password").String(),
MaxIdle: Get("default_redis_read.max_idle").String(),
MaxActive: Get("default_redis_read.max_active").String(),
},
"spu_read": {
Host: Get("default_redis_read.host").String(),
Password: Get("default_redis_read.password").String(),
MaxIdle: Get("default_redis_read.max_idle").String(),
MaxActive: Get("default_redis_read.max_active").String(),
},
"spu_write": {
Host: Get("default_redis_write.host").String(),
Password: Get("default_redis_write.password").String(),
MaxIdle: Get("default_redis_read.max_idle").String(),
MaxActive: Get("default_redis_read.max_active").String(),
},
"api_read": {
Host: Get("api_redis_read.host").String(),
Password: Get("api_redis_read.password").String(),
MaxIdle: Get("api_redis_read.max_idle").String(),
MaxActive: Get("api_redis_read.max_active").String(),
},
"api_write": {
Host: Get("api_redis_write.host").String(),
Password: Get("api_redis_write.password").String(),
MaxIdle: Get("api_redis_write.max_idle").String(),
MaxActive: Get("api_redis_write.max_active").String(),
},
}
}
package gredis
import (
"encoding/json"
"github.com/gomodule/redigo/redis"
"search_server/pkg/config"
"time"
)
type ichuntRedis struct {
RedisList map[string]*redis.Pool
}
var ichuntRedis_ = &ichuntRedis{}
func Conn(connection string) (redis.Conn){
return ichuntRedis_.RedisList[connection].Get()
}
var writeConn, readConn *redis.Pool
func Setup() (err error) {
writeHost := config.Get("redis.write_host").String()
readHost := config.Get("redis.read_host").String()
writePassword := config.Get("redis.write_password").String()
readPassword := config.Get("redis.read_password").String()
writeConn, err = getConn(writeHost, writePassword)
if err != nil {
return
}
readConn, err = getConn(readHost, readPassword)
if err != nil {
return
ichuntRedis_.RedisList = make(map[string]*redis.Pool,0)
RedisDatabaseMap := config.BuildRedisConfgs()
for redisKey,redisConfig := range RedisDatabaseMap{
ichuntRedis_.RedisList[redisKey],err = getConn(redisConfig.Host, redisConfig.Password)
if err != nil{
panic(err)
}
}
return nil
}
......@@ -50,92 +57,4 @@ func getConn(writeHost, password string) (pool *redis.Pool, err error) {
},
}
return
}
//最基础的键值操作
func Set(key string, data interface{}) error {
conn := writeConn.Get()
defer conn.Close()
value, err := json.Marshal(data)
if err != nil {
return err
}
_, err = conn.Do("SET", key, value)
if err != nil {
return err
}
return nil
}
func Exists(key string) bool {
conn := readConn.Get()
defer conn.Close()
exists, err := redis.Bool(conn.Do("EXISTS", key))
if err != nil {
return false
}
return exists
}
func Get(key string) (interface{}, error) {
conn := readConn.Get()
defer conn.Close()
reply, err := conn.Do("GET", key)
if err != nil {
return nil, err
}
return reply, nil
}
func Delete(key string) (bool, error) {
conn := writeConn.Get()
defer conn.Close()
return redis.Bool(conn.Do("DEL", key))
}
//哈希操作
func HSet(key string, k interface{}, data interface{}) error {
conn := writeConn.Get()
defer conn.Close()
value, err := json.Marshal(data)
if err != nil {
return err
}
_, err = conn.Do("HSET", key, k, value)
if err != nil {
return err
}
return nil
}
func HGet(key string, k interface{}) (interface{}, error) {
conn := readConn.Get()
defer conn.Close()
reply, err := conn.Do("HGET", key, k)
if err != nil {
return nil, err
}
return reply, nil
}
func HDelete(key string, k interface{}) (bool, error) {
conn := writeConn.Get()
defer conn.Close()
return redis.Bool(conn.Do("HDEL", key, k))
}
}
\ No newline at end of file
......@@ -2,21 +2,19 @@ package mysql
import (
"fmt"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
"github.com/go-xorm/xorm"
_ "github.com/go-sql-driver/mysql"
"search_server/pkg/config"
"search_server/pkg/e"
)
//mysql相关
var mysqlDb, bomDb *gorm.DB
var DatabaseConMap map[string]*xorm.Engine
var DatabaseConMap = map[string]*gorm.DB{
"mysql": mysqlDb,
"bom": bomDb,
}
func Setup() error {
func Setup() error{
DatabaseConMap = make(map[string]*xorm.Engine,0)
DatabaseList := config.BuildDatabaseList()
var err error
//循环生成数据库链接
......@@ -26,34 +24,27 @@ func Setup() error {
host := db.Host
database := db.Database
if db.Prefix != "" {
gorm.DefaultTableNameHandler = func(DB *gorm.DB, defaultTableName string) string {
return db.Prefix + defaultTableName
}
}
dataSourceName := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8",userName,password,host,database)
DatabaseConMap[conName], err = gorm.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:3306)/%s?charset=utf8&parseTime=True&loc=Local",
userName, password, host, database))
if err != nil {
DatabaseConMap[conName], err = xorm.NewEngine("mysql", dataSourceName)
if err!=nil{
return e.NewFatalError(err.Error()) //这里返回致命异常
}
DatabaseConMap[conName].SingularTable(true)
if db.MaxIdleCons == 0 {
db.MaxIdleCons = 10
}
if db.MaxOpenCons == 0 {
db.MaxOpenCons = 100
}
DatabaseConMap[conName].DB().SetMaxIdleConns(db.MaxIdleCons)
DatabaseConMap[conName].DB().SetMaxOpenConns(db.MaxOpenCons)
if config.Get("gorm.mode").String() == "debug" {
DatabaseConMap[conName].LogMode(true)
}
//日志打印SQL
DatabaseConMap[conName].ShowSQL(true)
//设置连接池的空闲数大小
DatabaseConMap[conName].SetMaxIdleConns(db.MaxIdleCons)
//设置最大打开连接数
DatabaseConMap[conName].SetMaxOpenConns(db.MaxOpenCons)
}
return nil
}
func Connection(conName string) *gorm.DB {
func Conn(conName string) *xorm.Engine {
return DatabaseConMap[conName]
}
......@@ -18,7 +18,7 @@ func InitRouter() *gin.Engine {
r.POST("search/ZiYing/zyh", controller.Zyh)
//快手平台相关
r.GET("/search/quote", controller.QuoteIndex)
//r.GET("/search/quote", controller.QuoteIndex)
return r
}
......@@ -3,6 +3,7 @@ package service
import (
"fmt"
"github.com/gomodule/redigo/redis"
_ "github.com/gomodule/redigo/redis"
"github.com/tidwall/gjson"
"gopkg.in/olivere/elastic.v5"
"regexp"
......@@ -10,6 +11,7 @@ import (
"search_server/pkg/config"
"search_server/pkg/es"
"search_server/pkg/gredis"
_ "search_server/pkg/gredis"
"strconv"
"strings"
)
......@@ -46,7 +48,9 @@ func SearchAttr(attrOrigin, encap string) (goodsName string, err error) {
numberR, _ := regexp.Compile(common.PureNumberRegular)
pureNumber := numberR.FindString(encap)
//再去找对应属性
attrName, _ := redis.String(gredis.HGet("sku_map2", pureNumber))
redisConn := gredis.Conn("default_redis_read")
defer redisConn.Close()
attrName, _ := redis.String(redisConn.Do("HGET", "sku_map2", pureNumber))
if attrName != "" {
attrValue := attrName + "€" + pureNumber
attrsSlice = append(attrsSlice, attrValue)
......@@ -199,7 +203,9 @@ func getAttrValueByAttr(attr string) (attrValue string) {
}
} else {
//再去找没有单位的对应属性
attrName, _ = redis.String(gredis.HGet("sku_map2", attr))
redisConn := gredis.Conn("default_redis_read")
defer redisConn.Close()
attrName, _ := redis.String(redisConn.Do("HGET", "sku_map2", pureNumber))
if attrName != "" {
attrValue = attrName + "€" + attr
} else {
......
......@@ -6,6 +6,7 @@ import (
_ "github.com/tidwall/gjson"
"search_server/model"
"search_server/pkg/config"
_ "search_server/pkg/gredis"
"strings"
)
......@@ -49,36 +50,42 @@ func getSkuByGoodsSn(goods_list map[string]*model.LyClearGoodsList, supplier_inf
//sku_id, _ := gredis.IchuntRedis.Hget(sku_uique_judge, sn_sku) //查询唯一值,反查sku_id
sku_id := ""
if sku_id == "" { //为空,先创建sku
//lock_key := "searchapi_"+sn_sku;
//flag := gredis.Setnx(lock_key,php2go.Time()+2)
//if flag { //存在锁
//
//}
//sku_id, _ := gredis.HGet(sku_uique_judge, sn_sku) //查询唯一值,反查sku_id
fmt.Println(sku_uique_judge, sn_sku)
sku_id := ""
if sku_id == "" { //为空,先创建sku
//lock_key := "searchapi_"+sn_sku;
//flag := gredis.Setnx(lock_key,php2go.Time()+2)
//if flag { //存在锁
//
//}
}
print(info)
//productList[goodsSn] = &LyClearGoodsList
}
print(info)
//productList[goodsSn] = &LyClearGoodsList
}
fmt.Println(originGoods)
return originGoods
fmt.Println(originGoods)
return originGoods
}
return nil
}
/*
新增或者更新redis
新增或者更新redis
*/
func toRedis() {
}
/*
推入队列
推入队列
*/
func toGoRabmq() {
}
/*
更新es
更新es
*/
func toEs() {
......
......@@ -188,7 +188,9 @@ func CurlGoodsInfo(goodsIdsStr string, params req.Param) (goodsList []*model.Api
func CheckIsNewCustomer(userId int) (isNewCustomer, isMember bool) {
//判断新客价
if userId != 0 {
userInfoStr, err := redis.String(gredis.HGet("api_user", userId))
redisConn := gredis.Conn("default_redis_read")
defer redisConn.Close()
userInfoStr, err := redis.String(redisConn.Do("HGET", "api_user", userId))
if err != nil {
return
}
......
......@@ -6,6 +6,7 @@ import (
"reflect"
"search_server/model"
"search_server/pkg/config"
"search_server/pkg/gredis"
)
type ZyServiceImpl struct{}
......@@ -59,5 +60,12 @@ func zy(ctx *gin.Context, req *model.ZyhRequest,_type byte,isCheck byte) (rsp *m
fmt.Printf("%+v",req)
fmt.Println(reflect.TypeOf(req.ClassIdcondition))
fmt.Println(req.ClassIdcondition)
redis_s_r :=gredis.Conn("search_r")
defer func(){
redis_s_r.Close()
}()
return nil
}
package main
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