package main import ( "context" "fmt" "github.com/grpc-ecosystem/go-grpc-prometheus" "github.com/importcjj/sensitive" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" "go.mongodb.org/mongo-driver/bson/primitive" "google.golang.org/grpc" "gopkg.in/olivere/elastic.v1" "gopkg.in/yaml.v2" "io/ioutil" "log" "math/big" "net" "net/http" "sensitiveWords.udp/proto_grpc" "sensitiveWords.udp/util" "strconv" "strings" ) const ( YAMLFILE = "./server.yaml" ) var YamlConfig YAMLConfig var MixDataMgo *util.MongodbSim var Filter *sensitive.Filter var es_type, es_index string var Client_Es *elastic.Client func init() { yamlFile, err := ioutil.ReadFile(YAMLFILE) if err != nil { log.Fatalln("load conf error") } err = yaml.Unmarshal(yamlFile, &YamlConfig) if err != nil { fmt.Println(err.Error()) } log.Printf("%#v", YamlConfig) MixDataMgo = &util.MongodbSim{ MongodbAddr: YamlConfig.MixdataMgoAddr, Size: YamlConfig.MongodbPoolSize, DbName: YamlConfig.DbName, UserName: YamlConfig.UserName, PassWord: YamlConfig.PassWord, } MixDataMgo.InitPool() Client_Es, _ = elastic.NewClient(http.DefaultClient, "http://192.168.3.11:9800") es_type, es_index = "azktest", "azktest" reg.MustRegister(grpcMetrics, customizedCounterMetric) } var ( // Create a metrics registry. reg = prometheus.NewRegistry() // Create some standard server metrics. grpcMetrics = grpc_prometheus.NewServerMetrics() // Create a customized counter metric. customizedCounterMetric = prometheus.NewCounterVec(prometheus.CounterOpts{ Name: "demo_server_search_method_handle_count", Help: "Total number of RPCs handled on the server.", }, []string{"name"}) ) func main() { //淡赌跑断 if YamlConfig.IsAddTask == 0 { initSensitiveWordsData() //初始化敏感词数据 } else { go addTaskSensitiveWordsData() //增量-改配置文件 } lis, err := net.Listen("tcp", YamlConfig.Port) if err != nil { log.Fatalf("failed to listen: %v", err) } defer lis.Close() // Create a HTTP server for prometheus. httpServer := &http.Server{ Handler: promhttp.HandlerFor(reg, promhttp.HandlerOpts{}), Addr: fmt.Sprintf("0.0.0.0:%d", 2092), } grpcServer := grpc.NewServer( //grpc.StreamInterceptor(grpcMetrics.StreamServerInterceptor()), grpc.UnaryInterceptor(grpcMetrics.UnaryServerInterceptor())) proto_grpc.RegisterSensitiveWordsServer(grpcServer, &server{}) // Initialize all metrics. grpcMetrics.InitializeMetrics(grpcServer) // Start your http server for prometheus. go func() { if err := httpServer.ListenAndServe(); err != nil { log.Fatal("Unable to start a http server.") } }() log.Println("server start:", YamlConfig.Port) if err := grpcServer.Serve(lis); err != nil { log.Fatalf("failed to serve: %v", err) } } //协议方法---等 type server struct { proto_grpc.SensitiveWordsServer } func (s *server) Search(ctx context.Context, in *proto_grpc.Request) (*proto_grpc.ResultSensitiveWords, error) { customizedCounterMetric.WithLabelValues("search_server").Inc() text := in.GetText() //log.Println(text) findAll := Filter.FindAll(text) rada := strings.Join(findAll, ",") return &proto_grpc.ResultSensitiveWords{SensitiveWords: rada}, nil } func (s *server) Registration(ctx context.Context, in *proto_grpc.NumberOfRegistrations) (*proto_grpc.RequestComplete, error) { customizedCounterMetric.WithLabelValues("Registration").Inc() return &proto_grpc.RequestComplete{Text: "ok"}, nil } type YAMLConfig struct { MixdataMgoAddr string `yaml:"mixdataMgoAddr"` UserName string `yaml:"userName"` PassWord string `yaml:"passWord"` DbName string `yaml:"dbName"` MongodbPoolSize int `yaml:"mongodbPoolSize"` TaskGteId string `yaml:"taskGteId"` TaskLteId string `yaml:"taskLteId"` IsAddTask int `yaml:"isAddTask"` Port string `yaml:"port"` } //其他方法 func StringTOBsonId(id string) primitive.ObjectID { objectId, _ := primitive.ObjectIDFromHex(id) return objectId } func BsonTOStringId(id interface{}) string { return id.(primitive.ObjectID).Hex() } func toMegaBytes(bytes uint64) float64 { return float64(bytes) / 1024 / 1024 } func IntAll(num interface{}) int { return IntAllDef(num, 0) } func Int64All(num interface{}) int64 { if i, ok := num.(int64); ok { return int64(i) } else if i0, ok0 := num.(int32); ok0 { return int64(i0) } else if i1, ok1 := num.(float64); ok1 { return int64(i1) } else if i2, ok2 := num.(int); ok2 { return int64(i2) } else if i3, ok3 := num.(float32); ok3 { return int64(i3) } else if i4, ok4 := num.(string); ok4 { i64, _ := strconv.ParseInt(i4, 10, 64) //in, _ := strconv.Atoi(i4) return i64 } else if i5, ok5 := num.(int16); ok5 { return int64(i5) } else if i6, ok6 := num.(int8); ok6 { return int64(i6) } else if i7, ok7 := num.(*big.Int); ok7 { in, _ := strconv.ParseInt(fmt.Sprint(i7), 10, 64) return int64(in) } else if i8, ok8 := num.(*big.Float); ok8 { in, _ := strconv.ParseInt(fmt.Sprint(i8), 10, 64) return int64(in) } else { return 0 } } func Float64All(num interface{}) float64 { if i, ok := num.(float64); ok { return float64(i) } else if i0, ok0 := num.(int32); ok0 { return float64(i0) } else if i1, ok1 := num.(int64); ok1 { return float64(i1) } else if i2, ok2 := num.(int); ok2 { return float64(i2) } else if i3, ok3 := num.(float32); ok3 { return float64(i3) } else if i4, ok4 := num.(string); ok4 { in, _ := strconv.ParseFloat(i4, 64) return in } else if i5, ok5 := num.(int16); ok5 { return float64(i5) } else if i6, ok6 := num.(int8); ok6 { return float64(i6) } else if i6, ok6 := num.(uint); ok6 { return float64(i6) } else if i6, ok6 := num.(uint8); ok6 { return float64(i6) } else if i6, ok6 := num.(uint16); ok6 { return float64(i6) } else if i6, ok6 := num.(uint32); ok6 { return float64(i6) } else if i6, ok6 := num.(uint64); ok6 { return float64(i6) } else if i7, ok7 := num.(*big.Float); ok7 { in, _ := strconv.ParseFloat(fmt.Sprint(i7), 64) return float64(in) } else if i8, ok8 := num.(*big.Int); ok8 { in, _ := strconv.ParseFloat(fmt.Sprint(i8), 64) return float64(in) } else { return 0 } } func IntAllDef(num interface{}, defaultNum int) int { if i, ok := num.(int); ok { return int(i) } else if i0, ok0 := num.(int32); ok0 { return int(i0) } else if i1, ok1 := num.(float64); ok1 { return int(i1) } else if i2, ok2 := num.(int64); ok2 { return int(i2) } else if i3, ok3 := num.(float32); ok3 { return int(i3) } else if i4, ok4 := num.(string); ok4 { in, _ := strconv.Atoi(i4) return int(in) } else if i5, ok5 := num.(int16); ok5 { return int(i5) } else if i6, ok6 := num.(int8); ok6 { return int(i6) } else if i7, ok7 := num.(*big.Int); ok7 { in, _ := strconv.Atoi(fmt.Sprint(i7)) return int(in) } else if i8, ok8 := num.(*big.Float); ok8 { in, _ := strconv.Atoi(fmt.Sprint(i8)) return int(in) } else { return defaultNum } } func ObjToString(old interface{}) string { if nil == old { return "" } else { r, _ := old.(string) return r } } func ObjToStringDef(old interface{}, defaultstr string) string { if nil == old { return defaultstr } else { r, _ := old.(string) if r == "" { return defaultstr } return r } } //对象数组转成string数组 func ObjArrToStringArr(old []interface{}) []string { if old != nil { new := make([]string, len(old)) for i, v := range old { new[i] = v.(string) } return new } else { return nil } } //对象数组转成map数组 func ObjArrToMapArr(old []interface{}) []map[string]interface{} { if old != nil { new := make([]map[string]interface{}, len(old)) for i, v := range old { new[i] = v.(map[string]interface{}) } return new } else { return nil } } //map数组转成对象数组 func MapArrToObjArr(old []map[string]interface{}) []interface{} { if old != nil { new := make([]interface{}, len(old)) for i, v := range old { new[i] = v } return new } else { return nil } }