# IdCreator **Repository Path**: man0sions/IdCreator ## Basic Information - **Project Name**: IdCreator - **Description**: golang分布式id生成器server端,底层使用SnowFlake,使用zookeeper作为服务注册中心,客户端服务发现进行rpc调用;也支持http短链 - **Primary Language**: Go - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 17 - **Forks**: 6 - **Created**: 2016-05-18 - **Last Updated**: 2022-07-15 ## Categories & Tags **Categories**: angular-extensions **Tags**: None ## README ### 整体架构 > php调用id生成器,php 需要借助motan-agent实现rpc通信 ![整体架构](https://gitee.com/man0sions/IdCreator/raw/master/doc/rpc-php.png) > golang/java调用id生成器 对于golang/java,具备tpc长链接能力 ![整体架构](https://gitee.com/man0sions/IdCreator/raw/master/doc/rpc-go.png) ### 算法 ``` +--------------------------------------------------------------------------+ | 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 下压测 ![image](https://gitee.com/man0sions/IdCreator/raw/master/doc/qps.png) ``` 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 main import ( "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: motanlogs motan-registry: zk-registry: protocol: zookeeper address: "127.0.0.1:2181" registrySessionTimeout: 10000 motan-basicRefer: basicRefer: group: test1 protocol: motan2 registry: zk-registry requestTimeout: 1000 haStrategy: failover loadbalance: roundrobin serialization: simple filter: "accessLog" retries: 0 motan-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/sh unset GOPROXY # go version export "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 setRequestId(1111); $request->setGroup($group); try { $res = $cx->doCall($request); var_dump($res); } catch (Exception $e) { var_dump($e->getMessage()); } ```