Skip to content

Commit

Permalink
fix:优化v2路由规则对于v1路由规则的兼容逻辑 (#707)
Browse files Browse the repository at this point in the history
* fix issue #629 (#693)

* docs:优化错误信息描述

* Update zh.toml

* fix:修复eureka心跳协议错误码不兼容问题

* fix:修复eureka心跳协议错误码不兼容问题

* unit:添加单元测试

* test:调整测试配置文件位置

* fix:issue #692

* fix:issue #692

* fix:issue #692

* fix:issue #692

* docs:add error code desc

* fix:优化v2路由规则对于v1路由规则的兼容逻辑

* fix:优化v2路由规则对于v1路由规则的兼容逻辑

* fix:优化v2路由规则对于v1路由规则的兼容逻辑
  • Loading branch information
chuntaojun committed Sep 29, 2022
1 parent 2d3807b commit d8d52bb
Show file tree
Hide file tree
Showing 13 changed files with 399 additions and 113 deletions.
10 changes: 8 additions & 2 deletions apiserver/httpserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,11 +358,17 @@ func (h *HTTPServer) createRestfulContainer() (*restful.Container, error) {
}
case "client":
if config.Enable {
service, err := h.GetClientAccessServer(config.Include)
serviceV1, err := h.v1Server.GetClientAccessServer(config.Include)
if err != nil {
return nil, err
}
wsContainer.Add(service)
wsContainer.Add(serviceV1)

serviceV2, err := h.v2Server.GetClientAccessServer(config.Include)
if err != nil {
return nil, err
}
wsContainer.Add(serviceV2)
}
case "config":
if config.Enable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* specific language governing permissions and limitations under the License.
*/

package httpserver
package v1

import (
"fmt"
Expand All @@ -30,7 +30,7 @@ import (
)

// GetClientAccessServer get client access server
func (h *HTTPServer) GetClientAccessServer(include []string) (*restful.WebService, error) {
func (h *HTTPServerV1) GetClientAccessServer(include []string) (*restful.WebService, error) {
clientAccess := []string{apiserver.DiscoverAccess, apiserver.RegisterAccess, apiserver.HealthcheckAccess}

ws := new(restful.WebService)
Expand Down Expand Up @@ -61,7 +61,7 @@ func (h *HTTPServer) GetClientAccessServer(include []string) (*restful.WebServic
}

// addDiscoverAccess 增加服务发现接口
func (h *HTTPServer) addDiscoverAccess(ws *restful.WebService) {
func (h *HTTPServerV1) addDiscoverAccess(ws *restful.WebService) {
tags := []string{"DiscoverAccess"}
ws.Route(ws.POST("/ReportClient").To(h.ReportClient).
Doc("上报客户端").
Expand All @@ -73,7 +73,7 @@ func (h *HTTPServer) addDiscoverAccess(ws *restful.WebService) {
}

// addRegisterAccess 增加注册/反注册接口
func (h *HTTPServer) addRegisterAccess(ws *restful.WebService) {
func (h *HTTPServerV1) addRegisterAccess(ws *restful.WebService) {
tags := []string{"Instances", "RegisterAccess"}
ws.Route(ws.POST("/RegisterInstance").
Doc("注册实例").
Expand All @@ -87,12 +87,12 @@ func (h *HTTPServer) addRegisterAccess(ws *restful.WebService) {
}

// addHealthCheckAccess 增加健康检查接口
func (h *HTTPServer) addHealthCheckAccess(ws *restful.WebService) {
func (h *HTTPServerV1) addHealthCheckAccess(ws *restful.WebService) {
ws.Route(ws.POST("/Heartbeat").To(h.Heartbeat))
}

// ReportClient 客户端上报信息
func (h *HTTPServer) ReportClient(req *restful.Request, rsp *restful.Response) {
func (h *HTTPServerV1) ReportClient(req *restful.Request, rsp *restful.Response) {
handler := &httpcommon.Handler{req, rsp}
client := &api.Client{}
ctx, err := handler.Parse(client)
Expand All @@ -105,7 +105,7 @@ func (h *HTTPServer) ReportClient(req *restful.Request, rsp *restful.Response) {
}

// RegisterInstance 注册服务实例
func (h *HTTPServer) RegisterInstance(req *restful.Request, rsp *restful.Response) {
func (h *HTTPServerV1) RegisterInstance(req *restful.Request, rsp *restful.Response) {
handler := &httpcommon.Handler{req, rsp}

instance := &api.Instance{}
Expand All @@ -119,7 +119,7 @@ func (h *HTTPServer) RegisterInstance(req *restful.Request, rsp *restful.Respons
}

// DeregisterInstance 反注册服务实例
func (h *HTTPServer) DeregisterInstance(req *restful.Request, rsp *restful.Response) {
func (h *HTTPServerV1) DeregisterInstance(req *restful.Request, rsp *restful.Response) {
handler := &httpcommon.Handler{req, rsp}

instance := &api.Instance{}
Expand All @@ -133,7 +133,7 @@ func (h *HTTPServer) DeregisterInstance(req *restful.Request, rsp *restful.Respo
}

// Discover 统一发现接口
func (h *HTTPServer) Discover(req *restful.Request, rsp *restful.Response) {
func (h *HTTPServerV1) Discover(req *restful.Request, rsp *restful.Response) {
handler := &httpcommon.Handler{req, rsp}

discoverRequest := &api.DiscoverRequest{}
Expand Down Expand Up @@ -171,7 +171,7 @@ func (h *HTTPServer) Discover(req *restful.Request, rsp *restful.Response) {
}

// Heartbeat 服务实例心跳
func (h *HTTPServer) Heartbeat(req *restful.Request, rsp *restful.Response) {
func (h *HTTPServerV1) Heartbeat(req *restful.Request, rsp *restful.Response) {
handler := &httpcommon.Handler{req, rsp}

instance := &api.Instance{}
Expand Down
93 changes: 93 additions & 0 deletions apiserver/httpserver/v2/naming_client_access.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* Tencent is pleased to support the open source community by making Polaris available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package v2

import (
"fmt"

"github.com/emicklei/go-restful/v3"
restfulspec "github.com/polarismesh/go-restful-openapi/v2"
"go.uber.org/zap"

"github.com/polarismesh/polaris-server/apiserver"
httpcommon "github.com/polarismesh/polaris-server/apiserver/httpserver/http"
api "github.com/polarismesh/polaris-server/common/api/v1"
apiv2 "github.com/polarismesh/polaris-server/common/api/v2"
)

// GetClientAccessServer get client access server
func (h *HTTPServerV2) GetClientAccessServer(include []string) (*restful.WebService, error) {
clientAccess := []string{apiserver.DiscoverAccess, apiserver.RegisterAccess, apiserver.HealthcheckAccess}

ws := new(restful.WebService)

ws.Path("/v2").Consumes(restful.MIME_JSON).Produces(restful.MIME_JSON)

// 如果为空,则开启全部接口
if len(include) == 0 {
include = clientAccess
}

// 客户端接口:增删改请求操作存储层,查请求访问缓存
for _, item := range include {
switch item {
case apiserver.DiscoverAccess:
h.addDiscoverAccess(ws)
}
}

return ws, nil
}

// addDiscoverAccess 增加服务发现接口
func (h *HTTPServerV2) addDiscoverAccess(ws *restful.WebService) {
tags := []string{"DiscoverAccess"}
ws.Route(ws.POST("/Discover").To(h.Discover).
Doc("服务发现").
Metadata(restfulspec.KeyOpenAPITags, tags))
}

// Discover 统一发现接口
func (h *HTTPServerV2) Discover(req *restful.Request, rsp *restful.Response) {
handler := &httpcommon.Handler{req, rsp}

discoverRequest := &apiv2.DiscoverRequest{}
ctx, err := handler.Parse(discoverRequest)
if err != nil {
handler.WriteHeaderAndProto(api.NewResponseWithMsg(api.ParseException, err.Error()))
return
}

msg := fmt.Sprintf("receive http discover request: %s", discoverRequest.GetSerivce().String())
namingLog.Info(msg,
zap.String("type", api.DiscoverRequest_DiscoverRequestType_name[int32(discoverRequest.Type)]),
zap.String("client-address", req.Request.RemoteAddr),
zap.String("user-agent", req.HeaderParameter("User-Agent")),
zap.String("request-id", req.HeaderParameter("Request-Id")),
)

var ret *apiv2.DiscoverResponse
switch discoverRequest.Type {
case apiv2.DiscoverRequest_ROUTING:
ret = h.namingServer.GetRoutingConfigV2WithCache(ctx, discoverRequest.GetSerivce())
default:
ret = apiv2.NewDiscoverRoutingResponse(api.InvalidDiscoverResource, discoverRequest.GetSerivce())
}

handler.WriteHeaderAndProtoV2(ret)
}
1 change: 0 additions & 1 deletion cache/routing_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ func formatRoutingResponseV1(ret *apiv1.Routing) *apiv1.Routing {
for i := range outBounds {
outBounds[i].ExtendInfo = nil
}

return ret
}

Expand Down
4 changes: 2 additions & 2 deletions common/api/v2/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ func NewRoutingResponse(code uint32, routing *Routing) *Response {
ret, err := ptypes.MarshalAny(routing)
if err != nil {
return &Response{
Code: v1.ExecuteException,
Info: v1.Code2Info(code) + ": " + err.Error(),
Code: code,
Info: v1.Code2Info(code),
}
}

Expand Down
1 change: 0 additions & 1 deletion common/model/v2/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ func (r *ExtendRoutingConfig) ToApi() (*apiv2.Routing, error) {
Etime: commontime.Time2String(r.EnableTime),
Priority: r.Priority,
Description: r.Description,
ExtendInfo: r.ExtendInfo,
}, nil
}

Expand Down
3 changes: 0 additions & 3 deletions common/routing/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,6 @@ func BuildInBoundsFromV2(service, namespace string, item *v2.ExtendRoutingConfig
{
Sources: v1sources,
Destinations: v1destinations,
ExtendInfo: map[string]string{
v2.V2RuleIDKey: item.ID,
},
},
}
}
Expand Down
6 changes: 3 additions & 3 deletions polaris-server.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,9 @@ store:
# master:
# dbType: mysql
# dbName: polaris_server
# dbUser: root ##DB_USER##
# dbPwd: polaris ##DB_PWD##
# dbAddr: 127.0.0.1:3306 ##DB_ADDR##
# dbUser: ##DB_USER##
# dbPwd: ##DB_PWD##
# dbAddr: ##DB_ADDR##
# maxOpenConns: 300
# maxIdleConns: 50
# connMaxLifetime: 300 # 单位秒
Expand Down
98 changes: 95 additions & 3 deletions service/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,97 @@ func (d *DiscoverTestSuit) createCommonRoutingConfig(t *testing.T, service *api.
return conf, resp.Responses[0].GetRouting()
}

// 创建一个路由配置
func (d *DiscoverTestSuit) createCommonRoutingConfigV1IntoOldStore(t *testing.T, service *api.Service,
inCount int, outCount int) (*api.Routing, *api.Routing) {

inBounds := make([]*api.Route, 0, inCount)
for i := 0; i < inCount; i++ {
matchString := &api.MatchString{
Type: api.MatchString_EXACT,
Value: utils.NewStringValue(fmt.Sprintf("in-meta-value-%d", i)),
}
source := &api.Source{
Service: utils.NewStringValue(fmt.Sprintf("in-source-service-%d", i)),
Namespace: utils.NewStringValue(fmt.Sprintf("in-source-service-%d", i)),
Metadata: map[string]*api.MatchString{
fmt.Sprintf("in-metadata-%d", i): matchString,
},
}
destination := &api.Destination{
Service: service.Name,
Namespace: service.Namespace,
Metadata: map[string]*api.MatchString{
fmt.Sprintf("in-metadata-%d", i): matchString,
},
Priority: utils.NewUInt32Value(120),
Weight: utils.NewUInt32Value(100),
Transfer: utils.NewStringValue("abcdefg"),
}

entry := &api.Route{
Sources: []*api.Source{source},
Destinations: []*api.Destination{destination},
}
inBounds = append(inBounds, entry)
}

conf := &api.Routing{
Service: utils.NewStringValue(service.GetName().GetValue()),
Namespace: utils.NewStringValue(service.GetNamespace().GetValue()),
Inbounds: inBounds,
ServiceToken: utils.NewStringValue(service.GetToken().GetValue()),
}

resp := d.server.(*serverAuthAbility).targetServer.CreateRoutingConfig(d.defaultCtx, conf)
if !respSuccess(resp) {
t.Fatalf("error: %+v", resp)
}

return conf, resp.GetRouting()
}

func mockRoutingV1(serviceName, serviceNamespace string, inCount int) *api.Routing {
inBounds := make([]*api.Route, 0, inCount)
for i := 0; i < inCount; i++ {
matchString := &api.MatchString{
Type: api.MatchString_EXACT,
Value: utils.NewStringValue(fmt.Sprintf("in-meta-value-%d", i)),
}
source := &api.Source{
Service: utils.NewStringValue(fmt.Sprintf("in-source-service-%d", i)),
Namespace: utils.NewStringValue(fmt.Sprintf("in-source-service-%d", i)),
Metadata: map[string]*api.MatchString{
fmt.Sprintf("in-metadata-%d", i): matchString,
},
}
destination := &api.Destination{
Service: utils.NewStringValue(serviceName),
Namespace: utils.NewStringValue(serviceNamespace),
Metadata: map[string]*api.MatchString{
fmt.Sprintf("in-metadata-%d", i): matchString,
},
Priority: utils.NewUInt32Value(120),
Weight: utils.NewUInt32Value(100),
Transfer: utils.NewStringValue("abcdefg"),
}

entry := &api.Route{
Sources: []*api.Source{source},
Destinations: []*api.Destination{destination},
}
inBounds = append(inBounds, entry)
}

conf := &api.Routing{
Service: utils.NewStringValue(serviceName),
Namespace: utils.NewStringValue(serviceNamespace),
Inbounds: inBounds,
}

return conf
}

func mockRoutingV2(t *testing.T, cnt int32) []*apiv2.Routing {
rules := make([]*apiv2.Routing, 0, cnt)
for i := int32(0); i < cnt; i++ {
Expand Down Expand Up @@ -790,9 +881,6 @@ func mockRoutingV2(t *testing.T, cnt int32) []*apiv2.Routing {
Etime: "",
Priority: 0,
Description: "",
ExtendInfo: map[string]string{
"": "",
},
}

rules = append(rules, item)
Expand All @@ -815,6 +903,10 @@ func (d *DiscoverTestSuit) createCommonRoutingConfigV2WithReq(t *testing.T, rule
t.Fatalf("error: %+v", resp)
}

if len(rules) != len(resp.GetResponses()) {
t.Fatal("error: create v2 routings not equal resp")
}

ret := []*apiv2.Routing{}
for i := range resp.GetResponses() {
item := resp.GetResponses()[i]
Expand Down
Loading

0 comments on commit d8d52bb

Please sign in to comment.