Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Request-Module header when requesting endpoint #685

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clients/config_client/config_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type ConfigProxy struct {
func NewConfigProxy(ctx context.Context, serverConfig []constant.ServerConfig, clientConfig constant.ClientConfig, httpAgent http_agent.IHttpAgent) (IConfigProxy, error) {
proxy := ConfigProxy{}
var err error
proxy.nacosServer, err = nacos_server.NewNacosServer(ctx, serverConfig, clientConfig, httpAgent, clientConfig.TimeoutMs, clientConfig.Endpoint)
proxy.nacosServer, err = nacos_server.NewNacosServer(ctx, serverConfig, clientConfig, httpAgent, clientConfig.TimeoutMs, clientConfig.Endpoint, nil)
proxy.clientConfig = clientConfig
return &proxy, err
}
Expand Down
13 changes: 12 additions & 1 deletion clients/naming_client/naming_proxy_delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package naming_client

import (
"context"
"github.com/nacos-group/nacos-sdk-go/v2/inner/uuid"

"github.com/nacos-group/nacos-sdk-go/v2/clients/naming_client/naming_cache"
"github.com/nacos-group/nacos-sdk-go/v2/clients/naming_client/naming_grpc"
Expand All @@ -40,7 +41,17 @@ type NamingProxyDelegate struct {
func NewNamingProxyDelegate(ctx context.Context, clientCfg constant.ClientConfig, serverCfgs []constant.ServerConfig,
httpAgent http_agent.IHttpAgent, serviceInfoHolder *naming_cache.ServiceInfoHolder) (naming_proxy.INamingProxy, error) {

nacosServer, err := nacos_server.NewNacosServer(ctx, serverCfgs, clientCfg, httpAgent, clientCfg.TimeoutMs, clientCfg.Endpoint)
uid, err := uuid.NewV4()
if err != nil {
return nil, err
}
namingHeader := map[string][]string{
AlbumenJ marked this conversation as resolved.
Show resolved Hide resolved
"Client-Version": {constant.CLIENT_VERSION},
"User-Agent": {constant.CLIENT_VERSION},
"RequestId": {uid.String()},
"Request-Module": {"Naming"},
}
nacosServer, err := nacos_server.NewNacosServer(ctx, serverCfgs, clientCfg, httpAgent, clientCfg.TimeoutMs, clientCfg.Endpoint, namingHeader)
if err != nil {
return nil, err
}
Expand Down
12 changes: 7 additions & 5 deletions common/nacos_server/nacos_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@ type NacosServer struct {
contextPath string
endpointContextPath string
endpointQueryParams string
endpointQueryHeader map[string][]string
clusterName string
currentIndex int32
ServerSrcChangeSignal chan struct{}
}

func NewNacosServer(ctx context.Context, serverList []constant.ServerConfig, clientCfg constant.ClientConfig, httpAgent http_agent.IHttpAgent, timeoutMs uint64, endpoint string) (*NacosServer, error) {
func NewNacosServer(ctx context.Context, serverList []constant.ServerConfig, clientCfg constant.ClientConfig, httpAgent http_agent.IHttpAgent, timeoutMs uint64, endpoint string, endpointQueryHeader map[string][]string) (*NacosServer, error) {
severLen := len(serverList)
if severLen == 0 && endpoint == "" {
return &NacosServer{}, errors.New("both serverlist and endpoint are empty")
Expand All @@ -80,6 +81,7 @@ func NewNacosServer(ctx context.Context, serverList []constant.ServerConfig, cli
vipSrvRefInterMills: 10000,
endpointContextPath: clientCfg.EndpointContextPath,
endpointQueryParams: clientCfg.EndpointQueryParams,
endpointQueryHeader: endpointQueryHeader,
clusterName: clientCfg.ClusterName,
contextPath: clientCfg.ContextPath,
ServerSrcChangeSignal: make(chan struct{}, 1),
Expand Down Expand Up @@ -285,29 +287,29 @@ func (server *NacosServer) initRefreshSrvIfNeed(ctx context.Context) {
}
logger.Infof("nacos address server url: <%s>", urlString)

server.refreshServerSrvIfNeed(urlString)
server.refreshServerSrvIfNeed(urlString, server.endpointQueryHeader)
go func() {
for {
select {
case <-ctx.Done():
return
default:
time.Sleep(time.Duration(10) * time.Second)
server.refreshServerSrvIfNeed(urlString)
server.refreshServerSrvIfNeed(urlString, server.endpointQueryHeader)
}
}
}()

}

func (server *NacosServer) refreshServerSrvIfNeed(urlString string) {
func (server *NacosServer) refreshServerSrvIfNeed(urlString string, header map[string][]string) {
if util.CurrentMillis()-server.lastSrvRefTime < server.vipSrvRefInterMills && len(server.serverList) > 0 {
return
}

var list []string

result := server.httpAgent.RequestOnlyResult(http.MethodGet, urlString, nil, server.timeoutMs, nil)
result := server.httpAgent.RequestOnlyResult(http.MethodGet, urlString, header, server.timeoutMs, nil)
list = strings.Split(result, "\n")

var servers []constant.ServerConfig
Expand Down
3 changes: 2 additions & 1 deletion common/nacos_server/nacos_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ func buildNacosServer(clientConfig constant.ClientConfig) (*NacosServer, error)
clientConfig,
&http_agent.HttpAgent{},
1000,
"")
"",
nil)
}

func TestNacosServer_InjectSignForNamingHttp_NoAk(t *testing.T) {
Expand Down
Loading