Skip to content

Commit

Permalink
Restore support for v3 api. (#140)
Browse files Browse the repository at this point in the history
* Optimize governance api.

* Print the version info when SC startup.

* Restore support for v3 api.

* Fix the CI failure.

* Fix the CI failure.
  • Loading branch information
little-cui committed Oct 26, 2017
1 parent d81050f commit d4585af
Show file tree
Hide file tree
Showing 19 changed files with 618 additions and 87 deletions.
58 changes: 58 additions & 0 deletions pkg/chain/callback.go
@@ -0,0 +1,58 @@
//Copyright 2017 Huawei Technologies Co., Ltd
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//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 chain

import (
"github.com/ServiceComb/service-center/pkg/util"
)

type Result struct {
OK bool
Err error
Args []interface{}
}

type CallbackFunc func(r Result)

type Callback struct {
Func CallbackFunc
}

func (cb *Callback) Invoke(r Result) {
go func() {
defer util.RecoverAndReport()
cb.Func(r)
}()
}

func (cb *Callback) Fail(err error, args ...interface{}) {
cb.Invoke(Result{
OK: false,
Err: err,
Args: args,
})
}

func (cb *Callback) Success(args ...interface{}) {
cb.Invoke(Result{
OK: true,
Args: args,
})
}

func NewCallback(f CallbackFunc) Callback {
return Callback{
Func: f,
}
}
57 changes: 57 additions & 0 deletions pkg/chain/chain.go
@@ -0,0 +1,57 @@
//Copyright 2017 Huawei Technologies Co., Ltd
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//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 chain

import (
"fmt"
"github.com/ServiceComb/service-center/pkg/util"
"sync"
)

var handlersMap map[string][]Handler

type Chain struct {
name string
handlers []Handler
currentIndex int
mux sync.Mutex
}

func (c *Chain) Init(chainName string, hs []Handler) {
c.name = chainName
c.currentIndex = -1
if len(hs) > 0 {
c.handlers = make([]Handler, 0, len(hs))
copy(c.handlers, hs)
}
}

func (c *Chain) Name() string {
return c.name
}

func (c *Chain) doNext(i *Invocation) {
defer util.RecoverAndReport()

if c.currentIndex >= len(c.handlers) {
i.Fail(fmt.Errorf("Over end of chain '%s'", c.name))
return
}
c.currentIndex += 1
c.handlers[c.currentIndex].Handle(i)
}

func (c *Chain) next(i *Invocation) {
go c.doNext(i)
}
18 changes: 18 additions & 0 deletions pkg/chain/handler.go
@@ -0,0 +1,18 @@
//Copyright 2017 Huawei Technologies Co., Ltd
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//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 chain

type Handler interface {
Handle(i *Invocation)
}
60 changes: 60 additions & 0 deletions pkg/chain/invocation.go
@@ -0,0 +1,60 @@
//Copyright 2017 Huawei Technologies Co., Ltd
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//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 chain

import (
"errors"
)

const DEFAULT_MAP_SIZE = 10

type Invocation struct {
Callback
handlerContext map[string]interface{}
context map[string]interface{}
chain *Chain
}

func (i *Invocation) Init(ch *Chain) {
i.handlerContext = make(map[string]interface{}, DEFAULT_MAP_SIZE)
i.context = make(map[string]interface{}, DEFAULT_MAP_SIZE)
i.chain = ch
}

func (i *Invocation) HandlerContext() map[string]interface{} {
return i.handlerContext
}

func (i *Invocation) Context() map[string]interface{} {
return i.context
}

func (i *Invocation) WithHandlerContext(key string, val interface{}) *Invocation {
i.handlerContext[key] = val
return i
}

func (i *Invocation) WithContext(key string, val interface{}) *Invocation {
i.context[key] = val
return i
}

func (i *Invocation) Next(f CallbackFunc) {
i.Func = f
if i.chain == nil {
i.Fail(errors.New("Can not find any chain for this invocation"))
return
}
i.chain.next(i)
}
10 changes: 9 additions & 1 deletion server/bootstrap/bootstrap.go
Expand Up @@ -14,11 +14,19 @@
package bootstrap

import _ "github.com/ServiceComb/service-center/server/core" // initialize
// cipher
import _ "github.com/ServiceComb/service-center/server/plugin/infra/security/plain"

// registry
import _ "github.com/ServiceComb/service-center/server/core/registry/etcd"
import _ "github.com/ServiceComb/service-center/server/core/registry/embededetcd"

// v3
import _ "github.com/ServiceComb/service-center/server/rest/controller/v3"

// quota
import _ "github.com/ServiceComb/service-center/server/plugin/infra/quota/buildin"
import _ "github.com/ServiceComb/service-center/server/plugin/infra/quota/unlimit"
import _ "github.com/ServiceComb/service-center/server/plugin/infra/security/plain"

import (
"github.com/ServiceComb/service-center/pkg/util"
Expand Down
47 changes: 44 additions & 3 deletions server/core/0_init.go
Expand Up @@ -11,32 +11,42 @@ import (
"github.com/ServiceComb/service-center/version"
"github.com/astaxie/beego"
"os"
"os/signal"
"path/filepath"
"runtime"
"syscall"
"time"
)

var printVersion bool
var printVer bool

func init() {
Initialize()
}

func Initialize() {
initCommandLine()

initLogger()

printVersion()

go handleSignals()

tlsutil.LoadServerSSLConfig()
tlsutil.LoadClientSSLConfig()

initLogRotate()

grace.Init()
}

func initCommandLine() {
flag.BoolVar(&printVersion, "v", false, "Print the version and exit.")
flag.BoolVar(&printVer, "v", false, "Print the version and exit.")
flag.CommandLine.Init(os.Args[0], flag.ContinueOnError)
flag.CommandLine.Parse(os.Args[1:])

if printVersion {
if printVer {
fmt.Printf("ServiceCenter version: %s\n", version.Ver().Version)
fmt.Printf("Build tag: %s\n", version.Ver().BuildTag)
fmt.Printf("Go version: %s\n", runtime.Version())
Expand All @@ -45,6 +55,17 @@ func initCommandLine() {
}
}

func printVersion() {
util.Logger().Infof("service center version: %s", version.Ver().Version)
util.Logger().Infof("Build tag: %s", version.Ver().BuildTag)
util.Logger().Infof("Go version: %s", runtime.Version())
util.Logger().Infof("Go OS/Arch: %s/%s", runtime.GOOS, runtime.GOARCH)

cores := runtime.NumCPU()
runtime.GOMAXPROCS(cores)
util.Logger().Infof("service center is running simultaneously with %d CPU cores", cores)
}

func initLogger() {
logFormatText, err := beego.AppConfig.Bool("LogFormatText")
loggerFile := os.ExpandEnv(beego.AppConfig.String("logfile"))
Expand Down Expand Up @@ -91,3 +112,23 @@ func initLogRotate() {
Period: rotatePeriod,
})
}

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)
}
}
}
33 changes: 33 additions & 0 deletions server/rest/controller/v3/governservice_controller.go
@@ -0,0 +1,33 @@
//Copyright 2017 Huawei Technologies Co., Ltd
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//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 v3

import (
"github.com/ServiceComb/service-center/pkg/rest"
"github.com/ServiceComb/service-center/server/rest/controller/v4"
)

// GovernService 治理相关接口服务
type GovernService struct {
v4.GovernService
}

// 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},
}
}
37 changes: 37 additions & 0 deletions server/rest/controller/v3/instance_controller.go
@@ -0,0 +1,37 @@
//Copyright 2017 Huawei Technologies Co., Ltd
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//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 v3

import (
"github.com/ServiceComb/service-center/pkg/rest"
"github.com/ServiceComb/service-center/server/rest/controller/v4"
)

type MicroServiceInstanceService struct {
v4.MicroServiceInstanceService
}

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},
}
}

0 comments on commit d4585af

Please sign in to comment.