Skip to content

Commit

Permalink
1.Support gateway;2.Add annotation;3.Simplify syntax and struct
Browse files Browse the repository at this point in the history
  • Loading branch information
AbericYang committed May 12, 2019
1 parent 7614853 commit 7ea8581
Show file tree
Hide file tree
Showing 19 changed files with 307 additions and 34 deletions.
110 changes: 110 additions & 0 deletions bow/bow.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright (c) 2019. ENNOO - All Rights Reserved.
*
* 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 bow

import (
"fmt"
"github.com/ennoo/rivet/trans/request"
"github.com/ennoo/rivet/trans/response"
"github.com/ennoo/rivet/utils/log"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"sync"
)

var (
instance *Bow
once sync.Once
serviceCount = 0
routeServices = make(map[string]*RouteService)
)

// GetBowInstance 获取路由管理对象 Bow 单例
func GetBowInstance() *Bow {
once.Do(func() {
instance = &Bow{AllWay: make(map[string]*RouteService)}
})
return instance
}

// Bow 路由入口对象
type Bow struct {
AllWay map[string]*RouteService
}

// RouteService 路由对象
type RouteService struct {
Name string
InURI string
OutRemote string
OutURI string
}

// Add 新增路由服务数组
func (s *Bow) Add(routeServiceArr ...*RouteService) {
for index := range routeServiceArr {
routeService := routeServiceArr[index]
routeServices[routeService.Name] = routeService
GetBowInstance().register(routeService)
serviceCount++
}
}

// AddService 新增路由服务
func (s *Bow) AddService(serviceName, inURI, outRemote, outURI string) {
routeServices[serviceName] = &RouteService{
Name: serviceName,
InURI: inURI,
OutRemote: outRemote,
OutURI: outURI,
}
GetBowInstance().register(&RouteService{
Name: serviceName,
InURI: inURI,
OutRemote: outRemote,
OutURI: outURI,
})
serviceCount++
}

// Register 注册新的路由方式
func (s *Bow) register(routeService *RouteService) {
instance.AllWay[routeService.Name] = routeService
}

// RunBow 开启路由
func RunBow(context *gin.Context, serviceName string) {
routeService, ok := instance.AllWay[serviceName]
if !ok {
err := fmt.Errorf("routeService not fount")
fmt.Println("not found ", serviceName)
log.Shunt.Error(err.Error(), zap.String("serviceName", serviceName))
} else {
request.SyncPoolGetRequest().Call(context, context.Request.Method, routeService.OutRemote, routeService.OutURI)
}
}

// RunBowCallback 开启路由并处理降级
func RunBowCallback(context *gin.Context, serviceName string, f func() *response.Result) {
routeService, ok := instance.AllWay[serviceName]
if !ok {
err := fmt.Errorf("service not fount")
fmt.Println("not found ", serviceName)
log.Shunt.Error(err.Error(), zap.String("serviceName", serviceName))
} else {
request.SyncPoolGetRequest().Callback(context, context.Request.Method, routeService.OutRemote, routeService.OutURI, f)
}
}
32 changes: 32 additions & 0 deletions bow/bow_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2019. ENNOO - All Rights Reserved.
*
* 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 bow

import (
"github.com/gin-gonic/gin"
)

