dubbo.apache.org/dubbo-go/v3@v3.1.1/protocol/dubbo3/internal/server.go (about) 1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package internal 19 20 import ( 21 "context" 22 ) 23 24 import ( 25 log "github.com/dubbogo/gost/log/logger" 26 ) 27 28 import ( 29 "dubbo.apache.org/dubbo-go/v3/common" 30 "dubbo.apache.org/dubbo-go/v3/config" 31 _ "dubbo.apache.org/dubbo-go/v3/filter/filter_impl" 32 _ "dubbo.apache.org/dubbo-go/v3/metrics/prometheus" 33 _ "dubbo.apache.org/dubbo-go/v3/proxy/proxy_factory" 34 ) 35 36 // server is used to implement helloworld.GreeterServer. 37 type Server struct { 38 UnimplementedGreeterServer 39 } 40 41 // SayHello implements helloworld.GreeterServer 42 func (s *Server) SayHello(ctx context.Context, in *HelloRequest) (*HelloReply, error) { 43 log.Infof("Received: %v", in.GetName()) 44 return &HelloReply{Message: "Hello " + in.GetName()}, nil 45 } 46 47 // InitDubboServer creates global gRPC server. 48 func InitDubboServer() { 49 serviceConfig := config.NewServiceConfigBuilder(). 50 SetInterface("org.apache.dubbo.DubboGreeterImpl"). 51 SetProtocolIDs("tripleKey").Build() 52 53 providerConfig := config.NewProviderConfigBuilder().SetServices(map[string]*config.ServiceConfig{ 54 common.GetReference(&Server{}): serviceConfig, 55 }).Build() 56 57 protocolConfig := config.NewProtocolConfigBuilder().SetName("tri").SetPort("20003").Build() 58 59 rootConfig := config.NewRootConfigBuilder().SetProvider(providerConfig).SetProtocols(map[string]*config.ProtocolConfig{ 60 "tripleKey": protocolConfig, 61 }).Build() 62 63 config.SetProviderService(&Server{}) 64 if err := rootConfig.Init(); err != nil { 65 panic(err) 66 } 67 rootConfig.Start() 68 }