Compare commits
33 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| faace02cb4 | |||
| 7c69590a1d | |||
| d5f0314b58 | |||
| 4f183541e6 | |||
| 003bf2bb88 | |||
| 03ec5a0a5a | |||
| fb251e3648 | |||
| 32a89d9747 | |||
| 3b6acdc195 | |||
| 128e3db0b6 | |||
| e0a18538d4 | |||
| d99436d2d6 | |||
| bc27da0be3 | |||
| b8cfbf6b99 | |||
| 2204f6116b | |||
| 65dbbc16df | |||
| e5c95b0476 | |||
| 34ace207a7 | |||
| ad4368cdf8 | |||
| a4f4b6e059 | |||
| 5bf58aad16 | |||
| ba6bd4137c | |||
| 48a7e968d1 | |||
| 6c46a0e6a4 | |||
| ceaeecf1b4 | |||
| 0b035dfcc9 | |||
| 73d302d2c3 | |||
| 1566f31f42 | |||
| c85d881954 | |||
| 0326e985cd | |||
| 5888636310 | |||
| e76fe0fead | |||
| 6188022ad6 |
154
consul/discovery.go
Normal file
154
consul/discovery.go
Normal file
@@ -0,0 +1,154 @@
|
|||||||
|
package consul
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
consul "github.com/hashicorp/consul/api"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Service struct {
|
||||||
|
AppID string
|
||||||
|
Name string
|
||||||
|
Domain string
|
||||||
|
Address string
|
||||||
|
Port int
|
||||||
|
TTL time.Duration
|
||||||
|
ConsulAgent *consul.Agent
|
||||||
|
}
|
||||||
|
|
||||||
|
var ErrServiceUnavailable = fmt.Errorf("Service is unavailable")
|
||||||
|
|
||||||
|
func NewService(servAddr, id, name, hostname, domain string, appPort int) (*Service, error) {
|
||||||
|
s := new(Service)
|
||||||
|
s.AppID = id
|
||||||
|
s.Name = name
|
||||||
|
s.Address = hostname
|
||||||
|
s.Domain = domain
|
||||||
|
s.Port = appPort
|
||||||
|
s.TTL = time.Second * 15
|
||||||
|
|
||||||
|
client, err := consul.NewClient(newClientConfig(servAddr))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
s.ConsulAgent = client.Agent()
|
||||||
|
|
||||||
|
return s, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func newClientConfig(serverAddr string) *consul.Config {
|
||||||
|
conf := consul.DefaultConfig()
|
||||||
|
conf.Address = serverAddr
|
||||||
|
|
||||||
|
return conf
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) GetID() string {
|
||||||
|
return fmt.Sprintf("%s_%s", s.Name, s.AppID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) GetFullAddr() string {
|
||||||
|
return fmt.Sprintf("http://%s:%d/", s.Address, s.Port)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) Register() error {
|
||||||
|
def := &consul.AgentServiceRegistration{
|
||||||
|
ID: s.GetID(),
|
||||||
|
Name: s.Name,
|
||||||
|
Address: s.Address,
|
||||||
|
Port: s.Port,
|
||||||
|
Tags: s.getTags(),
|
||||||
|
Check: &consul.AgentServiceCheck{
|
||||||
|
TTL: s.TTL.String(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.ConsulAgent.ServiceRegister(def); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() { // startup register
|
||||||
|
ticker := time.NewTicker(time.Millisecond * 100)
|
||||||
|
for range ticker.C {
|
||||||
|
ok, _ := s.healthCheck()
|
||||||
|
if ok {
|
||||||
|
ticker.Stop()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
go func() { // TTL
|
||||||
|
interval := s.TTL - time.Second*2
|
||||||
|
ticker := time.NewTicker(interval)
|
||||||
|
for range ticker.C {
|
||||||
|
_, err := s.healthCheck()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("TTL Error: %v\n", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
func (s *Service) Unregister() error {
|
||||||
|
return s.ConsulAgent.ServiceDeregister(s.GetID())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) healthCheck() (bool, error) {
|
||||||
|
alive := func() bool {
|
||||||
|
client := &http.Client{}
|
||||||
|
healthUrl := s.GetFullAddr() + "health"
|
||||||
|
req, err := http.NewRequest(http.MethodGet, healthUrl, nil)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
req.Header.Set("User-Agent", "Health Check")
|
||||||
|
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
return resp.StatusCode == http.StatusOK
|
||||||
|
}()
|
||||||
|
|
||||||
|
if alive {
|
||||||
|
if err := s.ConsulAgent.PassTTL("service:"+s.GetID(), "OK"); err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.ConsulAgent.FailTTL("service:"+s.GetID(), ErrServiceUnavailable.Error()); err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return false, ErrServiceUnavailable
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) getTags() []string {
|
||||||
|
tags := []string{
|
||||||
|
"traefik.enable=true",
|
||||||
|
"traefik.http.routers." + s.Name + ".rule=Headers(`X-API-SERVICE`, `" + s.Name + "`)",
|
||||||
|
"traefik.http.routers." + s.Name + ".entryPoints=https",
|
||||||
|
"traefik.http.routers." + s.Name + ".tls=true",
|
||||||
|
"traefik.http.routers." + s.Name + ".service=" + s.Name,
|
||||||
|
"traefik.http.routers." + s.Name + ".middlewares=compress,requestid",
|
||||||
|
"traefik.http.services." + s.Name + ".loadbalancer.server.scheme=http",
|
||||||
|
"traefik.http.services." + s.Name + ".loadbalancer.server.port=" + strconv.Itoa(s.Port),
|
||||||
|
"traefik.http.middlewares.compress.compress=true",
|
||||||
|
"traefik.http.middlewares.requestid.plugin.requestid.headerName=X-Request-ID",
|
||||||
|
"traefik.tls.certificates.certfile=/certs/client.cert",
|
||||||
|
"traefik.tls.certificates.keyfile=/certs/client.key",
|
||||||
|
// "traefik.http.services." + s.Name + ".loadbalancer.passhostheader=false",
|
||||||
|
// "traefik.http.services." + s.Name + ".loadbalancer.servers." + fullName + "=" + bFullAddr,
|
||||||
|
// "traefik.http.services." + s.Name + ".loadbalancer.servers." + fullName + ".url=" + bFullAddr,
|
||||||
|
// "traefik.http.services." + s.Name + ".loadbalancer.healthcheck.path=/health",
|
||||||
|
// "traefik.http.services." + s.Name + ".loadbalancer.healthcheck.interval=10s",
|
||||||
|
}
|
||||||
|
|
||||||
|
return tags
|
||||||
|
}
|
||||||
14
fluentd/config.go
Normal file
14
fluentd/config.go
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package fluentd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ParseAddr(addr string) (string, int) {
|
||||||
|
p := strings.Split(addr, ":")
|
||||||
|
fHost := p[0]
|
||||||
|
fPort, _ := strconv.Atoi(p[1])
|
||||||
|
|
||||||
|
return fHost, fPort
|
||||||
|
}
|
||||||
41
fluentd/logger.go
Normal file
41
fluentd/logger.go
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
package fluentd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/fluent/fluent-logger-golang/fluent"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Logger struct {
|
||||||
|
fluent *fluent.Fluent
|
||||||
|
appName string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewLogger(appName, fHost string, fPort int) *Logger {
|
||||||
|
config := fluent.Config{
|
||||||
|
FluentHost: fHost,
|
||||||
|
FluentPort: fPort,
|
||||||
|
// WriteTimeout: -1,
|
||||||
|
}
|
||||||
|
fluent, err := fluent.New(config)
|
||||||
|
if err != nil {
|
||||||
|
log.Panicf("Error connecting to %s: %v", fHost, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Logger{fluent, appName}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logger) Log(format string, v ...any) {
|
||||||
|
mapData := map[string]string{
|
||||||
|
"message": fmt.Sprintf(format, v...),
|
||||||
|
}
|
||||||
|
err := l.fluent.Post(l.appName, mapData)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error sending log: ", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logger) Close() error {
|
||||||
|
return l.fluent.Close()
|
||||||
|
}
|
||||||
26
rabbitmq/connect.go
Normal file
26
rabbitmq/connect.go
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
package rabbitmq
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/streadway/amqp"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Open(url string) (*amqp.Connection, *amqp.Channel, error) {
|
||||||
|
conn, err := amqp.Dial(url)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
ch, err := conn.Channel()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to open a channel: %v\n", err)
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return conn, ch, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func Close(conn *amqp.Connection) {
|
||||||
|
conn.Close()
|
||||||
|
}
|
||||||
85
rabbitmq/queue.go
Normal file
85
rabbitmq/queue.go
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
package rabbitmq
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/streadway/amqp"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Message map[string]interface{}
|
||||||
|
|
||||||
|
func Serialize(msg any) (string, error) {
|
||||||
|
var b bytes.Buffer
|
||||||
|
encoder := json.NewEncoder(&b)
|
||||||
|
err := encoder.Encode(msg)
|
||||||
|
|
||||||
|
return b.String(), err
|
||||||
|
}
|
||||||
|
|
||||||
|
func Deserialize(b []byte) (Message, error) {
|
||||||
|
var msg Message
|
||||||
|
buf := bytes.NewBuffer(b)
|
||||||
|
decoder := json.NewDecoder(buf)
|
||||||
|
err := decoder.Decode(&msg)
|
||||||
|
|
||||||
|
return msg, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewExchange(chn *amqp.Channel, name string) error {
|
||||||
|
err := chn.ExchangeDeclare(
|
||||||
|
name,
|
||||||
|
"direct", // type
|
||||||
|
true, // durable
|
||||||
|
false, // auto-deleted
|
||||||
|
false, // internal
|
||||||
|
false, // no-wait
|
||||||
|
nil, // arguments
|
||||||
|
)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func Publish(chn *amqp.Channel, name, routingKey string, msg any) error {
|
||||||
|
jsonData, err := Serialize(msg)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
msgBody := fmt.Sprintf(`{"event":"%T","data":%s}`, msg, jsonData)
|
||||||
|
chn.Publish(
|
||||||
|
name, // exchange name
|
||||||
|
routingKey, // routing key
|
||||||
|
false, // mandatory
|
||||||
|
false, // immediate
|
||||||
|
amqp.Publishing{
|
||||||
|
ContentType: "application/json",
|
||||||
|
Body: []byte(msgBody),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func BindQueueToExchange(chn *amqp.Channel, queueName, exchName, routingKey string) error {
|
||||||
|
err := chn.QueueBind(
|
||||||
|
queueName, // queue name
|
||||||
|
routingKey, // routing key
|
||||||
|
exchName, // exchange name
|
||||||
|
false,
|
||||||
|
nil,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to bind a queue: %s\n", queueName)
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user