From 278cb0f0dc0bcc108a04ec33c19e3db0517b37a2 Mon Sep 17 00:00:00 2001 From: little-cui Date: Sun, 22 Oct 2017 20:17:29 -0500 Subject: [PATCH] Update APIs version from v3 to v4. (#134) * Changes: Update APIs version from v3 to v4. * Changes: Update APIs version from v3 to v4. * Changes: Update APIs version from v3 to v4. * Add idle timeout in http server. * Bug fixes: Block in shutting down server gracefully. --- etc/conf/app.conf | 5 +- .../body/get_instance_response.json | 2 +- .../service_center/request/add_dependecy.sh | 2 +- .../service_center/request/create_service.sh | 2 +- .../service_center/request/delete_instance.sh | 2 +- .../service_center/request/delete_service.sh | 2 +- .../service_center/request/get_consumers.sh | 2 +- .../service_center/request/get_providers.sh | 2 +- .../service_center/request/get_service.sh | 2 +- examples/service_center/request/heartbeat.sh | 2 +- .../request/register_intance_json.sh | 2 +- .../request/update_properties.sh | 2 +- .../service_center/request/update_status.sh | 2 +- integration/apis.go | 72 +++---- integration/health_test.go | 2 +- pkg/rest/roa.go | 4 + pkg/rest/route.go | 35 +++- pkg/rest/server.go | 7 +- server/bootstrap/bootstrap.go | 2 - server/core/swagger/services.yaml | 182 +++++++++++++----- .../{v3 => v4}/governservice_controller.go | 32 +-- .../{v3 => v4}/instance_controller.go | 20 +- .../controller/{v3 => v4}/instance_watcher.go | 6 +- .../controller/{v3 => v4}/main_controller.go | 8 +- .../{v3 => v4}/microservice_controller.go | 30 +-- .../{v3 => v4}/query_rule_controller.go | 10 +- .../controller/{v3 => v4}/tag_controller.go | 10 +- server/rest/controller/{v3/v3.go => v4/v4.go} | 48 ++++- server/rest/handler.go | 8 +- server/rest/server.go | 2 + server/server.go | 24 +++ server/service/schema.go | 4 +- server/service/schema_test.go | 6 +- 33 files changed, 370 insertions(+), 171 deletions(-) rename server/rest/controller/{v3 => v4}/governservice_controller.go (93%) rename server/rest/controller/{v3 => v4}/instance_controller.go (89%) rename server/rest/controller/{v3 => v4}/instance_watcher.go (90%) rename server/rest/controller/{v3 => v4}/main_controller.go (91%) rename server/rest/controller/{v3 => v4}/microservice_controller.go (89%) rename server/rest/controller/{v3 => v4}/query_rule_controller.go (89%) rename server/rest/controller/{v3 => v4}/tag_controller.go (88%) rename server/rest/controller/{v3/v3.go => v4/v4.go} (53%) diff --git a/etc/conf/app.conf b/etc/conf/app.conf index 4f8b66b23..c50811065 100644 --- a/etc/conf/app.conf +++ b/etc/conf/app.conf @@ -1,12 +1,10 @@ ComponentName = service_center -version = v3 -tenant_mode = dedicated #run mode could be many options to specify the env like prod,dev runmode = dev #indicate how many revision you want to keep in etcd compact_index_delta=100 -cipher_plugin = +cipher_plugin = "" # listen address httpaddr = 127.0.0.1 @@ -34,6 +32,7 @@ auto_sync_interval = 30 read_header_timeout = 60s read_timeout = 60s +idle_timeout = 60s write_timeout = 60s max_header_bytes = 32768 # 32K max_body_bytes = 2097152 # 2M diff --git a/examples/service_center/body/get_instance_response.json b/examples/service_center/body/get_instance_response.json index 9df0773cc..a804e95a0 100644 --- a/examples/service_center/body/get_instance_response.json +++ b/examples/service_center/body/get_instance_response.json @@ -1,4 +1,4 @@ -GET /registry/v3/microservices/349524856cf811e7b7bc286ed488ff40/instances/838942c26cf911e7b7bc286ed488ff40 HTTP/1.1 +GET /v4/default/registry/microservices/349524856cf811e7b7bc286ed488ff40/instances/838942c26cf911e7b7bc286ed488ff40 HTTP/1.1 Host: 127.0.0.1:30100 X-tenant-name: default Content-Type: application/json diff --git a/examples/service_center/request/add_dependecy.sh b/examples/service_center/request/add_dependecy.sh index bca201f21..76b54cb8c 100644 --- a/examples/service_center/request/add_dependecy.sh +++ b/examples/service_center/request/add_dependecy.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -PUT /registry/v3/dependencies HTTP/1.1 +PUT /v4/default/registry/dependencies HTTP/1.1 Host: localhost:30100 Content-Type: application/json x-domain-name: default diff --git a/examples/service_center/request/create_service.sh b/examples/service_center/request/create_service.sh index 15b271d9a..7b06554a4 100644 --- a/examples/service_center/request/create_service.sh +++ b/examples/service_center/request/create_service.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -POST /registry/v3/microservices HTTP/1.1 +POST /v4/default/registry/microservices HTTP/1.1 Host: localhost:30100 Content-Type: application/json x-domain-name: default diff --git a/examples/service_center/request/delete_instance.sh b/examples/service_center/request/delete_instance.sh index 6dfd1e043..d7f25b5a2 100644 --- a/examples/service_center/request/delete_instance.sh +++ b/examples/service_center/request/delete_instance.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -DELETE /registry/v3/microservices/serviceID/instances/instanceID HTTP/1.1 +DELETE /v4/default/registry/microservices/serviceID/instances/instanceID HTTP/1.1 Host: localhost:30100 x-domain-name: default Cache-Control: no-cache diff --git a/examples/service_center/request/delete_service.sh b/examples/service_center/request/delete_service.sh index 53ae22741..8d191c9bf 100644 --- a/examples/service_center/request/delete_service.sh +++ b/examples/service_center/request/delete_service.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -DELETE /registry/v3/microservices/63cc69716a8f11e79855286ed488ca3e?force=1 HTTP/1.1 +DELETE /v4/default/registry/microservices/63cc69716a8f11e79855286ed488ca3e?force=1 HTTP/1.1 Host: localhost:30100 Content-Type: application/json x-domain-name: default diff --git a/examples/service_center/request/get_consumers.sh b/examples/service_center/request/get_consumers.sh index 57291620f..3aaa42b37 100644 --- a/examples/service_center/request/get_consumers.sh +++ b/examples/service_center/request/get_consumers.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -GET /registry/v3/microservices/3/consumers HTTP/1.1 +GET /v4/default/registry/microservices/3/consumers HTTP/1.1 Host: localhost:30100 x-domain-name: default Cache-Control: no-cache diff --git a/examples/service_center/request/get_providers.sh b/examples/service_center/request/get_providers.sh index da0869112..5ece4683a 100644 --- a/examples/service_center/request/get_providers.sh +++ b/examples/service_center/request/get_providers.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -GET /registry/v3/microservices/2/providers HTTP/1.1 +GET /v4/default/registry/microservices/2/providers HTTP/1.1 Host: localhost:30100 X-Tenant-Name: default X-Project-Name: default diff --git a/examples/service_center/request/get_service.sh b/examples/service_center/request/get_service.sh index a40b3652d..c6a40c636 100644 --- a/examples/service_center/request/get_service.sh +++ b/examples/service_center/request/get_service.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -GET /registry/v3/microservices HTTP/1.1 +GET /v4/default/registry/microservices HTTP/1.1 Host: localhost:30100 Content-Type: application/json x-domain-name: default diff --git a/examples/service_center/request/heartbeat.sh b/examples/service_center/request/heartbeat.sh index 88e1d2287..d0c21ee4a 100644 --- a/examples/service_center/request/heartbeat.sh +++ b/examples/service_center/request/heartbeat.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -PUT /registry/v3/microservices/2/instances/36/heartbeat HTTP/1.1 +PUT /v4/default/registry/microservices/2/instances/36/heartbeat HTTP/1.1 Host: localhost:30100 Content-Type: application/json x-domain-name: default diff --git a/examples/service_center/request/register_intance_json.sh b/examples/service_center/request/register_intance_json.sh index f1058ccd3..52753a040 100644 --- a/examples/service_center/request/register_intance_json.sh +++ b/examples/service_center/request/register_intance_json.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -POST /registry/v3/microservices/2/instances HTTP/1.1 +POST /v4/default/registry/microservices/2/instances HTTP/1.1 Host: localhost:30100 Content-Type: application/json x-domain-name: default diff --git a/examples/service_center/request/update_properties.sh b/examples/service_center/request/update_properties.sh index 0f6f43610..06991530a 100644 --- a/examples/service_center/request/update_properties.sh +++ b/examples/service_center/request/update_properties.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -PUT /registry/v3/microservices/2/properties HTTP/1.1 +PUT /v4/default/registry/microservices/2/properties HTTP/1.1 Host: localhost:30100 Content-Type: application/json x-domain-name: default diff --git a/examples/service_center/request/update_status.sh b/examples/service_center/request/update_status.sh index 8468033ee..3127e35f9 100644 --- a/examples/service_center/request/update_status.sh +++ b/examples/service_center/request/update_status.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -PUT /registry/v3/microservices/serviceID/instances/instanceID/status?value=UP HTTP/1.1 +PUT /v4/default/registry/microservices/serviceID/instances/instanceID/status?value=UP HTTP/1.1 Host: localhost:30100 Content-Type: application/json x-domain-name: default diff --git a/integration/apis.go b/integration/apis.go index 32fd61b1d..d9f0e07db 100644 --- a/integration/apis.go +++ b/integration/apis.go @@ -14,52 +14,52 @@ package integrationtest // Basic API -var HEALTH = "/health" -var VERSION = "/version" +var HEALTH = "/v4/default/registry/health" +var VERSION = "/v4/default/registry/version" // Micro-Service API's -var CHECKEXISTENCE = "/registry/v3/existence" -var GETALLSERVICE = "/registry/v3/microservices" -var GETSERVICEBYID = "/registry/v3/microservices/:serviceId" -var REGISTERMICROSERVICE = "/registry/v3/microservices" -var UPDATEMICROSERVICE = "/registry/v3/microservices/:serviceId/properties" -var UNREGISTERMICROSERVICE = "/registry/v3/microservices/:serviceId?force=1" -var GETSCHEMABYID = "/registry/v3/microservices/:serviceId/schemas/:schemaId" -var UPDATESCHEMA = "/registry/v3/microservices/:serviceId/schemas/:schemaId" -var UPDATESCHEMAS = "/registry/v3/microservices/:serviceId/schemas" -var DELETESCHEMA = "/registry/v3/microservices/:serviceId/schemas/:schemaId" -var CREATEDEPENDENCIES = "/registry/v3/dependencies" -var GETCONPRODEPENDENCY = "/registry/v3/microservices/:consumerId/providers" -var GETPROCONDEPENDENCY = "/registry/v3/microservices/:providerId/consumers" +var CHECKEXISTENCE = "/v4/default/registry/existence" +var GETALLSERVICE = "/v4/default/registry/microservices" +var GETSERVICEBYID = "/v4/default/registry/microservices/:serviceId" +var REGISTERMICROSERVICE = "/v4/default/registry/microservices" +var UPDATEMICROSERVICE = "/v4/default/registry/microservices/:serviceId/properties" +var UNREGISTERMICROSERVICE = "/v4/default/registry/microservices/:serviceId?force=1" +var GETSCHEMABYID = "/v4/default/registry/microservices/:serviceId/schemas/:schemaId" +var UPDATESCHEMA = "/v4/default/registry/microservices/:serviceId/schemas/:schemaId" +var UPDATESCHEMAS = "/v4/default/registry/microservices/:serviceId/schemas" +var DELETESCHEMA = "/v4/default/registry/microservices/:serviceId/schemas/:schemaId" +var CREATEDEPENDENCIES = "/v4/default/registry/dependencies" +var GETCONPRODEPENDENCY = "/v4/default/registry/microservices/:consumerId/providers" +var GETPROCONDEPENDENCY = "/v4/default/registry/microservices/:providerId/consumers" // Instance API's -var FINDINSTANCE = "/registry/v3/instances" -var GETINSTANCE = "/registry/v3/microservices/:serviceId/instances" -var GETINSTANCEBYINSTANCEID = "/registry/v3/microservices/:serviceId/instances/:instanceId" -var REGISTERINSTANCE = "/registry/v3/microservices/:serviceId/instances" -var UNREGISTERINSTANCE = "/registry/v3/microservices/:serviceId/instances/:instanceId" -var UPDATEINSTANCEMETADATA = "/registry/v3/microservices/:serviceId/instances/:instanceId/properties" -var UPDATEINSTANCESTATUS = "/registry/v3/microservices/:serviceId/instances/:instanceId/status" -var INSTANCEHEARTBEAT = "/registry/v3/microservices/:serviceId/instances/:instanceId/heartbeat" -var INSTANCEWATCHER = "/registry/v3/microservices/:serviceId/watcher" -var INSTANCELISTWATCHER = "/registry/v3/microservices/:serviceId/listwatcher" +var FINDINSTANCE = "/v4/default/registry/instances" +var GETINSTANCE = "/v4/default/registry/microservices/:serviceId/instances" +var GETINSTANCEBYINSTANCEID = "/v4/default/registry/microservices/:serviceId/instances/:instanceId" +var REGISTERINSTANCE = "/v4/default/registry/microservices/:serviceId/instances" +var UNREGISTERINSTANCE = "/v4/default/registry/microservices/:serviceId/instances/:instanceId" +var UPDATEINSTANCEMETADATA = "/v4/default/registry/microservices/:serviceId/instances/:instanceId/properties" +var UPDATEINSTANCESTATUS = "/v4/default/registry/microservices/:serviceId/instances/:instanceId/status" +var INSTANCEHEARTBEAT = "/v4/default/registry/microservices/:serviceId/instances/:instanceId/heartbeat" +var INSTANCEWATCHER = "/v4/default/registry/microservices/:serviceId/watcher" +var INSTANCELISTWATCHER = "/v4/default/registry/microservices/:serviceId/listwatcher" //Governance API's -var GETGOVERNANCESERVICEDETAILS = "/registry/v3/govern/service/:serviceId" -var GETRELATIONGRAPH = "/registry/v3/govern/relation" -var GETALLSERVICEGOVERNANCEINFO = "/registry/v3/govern/services" +var GETGOVERNANCESERVICEDETAILS = "/v4/default/govern/microservices/:serviceId" +var GETRELATIONGRAPH = "/v4/default/govern/relations" +var GETALLSERVICEGOVERNANCEINFO = "/v4/default/govern/microservices" //Rules API's -var ADDRULE = "/registry/v3/microservices/:serviceId/rules" -var GETRULES = "/registry/v3/microservices/:serviceId/rules" -var UPDATERULES = "/registry/v3/microservices/:serviceId/rules/:rule_id" -var DELETERULES = "/registry/v3/microservices/:serviceId/rules/:rule_id" +var ADDRULE = "/v4/default/registry/microservices/:serviceId/rules" +var GETRULES = "/v4/default/registry/microservices/:serviceId/rules" +var UPDATERULES = "/v4/default/registry/microservices/:serviceId/rules/:rule_id" +var DELETERULES = "/v4/default/registry/microservices/:serviceId/rules/:rule_id" //Tag API's -var ADDTAGE = "/registry/v3/microservices/:serviceId/tags" -var UPDATETAG = "/registry/v3/microservices/:serviceId/tags/:key" -var GETTAGS = "/registry/v3/microservices/:serviceId/tags" -var DELETETAG = "/registry/v3/microservices/:serviceId/tags/:key" +var ADDTAGE = "/v4/default/registry/microservices/:serviceId/tags" +var UPDATETAG = "/v4/default/registry/microservices/:serviceId/tags/:key" +var GETTAGS = "/v4/default/registry/microservices/:serviceId/tags" +var DELETETAG = "/v4/default/registry/microservices/:serviceId/tags/:key" // HTTP METHODS var GET = "GET" diff --git a/integration/health_test.go b/integration/health_test.go index fef5665e3..85161dbc3 100644 --- a/integration/health_test.go +++ b/integration/health_test.go @@ -47,7 +47,7 @@ var _ = Describe("Basic Api Test", func() { defer resp.Body.Close() Expect(resp.StatusCode).To(Equal(http.StatusOK)) respbody, _ := ioutil.ReadAll(resp.Body) - Expect(gojson.Json(string(respbody)).Get("apiVersion").Tostring()).To(Equal("3.0.0")) + Expect(gojson.Json(string(respbody)).Get("apiVersion").Tostring()).To(Equal("4.0.0")) }) }) }) diff --git a/pkg/rest/roa.go b/pkg/rest/roa.go index 3e55d4946..e93d21e31 100644 --- a/pkg/rest/roa.go +++ b/pkg/rest/roa.go @@ -72,3 +72,7 @@ func RegisterServent(servant interface{}) { util.Logger().Errorf(nil, " result of 'URLPatterns' function not []*Route type in servant struct `%s`", name) } } + +func RegisterFilter(f Filter) { + serverHandler.filters = append(serverHandler.filters, f) +} \ No newline at end of file diff --git a/pkg/rest/route.go b/pkg/rest/route.go index e3f9a7222..35a3ebaba 100644 --- a/pkg/rest/route.go +++ b/pkg/rest/route.go @@ -47,11 +47,14 @@ type Route struct { // 2. redirect not supported type ROAServerHandler struct { handlers map[string][]*urlPatternHandler + filters []Filter } func NewROAServerHander() *ROAServerHandler { return &ROAServerHandler{ - handlers: make(map[string][]*urlPatternHandler)} + handlers: make(map[string][]*urlPatternHandler), + filters: make([]Filter, 0, 5), + } } func (this *ROAServerHandler) addRoute(route *Route) (err error) { @@ -69,26 +72,36 @@ func (this *ROAServerHandler) addRoute(route *Route) (err error) { } func (this *ROAServerHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + var err error for _, ph := range this.handlers[r.Method] { if params, ok := ph.try(r.URL.Path); ok { if len(params) > 0 { r.URL.RawQuery = url.Values(params).Encode() + "&" + r.URL.RawQuery } + if err = this.doFilter(r); err != nil { + break + } + ph.ServeHTTP(w, r) return } } + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + allowed := make([]string, 0, len(this.handlers)) - for meth, handlers := range this.handlers { - if meth == r.Method { + for method, handlers := range this.handlers { + if method == r.Method { continue } for _, ph := range handlers { if _, ok := ph.try(r.URL.Path); ok { - allowed = append(allowed, meth) + allowed = append(allowed, method) } } } @@ -102,6 +115,15 @@ func (this *ROAServerHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) http.Error(w, "Method Not Allowed", 405) } +func (this *ROAServerHandler) doFilter(r *http.Request) error { + for _, f := range this.filters { + if f.IsMatch(r) { + return f.Do(r) + } + } + return nil +} + func (this *urlPatternHandler) try(path string) (p map[string][]string, _ bool) { var i, j int for i < len(path) { @@ -162,3 +184,8 @@ func isDigit(ch byte) bool { func isAlnum(ch byte) bool { return isAlpha(ch) || isDigit(ch) } + +type Filter interface { + IsMatch(r *http.Request) bool + Do(r *http.Request) error +} \ No newline at end of file diff --git a/pkg/rest/server.go b/pkg/rest/server.go index 7440f04fd..8259c0a4a 100644 --- a/pkg/rest/server.go +++ b/pkg/rest/server.go @@ -37,6 +37,7 @@ type ServerConfig struct { Handler http.Handler ReadTimeout time.Duration ReadHeaderTimeout time.Duration + IdleTimeout time.Duration WriteTimeout time.Duration KeepAliveTimeout time.Duration GraceTimeout time.Duration @@ -48,8 +49,9 @@ func DefaultServerConfig() *ServerConfig { return &ServerConfig{ ReadTimeout: 60 * time.Second, ReadHeaderTimeout: 60 * time.Second, + IdleTimeout: 60 * time.Second, WriteTimeout: 60 * time.Second, - KeepAliveTimeout: 3 * time.Minute, + KeepAliveTimeout: 1 * time.Minute, GraceTimeout: 3 * time.Second, MaxHeaderBytes: 16384, } @@ -66,6 +68,7 @@ func NewServer(srvCfg *ServerConfig) *Server { TLSConfig: srvCfg.TLSConfig, ReadTimeout: srvCfg.ReadTimeout, ReadHeaderTimeout: srvCfg.ReadHeaderTimeout, + IdleTimeout: srvCfg.IdleTimeout, WriteTimeout: srvCfg.WriteTimeout, MaxHeaderBytes: srvCfg.MaxHeaderBytes, }, @@ -243,7 +246,9 @@ func (srv *Server) gracefulStop(d time.Duration) { if srv.CloseOne() { n++ + continue } + break } if n != 0 { diff --git a/server/bootstrap/bootstrap.go b/server/bootstrap/bootstrap.go index 5a17e58ad..df2598540 100644 --- a/server/bootstrap/bootstrap.go +++ b/server/bootstrap/bootstrap.go @@ -25,7 +25,6 @@ import ( "github.com/ServiceComb/service-center/server/interceptor" "github.com/ServiceComb/service-center/server/interceptor/access" "github.com/ServiceComb/service-center/server/interceptor/cors" - "github.com/ServiceComb/service-center/server/interceptor/domain" "github.com/ServiceComb/service-center/server/interceptor/maxbody" "github.com/ServiceComb/service-center/server/interceptor/ratelimiter" ) @@ -36,7 +35,6 @@ func init() { interceptor.InterceptFunc(interceptor.ACCESS_PHASE, ratelimiter.Intercept) interceptor.InterceptFunc(interceptor.ACCESS_PHASE, access.Intercept) interceptor.InterceptFunc(interceptor.ACCESS_PHASE, cors.Intercept) - interceptor.InterceptFunc(interceptor.ACCESS_PHASE, domain.Intercept) interceptor.InterceptFunc(interceptor.CONTENT_PHASE, maxbody.Intercept) diff --git a/server/core/swagger/services.yaml b/server/core/swagger/services.yaml index 8073a97e5..92d3ed253 100644 --- a/server/core/swagger/services.yaml +++ b/server/core/swagger/services.yaml @@ -12,10 +12,19 @@ schemes: produces: - application/json paths: - /version: + /v4/{project}/registry/version: get: description: | 查询服务中心版本信息。 + parameters: + - name: x-domain-name + in: header + type: string + default: default + - name: project + in: path + required: true + type: string tags: - base responses: @@ -31,10 +40,19 @@ paths: description: 内部错误 schema: type: string - /health: + /v4/{project}/registry/health: get: description: | 查询服务中心集群信息。 + parameters: + - name: x-domain-name + in: header + type: string + default: default + - name: project + in: path + required: true + type: string tags: - base responses: @@ -50,7 +68,7 @@ paths: description: 内部错误 schema: type: string - /registry/v3/microservices/{serviceId}: + /v4/{project}/registry/microservices/{serviceId}: get: description: | 根据serviceId查询微服务定义信息。 @@ -58,9 +76,12 @@ paths: parameters: - name: x-domain-name in: header - required: true type: string default: default + - name: project + in: path + required: true + type: string - name: serviceId in: path description: 微服务唯一标识。 @@ -118,7 +139,7 @@ paths: description: 内部错误 schema: type: string - /registry/v3/microservices: + /v4/{project}/registry/microservices: get: description: | 根据条件组合,查询满足所有条件的微服务定义信息。 @@ -128,9 +149,12 @@ paths: parameters: - name: x-domain-name in: header - required: true type: string default: default + - name: project + in: path + required: true + type: string - name: noCache in: query description: 是否强一致性,1 是、0 否。 @@ -204,7 +228,7 @@ paths: description: 内部错误 schema: type: string - /registry/v3/microservices/{serviceId}/properties: + /v4/{project}/registry/microservices/{serviceId}/properties: put: description: | 创建微服务静态信息后可对服务部分字段进行更新,每次更新都需要传入完整的服务静态信息json,也就是说,即便不更新部分的字段也要作为json的属性传过去。 @@ -212,9 +236,12 @@ paths: parameters: - name: x-domain-name in: header - required: true type: string default: default + - name: project + in: path + required: true + type: string - name: serviceId in: path description: 微服务唯一标识。 @@ -239,7 +266,7 @@ paths: description: 内部错误 schema: type: string - /registry/v3/microservices/{serviceId}/tags: + /v4/{project}/registry/microservices/{serviceId}/tags: post: description: | 为serviceId的微服务创建tag。 @@ -247,9 +274,12 @@ paths: parameters: - name: x-domain-name in: header - required: true type: string default: default + - name: project + in: path + required: true + type: string - name: serviceId in: path description: 微服务唯一标识。 @@ -311,7 +341,7 @@ paths: description: 内部错误 schema: type: string - /registry/v3/microservices/{serviceId}/tags/{key}: + /v4/{project}/registry/microservices/{serviceId}/tags/{key}: put: description: | 为serviceId的微服务更新key对应的value值 @@ -319,9 +349,12 @@ paths: parameters: - name: x-domain-name in: header - required: true type: string default: default + - name: project + in: path + required: true + type: string - name: serviceId in: path description: 微服务唯一标识。 @@ -385,7 +418,7 @@ paths: description: 内部错误 schema: type: string - /registry/v3/microservices/{serviceId}/rules: + /v4/{project}/registry/microservices/{serviceId}/rules: post: description: | 为serviceId的服务新增黑白名单,同一服务,attribute和pattern唯一标识一份黑白名单。 @@ -393,9 +426,12 @@ paths: parameters: - name: x-domain-name in: header - required: true type: string default: default + - name: project + in: path + required: true + type: string - name: serviceId in: path description: 微服务唯一标识。 @@ -459,7 +495,7 @@ paths: description: 内部错误 schema: type: string - /registry/v3/microservices/{serviceId}/rules/{rule_id}: + /v4/{project}/registry/microservices/{serviceId}/rules/{rule_id}: put: description: | 为serviceId的服务更新黑白名单。 @@ -467,9 +503,12 @@ paths: parameters: - name: x-domain-name in: header - required: true type: string default: default + - name: project + in: path + required: true + type: string - name: serviceId in: path description: 微服务唯一标识。 @@ -538,7 +577,7 @@ paths: description: 内部错误 schema: type: string - /registry/v3/microservices/{serviceId}/schemas/{schemaId}: + /v4/{project}/registry/microservices/{serviceId}/schemas/{schemaId}: get: description: | 根据serviceId和schemaId查询微服务的schema信息。 @@ -546,9 +585,12 @@ paths: parameters: - name: x-domain-name in: header - required: true type: string default: default + - name: project + in: path + required: true + type: string - name: serviceId in: path description: 微服务唯一标识。 @@ -654,7 +696,7 @@ paths: description: 内部错误 schema: type: string - /registry/v3/microservices/{serviceId}/schemas: + /v4/{project}/registry/microservices/{serviceId}/schemas: post: description: | 批量上传schemas。 @@ -662,9 +704,12 @@ paths: parameters: - name: x-domain-name in: header - required: true type: string default: default + - name: project + in: path + required: true + type: string - name: serviceId in: path description: 唯一标识。 @@ -690,7 +735,7 @@ paths: description: 内部错误 schema: type: string - /registry/v3/dependencies: + /v4/{project}/registry/dependencies: put: description: | 创建服务间的依赖关系,consumer的version必须是确定的版本,serviceName不能为*,consumer必须是已存在的服务,provider可以是还未创建的。 @@ -698,9 +743,12 @@ paths: parameters: - name: x-domain-name in: header - required: true type: string default: default + - name: project + in: path + required: true + type: string - name: type in: body description: 创建服务间的依赖关系的请求体。 @@ -722,7 +770,7 @@ paths: description: 内部错误 schema: type: string - /registry/v3/microservices/{consumerId}/providers: + /v4/{project}/registry/microservices/{consumerId}/providers: get: description: | 根据consumerId获取该服务的所有providers @@ -730,9 +778,12 @@ paths: parameters: - name: x-domain-name in: header - required: true type: string default: default + - name: project + in: path + required: true + type: string - name: consumerId in: path description: 消费者的服务id。 @@ -758,7 +809,7 @@ paths: description: 内部错误 schema: type: string - /registry/v3/microservices/{providerId}/consumers: + /v4/{project}/registry/microservices/{providerId}/consumers: get: description: | 根据providerId获取该服务的所有consumers @@ -766,9 +817,12 @@ paths: parameters: - name: x-domain-name in: header - required: true type: string default: default + - name: project + in: path + required: true + type: string - name: providerId in: path description: 提供者的服务id。 @@ -794,7 +848,7 @@ paths: description: 内部错误 schema: type: string - /registry/v3/existence: + /v4/{project}/registry/existence: get: description: | 可通过指定条件,查询微服务serviceId或schema的唯一标识信息。 @@ -802,9 +856,12 @@ paths: parameters: - name: x-domain-name in: header - required: true type: string default: default + - name: project + in: path + required: true + type: string - name: type in: query description: 资源类型 microservice微服务 schema微服务访问契约。 @@ -856,7 +913,7 @@ paths: description: 内部错误 schema: type: string - /registry/v3/microservices/{serviceId}/instances: + /v4/{project}/registry/microservices/{serviceId}/instances: post: description: | 创建微服务后就可以注册该微服务的实例了。 注册微服务实例时,需提供该微服务实例相关的信息。 @@ -865,9 +922,12 @@ paths: parameters: - name: x-domain-name in: header - required: true type: string default: default + - name: project + in: path + required: true + type: string - name: serviceId in: path description: 微服务唯一标识。 @@ -942,7 +1002,7 @@ paths: description: 内部错误 schema: type: string - /registry/v3/microservices/{serviceId}/instances/{instanceId}: + /v4/{project}/registry/microservices/{serviceId}/instances/{instanceId}: delete: description: | 实例注册后可以根据 instance_id 进行实例注销。 @@ -950,9 +1010,12 @@ paths: parameters: - name: x-domain-name in: header - required: true type: string default: default + - name: project + in: path + required: true + type: string - name: serviceId in: path description: 微服务唯一标识。 @@ -1029,7 +1092,7 @@ paths: description: 内部错误 schema: type: string - /registry/v3/microservices/{serviceId}/instances/{instanceId}/properties: + /v4/{project}/registry/microservices/{serviceId}/instances/{instanceId}/properties: put: description: | 实例注册后可以根据 instance_id 进行添加/更新一个微服务实例扩展信息。 @@ -1037,9 +1100,12 @@ paths: parameters: - name: x-domain-name in: header - required: true type: string default: default + - name: project + in: path + required: true + type: string - name: serviceId in: path description: 微服务唯一标识。 @@ -1069,7 +1135,7 @@ paths: description: 内部错误 schema: type: string - /registry/v3/microservices/{serviceId}/instances/{instanceId}/status: + /v4/{project}/registry/microservices/{serviceId}/instances/{instanceId}/status: put: description: | 实例注册后可以根据 instance_id 进行更新一个微服务实例状态。 @@ -1077,9 +1143,12 @@ paths: parameters: - name: x-domain-name in: header - required: true type: string default: default + - name: project + in: path + required: true + type: string - name: serviceId in: path description: 微服务唯一标识。 @@ -1108,7 +1177,7 @@ paths: description: 内部错误 schema: type: string - /registry/v3/microservices/{serviceId}/instances/{instanceId}/heartbeat: + /v4/{project}/registry/microservices/{serviceId}/instances/{instanceId}/heartbeat: put: description: | 服务提供端需要向服务中心发送心跳信息,以保证服务中心知道服务实例是否健康。 @@ -1116,9 +1185,12 @@ paths: parameters: - name: x-domain-name in: header - required: true type: string default: default + - name: project + in: path + required: true + type: string - name: serviceId in: path description: 微服务唯一标识。 @@ -1142,7 +1214,7 @@ paths: description: 内部错误 schema: type: string - /registry/v3/heartbeats: + /v4/{project}/registry/heartbeats: put: description: | 服务提供端需要向服务中心发送心跳信息,以保证服务中心知道服务实例是否健康。该接口为批量接口 @@ -1150,9 +1222,12 @@ paths: parameters: - name: x-domain-name in: header - required: true type: string default: default + - name: project + in: path + required: true + type: string - name: Instances in: body description: 批量上报心跳的实例的标识。 @@ -1172,7 +1247,7 @@ paths: description: 内部错误 schema: type: string - /registry/v3/instances: + /v4/{project}/registry/instances: get: description: | 实例注册后可以根据微服务版本规则或字段条件 发现该微服务的实例。 @@ -1180,9 +1255,12 @@ paths: parameters: - name: x-domain-name in: header - required: true type: string default: default + - name: project + in: path + required: true + type: string - name: X-ConsumerId in: header description: 微服务消费者的微服务唯一标识。 @@ -1231,12 +1309,20 @@ paths: description: 内部错误 schema: type: string - /registry/v3/microservices/{serviceId}/watcher: + /v4/{project}/registry/microservices/{serviceId}/watcher: get: description: | 当服务在心跳消失,注册,注销,状态更新时, 将这些变化主动推送到客户端。 operationId: watch parameters: + - name: x-domain-name + in: header + type: string + default: default + - name: project + in: path + required: true + type: string - name: serviceId in: path description: 微服务消费者的微服务唯一标识。 @@ -1257,11 +1343,19 @@ paths: description: 内部错误 schema: type: string - /registry/v3/microservices/{serviceId}/listwatcher: + /v4/{project}/registry/microservices/{serviceId}/listwatcher: get: description: | watch成功后返回完整的微服务提供者的实例信息,且服务在心跳消失,注册,注销,状态更新时,将这些变化主动推送到客户端。 parameters: + - name: x-domain-name + in: header + type: string + default: default + - name: project + in: path + required: true + type: string - name: serviceId in: path description: 微服务消费者的微服务唯一标识。 diff --git a/server/rest/controller/v3/governservice_controller.go b/server/rest/controller/v4/governservice_controller.go similarity index 93% rename from server/rest/controller/v3/governservice_controller.go rename to server/rest/controller/v4/governservice_controller.go index 6950ed587..57cb0d88e 100644 --- a/server/rest/controller/v3/governservice_controller.go +++ b/server/rest/controller/v4/governservice_controller.go @@ -11,7 +11,7 @@ //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 v3 +package v4 import ( "net/http" @@ -25,9 +25,23 @@ import ( "strings" ) +// GovernService 治理相关接口服务 +type GovernService struct { + // +} + +// URLPatterns 路由 +func (governService *GovernService) URLPatterns() []rest.Route { + return []rest.Route{ + {rest.HTTP_METHOD_GET, "/v4/:domain/govern/microservices/:serviceId", governService.GetServiceDetail}, + {rest.HTTP_METHOD_GET, "/v4/:domain/govern/relations", governService.GetGraph}, + {rest.HTTP_METHOD_GET, "/v4/:domain/govern/microservices", governService.GetAllServicesInfo}, + } +} + //Node 节点信息 type Node struct { - Id string `json:"id";` + Id string `json:"id"` Name string `json:"name"` AppID string `json:"appId"` Version string `json:"version"` @@ -114,20 +128,6 @@ func (governService *GovernService) GetGraph(w http.ResponseWriter, r *http.Requ controller.WriteJsonObject(http.StatusOK, graph, w) } -// GovernService 治理相关接口服务 -type GovernService struct { - // -} - -// URLPatterns 路由 -func (governService *GovernService) URLPatterns() []rest.Route { - return []rest.Route{ - {rest.HTTP_METHOD_GET, "/registry/v3/govern/service/:serviceId", governService.GetServiceDetail}, - {rest.HTTP_METHOD_GET, "/registry/v3/govern/relation", governService.GetGraph}, - {rest.HTTP_METHOD_GET, "/registry/v3/govern/services", governService.GetAllServicesInfo}, - } -} - // GetServiceDetail 查询服务详细信息 func (governService *GovernService) GetServiceDetail(w http.ResponseWriter, r *http.Request) { serviceID := r.URL.Query().Get(":serviceId") diff --git a/server/rest/controller/v3/instance_controller.go b/server/rest/controller/v4/instance_controller.go similarity index 89% rename from server/rest/controller/v3/instance_controller.go rename to server/rest/controller/v4/instance_controller.go index d78252fdc..bf7f03e67 100644 --- a/server/rest/controller/v3/instance_controller.go +++ b/server/rest/controller/v4/instance_controller.go @@ -11,7 +11,7 @@ //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 v3 +package v4 import ( "encoding/json" @@ -31,15 +31,15 @@ type MicroServiceInstanceService struct { func (this *MicroServiceInstanceService) URLPatterns() []rest.Route { return []rest.Route{ - {rest.HTTP_METHOD_GET, "/registry/v3/instances", this.FindInstances}, - {rest.HTTP_METHOD_GET, "/registry/v3/microservices/:serviceId/instances", this.GetInstances}, - {rest.HTTP_METHOD_GET, "/registry/v3/microservices/:serviceId/instances/:instanceId", this.GetOneInstance}, - {rest.HTTP_METHOD_POST, "/registry/v3/microservices/:serviceId/instances", this.RegisterInstance}, - {rest.HTTP_METHOD_DELETE, "/registry/v3/microservices/:serviceId/instances/:instanceId", this.UnregisterInstance}, - {rest.HTTP_METHOD_PUT, "/registry/v3/microservices/:serviceId/instances/:instanceId/properties", this.UpdateMetadata}, - {rest.HTTP_METHOD_PUT, "/registry/v3/microservices/:serviceId/instances/:instanceId/status", this.UpdateStatus}, - {rest.HTTP_METHOD_PUT, "/registry/v3/microservices/:serviceId/instances/:instanceId/heartbeat", this.Heartbeat}, - {rest.HTTP_METHOD_PUT, "/registry/v3/heartbeats", this.HeartbeatSet}, + {rest.HTTP_METHOD_GET, "/v4/:domain/registry/instances", this.FindInstances}, + {rest.HTTP_METHOD_GET, "/v4/:domain/registry/microservices/:serviceId/instances", this.GetInstances}, + {rest.HTTP_METHOD_GET, "/v4/:domain/registry/microservices/:serviceId/instances/:instanceId", this.GetOneInstance}, + {rest.HTTP_METHOD_POST, "/v4/:domain/registry/microservices/:serviceId/instances", this.RegisterInstance}, + {rest.HTTP_METHOD_DELETE, "/v4/:domain/registry/microservices/:serviceId/instances/:instanceId", this.UnregisterInstance}, + {rest.HTTP_METHOD_PUT, "/v4/:domain/registry/microservices/:serviceId/instances/:instanceId/properties", this.UpdateMetadata}, + {rest.HTTP_METHOD_PUT, "/v4/:domain/registry/microservices/:serviceId/instances/:instanceId/status", this.UpdateStatus}, + {rest.HTTP_METHOD_PUT, "/v4/:domain/registry/microservices/:serviceId/instances/:instanceId/heartbeat", this.Heartbeat}, + {rest.HTTP_METHOD_PUT, "/v4/:domain/registry/heartbeats", this.HeartbeatSet}, } } func (this *MicroServiceInstanceService) RegisterInstance(w http.ResponseWriter, r *http.Request) { diff --git a/server/rest/controller/v3/instance_watcher.go b/server/rest/controller/v4/instance_watcher.go similarity index 90% rename from server/rest/controller/v3/instance_watcher.go rename to server/rest/controller/v4/instance_watcher.go index d61121390..e3ec4c036 100644 --- a/server/rest/controller/v3/instance_watcher.go +++ b/server/rest/controller/v4/instance_watcher.go @@ -11,7 +11,7 @@ //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 v3 +package v4 import ( "github.com/ServiceComb/service-center/pkg/rest" @@ -29,8 +29,8 @@ type WatchService struct { func (this *WatchService) URLPatterns() []rest.Route { return []rest.Route{ - {rest.HTTP_METHOD_GET, "/registry/v3/microservices/:serviceId/watcher", this.Watch}, - {rest.HTTP_METHOD_GET, "/registry/v3/microservices/:serviceId/listwatcher", this.ListAndWatch}, + {rest.HTTP_METHOD_GET, "/v4/:domain/registry/microservices/:serviceId/watcher", this.Watch}, + {rest.HTTP_METHOD_GET, "/v4/:domain/registry/microservices/:serviceId/listwatcher", this.ListAndWatch}, } } diff --git a/server/rest/controller/v3/main_controller.go b/server/rest/controller/v4/main_controller.go similarity index 91% rename from server/rest/controller/v3/main_controller.go rename to server/rest/controller/v4/main_controller.go index c16bfa6cb..402735c08 100644 --- a/server/rest/controller/v3/main_controller.go +++ b/server/rest/controller/v4/main_controller.go @@ -11,7 +11,7 @@ //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 v3 +package v4 import ( "encoding/json" @@ -24,7 +24,7 @@ import ( "net/http" ) -const API_VERSION = "3.0.0" +const API_VERSION = "4.0.0" var RunMode string @@ -44,8 +44,8 @@ func init() { func (this *MainService) URLPatterns() []rest.Route { return []rest.Route{ - {rest.HTTP_METHOD_GET, "/version", this.GetVersion}, - {rest.HTTP_METHOD_GET, "/health", this.ClusterHealth}, + {rest.HTTP_METHOD_GET, "/v4/:domain/registry/version", this.GetVersion}, + {rest.HTTP_METHOD_GET, "/v4/:domain/registry/health", this.ClusterHealth}, } } diff --git a/server/rest/controller/v3/microservice_controller.go b/server/rest/controller/v4/microservice_controller.go similarity index 89% rename from server/rest/controller/v3/microservice_controller.go rename to server/rest/controller/v4/microservice_controller.go index 9aab52b43..5e1da7a62 100644 --- a/server/rest/controller/v3/microservice_controller.go +++ b/server/rest/controller/v4/microservice_controller.go @@ -11,7 +11,7 @@ //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 v3 +package v4 import ( "encoding/json" @@ -31,21 +31,21 @@ type MicroServiceService struct { func (this *MicroServiceService) URLPatterns() []rest.Route { return []rest.Route{ - {rest.HTTP_METHOD_GET, "/registry/v3/existence", this.GetExistence}, - {rest.HTTP_METHOD_GET, "/registry/v3/microservices", this.GetServices}, - {rest.HTTP_METHOD_GET, "/registry/v3/microservices/:serviceId", this.GetServiceOne}, - {rest.HTTP_METHOD_POST, "/registry/v3/microservices", this.Register}, - {rest.HTTP_METHOD_PUT, "/registry/v3/microservices/:serviceId/properties", this.Update}, - {rest.HTTP_METHOD_DELETE, "/registry/v3/microservices/:serviceId", this.Unregister}, - {rest.HTTP_METHOD_GET, "/registry/v3/microservices/:serviceId/schemas/:schemaId", this.GetSchemas}, - {rest.HTTP_METHOD_PUT, "/registry/v3/microservices/:serviceId/schemas/:schemaId", this.ModifySchema}, - {rest.HTTP_METHOD_DELETE, "/registry/v3/microservices/:serviceId/schemas/:schemaId", this.DeleteSchemas}, - {rest.HTTP_METHOD_POST, "/registry/v3/microservices/:serviceId/schemas", this.ModifySchemas}, + {rest.HTTP_METHOD_GET, "/v4/:domain/registry/existence", this.GetExistence}, + {rest.HTTP_METHOD_GET, "/v4/:domain/registry/microservices", this.GetServices}, + {rest.HTTP_METHOD_GET, "/v4/:domain/registry/microservices/:serviceId", this.GetServiceOne}, + {rest.HTTP_METHOD_POST, "/v4/:domain/registry/microservices", this.Register}, + {rest.HTTP_METHOD_PUT, "/v4/:domain/registry/microservices/:serviceId/properties", this.Update}, + {rest.HTTP_METHOD_DELETE, "/v4/:domain/registry/microservices/:serviceId", this.Unregister}, + {rest.HTTP_METHOD_GET, "/v4/:domain/registry/microservices/:serviceId/schemas/:schemaId", this.GetSchemas}, + {rest.HTTP_METHOD_PUT, "/v4/:domain/registry/microservices/:serviceId/schemas/:schemaId", this.ModifySchema}, + {rest.HTTP_METHOD_DELETE, "/v4/:domain/registry/microservices/:serviceId/schemas/:schemaId", this.DeleteSchemas}, + {rest.HTTP_METHOD_POST, "/v4/:domain/registry/microservices/:serviceId/schemas", this.ModifySchemas}, - {rest.HTTP_METHOD_PUT, "/registry/v3/dependencies", this.CreateDependenciesForMicroServices}, - {rest.HTTP_METHOD_GET, "/registry/v3/microservices/:consumerId/providers", this.GetConProDependencies}, - {rest.HTTP_METHOD_GET, "/registry/v3/microservices/:providerId/consumers", this.GetProConDependencies}, - {rest.HTTP_METHOD_DELETE, "/registry/v3/microservices", this.UnregisterServices}, + {rest.HTTP_METHOD_PUT, "/v4/:domain/registry/dependencies", this.CreateDependenciesForMicroServices}, + {rest.HTTP_METHOD_GET, "/v4/:domain/registry/microservices/:consumerId/providers", this.GetConProDependencies}, + {rest.HTTP_METHOD_GET, "/v4/:domain/registry/microservices/:providerId/consumers", this.GetProConDependencies}, + {rest.HTTP_METHOD_DELETE, "/v4/:domain/registry/microservices", this.UnregisterServices}, } } diff --git a/server/rest/controller/v3/query_rule_controller.go b/server/rest/controller/v4/query_rule_controller.go similarity index 89% rename from server/rest/controller/v3/query_rule_controller.go rename to server/rest/controller/v4/query_rule_controller.go index 7ca4e5e10..a51e7ab1b 100644 --- a/server/rest/controller/v3/query_rule_controller.go +++ b/server/rest/controller/v4/query_rule_controller.go @@ -11,7 +11,7 @@ //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 v3 +package v4 import ( "encoding/json" @@ -32,10 +32,10 @@ type RuleService struct { func (this *RuleService) URLPatterns() []rest.Route { return []rest.Route{ - {rest.HTTP_METHOD_POST, "/registry/v3/microservices/:serviceId/rules", this.AddRule}, - {rest.HTTP_METHOD_GET, "/registry/v3/microservices/:serviceId/rules", this.GetRules}, - {rest.HTTP_METHOD_PUT, "/registry/v3/microservices/:serviceId/rules/:rule_id", this.UpdateRule}, - {rest.HTTP_METHOD_DELETE, "/registry/v3/microservices/:serviceId/rules/:rule_id", this.DeleteRule}, + {rest.HTTP_METHOD_POST, "/v4/:domain/registry/microservices/:serviceId/rules", this.AddRule}, + {rest.HTTP_METHOD_GET, "/v4/:domain/registry/microservices/:serviceId/rules", this.GetRules}, + {rest.HTTP_METHOD_PUT, "/v4/:domain/registry/microservices/:serviceId/rules/:rule_id", this.UpdateRule}, + {rest.HTTP_METHOD_DELETE, "/v4/:domain/registry/microservices/:serviceId/rules/:rule_id", this.DeleteRule}, } } func (this *RuleService) AddRule(w http.ResponseWriter, r *http.Request) { diff --git a/server/rest/controller/v3/tag_controller.go b/server/rest/controller/v4/tag_controller.go similarity index 88% rename from server/rest/controller/v3/tag_controller.go rename to server/rest/controller/v4/tag_controller.go index 3c7ada9cf..a08668c34 100644 --- a/server/rest/controller/v3/tag_controller.go +++ b/server/rest/controller/v4/tag_controller.go @@ -11,7 +11,7 @@ //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 v3 +package v4 import ( "encoding/json" @@ -32,10 +32,10 @@ type TagService struct { func (this *TagService) URLPatterns() []rest.Route { return []rest.Route{ - {rest.HTTP_METHOD_POST, "/registry/v3/microservices/:serviceId/tags", this.AddTags}, - {rest.HTTP_METHOD_PUT, "/registry/v3/microservices/:serviceId/tags/:key", this.UpdateTag}, - {rest.HTTP_METHOD_GET, "/registry/v3/microservices/:serviceId/tags", this.GetTags}, - {rest.HTTP_METHOD_DELETE, "/registry/v3/microservices/:serviceId/tags/:key", this.DeleteTags}, + {rest.HTTP_METHOD_POST, "/v4/:domain/registry/microservices/:serviceId/tags", this.AddTags}, + {rest.HTTP_METHOD_PUT, "/v4/:domain/registry/microservices/:serviceId/tags/:key", this.UpdateTag}, + {rest.HTTP_METHOD_GET, "/v4/:domain/registry/microservices/:serviceId/tags", this.GetTags}, + {rest.HTTP_METHOD_DELETE, "/v4/:domain/registry/microservices/:serviceId/tags/:key", this.DeleteTags}, } } diff --git a/server/rest/controller/v3/v3.go b/server/rest/controller/v4/v4.go similarity index 53% rename from server/rest/controller/v3/v3.go rename to server/rest/controller/v4/v4.go index 28ceff24d..4b0cb5bb4 100644 --- a/server/rest/controller/v3/v3.go +++ b/server/rest/controller/v4/v4.go @@ -11,12 +11,16 @@ //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 v3 +package v4 import ( roa "github.com/ServiceComb/service-center/pkg/rest" "github.com/ServiceComb/service-center/pkg/util" "net/http" + "strings" + "errors" + "github.com/ServiceComb/service-center/server/core" + "net/url" ) var router http.Handler @@ -28,6 +32,7 @@ func init() { } func initRouter() { + roa.RegisterFilter(&v4Context{}) roa.RegisterServent(&MainService{}) roa.RegisterServent(&MicroServiceService{}) roa.RegisterServent(&TagService{}) @@ -41,3 +46,44 @@ func initRouter() { func GetRouter() http.Handler { return router } + +type v4Context struct { + +} + +func (v *v4Context) IsMatch(r *http.Request) bool { + return strings.Index(r.RequestURI, "/v4/") == 0 +} + +func (v *v4Context) Do(r *http.Request) error { + path, err := url.PathUnescape(r.RequestURI) + if err != nil { + util.Logger().Errorf(err, "Invalid Request URI %s", r.RequestURI) + return err + } + + start := len("/v4/") + end := start + strings.Index(path[start:], "/") + + tenant := strings.TrimSpace(path[start:end]) + if len(tenant) == 0 { + err := errors.New("Header does not contain domain.") + util.Logger().Errorf(err, "Invalid Request URI %s", r.RequestURI) + return err + } + + project := r.Header.Get("X-Domain-Name") + if len(project) == 0 { + project = r.Header.Get("X-Tenant-Name") + if len(project) == 0 { + project = core.REGISTRY_PROJECT + } + } + + ctx := r.Context() + ctx = util.NewContext(ctx, "tenant", tenant) + ctx = util.NewContext(ctx, "project", project) + request := r.WithContext(ctx) + *r = *request + return nil +} diff --git a/server/rest/handler.go b/server/rest/handler.go index 22d51b8de..6439212ac 100644 --- a/server/rest/handler.go +++ b/server/rest/handler.go @@ -2,21 +2,21 @@ package rest import ( "github.com/ServiceComb/service-center/server/interceptor" - "github.com/ServiceComb/service-center/server/rest/controller/v3" - "github.com/prometheus/client_golang/prometheus/promhttp" "net/http" + "github.com/ServiceComb/service-center/server/rest/controller/v4" + "github.com/prometheus/client_golang/prometheus" ) var router http.Handler func init() { - router = v3.GetRouter() + router = v4.GetRouter() // api http.Handle("/", DefaultServerHandler()) // prometheus metrics - http.Handle("/metrics", promhttp.Handler()) + http.Handle("/metrics", prometheus.Handler()) } type ServerHandler struct { diff --git a/server/rest/server.go b/server/rest/server.go index 67ceec2e8..db7ae52a2 100644 --- a/server/rest/server.go +++ b/server/rest/server.go @@ -31,6 +31,7 @@ func LoadConfig() (srvCfg *rest.ServerConfig, err error) { srvCfg = rest.DefaultServerConfig() readHeaderTimeout, _ := time.ParseDuration(beego.AppConfig.DefaultString("read_header_timeout", "60s")) readTimeout, _ := time.ParseDuration(beego.AppConfig.DefaultString("read_timeout", "60s")) + idleTimeout, _ := time.ParseDuration(beego.AppConfig.DefaultString("idle_timeout", "60s")) writeTimeout, _ := time.ParseDuration(beego.AppConfig.DefaultString("write_timeout", "60s")) maxHeaderBytes := beego.AppConfig.DefaultInt("max_header_bytes", 16384) var tlsConfig *tls.Config @@ -43,6 +44,7 @@ func LoadConfig() (srvCfg *rest.ServerConfig, err error) { } srvCfg.ReadHeaderTimeout = readHeaderTimeout srvCfg.ReadTimeout = readTimeout + srvCfg.IdleTimeout = idleTimeout srvCfg.WriteTimeout = writeTimeout srvCfg.MaxHeaderBytes = maxHeaderBytes srvCfg.TLSConfig = tlsConfig diff --git a/server/server.go b/server/server.go index f5653ecb7..116ce404a 100644 --- a/server/server.go +++ b/server/server.go @@ -27,8 +27,10 @@ import ( "github.com/ServiceComb/service-center/version" "github.com/astaxie/beego" "os" + "os/signal" "runtime" "strings" + "syscall" "time" ) @@ -66,6 +68,28 @@ func (s *ServiceCenterServer) initialize() { cores := runtime.NumCPU() runtime.GOMAXPROCS(cores) util.Logger().Infof("service center have running simultaneously with %d CPU cores", cores) + + go handleSignals() +} + +func handleSignals() { + var sig os.Signal + sigCh := make(chan os.Signal) + signal.Notify(sigCh, + syscall.SIGINT, + syscall.SIGKILL, + syscall.SIGTERM, + ) + wait := 5 * time.Second + for { + sig = <-sigCh + switch sig { + case syscall.SIGINT, syscall.SIGKILL, syscall.SIGTERM: + <-time.After(wait) + util.Logger().Warnf(nil, "Clean up resources timed out(%s), force shutdown.", wait) + os.Exit(1) + } + } } func (s *ServiceCenterServer) waitForQuit() { diff --git a/server/service/schema.go b/server/service/schema.go index db18e3681..826aa08e6 100644 --- a/server/service/schema.go +++ b/server/service/schema.go @@ -23,7 +23,7 @@ import ( serviceUtil "github.com/ServiceComb/service-center/server/service/util" "golang.org/x/net/context" "errors" - "github.com/ServiceComb/service-center/server/rest/controller/v3" + "github.com/ServiceComb/service-center/server/rest/controller/v4" "strings" ) @@ -202,7 +202,7 @@ func modifySchemas(ctx context.Context, tenant string, service *pb.MicroService, } pluginOps := []registry.PluginOp{} - switch v3.RunMode { + switch v4.RunMode { case "dev": needDeleteSchemaList := make([]*pb.Schema, 0, len(schemasInDataBase)) for _, schemasInner := range schemasInDataBase { diff --git a/server/service/schema_test.go b/server/service/schema_test.go index 2ab85e1bf..0ce1b8dcf 100644 --- a/server/service/schema_test.go +++ b/server/service/schema_test.go @@ -18,7 +18,7 @@ import ( pb "github.com/ServiceComb/service-center/server/core/proto" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" - "github.com/ServiceComb/service-center/server/rest/controller/v3" + "github.com/ServiceComb/service-center/server/rest/controller/v4" ) const ( @@ -406,7 +406,7 @@ var _ = Describe("ServiceController", func() { }) It("create schemas, prod mode", func() { - v3.RunMode = "prod" + v4.RunMode = "prod" respModifySchema, err := serviceResource.ModifySchema(getContext(), &pb.ModifySchemaRequest{ ServiceId: serviceId, SchemaId: "first_schemaId", @@ -457,7 +457,7 @@ var _ = Describe("ServiceController", func() { }) Expect(err).To(BeNil()) Expect(respModifySchemas.GetResponse().Code).To(Equal(pb.Response_SUCCESS)) - v3.RunMode = "dev" + v4.RunMode = "dev" }) It("clean", func() { respDeleteService, err := serviceResource.Delete(getContext(), &pb.DeleteServiceRequest{