10 Star 17 Fork 6

0-hun / IdCreator

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

整体架构

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 下压测

image

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
    <?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());
        }

空文件

简介

golang分布式id生成器server端,底层使用SnowFlake,使用zookeeper作为服务注册中心,客户端服务发现进行rpc调用;也支持http短链 展开 收起
Go 等 2 种语言
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Go
1
https://gitee.com/man0sions/IdCreator.git
git@gitee.com:man0sions/IdCreator.git
man0sions
IdCreator
IdCreator
master

搜索帮助

14c37bed 8189591 565d56ea 8189591