Skip to content
Snippets Groups Projects
Commit c47b0899 authored by xujianhai666's avatar xujianhai666
Browse files

support grpc server

parent 35abd82c
No related branches found
No related tags found
No related merge requests found
......@@ -45,6 +45,7 @@ type GrpcProtocol struct {
func NewGRPCProtocol() *GrpcProtocol {
return &GrpcProtocol{
BaseProtocol: protocol.NewBaseProtocol(),
serverMap: make(map[string]*Server),
}
}
......@@ -60,7 +61,22 @@ func (gp *GrpcProtocol) Export(invoker protocol.Invoker) protocol.Exporter {
}
func (gp *GrpcProtocol) openServer(url common.URL) {
return
_, ok := gp.serverMap[url.Location]
if !ok {
_, ok := gp.ExporterMap().Load(url.ServiceKey())
if !ok {
panic("[GrpcProtocol]" + url.Key() + "is not existing")
}
gp.serverLock.Lock()
_, ok = gp.serverMap[url.Location]
if !ok {
srv := NewServer()
gp.serverMap[url.Location] = srv
srv.Start(url)
}
gp.serverLock.Unlock()
}
}
func (gp *GrpcProtocol) Refer(url common.URL, impl interface{}) protocol.Invoker {
......
......@@ -18,7 +18,9 @@ limitations under the License.
package grpc
import (
"fmt"
"net"
"reflect"
)
import (
......@@ -27,6 +29,9 @@ import (
import (
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/common/constant"
"github.com/apache/dubbo-go/config"
"github.com/apache/dubbo-go/protocol"
)
type Server struct {
......@@ -34,11 +39,15 @@ type Server struct {
}
func NewServer() *Server {
return &Server{}
}
return nil
type DubboGrpcService interface {
SetProxyImpl(impl protocol.Invoker)
GetProxyImpl() protocol.Invoker
ServiceDesc() *grpc.ServiceDesc
}
// TODO: unimplemented
func (s *Server) Start(url common.URL) {
var (
addr string
......@@ -51,12 +60,34 @@ func (s *Server) Start(url common.URL) {
}
server := grpc.NewServer()
key := url.GetParam(constant.BEAN_NAME_KEY, "")
service := config.GetProviderService(key)
ds, ok := service.(DubboGrpcService)
if !ok {
panic("illegal service type registered")
}
m, ok := reflect.TypeOf(service).MethodByName("SetProxyImpl")
if !ok {
panic("method SetProxyImpl is necessary for grpc service")
}
exporter, _ := grpcProtocol.ExporterMap().Load(url.ServiceKey())
if exporter == nil {
panic(fmt.Sprintf("no exporter found for servicekey: %v", url.ServiceKey()))
}
invoker := exporter.(protocol.Exporter).GetInvoker()
if invoker == nil {
panic(fmt.Sprintf("no invoker found for servicekey: %v", url.ServiceKey()))
}
in := []reflect.Value{reflect.ValueOf(service)}
in = append(in, reflect.ValueOf(invoker))
m.Func.Call(in)
server.RegisterService(ds.ServiceDesc(), service)
s.grpcServer = server
// grpc-go 必须提前注册
// ServiceDesc 这个信息需要有
// 需要找一个方法。
//server.RegisterService()
// 想个办法注册下
if err = server.Serve(lis); err != nil {
panic(err)
}
......
......@@ -370,6 +370,7 @@ func (r *zkRegistry) register(c common.URL) error {
return perrors.Errorf("@c{%v} type is not referencer or provider", c)
}
dubboPath = strings.ReplaceAll(dubboPath, "$", "%24")
err = r.registerTempZookeeperNode(dubboPath, encodedURL)
if err != nil {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment