修改配置文件,心跳机制
This commit is contained in:
parent
6877ade6c6
commit
cc5aef3d87
|
@ -26,15 +26,17 @@ type sqlite struct {
|
|||
}
|
||||
|
||||
type cloud struct {
|
||||
ServiceName string `json:"serviceName"`
|
||||
Address string `json:"address"`
|
||||
Port string `json:"port"`
|
||||
ServiceName string `json:"serviceName"`
|
||||
Host string `json:"host"`
|
||||
RegisterCenter string `json:"registerCenter"`
|
||||
RegisterCenterDb int `json:"registerCenterDb"`
|
||||
Heartbeat int `json:"heartbeat"`
|
||||
}
|
||||
|
||||
type session struct {
|
||||
Addr string `json:"addr"`
|
||||
Db int `json:"db"`
|
||||
Timeout int `json:"timeout"`
|
||||
Expires int `json:"expires"`
|
||||
Password string `json:"password"`
|
||||
Database string `json:"database"`
|
||||
Prefix string `json:"prefix"`
|
||||
|
|
|
@ -1,27 +1,50 @@
|
|||
package cloud
|
||||
|
||||
import (
|
||||
"BlogAdmin/bootstrap"
|
||||
"BlogAdmin/third_party/database"
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
var (
|
||||
serviceName string = "BlogAdminService"
|
||||
host string = "192.168.0.50"
|
||||
port string = "8081"
|
||||
countMap sync.Map
|
||||
// serviceName string = "BlogAdminService"
|
||||
// host string = "192.168.0.50"
|
||||
// port string = "8081"
|
||||
countMap sync.Map
|
||||
)
|
||||
|
||||
type distributedService struct {
|
||||
}
|
||||
|
||||
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 {
|
||||
|
@ -48,11 +71,23 @@ func (*distributedService) Req(service string, url string) {
|
|||
}
|
||||
|
||||
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()
|
||||
key := fmt.Sprintf("distributed:%v", serviceName)
|
||||
addr := fmt.Sprintf("%v:%v", host, port)
|
||||
database.RedisTemplate.SAdd(ctx, key, addr)
|
||||
for {
|
||||
if down {
|
||||
break
|
||||
}
|
||||
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()
|
||||
// i := len(keys)
|
||||
|
@ -62,8 +97,11 @@ func (*distributedService) ServiceUp() {
|
|||
}
|
||||
|
||||
func (*distributedService) ServiceDown() {
|
||||
down = true
|
||||
ctx := context.Background()
|
||||
serviceName := bootstrap.Config.Cloud.ServiceName
|
||||
host := bootstrap.Config.Cloud.Host
|
||||
key := fmt.Sprintf("distributed:%v", serviceName)
|
||||
addr := fmt.Sprintf("%v:%v", host, port)
|
||||
database.RedisTemplate.SRem(ctx, key, addr)
|
||||
// addr := fmt.Sprintf("%v:%v", host, port)
|
||||
redisClient.SRem(ctx, key, host)
|
||||
}
|
||||
|
|
|
@ -1,21 +1,22 @@
|
|||
{
|
||||
"cloud": {
|
||||
"serviceName": "BlogAdminService",
|
||||
"host": "localhost:8081",
|
||||
"registerCenter": "localhost:6379",
|
||||
"address": "localhost:8081",
|
||||
"db": "0"
|
||||
"registerCenterDb": 0,
|
||||
"heartbeat": 10
|
||||
},
|
||||
"session": {
|
||||
"address": "localhost:6379",
|
||||
"database": "0",
|
||||
"prefix": "iris:session:",
|
||||
"timeout": 120
|
||||
"expires": 10
|
||||
},
|
||||
"sqlite": {
|
||||
"path": "./db_blog_admin.db"
|
||||
},
|
||||
"redis": {
|
||||
"addr": "localhost:6379",
|
||||
"db": 0
|
||||
"db": 1
|
||||
}
|
||||
}
|
|
@ -55,7 +55,7 @@ func main() {
|
|||
addr := bootstrap.Config.Session.Addr
|
||||
redisDatabase := bootstrap.Config.Session.Database
|
||||
prefix := bootstrap.Config.Session.Prefix
|
||||
timeout := bootstrap.Config.Session.Timeout
|
||||
expires := bootstrap.Config.Session.Expires
|
||||
db := redis.New(redis.Config{
|
||||
Network: "tcp",
|
||||
Addr: addr,
|
||||
|
@ -87,7 +87,7 @@ func main() {
|
|||
// using the standard `html/template` package.
|
||||
sess := sessions.New(sessions.Config{
|
||||
Cookie: "session_id_cookie",
|
||||
Expires: time.Duration(timeout) * time.Hour,
|
||||
Expires: time.Duration(expires) * time.Hour,
|
||||
AllowReclaim: true,
|
||||
})
|
||||
sess.UseDatabase(db)
|
||||
|
@ -98,13 +98,13 @@ func main() {
|
|||
ctx.View("index.html")
|
||||
})
|
||||
|
||||
adminApi := app.Party("/")
|
||||
adminLoginApi := app.Party("/login")
|
||||
adminArticleApi := app.Party("/article")
|
||||
adminDinaryApi := app.Party("/diary")
|
||||
adminVersionApi := app.Party("/version")
|
||||
adminUserApi := app.Party("/user")
|
||||
adminFileApi := app.Party("/file")
|
||||
adminApi := app.Party("/admin")
|
||||
adminLoginApi := app.Party("/admin/login")
|
||||
adminArticleApi := app.Party("/admin/article")
|
||||
adminDinaryApi := app.Party("/admin/diary")
|
||||
adminVersionApi := app.Party("/admin/version")
|
||||
adminUserApi := app.Party("/admin/user")
|
||||
adminFileApi := app.Party("/admin/file")
|
||||
|
||||
adminApi.Use(middleware.Auth)
|
||||
adminArticleApi.Use(middleware.Auth)
|
||||
|
@ -123,7 +123,8 @@ func main() {
|
|||
|
||||
go cloud.DistributedService.ServiceUp()
|
||||
|
||||
app.Listen("localhost:8081")
|
||||
host := bootstrap.Config.Cloud.Host
|
||||
app.Listen(host)
|
||||
|
||||
defer func() {
|
||||
log.Println("程序结束,关闭资源")
|
||||
|
|
|
@ -22,6 +22,6 @@ func init() {
|
|||
pong := sc.Val()
|
||||
if pong == "PONG" {
|
||||
log.Println("Redis连接成功")
|
||||
RedisTemplate.FlushAll(ctx)
|
||||
// RedisTemplate.FlushAll(ctx)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,15 +26,17 @@ type sqlite struct {
|
|||
}
|
||||
|
||||
type cloud struct {
|
||||
ServiceName string `json:"serviceName"`
|
||||
Address string `json:"address"`
|
||||
Port string `json:"port"`
|
||||
ServiceName string `json:"serviceName"`
|
||||
Host string `json:"host"`
|
||||
RegisterCenter string `json:"registerCenter"`
|
||||
RegisterCenterDb int `json:"registerCenterDb"`
|
||||
Heartbeat int `json:"heartbeat"`
|
||||
}
|
||||
|
||||
type session struct {
|
||||
Addr string `json:"addr"`
|
||||
Db int `json:"db"`
|
||||
Timeout int `json:"timeout"`
|
||||
Expires int `json:"expires"`
|
||||
Password string `json:"password"`
|
||||
Database string `json:"database"`
|
||||
Prefix string `json:"prefix"`
|
||||
|
|
|
@ -1,27 +1,50 @@
|
|||
package cloud
|
||||
|
||||
import (
|
||||
"BlogAdmin/third_party/database"
|
||||
"BlogFile/bootstrap"
|
||||
"BlogFile/third_party/database"
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
var (
|
||||
serviceName string = "BlogAdminService"
|
||||
host string = "192.168.0.50"
|
||||
port string = "8081"
|
||||
countMap sync.Map
|
||||
// serviceName string = "BlogAdminService"
|
||||
// host string = "192.168.0.50"
|
||||
// port string = "8081"
|
||||
countMap sync.Map
|
||||
)
|
||||
|
||||
type distributedService struct {
|
||||
}
|
||||
|
||||
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 {
|
||||
|
@ -48,11 +71,23 @@ func (*distributedService) Req(service string, url string) {
|
|||
}
|
||||
|
||||
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()
|
||||
key := fmt.Sprintf("distributed:%v", serviceName)
|
||||
addr := fmt.Sprintf("%v:%v", host, port)
|
||||
database.RedisTemplate.SAdd(ctx, key, addr)
|
||||
for {
|
||||
if down {
|
||||
break
|
||||
}
|
||||
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()
|
||||
// i := len(keys)
|
||||
|
@ -62,8 +97,11 @@ func (*distributedService) ServiceUp() {
|
|||
}
|
||||
|
||||
func (*distributedService) ServiceDown() {
|
||||
down = true
|
||||
ctx := context.Background()
|
||||
serviceName := bootstrap.Config.Cloud.ServiceName
|
||||
host := bootstrap.Config.Cloud.Host
|
||||
key := fmt.Sprintf("distributed:%v", serviceName)
|
||||
addr := fmt.Sprintf("%v:%v", host, port)
|
||||
database.RedisTemplate.SRem(ctx, key, addr)
|
||||
// addr := fmt.Sprintf("%v:%v", host, port)
|
||||
redisClient.SRem(ctx, key, host)
|
||||
}
|
||||
|
|
|
@ -1,21 +1,22 @@
|
|||
{
|
||||
"cloud": {
|
||||
"serviceName": "BlogFileService",
|
||||
"host": "localhost:8088",
|
||||
"registerCenter": "localhost:6379",
|
||||
"address": "localhost:8081",
|
||||
"db": "0"
|
||||
"registerCenterDb": 0,
|
||||
"heartbeat": 10
|
||||
},
|
||||
"session": {
|
||||
"address": "localhost:6379",
|
||||
"database": "0",
|
||||
"prefix": "iris:session:",
|
||||
"timeout": 10
|
||||
"expires": 10
|
||||
},
|
||||
"sqlite": {
|
||||
"path": "./db_blog_file.db"
|
||||
},
|
||||
"redis": {
|
||||
"addr": "localhost:6379",
|
||||
"db": 0
|
||||
"db": 1
|
||||
}
|
||||
}
|
|
@ -51,7 +51,7 @@ func main() {
|
|||
addr := bootstrap.Config.Session.Addr
|
||||
redisDatabase := bootstrap.Config.Session.Database
|
||||
prefix := bootstrap.Config.Session.Prefix
|
||||
timeout := bootstrap.Config.Session.Timeout
|
||||
expires := bootstrap.Config.Session.Expires
|
||||
db := redisSession.New(redisSession.Config{
|
||||
Network: "tcp",
|
||||
Addr: addr,
|
||||
|
@ -66,7 +66,7 @@ func main() {
|
|||
// using the standard `html/template` package.
|
||||
sess := sessions.New(sessions.Config{
|
||||
Cookie: "session_id_cookie",
|
||||
Expires: time.Duration(timeout) * time.Hour,
|
||||
Expires: time.Duration(expires) * time.Hour,
|
||||
AllowReclaim: true,
|
||||
})
|
||||
sess.UseDatabase(db)
|
||||
|
@ -85,7 +85,8 @@ func main() {
|
|||
|
||||
go cloud.DistributedService.ServiceUp()
|
||||
|
||||
app.Listen("localhost:8088")
|
||||
host := bootstrap.Config.Cloud.Host
|
||||
app.Listen(host)
|
||||
|
||||
defer func() {
|
||||
log.Println("程序结束,关闭资源")
|
||||
|
|
|
@ -27,6 +27,6 @@ func init() {
|
|||
pong := sc.Val()
|
||||
if pong == "PONG" {
|
||||
log.Println("Redis连接成功")
|
||||
RedisTemplate.FlushAll(ctx)
|
||||
// RedisTemplate.FlushAll(ctx)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,15 +26,17 @@ type sqlite struct {
|
|||
}
|
||||
|
||||
type cloud struct {
|
||||
ServiceName string `json:"serviceName"`
|
||||
Address string `json:"address"`
|
||||
Port string `json:"port"`
|
||||
ServiceName string `json:"serviceName"`
|
||||
Host string `json:"host"`
|
||||
RegisterCenter string `json:"registerCenter"`
|
||||
RegisterCenterDb int `json:"registerCenterDb"`
|
||||
Heartbeat int `json:"heartbeat"`
|
||||
}
|
||||
|
||||
type session struct {
|
||||
Addr string `json:"addr"`
|
||||
Db int `json:"db"`
|
||||
Timeout int `json:"timeout"`
|
||||
Expires int `json:"expires"`
|
||||
Password string `json:"password"`
|
||||
Database string `json:"database"`
|
||||
Prefix string `json:"prefix"`
|
||||
|
|
|
@ -1,24 +1,51 @@
|
|||
package cloud
|
||||
|
||||
import (
|
||||
"Blog/bootstrap"
|
||||
"Blog/third_party/database"
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
var (
|
||||
serviceName string = "BlogService"
|
||||
host string = "192.168.0.50"
|
||||
port string = "8080"
|
||||
countMap sync.Map
|
||||
// serviceName string = "BlogAdminService"
|
||||
// host string = "192.168.0.50"
|
||||
// port string = "8081"
|
||||
countMap sync.Map
|
||||
)
|
||||
|
||||
type distributedService struct {
|
||||
}
|
||||
|
||||
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 {
|
||||
val, _ := countMap.Load(service)
|
||||
|
@ -44,11 +71,23 @@ func (*distributedService) Req(service string, url string) {
|
|||
}
|
||||
|
||||
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()
|
||||
key := fmt.Sprintf("distributed:%v", serviceName)
|
||||
addr := fmt.Sprintf("%v:%v", host, port)
|
||||
database.RedisTemplate.SAdd(ctx, key, addr)
|
||||
for {
|
||||
if down {
|
||||
break
|
||||
}
|
||||
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()
|
||||
// i := len(keys)
|
||||
|
@ -58,8 +97,11 @@ func (*distributedService) ServiceUp() {
|
|||
}
|
||||
|
||||
func (*distributedService) ServiceDown() {
|
||||
down = true
|
||||
ctx := context.Background()
|
||||
serviceName := bootstrap.Config.Cloud.ServiceName
|
||||
host := bootstrap.Config.Cloud.Host
|
||||
key := fmt.Sprintf("distributed:%v", serviceName)
|
||||
addr := fmt.Sprintf("%v:%v", host, port)
|
||||
database.RedisTemplate.SRem(ctx, key, addr)
|
||||
// addr := fmt.Sprintf("%v:%v", host, port)
|
||||
redisClient.SRem(ctx, key, host)
|
||||
}
|
||||
|
|
|
@ -1,21 +1,22 @@
|
|||
{
|
||||
"cloud": {
|
||||
"serviceName": "BlogService",
|
||||
"host": "localhost:8080",
|
||||
"registerCenter": "localhost:6379",
|
||||
"address": "localhost:8081",
|
||||
"db": "0"
|
||||
"registerCenterDb": 0,
|
||||
"heartbeat": 10
|
||||
},
|
||||
"session": {
|
||||
"address": "localhost:6379",
|
||||
"database": "0",
|
||||
"prefix": "iris:session:",
|
||||
"timeout": 120
|
||||
"expires": 10
|
||||
},
|
||||
"sqlite": {
|
||||
"path": "./db_blog.db"
|
||||
},
|
||||
"redis": {
|
||||
"addr": "localhost:6379",
|
||||
"db": 0
|
||||
"db": 1
|
||||
}
|
||||
}
|
|
@ -56,7 +56,7 @@ func main() {
|
|||
addr := bootstrap.Config.Session.Addr
|
||||
redisDatabase := bootstrap.Config.Session.Database
|
||||
prefix := bootstrap.Config.Session.Prefix
|
||||
timeout := bootstrap.Config.Session.Timeout
|
||||
expires := bootstrap.Config.Session.Expires
|
||||
db := redis.New(redis.Config{
|
||||
Network: "tcp",
|
||||
Addr: addr,
|
||||
|
@ -88,7 +88,7 @@ func main() {
|
|||
// using the standard `html/template` package.
|
||||
sess := sessions.New(sessions.Config{
|
||||
Cookie: "session_id_cookie",
|
||||
Expires: time.Duration(timeout) * time.Hour,
|
||||
Expires: time.Duration(expires) * time.Hour,
|
||||
AllowReclaim: true,
|
||||
})
|
||||
sess.UseDatabase(db)
|
||||
|
@ -109,7 +109,8 @@ func main() {
|
|||
go service.ContentService.InitContentData()
|
||||
go cloud.DistributedService.ServiceUp()
|
||||
|
||||
app.Listen("localhost:8080")
|
||||
host := bootstrap.Config.Cloud.Host
|
||||
app.Listen(host)
|
||||
|
||||
defer func() {
|
||||
log.Println("程序结束,关闭资源")
|
||||
|
|
Loading…
Reference in New Issue