原来dubbo 还支持 golang 呢,可以用相互调用,相爱了。使用java 做服务端,golang做客户端

版权声明:本文为博主原创文章,未经博主允许不得转载。博主地址:http://blog.csdn.net/freewebsys https://blog.csdn.net/freewebsys/article/details/82780264

1,关于 dubbo-go


https://dubbo.apache.org/zh-cn/

官方提供了 dubbo-go 的代码
但是并不支持试 Hessian(X)
https://github.com/dubbo/dubbo-go
具体说明:

  • 1 Transport: HTTP(√)
  • 2 Codec: JsonRPC(√), Hessian(X)
  • 3 Service discovery:Service Register(√), Service Watch(√)
  • 4 Registry: ZooKeeper(√), Etcd(X), Redis(X)
  • 5 Strategy: Failover(√), Failfast(√)
  • 6 Load Balance: Random(√), RoundRobin(√)
  • 7 Role: Consumer(√), Provider(X)

2,栗子


本来想自己写一个例子。
没有想到官方都做好了。只要调用就可以了。

https://github.com/dubbo/dubbo-go/tree/master/examples
但是没有跑通,不过没有关系,发现另外一个项目
也是这个作者写的。不知道为啥没有迁移过来。
https://github.com/AlexStocks/dubbogo-examples
https://github.com/AlexStocks/dubbogo
这个项目:
两个项目需要合到一起次才能跑起来。
dubbo-go 里面跑 java ,dubbogo-examples 里面跑 golang
java:

package com.ikurento.user;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Provider {

    /**
     * To get ipv6 address to work, add
     * System.setProperty("java.net.preferIPv6Addresses", "true");
     * before running your application.
     */
    public static void main(String[] args) throws Exception {
        String[] confs = new String[]{"META-INF/spring/dubbo.provider.xml"};
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(confs);
        context.start();
        System.out.println("################## start ##################");
        System.in.read(); // press any key to exit
    }
}
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 应用名 -->
    <dubbo:application name="user-info-server"/>
    <!-- 连接到哪个本地注册中心 -->
    <dubbo:registry id="ikurento" address="zookeeper://127.0.0.1:2181"/>
    <!--<dubbo:registry id="ikurento"  address="N/A"  />-->
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <!-- dubbo:protocol host="127.0.0.1" / -->
    <dubbo:protocol id="dubbo" name="dubbo" host="127.0.0.1" port="20880"/>
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service registry="ikurento" protocol="dubbo" timeout="3000" interface="com.ikurento.user.UserProvider"
                   ref="demoService"/>

    <bean id="demoService" class="com.ikurento.user.UserProviderImpl"/>

</beans>

golang clinet:

cd dubbogo-examples\user-info\client
sh .\assembly\windows\test.sh
cd .\target\windows\user_info_client-*
sh .\bin\load_user_info_client.sh start

配置: client.toml

# dubbo client toml configure file

# pprof
Pprof_Enabled           = true
Pprof_Port              = 10086

# client
Request_Timeout         = "350ms"
NET_IO_Timeout          = "2s"
Retries                 = 2
# 连接池中每个地址的最大连接数
Pool_Size               = 32
# 连接池中每个连接的有效时间
Pool_TTL                = "10m"
# connect timeout
Connect_Timeout         = "100ms"
Selector                = "cache"
Selector_TTL            = "10m"
Registry                = "zookeeper"

# application config
[Application_Config]
Organization            = "ikurento.com"
Name                    = "Pusher"
Module                  = "dubbogo user-info client"
Version                 = "0.0.1"
Owner                   = "ZX"

[Registry_Config]
   # You can indent as you please. Tabs or spaces. TOML don't care.
    Address				= ["127.0.0.1:2181"]
    Timeout				= 3

[[Service_List]]
    Protocol            = "dubbo"
    Service             = "com.ikurento.user.UserProvider"

client.go :

....
func main() {
	var (
		err error
	)

	err = initClientConfig()
	if err != nil {
		log.Error("initClientConfig() = error{%#v}", err)
		return
	}
	initProfiling()
	initClient()

	hessian.RegisterJavaEnum(Gender(MAN))
	hessian.RegisterPOJO(&DubboUser{})
	hessian.RegisterPOJO(&Response{})

	time.Sleep(1) // wait for selector
	gxlog.CInfo("###################################################")
	gxlog.CInfo("\n\n\nstart to test jsonrpc")
	//testJsonrpc("A003")
	gxlog.CInfo("\n\n\nstart to test dubbo Calc")
	testDubboCalc()
	gxlog.CInfo("\n\n\nstart to test dubbo Sum")
	testDubboSum()
	gxlog.CInfo("\n\n\nstart to test dubbo GetUsers")
	testDubboGetUsers()
	gxlog.CInfo("\n\n\nstart to test dubbo GetUserMap")
	testDubboGetUserMap()
	gxlog.CInfo("###################################################")
	time.Sleep(1) // wait for selector
	initSignal()
}
....

