This commit is contained in:
crusader 2018-04-10 14:21:48 +09:00
parent 3a7af25014
commit b55a07327e
4 changed files with 126 additions and 33 deletions

41
external/external.go vendored
View File

@ -1,9 +1,44 @@
package external package external
func InitPackage() { import (
occe "git.loafle.net/overflow/commons-go/config/external"
"git.loafle.net/overflow/gateway/external/grpc"
"git.loafle.net/overflow/gateway/external/kafka"
"git.loafle.net/overflow/gateway/external/redis"
)
func InitPackage(config *occe.ExternalConfig) {
if nil == config {
return
}
grpc.InitPackage(config.GRPC)
redis.InitPackage(config.Redis)
kafka.InitPackage(config.Kafka)
} }
func DestroyPackage() { func StartPackage(config *occe.ExternalConfig) {
if nil == config {
return
}
grpc.StartPackage(config.GRPC)
redis.StartPackage(config.Redis)
kafka.StartPackage(config.Kafka)
}
func StopPackage(config *occe.ExternalConfig) {
if nil == config {
return
}
grpc.StopPackage(config.GRPC)
redis.StopPackage(config.Redis)
kafka.StopPackage(config.Kafka)
}
func DestroyPackage(config *occe.ExternalConfig) {
if nil == config {
return
}
grpc.DestroyPackage(config.GRPC)
redis.DestroyPackage(config.Redis)
kafka.DestroyPackage(config.Kafka)
} }

24
external/grpc/grpc.go vendored
View File

@ -8,12 +8,16 @@ import (
"git.loafle.net/commons/logging-go" "git.loafle.net/commons/logging-go"
oci "git.loafle.net/overflow/central_api/golang" oci "git.loafle.net/overflow/central_api/golang"
occeg "git.loafle.net/overflow/commons-go/config/external/grpc"
"google.golang.org/grpc" "google.golang.org/grpc"
) )
var grpcClient oci.OverflowApiServerClient var grpcClient oci.OverflowApiServerClient
func InitPackage() { func InitPackage(config *occeg.GRPConfig) {
if nil == config {
return
}
go func() { go func() {
conn, err := grpc.Dial("192.168.1.50:50006", grpc.WithInsecure()) conn, err := grpc.Dial("192.168.1.50:50006", grpc.WithInsecure())
if nil != err { if nil != err {
@ -21,10 +25,26 @@ func InitPackage() {
} }
grpcClient = oci.NewOverflowApiServerClient(conn) grpcClient = oci.NewOverflowApiServerClient(conn)
}() }()
}
func StartPackage(config *occeg.GRPConfig) {
if nil == config {
return
}
} }
func DestroyPackage() { func StopPackage(config *occeg.GRPConfig) {
if nil == config {
return
}
}
func DestroyPackage(config *occeg.GRPConfig) {
if nil == config {
return
}
} }

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"git.loafle.net/commons/logging-go" "git.loafle.net/commons/logging-go"
occek "git.loafle.net/overflow/commons-go/config/external/kafka"
"github.com/segmentio/kafka-go" "github.com/segmentio/kafka-go"
) )
@ -12,22 +13,38 @@ var (
kafkaWriter *kafka.Writer kafkaWriter *kafka.Writer
) )
func InitPackage() { func InitPackage(config *occek.KafkaConfig) {
go func() {
kafkaWriter = kafka.NewWriter(kafka.WriterConfig{ kafkaWriter = kafka.NewWriter(kafka.WriterConfig{
Brokers: []string{"192.168.1.50:9092"}, Brokers: []string{"192.168.1.50:9092"},
Topic: "overflow-metric-topic", Topic: "overflow-metric-topic",
Balancer: &kafka.LeastBytes{}, Balancer: &kafka.LeastBytes{},
}) })
}()
} }
func DestroyPackage() { func StartPackage(config *occek.KafkaConfig) {
go func() { if nil == config {
if err := kafkaWriter.Close(); nil != err { return
logging.Logger().Errorf("%v", err) }
}
}() }
func StopPackage(config *occek.KafkaConfig) {
if nil == config {
return
}
}
func DestroyPackage(config *occek.KafkaConfig) {
if nil == config {
return
}
if err := kafkaWriter.Close(); nil != err {
logging.Logger().Errorf("%v", err)
}
} }
func Write(key []byte, value []byte) error { func Write(key []byte, value []byte) error {

View File

@ -1,26 +1,47 @@
package redis package redis
import ( import (
occer "git.loafle.net/overflow/commons-go/config/external/redis"
"github.com/gomodule/redigo/redis" "github.com/gomodule/redigo/redis"
) )
var Pool *redis.Pool var Pool *redis.Pool
func InitPackage() { func InitPackage(config *occer.RedisConfig) {
go func() { if nil == config {
Pool = &redis.Pool{ return
MaxIdle: 1, }
MaxActive: 3,
IdleTimeout: 240, Pool = &redis.Pool{
Wait: true, MaxIdle: 1,
MaxConnLifetime: 1, MaxActive: 3,
Dial: func() (redis.Conn, error) { IdleTimeout: 240,
return redis.Dial("tcp", "192.168.1.50:6379") Wait: true,
}, MaxConnLifetime: 1,
} Dial: func() (redis.Conn, error) {
}() return redis.Dial("tcp", "192.168.1.50:6379")
},
}
} }
func DestroyPackage() { func StartPackage(config *occer.RedisConfig) {
if nil == config {
return
}
}
func StopPackage(config *occer.RedisConfig) {
if nil == config {
return
}
}
func DestroyPackage(config *occer.RedisConfig) {
if nil == config {
return
}
Pool.Close() Pool.Close()
} }