mirror of
https://github.com/duanhf2012/origin.git
synced 2026-05-19 23:57:28 +08:00
补充http服务
This commit is contained in:
4
Test/build.bat
Normal file
4
Test/build.bat
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
SET CGO_ENABLED=0
|
||||||
|
SET GOOS=linux
|
||||||
|
SET GOARCH=amd64
|
||||||
|
go build -v
|
||||||
27
Test/config/cluster.json
Normal file
27
Test/config/cluster.json
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
{
|
||||||
|
"NodeList":[
|
||||||
|
|
||||||
|
{
|
||||||
|
"NodeID":1,
|
||||||
|
"NodeName":"N_Node1",
|
||||||
|
"ServerAddr":"127.0.0.1:8080",
|
||||||
|
|
||||||
|
"ServiceList":["WSServerService","CTest","HttpServerService"],
|
||||||
|
"ClusterNode":["N_Node2"]
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
|
{
|
||||||
|
"NodeID":2,
|
||||||
|
"NodeName":"N_Node2",
|
||||||
|
"ServerAddr":"127.0.0.1:8081",
|
||||||
|
"ServiceList":["TestService1","collectTickLogService"],
|
||||||
|
"ClusterNode":[]
|
||||||
|
}
|
||||||
|
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
BIN
Test/debug
Normal file
BIN
Test/debug
Normal file
Binary file not shown.
119
Test/main.go
Normal file
119
Test/main.go
Normal file
@@ -0,0 +1,119 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/duanhf2012/origin/cluster"
|
||||||
|
"github.com/duanhf2012/origin/network"
|
||||||
|
"github.com/duanhf2012/origin/server"
|
||||||
|
"github.com/duanhf2012/origin/service"
|
||||||
|
"github.com/duanhf2012/origin/sysservice"
|
||||||
|
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
)
|
||||||
|
|
||||||
|
type CMessageReceiver struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (slf *CMessageReceiver) OnConnected(webServer network.IWebsocketServer, clientid uint64) {
|
||||||
|
fmt.Printf("%d\n", clientid)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (slf *CMessageReceiver) OnDisconnect(webServer network.IWebsocketServer, clientid uint64, err error) {
|
||||||
|
fmt.Printf("%d\n", clientid)
|
||||||
|
fmt.Print(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (slf *CMessageReceiver) OnRecvMsg(webServer network.IWebsocketServer, clientid uint64, msgtype int, data []byte) {
|
||||||
|
fmt.Printf("%d,%d\n", clientid, msgtype)
|
||||||
|
fmt.Print(string(data))
|
||||||
|
|
||||||
|
webServer.SendMsg(clientid, websocket.TextMessage, data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test(res http.ResponseWriter, req *http.Request) {
|
||||||
|
io.WriteString(res, "test..........!\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
type CTest struct {
|
||||||
|
service.BaseService
|
||||||
|
tmp int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ws *CTest) OnInit() error {
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type CTestData struct {
|
||||||
|
Bbbb int64
|
||||||
|
Cccc int
|
||||||
|
Ddd string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ws *CTest) RPC_LogTicker2(args *CTestData, quo *CTestData) error {
|
||||||
|
|
||||||
|
*quo = *args
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ws *CTest) Http_LogTicker2(request *sysservice.HttpRequest, resp *sysservice.HttpRespone) error {
|
||||||
|
|
||||||
|
resp.Respone = "hello world!"
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ws *CTest) OnRun() error {
|
||||||
|
|
||||||
|
ws.tmp = ws.tmp + 1
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
//if ws.tmp%10 == 0 {
|
||||||
|
var test CTestData
|
||||||
|
test.Bbbb = 1111
|
||||||
|
test.Cccc = 111
|
||||||
|
test.Ddd = "1111"
|
||||||
|
var test2 CTestData
|
||||||
|
err := cluster.Call("_CTest.RPC_LogTicker2", &test, &test2)
|
||||||
|
fmt.Print(err, test2)
|
||||||
|
//}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewCTest(servicetype int) *CTest {
|
||||||
|
wss := new(CTest)
|
||||||
|
wss.Init(wss, servicetype)
|
||||||
|
return wss
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkFileIsExist(filename string) bool {
|
||||||
|
var exist = true
|
||||||
|
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
||||||
|
exist = false
|
||||||
|
}
|
||||||
|
return exist
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ws *CTest) OnDestory() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
server := server.NewServer()
|
||||||
|
if server == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var receiver CMessageReceiver
|
||||||
|
wsservice := sysservice.NewWSServerService("/ws", 1314, &receiver, false)
|
||||||
|
test := NewCTest(0)
|
||||||
|
httpserver := sysservice.NewHttpServerService(80)
|
||||||
|
server.SetupService(wsservice, test, httpserver)
|
||||||
|
|
||||||
|
server.Init()
|
||||||
|
server.Start()
|
||||||
|
}
|
||||||
@@ -133,7 +133,7 @@ func (slf *CPing) Ping(ping *CPing, pong *CPong) error {
|
|||||||
func (slf *CCluster) ConnService() error {
|
func (slf *CCluster) ConnService() error {
|
||||||
ping := CPing{0}
|
ping := CPing{0}
|
||||||
pong := CPong{0}
|
pong := CPong{0}
|
||||||
fmt.Println(rpc.RegisterName("CPing", &ping))
|
fmt.Println(rpc.RegisterName("CPing", "", &ping))
|
||||||
|
|
||||||
//连接集群服务器
|
//连接集群服务器
|
||||||
for _, nodeList := range slf.cfg.mapClusterNodeService {
|
for _, nodeList := range slf.cfg.mapClusterNodeService {
|
||||||
@@ -333,7 +333,7 @@ func (slf *CCluster) GoNode(nodeid int, args interface{}, servicemethod string)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ws *CCluster) OnFetchService(iservice service.IService) error {
|
func (ws *CCluster) OnFetchService(iservice service.IService) error {
|
||||||
rpc.RegisterName(iservice.GetServiceName(), iservice)
|
rpc.RegisterName(iservice.GetServiceName(), "RPC_", iservice)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -229,16 +229,16 @@ func isExportedOrBuiltinType(t reflect.Type) bool {
|
|||||||
// The client accesses each method using a string of the form "Type.Method",
|
// The client accesses each method using a string of the form "Type.Method",
|
||||||
// where Type is the receiver's concrete type.
|
// where Type is the receiver's concrete type.
|
||||||
func (server *Server) Register(rcvr interface{}) error {
|
func (server *Server) Register(rcvr interface{}) error {
|
||||||
return server.register(rcvr, "", false)
|
return server.register(rcvr, "", "", false)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RegisterName is like Register but uses the provided name for the type
|
// RegisterName is like Register but uses the provided name for the type
|
||||||
// instead of the receiver's concrete type.
|
// instead of the receiver's concrete type.
|
||||||
func (server *Server) RegisterName(name string, rcvr interface{}) error {
|
func (server *Server) RegisterName(name string, prefix string, rcvr interface{}) error {
|
||||||
return server.register(rcvr, name, true)
|
return server.register(rcvr, name, prefix, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (server *Server) register(rcvr interface{}, name string, useName bool) error {
|
func (server *Server) register(rcvr interface{}, name string, prefix string, useName bool) error {
|
||||||
s := new(service)
|
s := new(service)
|
||||||
s.typ = reflect.TypeOf(rcvr)
|
s.typ = reflect.TypeOf(rcvr)
|
||||||
s.rcvr = reflect.ValueOf(rcvr)
|
s.rcvr = reflect.ValueOf(rcvr)
|
||||||
@@ -260,13 +260,13 @@ func (server *Server) register(rcvr interface{}, name string, useName bool) erro
|
|||||||
s.name = sname
|
s.name = sname
|
||||||
|
|
||||||
// Install the methods
|
// Install the methods
|
||||||
s.method = suitableMethods(s.typ, true)
|
s.method = suitableMethods(prefix, s.typ, true)
|
||||||
|
|
||||||
if len(s.method) == 0 {
|
if len(s.method) == 0 {
|
||||||
str := ""
|
str := ""
|
||||||
|
|
||||||
// To help the user, see if a pointer receiver would work.
|
// To help the user, see if a pointer receiver would work.
|
||||||
method := suitableMethods(reflect.PtrTo(s.typ), false)
|
method := suitableMethods(prefix, reflect.PtrTo(s.typ), false)
|
||||||
if len(method) != 0 {
|
if len(method) != 0 {
|
||||||
str = "rpc.Register: type " + sname + " has no exported methods of suitable type (hint: pass a pointer to value of that type)"
|
str = "rpc.Register: type " + sname + " has no exported methods of suitable type (hint: pass a pointer to value of that type)"
|
||||||
} else {
|
} else {
|
||||||
@@ -285,15 +285,17 @@ func (server *Server) register(rcvr interface{}, name string, useName bool) erro
|
|||||||
|
|
||||||
// suitableMethods returns suitable Rpc methods of typ, it will report
|
// suitableMethods returns suitable Rpc methods of typ, it will report
|
||||||
// error using log if reportErr is true.
|
// error using log if reportErr is true.
|
||||||
func suitableMethods(typ reflect.Type, reportErr bool) map[string]*methodType {
|
func suitableMethods(prefix string, typ reflect.Type, reportErr bool) map[string]*methodType {
|
||||||
methods := make(map[string]*methodType)
|
methods := make(map[string]*methodType)
|
||||||
for m := 0; m < typ.NumMethod(); m++ {
|
for m := 0; m < typ.NumMethod(); m++ {
|
||||||
method := typ.Method(m)
|
method := typ.Method(m)
|
||||||
mtype := method.Type
|
mtype := method.Type
|
||||||
mname := method.Name
|
mname := method.Name
|
||||||
|
|
||||||
if len(mname) > 4 && mname[:4] != "RPC_" {
|
if prefix != "" {
|
||||||
continue
|
if len(mname) < 4 || mname[:4] != prefix {
|
||||||
|
continue
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Method must be exported.
|
// Method must be exported.
|
||||||
@@ -650,8 +652,8 @@ func Register(rcvr interface{}) error { return DefaultServer.Register(rcvr) }
|
|||||||
|
|
||||||
// RegisterName is like Register but uses the provided name for the type
|
// RegisterName is like Register but uses the provided name for the type
|
||||||
// instead of the receiver's concrete type.
|
// instead of the receiver's concrete type.
|
||||||
func RegisterName(name string, rcvr interface{}) error {
|
func RegisterName(name string, prefix string, rcvr interface{}) error {
|
||||||
return DefaultServer.RegisterName(name, rcvr)
|
return DefaultServer.RegisterName(name, prefix, rcvr)
|
||||||
}
|
}
|
||||||
|
|
||||||
// A ServerCodec implements reading of RPC requests and writing of
|
// A ServerCodec implements reading of RPC requests and writing of
|
||||||
|
|||||||
@@ -66,9 +66,8 @@ func (s *cserver) Start() {
|
|||||||
|
|
||||||
log.Println(http.ListenAndServe("localhost:6060", nil))
|
log.Println(http.ListenAndServe("localhost:6060", nil))
|
||||||
}()
|
}()
|
||||||
|
|
||||||
service.InstanceServiceMgr().Start(s.exit, s.waitGroup)
|
|
||||||
cluster.InstanceClusterMgr().Start()
|
cluster.InstanceClusterMgr().Start()
|
||||||
|
service.InstanceServiceMgr().Start(s.exit, s.waitGroup)
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-s.sigs:
|
case <-s.sigs:
|
||||||
|
|||||||
@@ -7,10 +7,21 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/duanhf2012/origin/rpc"
|
||||||
|
|
||||||
|
"github.com/duanhf2012/origin/cluster"
|
||||||
"github.com/duanhf2012/origin/network"
|
"github.com/duanhf2012/origin/network"
|
||||||
"github.com/duanhf2012/origin/service"
|
"github.com/duanhf2012/origin/service"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type HttpRequest struct {
|
||||||
|
Body string
|
||||||
|
}
|
||||||
|
|
||||||
|
type HttpRespone struct {
|
||||||
|
Respone string
|
||||||
|
}
|
||||||
|
|
||||||
type ControllerMapsType map[string]reflect.Value
|
type ControllerMapsType map[string]reflect.Value
|
||||||
|
|
||||||
type HttpServerService struct {
|
type HttpServerService struct {
|
||||||
@@ -22,7 +33,6 @@ type HttpServerService struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (slf *HttpServerService) OnInit() error {
|
func (slf *HttpServerService) OnInit() error {
|
||||||
|
|
||||||
slf.httpserver.Init(slf.port)
|
slf.httpserver.Init(slf.port)
|
||||||
slf.httpserver.HandleFunc("/{server:[a-zA-Z0-9]+}/{method:[a-zA-Z0-9]+}", func(w http.ResponseWriter, r *http.Request) {
|
slf.httpserver.HandleFunc("/{server:[a-zA-Z0-9]+}/{method:[a-zA-Z0-9]+}", func(w http.ResponseWriter, r *http.Request) {
|
||||||
slf.httpHandler(w, r)
|
slf.httpHandler(w, r)
|
||||||
@@ -51,21 +61,7 @@ func (slf *HttpServerService) OnDestory() error {
|
|||||||
|
|
||||||
func (slf *HttpServerService) OnSetupService(iservice service.IService) {
|
func (slf *HttpServerService) OnSetupService(iservice service.IService) {
|
||||||
//
|
//
|
||||||
|
rpc.RegisterName(iservice.GetServiceName(), "HTTP_", iservice)
|
||||||
//反射
|
|
||||||
vf := reflect.ValueOf(iservice)
|
|
||||||
vft := vf.Type()
|
|
||||||
//读取方法数量
|
|
||||||
mNum := vf.NumMethod()
|
|
||||||
for i := 0; i < mNum; i++ {
|
|
||||||
mName := vft.Method(i).Name
|
|
||||||
startPos := strings.Index(mName, "Http_")
|
|
||||||
if startPos != 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
fmt.Println("index:", i, " MethodName:", mName)
|
|
||||||
slf.controllerMaps[mName] = vf.Method(i)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *HttpServerService) OnRemoveService(iservice service.IService) {
|
func (slf *HttpServerService) OnRemoveService(iservice service.IService) {
|
||||||
@@ -97,22 +93,23 @@ func (slf *HttpServerService) httpHandler(w http.ResponseWriter, r *http.Request
|
|||||||
writeError(http.StatusBadRequest, "rpc: ioutil.ReadAll "+err.Error())
|
writeError(http.StatusBadRequest, "rpc: ioutil.ReadAll "+err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
strCallPath := vstr[1] + "." + vstr[2]
|
strCallPath := "_" + vstr[1] + "." + vstr[2]
|
||||||
|
/*
|
||||||
|
method, err := slf.GetMethod(strCallPath)
|
||||||
|
var respone string
|
||||||
|
if err != nil {
|
||||||
|
respone = fmt.Sprintf("http respone => %v\n", err)
|
||||||
|
|
||||||
method, err := slf.GetMethod(strCallPath)
|
} else {
|
||||||
var respone string
|
cluster.InstanceClusterMgr().Call(NodeServiceMethod, args, reply)
|
||||||
if err != nil {
|
}
|
||||||
respone = fmt.Sprintf("http respone => %v\n", err)
|
*/
|
||||||
|
request := HttpRequest{string(msg)}
|
||||||
} else {
|
var resp HttpRespone
|
||||||
params := make([]reflect.Value, 0) // 参数
|
|
||||||
params = append(params, reflect.ValueOf(msg))
|
|
||||||
method.Call(params)
|
|
||||||
//respone = method.Call(params)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
cluster.InstanceClusterMgr().Call(strCallPath, &request, &resp)
|
||||||
w.Header().Set("Content-Type", "application/json;charset=utf-8")
|
w.Header().Set("Content-Type", "application/json;charset=utf-8")
|
||||||
w.Write([]byte(respone))
|
w.Write([]byte(resp.Respone))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *HttpServerService) GetMethod(strCallPath string) (*reflect.Value, error) {
|
func (slf *HttpServerService) GetMethod(strCallPath string) (*reflect.Value, error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user