3,运行 日志


再golang里面:

[2018-09-19/23:51:59 main.testDubboGetUserMap: test.go: 256] dubbo selected service registry.ServiceConfig{Protocol:"dubbo", Service:"com.ikurento.user.UserProvider", Group:"", Version:""}
0[2018/09/19 23:51:59 CST] [DEBG] (rpc_client.go:github.com/AlexStocks/dubbogo/client.(*rpcClient).Call:325) reqID{2664339703}, err:<nil>
[2018/09/19 23:51:59 CST] [INFO] (test.go:main.testDubboGetUsers:224) response result:[User{Id:001, Name:demo-zhangsan, Age:18, Time:2018-09-19 23:51:51.981 +0800 CST, Sex:MAN} User{Id:003, Name:demo-lily, Age:23, Time:2018-09-19 23:51:51.981 +0800 CST, Sex:MAN} User{Id:004, Name:demo-lisa, Age:32, Time:2018-09-19 23:51:51.981 +0800 CST, Sex:MAN}]
0[2018/09/19 23:51:59 CST] [DEBG] (cache.go:github.com/AlexStocks/dubbogo/selector/cache.(*cacheSelector).Select:296) @service:&registry.ServiceConfig{Protocol:"dubbo", Service:"com.ikurento.user.UserProvider", Group:"", Version:""}
0[2018/09/19 23:51:59 CST] [DEBG] (cache.go:github.com/AlexStocks/dubbogo/selector/cache.(*cacheSelector).get:85) c.get(@s:&registry.ServiceConfig{Protocol:"dubbo", Service:"com.ikurento.user.UserProvider", Group:"", Version:""})
0[2018/09/19 23:51:59 CST] [DEBG] (cache.go:github.com/AlexStocks/dubbogo/selector/cache.(*cacheSelector).get:100) c.cache[service{registry.ServiceConfig{Protocol:"dubbo", Service:"com.ikurento.user.UserProvider", Group:"", Version:""}}] = services{[ServiceURL{Protocol:dubbo, Location:10.0.75.1:20880, Path:/com.ikurento.user.UserProvider, Ip:10.0.75.1, Port:20880, Version:, Group:, Weight:0, Query:map[generic:[false] pid:[13908] anyhost:[true] application:[user-info-server] dubbo:[2.5.4] interface:[com.ikurento.user.UserProvider] methods:[GetUserMap,getUser,isLimit,Calc,Sum,queryAll,GetUsers,queryUser,GetUser] side:[provider] timeout:[3000] timestamp:[1537372312034]]}]}
0[2018/09/19 23:51:59 CST] [DEBG] (rpc_client.go:github.com/AlexStocks/dubbogo/client.(*rpcClient).Call:272) ctx:(*context.emptyCtx)(0xc042066058), d:time.Time{wall:0x0, ext:0, loc:(*time.Location)(nil)}, ok:false
0[2018/09/19 23:51:59 CST] [DEBG] (rpc_client.go:github.com/AlexStocks/dubbogo/client.(*rpcClient).Call:275) create timeout context, timeout:350ms
0[2018/09/19 23:51:59 CST] [DEBG] (rpc_client.go:github.com/AlexStocks/dubbogo/client.(*rpcClient).call.func1:189) store connection:{protocol:dubbo, location:10.0.75.1:20880, conn:&client.poolConn{once:(*sync.Once)(0xc0421a8bf0), Client:(*transport.tcpTransportClient)(0xc0421d4c40), created:1537372319}}, gerr:<nil>
[2018-09-19/23:51:59 main.main: client.go: 74] ###################################################
0[2018/09/19 23:51:59 CST] [DEBG] (rpc_client.go:github.com/AlexStocks/dubbogo/client.(*rpcClient).Call.func1:304) @i{0}, call(ID{2664339704}, ctx{context.Background.WithDeadline(2018-09-19 23:51:59.6081597 +0800 CST m=+0.815997701 [338.9607ms])}, serviceURL{ServiceURL{Protocol:dubbo, Location:10.0.75.1:20880, Path:/com.ikurento.user.UserProvider, Ip:10.0.75.1, Port:20880, Version:, Group:, Weight:0, Query:map[anyhost:[true] application:[user-info-server] dubbo:[2.5.4] interface:[com.ikurento.user.UserProvider] methods:[GetUserMap,getUser,isLimit,Calc,Sum,queryAll,GetUsers,queryUser,GetUser] side:[provider] timeout:[3000] timestamp:[1537372312034] generic:[false] pid:[13908]]}}, request{&{ dubbo  com.ikurento.user.UserProvider GetUserMap [[001 003 004]] application/dubbo {true <nil>}}}, response{&map[003:User{Id:003, Name:demo-lily, Age:23, Time:2018-09-19 23:51:51.981 +0800 CST, Sex:MAN} 004:User{Id:004, Name:demo-lisa, Age:32, Time:2018-09-19 23:51:51.981 +0800 CST, Sex:MAN} 001:User{Id:001, Name:demo-zhangsan, Age:18, Time:2018-09-19 23:51:51.981 +0800 CST, Sex:MAN}]}) = err{}
0[2018/09/19 23:51:59 CST] [DEBG] (rpc_client.go:github.com/AlexStocks/dubbogo/client.(*rpcClient).Call:325) reqID{2664339704}, err:<nil>
[2018/09/19 23:51:59 CST] [INFO] (test.go:main.testDubboGetUserMap:271) response result:map[003:User{Id:003, Name:demo-lily, Age:23, Time:2018-09-19 23:51:51.981 +0800 CST, Sex:MAN} 004:User{Id:004, Name:demo-lisa, Age:32, Time:2018-09-19 23:51:51.981 +0800 CST, Sex:MAN} 001:User{Id:001, Name:demo-zhangsan, Age:18, Time:2018-09-19 23:51:51.981 +0800 CST, Sex:MAN}]

