开源软件名称:IdCreator
开源软件地址:https://gitee.com/man0sions/IdCreator
开源软件介绍:
整体架构php调用id生成器,php 需要借助motan-agent实现rpc通信
golang/java调用id生成器 对于golang/java,具备tpc长链接能力
算法+--------------------------------------------------------------------------+| 1 Bit Unused | 41 Bit Timestamp | 10 Bit NodeID | 12 Bit Sequence ID |+--------------------------------------------------------------------------+雪花算法,每个node,每毫秒产生4096个唯一id 启动//先修改config/mesh-confs/basic.yaml 的zk地址 address: "xxx.xxx.xxx.xxx:2181"go run main.go --conf=config/main.yaml --pool=idcreator --nodeId=20//nodeId-支持0-1023,启动后不要更改,否则会产生重复id//pool-提供的motan服务配置 http调用12核12G 下压测
curl http://127.0.0.1:8778/v1/IdCreator //本机localhost返回值{"time": 1601191174,"status": 10000,"msg": "操作成功","data": "334011185131241472"} motan-rpc调用也可以参考tests/rpc_test.go 中的例子
1:在go.mod 中加入
replace ( github.com/weibreeze/breeze-go => github.com/weibreeze/breeze-go v0.1.1 go.uber.org/zap => github.com/uber-go/zap v1.9.1) 2:vim main.go
package mainimport ( "gitee.com/man0sions/IdCreator/lib/tmotan" "log")func main() { log.SetFlags(log.Lshortfile) client := tmotan.NewClient("./my_app.yaml") ret, err := client.CallIdCreator() log.Println(ret,err)} 3:vim my_app.yaml
motan-client: log_dir: motanlogsmotan-registry: zk-registry: protocol: zookeeper address: "127.0.0.1:2181" registrySessionTimeout: 10000motan-basicRefer: basicRefer: group: test1 protocol: motan2 registry: zk-registry requestTimeout: 1000 haStrategy: failover loadbalance: roundrobin serialization: simple filter: "accessLog" retries: 0motan-refer: call_proxy: group: test1 path: com.xxx.yyy.service.IdCreatorService registry: zk-registry basicRefer: basicRefer # requestTimeout: 250 retries: 1 4: vim build.sh
#!/bin/shunset GOPROXY# go versionexport "GOPROXY=https://goproxy.cn/,direct"go get -insecure -v && CGO_ENABLED=0 go build -o my_app main.go 5: ./build.sh 编译
6: ./my_app 运行
php 用法php 不能连zk发现节点,只能自己轮训,也可以自己配置motan-agent
//先安装 https://github.com/weibocom/motan-php <?php define('D_AGENT_ADDR', '10.x.x.2:' . env('WEIBO_MESH_UG_PORT', 8841)); $service = 'com.xxx.yyy.service.IdCreatorService'; $group = 'test1'; $remote_method = '/v1/IdCreator'; $str = ""; $params = [$str]; $cx = new \Motan\MClient(); $request = new \Motan\Request($service, $remote_method, $params); $request->setRequestId(1111); $request->setGroup($group); try { $res = $cx->doCall($request); var_dump($res); } catch (Exception $e) { var_dump($e->getMessage()); } |
请发表评论