Dubbo-go Consumer调用Dubbo-java Provider
一、必要说明
需求: 实现golang
客户端调用java
服务端dubbo
接口发送短信的功能
- 服务端使用
Java
语言提供了短信发送的Dubbo
接口(基于Dubbo 2.7.*
) - 客户端基于
dubbo-go 1.5.2-rc2
调用以上Java
提供的Dubbo Provider
- 客户端要想使用
dubbo-go 3.0
,Provider
也必须使用dubbo-java 3.0
二、根据Java Provider Interface
定义对应的golang Consumer
客户端映射关系
import "context"
// SmsAlarmSendDTO 告警信息Dubbo调用数据结构
type SmsAlarmSendDTO struct {
AppId string `json:"appId"`
Token string `json:"token"`
TemplateName string `json:"templateName"`
TemplateData map[string]interface{} `json:"templateData"`
TemplateSign string `json:"templateSign"`
ExtendData map[string]interface{} `json:"extendData"`
PhoneNo string `json:"phoneNo"`
}
func (SmsAlarmSendDTO) JavaClassName() string {
return "com.xxx.fd.sms.send.dto.SmsAlarmSendDTO"
}
type ResultTo struct {
Success bool `json:"success"`
ErrorContext ErrorContext `json:"errorContext"`
Data bool `json:"data"`
}
func (ResultTo) JavaClassName() string {
return "com.xxx.fd.common.result.ResultTo"
}
type ErrorContext struct {
ErrorStack []CommonError `json:"errorStack"`
ThirdPartyError string `json:"thirdPartyError"`
}
func (ErrorContext) JavaClassName() string {
return "com.xxx.fd.common.exception.ErrorContext"
}
type CommonError struct {
ErrorCode ErrorCode `json:"errorCode"`
ErrorMsg string `json:"errorMsg"`
Location string `json:"location"`
}
func (CommonError) JavaClassName() string {
return "com.xxx.fd.common.exception.CommonError"
}
type ErrorCode struct {
SystemCode string `json:"systemCode"`
ErrorLevel string `json:"errorLevel"`
ErrorType string `json:"errorType"`
ErrorScene string `json:"errorScene"`
ErrorSpecific string `json:"errorSpecific"`
}
func (ErrorCode) JavaClassName() string {
return "com.xxx.fd.common.exception.ErrorCode"
}
type SmsFacade struct {
SendSmsAlarm func(ctx context.Context, dto SmsAlarmSendDTO) (ResultTo, error) `dubbo:"sendSmsAlarm"`
}
func (s *SmsFacade) Reference() string {
return "SmsFacade"
}
三、Dubbo配置
客户端配置 dubbo-client.yml
:
# dubbo client yaml configure file
check: true
# client
request_timeout: "3s"
# connect timeout
connect_timeout: "3s"
# application config
application:
organization: "xxx.com"
name: "SmsSendClient"
module: "dubbo-go sms and voice send client"
version: "0.0.1"
environment: "dev"
# registry config
registries:
"smsAndVoiceZk":
protocol: "zookeeper"
timeout: "10s"
address: "10.255.23.5:2181"
# reference config
references:
"SmsFacade":
registry: "smsAndVoiceZk"
protocol: "dubbo"
version: "1.0.0"
interface: "com.xxx.fd.sms.send.facade.SmsFacade"
methods:
- name: "sendSmsAlarm"
retries: 0
"VoiceSendFacade":
registry: "smsAndVoiceZk"
protocol: "dubbo"
version: "1.0.0"
interface: "com.xxx.fd.vms.send.facade.VoiceSendFacade"
methods:
- name: "sendVoice"
retries: 0
# protocol config
protocol_conf:
dubbo:
reconnect_interval: 0
connection_number: 1
heartbeat_period: "5s"
session_timeout: "180s"
pool_size: 64
pool_ttl: 600
getty_session_param:
compress_encoding: false
tcp_no_delay: true
tcp_keep_alive: true
keep_alive_period: "120s"
tcp_r_buf_size: 262144
tcp_w_buf_size: 65536
pkg_rq_size: 1024
pkg_wq_size: 512
tcp_read_timeout: "5s"
tcp_write_timeout: "10s"
wait_timeout: "3s"
max_msg_len: 1024000
session_name: "client"
日志配置文件:dubbo-client-log.yml
level: "debug"
development: true
disableCaller: false
disableStacktrace: false
sampling:
encoding: "console"
# encoder
encoderConfig:
messageKey: "message"
levelKey: "level"
timeKey: "time"
nameKey: "logger"
callerKey: "caller"
stacktraceKey: "stacktrace"
lineEnding: ""
levelEncoder: "capital"
timeEncoder: "iso8601"
durationEncoder: "seconds"
callerEncoder: "short"
nameEncoder: ""
outputPaths:
- "stderr"
errorOutputPaths:
- "stderr"
initialFields:
四、服务调用
- 注意启动前,启动前,需要设置配置文件路径的环境变量
CONF_CONSUMER_FILE_PATH=/etc/conf/
APP_LOG_CONF_FILE=/etc/conf/dubbo-client-log.yml
var (
smsSendProvider *sms.SmsFacade
)
// InitDubboClient 初始化短信和语音发送Dubbo 客户端
func InitDubboClient() {
smsSendProvider := new(sms.SmsFacade)
hessian.RegisterPOJO(&sms.SmsAlarmSendDTO{})
hessian.RegisterPOJO(&sms.ResultTo{})
hessian.RegisterPOJO(&sms.ErrorContext{})
hessian.RegisterPOJO(&sms.ErrorCode{})
hessian.RegisterPOJO(&sms.CommonError{})
dubboConfig.SetConsumerService(smsSendProvider)
dubboConfig.Load()
}
// SendSmsAlarm 发送告警短信
func SendSmsAlarm(ctx context.Context, smsDto sms.SmsAlarmSendDTO) error {
if smsSendProvider == nil {
return fmt.Errorf("smsSendProvider consumer not initialized properly")
}
result, err := smsSendProvider.SendSmsAlarm(ctx, smsDto)
if err != nil {
return fmt.Errorf("SendSmsAlarm failed:%v", err)
}
seelog.Debugf("SendSmsAlarm success, result is:%v", result)
return nil
}