....

java 服务端代码。

2018-09-19/23:51:59.239  INFO: com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol$1.disconnected(DubboProtocol.java:124) -  [DUBBO] disconected from /10.0.75.1:58445,url:dubbo://10.0.75.1:20880/com.ikurento.user.UserProvider?anyhost=true&application=user-info-server&channel.readonly.sent=true&codec=dubbo&dubbo=2.5.4&generic=false&heartbeat=60000&interface=com.ikurento.user.UserProvider&methods=GetUserMap,getUser,isLimit,Calc,Sum,queryAll,GetUsers,queryUser,GetUser&pid=13908&side=provider&timeout=3000&timestamp=1537372312034, dubbo version: 2.5.4, current host: 127.0.0.1
2018-09-19/23:51:59.244 DEBUG: com.alibaba.dubbo.remoting.transport.DecodeHandler.decode(DecodeHandler.java:60) -  [DUBBO] Decode decodeable message com.alibaba.dubbo.rpc.protocol.dubbo.DecodeableRpcInvocation, dubbo version: 2.5.4, current host: 127.0.0.1
2018-09-19/23:51:59.244  WARN: com.ikurento.user.UserProviderImpl.GetUsers(UserProviderImpl.java:39) - @userIdList size:3
2018-09-19/23:51:59.244  INFO: com.ikurento.user.UserProviderImpl.GetUsers(UserProviderImpl.java:43) - GetUsers(@uid:001)
2018-09-19/23:51:59.245  INFO: com.ikurento.user.UserProviderImpl.GetUsers(UserProviderImpl.java:46) - id:001, user:User{id:001, name:demo-zhangsan, age:18, time:Wed Sep 19 23:51:51 CST 2018, gender:MAN}
2018-09-19/23:51:59.245  INFO: com.ikurento.user.UserProviderImpl.GetUsers(UserProviderImpl.java:43) - GetUsers(@uid:003)
2018-09-19/23:51:59.245  INFO: com.ikurento.user.UserProviderImpl.GetUsers(UserProviderImpl.java:46) - id:003, user:User{id:003, name:demo-lily, age:23, time:Wed Sep 19 23:51:51 CST 2018, gender:MAN}
2018-09-19/23:51:59.245  INFO: com.ikurento.user.UserProviderImpl.GetUsers(UserProviderImpl.java:43) - GetUsers(@uid:004)
2018-09-19/23:51:59.245  INFO: com.ikurento.user.UserProviderImpl.GetUsers(UserProviderImpl.java:46) - id:004, user:User{id:004, name:demo-lisa, age:32, time:Wed Sep 19 23:51:51 CST 2018, gender:MAN}
2018-09-19/23:51:59.255  WARN: com.alibaba.dubbo.remoting.transport.AbstractServer.disconnected(AbstractServer.java:199) -  [DUBBO] All clients has discontected from /10.0.75.1:20880. You can graceful shutdown now., dubbo version: 2.5.4, current host: 127.0.0.1
2018-09-19/23:51:59.256  INFO: com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol$1.disconnected(DubboProtocol.java:124) -  [DUBBO] disconected from /10.0.75.1:58448,url:dubbo://10.0.75.1:20880/com.ikurento.user.UserProvider?anyhost=true&application=user-info-server&channel.readonly.sent=true&codec=dubbo&dubbo=2.5.4&generic=false&heartbeat=60000&interface=com.ikurento.user.UserProvider&methods=GetUserMap,getUser,isLimit,Calc,Sum,queryAll,GetUsers,queryUser,GetUser&pid=13908&side=provider&timeout=3000&timestamp=1537372312034, dubbo version: 2.5.4, current host: 127.0.0.1
2018-09-19/23:51:59.261 DEBUG: com.alibaba.dubbo.remoting.transport.DecodeHandler.decode(DecodeHandler.java:60) -  [DUBBO] Decode decodeable message com.alibaba.dubbo.rpc.protocol.dubbo.DecodeableRpcInvocation, dubbo version: 2.5.4, current host: 127.0.0.1
2018-09-19/23:51:59.262  WARN: com.ikurento.user.UserProviderImpl.GetUserMap(UserProviderImpl.java:56) - @userIdList size:3
2018-09-19/23:51:59.262  INFO: com.ikurento.user.UserProviderImpl.GetUserMap(UserProviderImpl.java:60) - GetUsers(@uid:001)
2018-09-19/23:51:59.262  INFO: com.ikurento.user.UserProviderImpl.GetUserMap(UserProviderImpl.java:63) - id:001, user:User{id:001, name:demo-zhangsan, age:18, time:Wed Sep 19 23:51:51 CST 2018, gender:MAN}
2018-09-19/23:51:59.262  INFO: com.ikurento.user.UserProviderImpl.GetUserMap(UserProviderImpl.java:60) - GetUsers(@uid:003)
2018-09-19/23:51:59.263  INFO: com.ikurento.user.UserProviderImpl.GetUserMap(UserProviderImpl.java:63) - id:003, user:User{id:003, name:demo-lily, age:23, time:Wed Sep 19 23:51:51 CST 2018, gender:MAN}
2018-09-19/23:51:59.263  INFO: com.ikurento.user.UserProviderImpl.GetUserMap(UserProviderImpl.java:60) - GetUsers(@uid:004)
2018-09-19/23:51:59.263  INFO: com.ikurento.user.UserProviderImpl.GetUserMap(UserProviderImpl.java:63) - id:004, user:User{id:004, name:demo-lisa, age:32, time:Wed Sep 19 23:51:51 CST 2018, gender:MAN}
2018-09-19/23:51:59.271  WARN: com.alibaba.dubbo.remoting.transport.AbstractServer.disconnected(AbstractServer.java:199) -  [DUBBO] All clients has discontected from /10.0.75.1:20880. You can graceful shutdown now., dubbo version: 2.5.4, current host: 127.0.0.1
2018-09-19/23:51:59.272  INFO: com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol$1.disconnected(DubboProtocol.java:124) -  [DUBBO] disconected from /10.0.75.1:58451,url:dubbo://10.0.75.1:20880/com.ikurento.user.UserProvider?anyhost=true&application=user-info-server&channel.readonly.sent=true&codec=dubbo&dubbo=2.5.4&generic=false&heartbeat=60000&interface=com.ikurento.user.UserProvider&methods=GetUserMap,getUser,isLimit,Calc,Sum,queryAll,GetUsers,queryUser,GetUser&pid=13908&side=provider&timeout=3000&timestamp=1537372312034, dubbo version: 2.5.4, current host: 127.0.0.1
2018-09-19/23:52:02.633 DEBUG: org.apache.zookeeper.ClientCnxn$SendThread.readResponse(ClientCnxn.java:717) - Got ping response for sessionid: 0x1000002d22f001b after 1ms
....

4,总结


以前有很多项目都是java写的,业务逻辑是用dubbo做的服务。
这部分是不可能重写了。但是要是使用golang开发新项目。需要用到 dubbo服务。
支持的协议 hessian2 ,这样写的程序就可以相互调用了。
超级兴奋。同时问了下作者, dubbo-go 再过1 - 2 月也能完善了。
同时也可以使用 golang 做server服务,让java 程序去做client 调用。
非常非常帮的。

本文的原文连接是:
https://blog.csdn.net/freewebsys/article/details/82780264

博主地址是:http://blog.csdn.net/freewebsys

猜你喜欢

转载自blog.csdn.net/freewebsys/article/details/82780264