连接阿波罗:
默认properties类型
go
复制代码
package main
import (
"fmt"
"github.com/apolloconfig/agollo/v4"
"github.com/apolloconfig/agollo/v4/env/config"
)
func main() {
c := &config.AppConfig{
AppID: "2222",
Cluster: "dev",
IP: "http://192.168.0.52:8080/",
NamespaceName: "100086.properties",
IsBackupConfig: true,
Secret: "36ae07ed3b8d4885b2c7fa8538c091c1",
}
client, _ := agollo.StartWithConfig(func() (*config.AppConfig, error) {
return c, nil
})
fmt.Println("初始化Apollo配置成功")
//Use your apollo key to test
cache := client.GetConfigCache(c.NamespaceName)
value, _ := cache.Get("2323423")
fmt.Println(value)
}
json类型
go
复制代码
package main
import (
"fmt"
"github.com/apolloconfig/agollo/v4"
"github.com/apolloconfig/agollo/v4/env/config"
)
func main() {
c := &config.AppConfig{
AppID: "2222",
Cluster: "dev",
IP: "http://192.168.0.52:8080/",
NamespaceName: "1008611.json",
IsBackupConfig: true,
Secret: "36ae07ed3b8d4885b2c7fa8538c091c1",
}
client, _ := agollo.StartWithConfig(func() (*config.AppConfig, error) {
return c, nil
})
fmt.Println("初始化Apollo配置成功")
//Use your apollo key to test
cache := client.GetConfigCache(c.NamespaceName)
cache.Range(func(key, value interface{}) bool {
fmt.Println(key, value)
return true
})
// fmt.Println(value)
}
连接redis
集群:
go
复制代码
package main
import (
"context"
"fmt"
"time"
"github.com/redis/go-redis/v9"
)
func main() {
rdb := redis.NewClusterClient(&redis.ClusterOptions{
Addrs: Jobconfig.Redis.Ip,
Password: Jobconfig.Redis.Password,
PoolSize: 10,
})
err := rdb.Ping(context.Background()).Err()
if err != nil {
panic("redis初始化出现问题")
}
fmt.Println("连接成功")
time.Sleep(10 * time.Second)
}
单机:
go
复制代码
rdb := redis.NewClient(&redis.Options{
Addr: "192.168.0.52:6379",
Password: "lushouxin@123", // no password set
DB: 0, // use default DB
})
err := rdb.Ping(context.Background()).Err()
if err != nil {
panic("redis初始化出现问题")
}
RDB = rdb
log.Info("redis初始化成功")