Skip to content

Commit

Permalink
fix:修复demo配置不正确问题
Browse files Browse the repository at this point in the history
  • Loading branch information
chuntaojun committed Jul 19, 2024
1 parent 2aab751 commit e3d2d51
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 11 deletions.
1 change: 1 addition & 0 deletions examples/circuitbreaker/instance/consumer/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+Ce
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
Expand Down
1 change: 1 addition & 0 deletions examples/circuitbreaker/instance/consumer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func (svr *PolarisConsumer) discoverInstance() (string, model.Resource, error) {
getOneRequest := &polaris.GetOneInstanceRequest{}
getOneRequest.Namespace = namespace
getOneRequest.Service = service
// 允许返回熔断半开的实例
getOneRequest.IncludeCircuitBreakInstances = true
oneInstResp, err := svr.consumer.GetOneInstance(getOneRequest)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions examples/circuitbreaker/instance/provider/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+Ce
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
Expand Down
30 changes: 24 additions & 6 deletions examples/circuitbreaker/instance/provider/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func initArgs() {

// PolarisProvider is a provider for polaris
type PolarisProvider struct {
webSvr *http.Server
provider polaris.ProviderAPI
namespace string
service string
Expand Down Expand Up @@ -87,6 +88,7 @@ func (svr *PolarisProvider) runWebServer() {
} else {
atomic.StoreInt32(&svr.needErr, 0)
}
log.Printf("switch success openError:" + val)
rw.WriteHeader(http.StatusOK)
})

Expand All @@ -99,8 +101,9 @@ func (svr *PolarisProvider) runWebServer() {

go func() {
log.Printf("[INFO] start http server, listen port is %v", svr.port)
if err := http.Serve(ln, nil); err != nil {
log.Fatalf("[ERROR]fail to run webServer, err is %v", err)
svr.webSvr = &http.Server{}
if err := svr.webSvr.Serve(ln); err != nil {
log.Printf("[ERROR]fail to run webServer, err is %v", err)
}
}()
}
Expand All @@ -113,14 +116,28 @@ func (svr *PolarisProvider) registerService() {
registerRequest.Host = svr.host
registerRequest.Port = svr.port
registerRequest.ServiceToken = token
resp, err := svr.provider.Register(registerRequest)
resp, err := svr.provider.RegisterInstance(registerRequest)
if err != nil {
log.Fatalf("fail to register instance, err is %v", err)
}
log.Printf("register response: instanceId %s", resp.InstanceID)
}

func runMainLoop() {
func (svr *PolarisProvider) deregisterService() {
log.Printf("start to invoke deregister operation")
registerRequest := &polaris.InstanceDeRegisterRequest{}
registerRequest.Service = service
registerRequest.Namespace = namespace
registerRequest.Host = svr.host
registerRequest.Port = svr.port
registerRequest.ServiceToken = token
if err := svr.provider.Deregister(registerRequest); err != nil {
log.Fatalf("fail to deregister instance, err is %v", err)
}
log.Printf("deregister finished")
}

func (svr *PolarisProvider) runMainLoop() {
ch := make(chan os.Signal, 1)
signal.Notify(ch, []os.Signal{
syscall.SIGINT, syscall.SIGTERM,
Expand All @@ -129,6 +146,8 @@ func runMainLoop() {

for s := range ch {
log.Printf("catch signal(%+v), stop servers", s)
_ = svr.webSvr.Close()
svr.deregisterService()
return
}
}
Expand Down Expand Up @@ -156,8 +175,7 @@ func main() {
}

svr.Run()

runMainLoop()
svr.runMainLoop()
}

func getLocalHost(serverAddr string) (string, error) {
Expand Down
12 changes: 7 additions & 5 deletions polaris.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ global:
#默认值:1s
retryInterval: 1s
#描述:客户端绑定的网卡地址
bindIf:
bindIf:
#描述:对接polaris server的相关配置
serverConnector:
#描述:访问server的连接协议,SDK会根据协议名称会加载对应的插件
Expand Down Expand Up @@ -118,7 +118,7 @@ global:
#类型:string
#默认值: ${global.api.bindIP}
#默认使用SDK的绑定IP
metricHost:
metricHost:
#描述: 设置 prometheus http-server 的监听端口, 仅 type == pull 时生效
#类型:int
#默认值: 28080
Expand Down Expand Up @@ -287,12 +287,14 @@ config:
serverSwitchInterval: 10m
#描述:重连间隔时间
reconnectInterval: 500ms
#描述: 开启客户端鉴权后,需要填写用户/用户组的访问凭据
token: ""
#描述:连接器插件配置
plugin:
polaris:
#描述:GRPC客户端单次最大链路接收报文
#类型:int
#范围:(0:524288000]
#描述:GRPC客户端单次最大链路接收报文
#类型:int
#范围:(0:524288000]
maxCallRecvMsgSize: 52428800
# 配置过滤器
configFilter:
Expand Down

0 comments on commit e3d2d51

Please sign in to comment.