// Route 网关服务路由
func Route(engine *gin.Engine) {
// 仓库相关路由设置
vRepo := engine.Group("/")
for x := range routeServices {
bowService := routeServices[x]
vRepo.Any(bowService.InURI, func(context *gin.Context) {
RunBow(context, bowService.Name)
})
}
}
1 change: 1 addition & 0 deletions discovery/consul/consul_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func logger() {
func TestEnroll(t *testing.T) {
logger()
Enroll("127.0.0.1:8500", "ididididid", "rivet", "127.0.0.1", 8080)
ReEnroll()
}

func TestChecks(t *testing.T) {
Expand Down
32 changes: 32 additions & 0 deletions examples/bow1/bow1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2019. ENNOO - All Rights Reserved.
*
* 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 main

import (
"github.com/ennoo/rivet/rivet"
"github.com/ennoo/rivet/utils/env"
"strings"
)

func main() {
rivet.Initialize(true, false, true, false)
rivet.Bow().AddService("test1", "hello1", "http://localhost:8081", "rivet/shunt")
rivet.Bow().AddService("test2", "hello2", "https://localhost:8092", "rivet/shunt")
rivet.ListenAndServe(&rivet.ListenServe{
Engine: rivet.SetupRouter(),
DefaultPort: "8084",
}, strings.Join([]string{env.GetEnv(env.GOPath), "/src/github.com/ennoo/rivet/examples/tls/rootCA.crt"}, ""))
}
45 changes: 45 additions & 0 deletions examples/bow2/bow2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2019. ENNOO - All Rights Reserved.
*
* 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 main

import (
"github.com/ennoo/rivet/bow"
"github.com/ennoo/rivet/rivet"
"github.com/ennoo/rivet/utils/env"
"strings"
)

func main() {
rivet.Initialize(true, false, true, false)
rivet.Bow().Add(
&bow.RouteService{
Name: "test1",
InURI: "hello1",
OutRemote: "http://localhost:8081",
OutURI: "rivet/shunt",
},
&bow.RouteService{
Name: "test2",
InURI: "hello2",
OutRemote: "https://localhost:8092",
OutURI: "rivet/shunt",
},
)
rivet.ListenAndServe(&rivet.ListenServe{
Engine: rivet.SetupRouter(),
DefaultPort: "8085",
}, strings.Join([]string{env.GetEnv(env.GOPath), "/src/github.com/ennoo/rivet/examples/tls/rootCA.crt"}, ""))
}
2 changes: 1 addition & 1 deletion examples/shunt1/shunt1.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
)

func main() {
rivet.Initialize(true, true, true)
rivet.Initialize(false, true, true, true)
//rivet.Log().Conf(&log.Config{
// FilePath: strings.Join([]string{"./logs/rivet.log"}, ""),
// Level: zapcore.DebugLevel,
Expand Down
2 changes: 1 addition & 1 deletion examples/trans1/trans1.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
)

func main() {
rivet.Initialize(true, false, false)
rivet.Initialize(false, true, false, false)
rivet.UseDiscovery(discovery.ComponentConsul, "127.0.0.1:8500", "test", "127.0.0.1", 8081)
rivet.ListenAndServe(&rivet.ListenServe{
Engine: rivet.SetupRouter(testRouter1),
Expand Down
6 changes: 4 additions & 2 deletions examples/trans1/trans_tls1.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ import (
)

func main() {
rivet.Initialize(true, false, false)
rivet.ListenAndServe(&rivet.ListenServe{
rivet.Initialize(false, true, false, false)
rivet.ListenAndServeTLS(&rivet.ListenServe{
Engine: rivet.SetupRouter(testRouterTLS1),
DefaultPort: "8091",
ConnectTimeout: 3 * time.Second,
KeepAlive: 30 * time.Second,
CertFile: strings.Join([]string{env.GetEnv(env.GOPath), "/src/github.com/ennoo/rivet/examples/tls/server/server.crt"}, ""),
KeyFile: strings.Join([]string{env.GetEnv(env.GOPath), "/src/github.com/ennoo/rivet/examples/tls/server/server.key"}, ""),
}, strings.Join([]string{env.GetEnv(env.GOPath), "/src/github.com/ennoo/rivet/examples/tls/rootCA.crt"}, ""))
}

Expand Down
2 changes: 1 addition & 1 deletion examples/trans2/trans2.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
)

func main() {
rivet.Initialize(true, false, false)
rivet.Initialize(false, true, false, false)
rivet.UseDiscovery(discovery.ComponentConsul, "127.0.0.1:8500", "test", "127.0.0.1", 8082)
rivet.ListenAndServe(&rivet.ListenServe{
Engine: rivet.SetupRouter(testRouter2),
Expand Down
2 changes: 1 addition & 1 deletion examples/trans2/trans_tls2.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
)

func main() {
rivet.Initialize(true, false, false)
rivet.Initialize(false, true, false, false)

rivet.ListenAndServeTLS(&rivet.ListenServe{
Engine: rivet.SetupRouter(testRouterTLS2),
Expand Down
20 changes: 14 additions & 6 deletions rivet/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package rivet

import (
"github.com/ennoo/rivet/bow"
"github.com/ennoo/rivet/discovery"
"github.com/ennoo/rivet/discovery/consul"
"github.com/ennoo/rivet/scheduled"
Expand All @@ -30,11 +31,12 @@ import (
)

var (
hc = false // 是否开启健康检查。开启后为 Get 请求,路径为 /health/check
sm = false // 是否开启外界服务管理功能
ud = false // 是否启用发现服务
cp string // 启用的发现服务组件类型
sn string // 注册到发现服务的服务名称(优先通过环境变量 SERVICE_NAME 获取)
route = false // 是否开启网关路由
hc = false // 是否开启健康检查。开启后为 Get 请求,路径为 /health/check
sm = false // 是否开启外界服务管理功能
ud = false // 是否启用发现服务
cp string // 启用的发现服务组件类型
sn string // 注册到发现服务的服务名称(优先通过环境变量 SERVICE_NAME 获取)
)

// ListenServe 启动监听端口服务对象
Expand All @@ -53,13 +55,16 @@ type ListenServe struct {

// Initialize rivet 初始化方法,必须最先调用
//
// bow:是否开启网关路由
//
// healthCheck:是否开启健康检查。开启后为 Get 请求,路径为 /health/check
//
// serverManager:是否开启外界服务管理功能
//
// loadBalance:是否开启负载均衡
func Initialize(healthCheck bool, serverManager bool, loadBalance bool) {
func Initialize(bow bool, healthCheck bool, serverManager bool, loadBalance bool) {
runtime.GOMAXPROCS(runtime.NumCPU())
route = bow
hc = healthCheck
sm = serverManager
request.LB = loadBalance
Expand Down Expand Up @@ -97,6 +102,9 @@ func UseDiscovery(component, url, serviceName, hostname string, port int) {
// SetupRouter 设置路由器相关选项
func SetupRouter(routes ...func(*gin.Engine)) *gin.Engine {
engine := gin.Default()
if route {
bow.Route(engine)
}
if hc {
Health(engine)
}
Expand Down
6 changes: 6 additions & 0 deletions rivet/rivet.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package rivet

import (
"github.com/ennoo/rivet/bow"
"github.com/ennoo/rivet/shunt"
"github.com/ennoo/rivet/trans/request"
"github.com/ennoo/rivet/trans/response"
Expand Down Expand Up @@ -56,3 +57,8 @@ func Response() *response.Response {
func Request() *request.Request {
return request.SyncPoolGetRequest()
}

// Bow 提供实例化调用路由,并内置返回策略
func Bow() *bow.Bow {
return bow.GetBowInstance()
}
1 change: 1 addition & 0 deletions scheduled/task_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func checkSelfService() {

// compareAndResetServices 通过比较对象移除原本对象中多余项
func compareAndResetServices(services, servicesCompare *server.Services) {
// todo 发现服务中没有的 service 应该交由自检查服务进行管理
servicesArr := services.Services
size := len(servicesArr)
for i := 0; i < size; i++ {
Expand Down
6 changes: 1 addition & 5 deletions scheduled/task_service_consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,7 @@ func checkServicesByConsul(abortDiscovery chan int) {
continue
}
// 获取本地本地列表
services := server.ServiceGroup()[serviceName]
if nil == services {
services = &server.Services{}
server.ServiceGroup()[serviceName] = services
}
services := server.GetServices(serviceName)
// 新建空服务列表
servicesCompare := server.Services{}
// 如存在且列表大于0,遍历线上服务列表并检查线上服务状态是否为可用
Expand Down
5 changes: 1 addition & 4 deletions scheduled/task_services.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ func checkServices(abortServices chan int) {
// 根据本地可负载服务列表遍历发现服务(线上)中是否存在
for serviceName := range allWay {
// 获取本地服务列表
services := server.ServiceGroup()[serviceName]
if nil == services {
continue
}
services := server.GetServices(serviceName)
log.Scheduled.Debug("获取本地服务列表", zap.Any("servicesArr", services.Services))
servicesArr := services.Services
size := len(servicesArr)
Expand Down

0 comments on commit 7ea8581

Please sign in to comment.