修改配置文件,心跳机制

This commit is contained in:
sysnix 2023-10-23 04:16:18 +08:00
parent 6877ade6c6
commit cc5aef3d87
14 changed files with 205 additions and 75 deletions

View File

@ -26,15 +26,17 @@ type sqlite struct {
} }
type cloud struct { type cloud struct {
ServiceName string `json:"serviceName"` ServiceName string `json:"serviceName"`
Address string `json:"address"` Host string `json:"host"`
Port string `json:"port"` RegisterCenter string `json:"registerCenter"`
RegisterCenterDb int `json:"registerCenterDb"`
Heartbeat int `json:"heartbeat"`
} }
type session struct { type session struct {
Addr string `json:"addr"` Addr string `json:"addr"`
Db int `json:"db"` Db int `json:"db"`
Timeout int `json:"timeout"` Expires int `json:"expires"`
Password string `json:"password"` Password string `json:"password"`
Database string `json:"database"` Database string `json:"database"`
Prefix string `json:"prefix"` Prefix string `json:"prefix"`

View File

@ -1,27 +1,50 @@
package cloud package cloud
import ( import (
"BlogAdmin/bootstrap"
"BlogAdmin/third_party/database" "BlogAdmin/third_party/database"
"context" "context"
"fmt" "fmt"
"log" "log"
"sync" "sync"
"time"
"github.com/redis/go-redis/v9"
) )
var ( var (
serviceName string = "BlogAdminService" // serviceName string = "BlogAdminService"
host string = "192.168.0.50" // host string = "192.168.0.50"
port string = "8081" // port string = "8081"
countMap sync.Map countMap sync.Map
) )
type distributedService struct { type distributedService struct {
} }
var DistributedService distributedService var DistributedService distributedService
var redisClient *redis.Client
var down bool
func init() { func init() {
registerCenter := bootstrap.Config.Cloud.RegisterCenter
db := bootstrap.Config.Cloud.RegisterCenterDb
redisClient = redis.NewClient(&redis.Options{
Addr: registerCenter,
DB: db, // 默认DB 0
})
// RedisTemplate = redis.NewClient(&redis.Options{
// Addr: "localhost:6379",
// Password: "", // 没有密码,默认值
// DB: 0, // 默认DB 0
// })
ctx := context.Background()
sc := redisClient.Ping(ctx)
pong := sc.Val()
if pong == "PONG" {
log.Println("注册中心Redis连接成功")
// RedisTemplate.FlushAll(ctx)
}
} }
func addReqCount(service string) int64 { func addReqCount(service string) int64 {
@ -48,11 +71,23 @@ func (*distributedService) Req(service string, url string) {
} }
func (*distributedService) ServiceUp() { func (*distributedService) ServiceUp() {
go func() {
log.Println("心跳开始")
ctx := context.Background()
serviceName := bootstrap.Config.Cloud.ServiceName
host := bootstrap.Config.Cloud.Host
heartbeat := bootstrap.Config.Cloud.Heartbeat
ctx := context.Background() for {
key := fmt.Sprintf("distributed:%v", serviceName) if down {
addr := fmt.Sprintf("%v:%v", host, port) break
database.RedisTemplate.SAdd(ctx, key, addr) }
time.Sleep(time.Second * time.Duration(heartbeat))
key := fmt.Sprintf("distributed:%v", serviceName)
// addr := fmt.Sprintf("%v:%v", host, port)
redisClient.SAdd(ctx, key, host)
}
}()
// keys, _ := database.RedisTemplate.Scan(ctx, 0, keyPerfix, 0).Val() // keys, _ := database.RedisTemplate.Scan(ctx, 0, keyPerfix, 0).Val()
// i := len(keys) // i := len(keys)
@ -62,8 +97,11 @@ func (*distributedService) ServiceUp() {
} }
func (*distributedService) ServiceDown() { func (*distributedService) ServiceDown() {
down = true
ctx := context.Background() ctx := context.Background()
serviceName := bootstrap.Config.Cloud.ServiceName
host := bootstrap.Config.Cloud.Host
key := fmt.Sprintf("distributed:%v", serviceName) key := fmt.Sprintf("distributed:%v", serviceName)
addr := fmt.Sprintf("%v:%v", host, port) // addr := fmt.Sprintf("%v:%v", host, port)
database.RedisTemplate.SRem(ctx, key, addr) redisClient.SRem(ctx, key, host)
} }

View File

@ -1,21 +1,22 @@
{ {
"cloud": { "cloud": {
"serviceName": "BlogAdminService", "serviceName": "BlogAdminService",
"host": "localhost:8081",
"registerCenter": "localhost:6379", "registerCenter": "localhost:6379",
"address": "localhost:8081", "registerCenterDb": 0,
"db": "0" "heartbeat": 10
}, },
"session": { "session": {
"address": "localhost:6379", "address": "localhost:6379",
"database": "0", "database": "0",
"prefix": "iris:session:", "prefix": "iris:session:",
"timeout": 120 "expires": 10
}, },
"sqlite": { "sqlite": {
"path": "./db_blog_admin.db" "path": "./db_blog_admin.db"
}, },
"redis": { "redis": {
"addr": "localhost:6379", "addr": "localhost:6379",
"db": 0 "db": 1
} }
} }

View File

@ -55,7 +55,7 @@ func main() {
addr := bootstrap.Config.Session.Addr addr := bootstrap.Config.Session.Addr
redisDatabase := bootstrap.Config.Session.Database redisDatabase := bootstrap.Config.Session.Database
prefix := bootstrap.Config.Session.Prefix prefix := bootstrap.Config.Session.Prefix
timeout := bootstrap.Config.Session.Timeout expires := bootstrap.Config.Session.Expires
db := redis.New(redis.Config{ db := redis.New(redis.Config{
Network: "tcp", Network: "tcp",
Addr: addr, Addr: addr,
@ -87,7 +87,7 @@ func main() {
// using the standard `html/template` package. // using the standard `html/template` package.
sess := sessions.New(sessions.Config{ sess := sessions.New(sessions.Config{
Cookie: "session_id_cookie", Cookie: "session_id_cookie",
Expires: time.Duration(timeout) * time.Hour, Expires: time.Duration(expires) * time.Hour,
AllowReclaim: true, AllowReclaim: true,
}) })
sess.UseDatabase(db) sess.UseDatabase(db)
@ -98,13 +98,13 @@ func main() {
ctx.View("index.html") ctx.View("index.html")
}) })
adminApi := app.Party("/") adminApi := app.Party("/admin")
adminLoginApi := app.Party("/login") adminLoginApi := app.Party("/admin/login")
adminArticleApi := app.Party("/article") adminArticleApi := app.Party("/admin/article")
adminDinaryApi := app.Party("/diary") adminDinaryApi := app.Party("/admin/diary")
adminVersionApi := app.Party("/version") adminVersionApi := app.Party("/admin/version")
adminUserApi := app.Party("/user") adminUserApi := app.Party("/admin/user")
adminFileApi := app.Party("/file") adminFileApi := app.Party("/admin/file")
adminApi.Use(middleware.Auth) adminApi.Use(middleware.Auth)
adminArticleApi.Use(middleware.Auth) adminArticleApi.Use(middleware.Auth)
@ -123,7 +123,8 @@ func main() {
go cloud.DistributedService.ServiceUp() go cloud.DistributedService.ServiceUp()
app.Listen("localhost:8081") host := bootstrap.Config.Cloud.Host
app.Listen(host)
defer func() { defer func() {
log.Println("程序结束,关闭资源") log.Println("程序结束,关闭资源")

View File

@ -22,6 +22,6 @@ func init() {
pong := sc.Val() pong := sc.Val()
if pong == "PONG" { if pong == "PONG" {
log.Println("Redis连接成功") log.Println("Redis连接成功")
RedisTemplate.FlushAll(ctx) // RedisTemplate.FlushAll(ctx)
} }
} }

View File

@ -26,15 +26,17 @@ type sqlite struct {
} }
type cloud struct { type cloud struct {
ServiceName string `json:"serviceName"` ServiceName string `json:"serviceName"`
Address string `json:"address"` Host string `json:"host"`
Port string `json:"port"` RegisterCenter string `json:"registerCenter"`
RegisterCenterDb int `json:"registerCenterDb"`
Heartbeat int `json:"heartbeat"`
} }
type session struct { type session struct {
Addr string `json:"addr"` Addr string `json:"addr"`
Db int `json:"db"` Db int `json:"db"`
Timeout int `json:"timeout"` Expires int `json:"expires"`
Password string `json:"password"` Password string `json:"password"`
Database string `json:"database"` Database string `json:"database"`
Prefix string `json:"prefix"` Prefix string `json:"prefix"`

View File

@ -1,27 +1,50 @@
package cloud package cloud
import ( import (
"BlogAdmin/third_party/database" "BlogFile/bootstrap"
"BlogFile/third_party/database"
"context" "context"
"fmt" "fmt"
"log" "log"
"sync" "sync"
"time"
"github.com/redis/go-redis/v9"
) )
var ( var (
serviceName string = "BlogAdminService" // serviceName string = "BlogAdminService"
host string = "192.168.0.50" // host string = "192.168.0.50"
port string = "8081" // port string = "8081"
countMap sync.Map countMap sync.Map
) )
type distributedService struct { type distributedService struct {
} }
var DistributedService distributedService var DistributedService distributedService
var redisClient *redis.Client
var down bool
func init() { func init() {
registerCenter := bootstrap.Config.Cloud.RegisterCenter
db := bootstrap.Config.Cloud.RegisterCenterDb
redisClient = redis.NewClient(&redis.Options{
Addr: registerCenter,
DB: db, // 默认DB 0
})
// RedisTemplate = redis.NewClient(&redis.Options{
// Addr: "localhost:6379",
// Password: "", // 没有密码,默认值
// DB: 0, // 默认DB 0
// })
ctx := context.Background()
sc := redisClient.Ping(ctx)
pong := sc.Val()
if pong == "PONG" {
log.Println("注册中心Redis连接成功")
// RedisTemplate.FlushAll(ctx)
}
} }
func addReqCount(service string) int64 { func addReqCount(service string) int64 {
@ -48,11 +71,23 @@ func (*distributedService) Req(service string, url string) {
} }
func (*distributedService) ServiceUp() { func (*distributedService) ServiceUp() {
go func() {
log.Println("心跳开始")
ctx := context.Background()
serviceName := bootstrap.Config.Cloud.ServiceName
host := bootstrap.Config.Cloud.Host
heartbeat := bootstrap.Config.Cloud.Heartbeat
ctx := context.Background() for {
key := fmt.Sprintf("distributed:%v", serviceName) if down {
addr := fmt.Sprintf("%v:%v", host, port) break
database.RedisTemplate.SAdd(ctx, key, addr) }
time.Sleep(time.Second * time.Duration(heartbeat))
key := fmt.Sprintf("distributed:%v", serviceName)
// addr := fmt.Sprintf("%v:%v", host, port)
redisClient.SAdd(ctx, key, host)
}
}()
// keys, _ := database.RedisTemplate.Scan(ctx, 0, keyPerfix, 0).Val() // keys, _ := database.RedisTemplate.Scan(ctx, 0, keyPerfix, 0).Val()
// i := len(keys) // i := len(keys)
@ -62,8 +97,11 @@ func (*distributedService) ServiceUp() {
} }
func (*distributedService) ServiceDown() { func (*distributedService) ServiceDown() {
down = true
ctx := context.Background() ctx := context.Background()
serviceName := bootstrap.Config.Cloud.ServiceName
host := bootstrap.Config.Cloud.Host
key := fmt.Sprintf("distributed:%v", serviceName) key := fmt.Sprintf("distributed:%v", serviceName)
addr := fmt.Sprintf("%v:%v", host, port) // addr := fmt.Sprintf("%v:%v", host, port)
database.RedisTemplate.SRem(ctx, key, addr) redisClient.SRem(ctx, key, host)
} }

View File

@ -1,21 +1,22 @@
{ {
"cloud": { "cloud": {
"serviceName": "BlogFileService", "serviceName": "BlogFileService",
"host": "localhost:8088",
"registerCenter": "localhost:6379", "registerCenter": "localhost:6379",
"address": "localhost:8081", "registerCenterDb": 0,
"db": "0" "heartbeat": 10
}, },
"session": { "session": {
"address": "localhost:6379", "address": "localhost:6379",
"database": "0", "database": "0",
"prefix": "iris:session:", "prefix": "iris:session:",
"timeout": 10 "expires": 10
}, },
"sqlite": { "sqlite": {
"path": "./db_blog_file.db" "path": "./db_blog_file.db"
}, },
"redis": { "redis": {
"addr": "localhost:6379", "addr": "localhost:6379",
"db": 0 "db": 1
} }
} }

View File

@ -51,7 +51,7 @@ func main() {
addr := bootstrap.Config.Session.Addr addr := bootstrap.Config.Session.Addr
redisDatabase := bootstrap.Config.Session.Database redisDatabase := bootstrap.Config.Session.Database
prefix := bootstrap.Config.Session.Prefix prefix := bootstrap.Config.Session.Prefix
timeout := bootstrap.Config.Session.Timeout expires := bootstrap.Config.Session.Expires
db := redisSession.New(redisSession.Config{ db := redisSession.New(redisSession.Config{
Network: "tcp", Network: "tcp",
Addr: addr, Addr: addr,
@ -66,7 +66,7 @@ func main() {
// using the standard `html/template` package. // using the standard `html/template` package.
sess := sessions.New(sessions.Config{ sess := sessions.New(sessions.Config{
Cookie: "session_id_cookie", Cookie: "session_id_cookie",
Expires: time.Duration(timeout) * time.Hour, Expires: time.Duration(expires) * time.Hour,
AllowReclaim: true, AllowReclaim: true,
}) })
sess.UseDatabase(db) sess.UseDatabase(db)
@ -85,7 +85,8 @@ func main() {
go cloud.DistributedService.ServiceUp() go cloud.DistributedService.ServiceUp()
app.Listen("localhost:8088") host := bootstrap.Config.Cloud.Host
app.Listen(host)
defer func() { defer func() {
log.Println("程序结束,关闭资源") log.Println("程序结束,关闭资源")

View File

@ -27,6 +27,6 @@ func init() {
pong := sc.Val() pong := sc.Val()
if pong == "PONG" { if pong == "PONG" {
log.Println("Redis连接成功") log.Println("Redis连接成功")
RedisTemplate.FlushAll(ctx) // RedisTemplate.FlushAll(ctx)
} }
} }

View File

@ -26,15 +26,17 @@ type sqlite struct {
} }
type cloud struct { type cloud struct {
ServiceName string `json:"serviceName"` ServiceName string `json:"serviceName"`
Address string `json:"address"` Host string `json:"host"`
Port string `json:"port"` RegisterCenter string `json:"registerCenter"`
RegisterCenterDb int `json:"registerCenterDb"`
Heartbeat int `json:"heartbeat"`
} }
type session struct { type session struct {
Addr string `json:"addr"` Addr string `json:"addr"`
Db int `json:"db"` Db int `json:"db"`
Timeout int `json:"timeout"` Expires int `json:"expires"`
Password string `json:"password"` Password string `json:"password"`
Database string `json:"database"` Database string `json:"database"`
Prefix string `json:"prefix"` Prefix string `json:"prefix"`

View File

@ -1,24 +1,51 @@
package cloud package cloud
import ( import (
"Blog/bootstrap"
"Blog/third_party/database" "Blog/third_party/database"
"context" "context"
"fmt" "fmt"
"log" "log"
"sync" "sync"
"time"
"github.com/redis/go-redis/v9"
) )
var ( var (
serviceName string = "BlogService" // serviceName string = "BlogAdminService"
host string = "192.168.0.50" // host string = "192.168.0.50"
port string = "8080" // port string = "8081"
countMap sync.Map countMap sync.Map
) )
type distributedService struct { type distributedService struct {
} }
var DistributedService distributedService var DistributedService distributedService
var redisClient *redis.Client
var down bool
func init() {
registerCenter := bootstrap.Config.Cloud.RegisterCenter
db := bootstrap.Config.Cloud.RegisterCenterDb
redisClient = redis.NewClient(&redis.Options{
Addr: registerCenter,
DB: db, // 默认DB 0
})
// RedisTemplate = redis.NewClient(&redis.Options{
// Addr: "localhost:6379",
// Password: "", // 没有密码,默认值
// DB: 0, // 默认DB 0
// })
ctx := context.Background()
sc := redisClient.Ping(ctx)
pong := sc.Val()
if pong == "PONG" {
log.Println("注册中心Redis连接成功")
// RedisTemplate.FlushAll(ctx)
}
}
func addReqCount(service string) int64 { func addReqCount(service string) int64 {
val, _ := countMap.Load(service) val, _ := countMap.Load(service)
@ -44,11 +71,23 @@ func (*distributedService) Req(service string, url string) {
} }
func (*distributedService) ServiceUp() { func (*distributedService) ServiceUp() {
go func() {
log.Println("心跳开始")
ctx := context.Background()
serviceName := bootstrap.Config.Cloud.ServiceName
host := bootstrap.Config.Cloud.Host
heartbeat := bootstrap.Config.Cloud.Heartbeat
ctx := context.Background() for {
key := fmt.Sprintf("distributed:%v", serviceName) if down {
addr := fmt.Sprintf("%v:%v", host, port) break
database.RedisTemplate.SAdd(ctx, key, addr) }
time.Sleep(time.Second * time.Duration(heartbeat))
key := fmt.Sprintf("distributed:%v", serviceName)
// addr := fmt.Sprintf("%v:%v", host, port)
redisClient.SAdd(ctx, key, host)
}
}()
// keys, _ := database.RedisTemplate.Scan(ctx, 0, keyPerfix, 0).Val() // keys, _ := database.RedisTemplate.Scan(ctx, 0, keyPerfix, 0).Val()
// i := len(keys) // i := len(keys)
@ -58,8 +97,11 @@ func (*distributedService) ServiceUp() {
} }
func (*distributedService) ServiceDown() { func (*distributedService) ServiceDown() {
down = true
ctx := context.Background() ctx := context.Background()
serviceName := bootstrap.Config.Cloud.ServiceName
host := bootstrap.Config.Cloud.Host
key := fmt.Sprintf("distributed:%v", serviceName) key := fmt.Sprintf("distributed:%v", serviceName)
addr := fmt.Sprintf("%v:%v", host, port) // addr := fmt.Sprintf("%v:%v", host, port)
database.RedisTemplate.SRem(ctx, key, addr) redisClient.SRem(ctx, key, host)
} }

View File

@ -1,21 +1,22 @@
{ {
"cloud": { "cloud": {
"serviceName": "BlogService", "serviceName": "BlogService",
"host": "localhost:8080",
"registerCenter": "localhost:6379", "registerCenter": "localhost:6379",
"address": "localhost:8081", "registerCenterDb": 0,
"db": "0" "heartbeat": 10
}, },
"session": { "session": {
"address": "localhost:6379", "address": "localhost:6379",
"database": "0", "database": "0",
"prefix": "iris:session:", "prefix": "iris:session:",
"timeout": 120 "expires": 10
}, },
"sqlite": { "sqlite": {
"path": "./db_blog.db" "path": "./db_blog.db"
}, },
"redis": { "redis": {
"addr": "localhost:6379", "addr": "localhost:6379",
"db": 0 "db": 1
} }
} }

View File

@ -56,7 +56,7 @@ func main() {
addr := bootstrap.Config.Session.Addr addr := bootstrap.Config.Session.Addr
redisDatabase := bootstrap.Config.Session.Database redisDatabase := bootstrap.Config.Session.Database
prefix := bootstrap.Config.Session.Prefix prefix := bootstrap.Config.Session.Prefix
timeout := bootstrap.Config.Session.Timeout expires := bootstrap.Config.Session.Expires
db := redis.New(redis.Config{ db := redis.New(redis.Config{
Network: "tcp", Network: "tcp",
Addr: addr, Addr: addr,
@ -88,7 +88,7 @@ func main() {
// using the standard `html/template` package. // using the standard `html/template` package.
sess := sessions.New(sessions.Config{ sess := sessions.New(sessions.Config{
Cookie: "session_id_cookie", Cookie: "session_id_cookie",
Expires: time.Duration(timeout) * time.Hour, Expires: time.Duration(expires) * time.Hour,
AllowReclaim: true, AllowReclaim: true,
}) })
sess.UseDatabase(db) sess.UseDatabase(db)
@ -109,7 +109,8 @@ func main() {
go service.ContentService.InitContentData() go service.ContentService.InitContentData()
go cloud.DistributedService.ServiceUp() go cloud.DistributedService.ServiceUp()
app.Listen("localhost:8080") host := bootstrap.Config.Cloud.Host
app.Listen(host)
defer func() { defer func() {
log.Println("程序结束,关闭资源") log.Println("程序结束,关闭资源")