# bsfswagger **Repository Path**: chejiangyi/bsfswagger ## Basic Information - **Project Name**: bsfswagger - **Description**: go语言写的swagger 协议转换成go和java api代码 - **Primary Language**: Go - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2025-08-04 - **Last Updated**: 2025-08-05 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # bsfswagger 一个将swagger协议转成go和java api client的工具,便于多语言http rpc调用。 #### 介绍 目前支持swagger 2.0协议转成go或java语言的api client;当前正在内测中,谨慎使用。 #### 使用介绍 ``` -- 参数说明: -- "package":"demo_example" //"生成的包名" -- "language":"go" //"开发语言" -- "outDir":"out" //"输出目录" -- "swaggerJsonPath":"http://localhost:8081/swagger/_doc.json" //"json文件(http url或文件路径)" -- "replaceTypeNames":"auto_models_,http_models_,base2_" //"需要替换部分类型名为空,逗号分隔多个" ``` #### go 生成案例 * go api client 生成 ``` bsfswagger.exe -package "demo_example" -language "go" -outDir "out" -swaggerJsonPath "http://localhost:8081/swagger/_doc.json" ``` * go 使用api client ``` import ( "bsfswagger/out/demo_example" "gitee.com/chejiangyi/bsfgo/core/utils" // "gitee.com/chejiangyi/bsfgo/nacos2" ) func main() { /*使用nacos读取本地配置初始化,并使用nacos注册中心动态获取服务列表*/ //utils.ConfigUtil.InitConfig([]string{"out/config.dev.yaml"}, false) //nacos2.NacosUtil.InitNacos() //hosts := nacos2.NacosUtil.GetServiceList2("demo") /*使用本地服务列表或统一网关服务地址*/ hosts:=[]string{"localhost:8081"} client := demo_example.Instance.RefreshHostPorts(hosts)//每次调用传入最新可用host地址 response := client.ApiGetTestInfo(&demo_example.GetTestInfoRequest{Name: "a", PageIndex: 1, PageSize: 20}) utils.LogUtil.Info("response:%v", utils.JsonUtil.ToJson(response)) response2 := client.ApiGetTestInfo2PathId(1, "bbb", "a") utils.LogUtil.Info("response2:%v", utils.JsonUtil.ToJson(response2)) } ``` * go 使用api client中bsf配置 ``` # ==================== RPC配置 ==================== rpc: printTime: # 打印时间配置 detail: enabled: true # 是否打印详细时间 enabled: true # 是否打印时间 retryCount: 2 # RPC重试次数 ``` #### java 生成案例 * java 生成api client ``` bsfswagger.exe -package "com.free.bsf.demo.example" -language "java" -outDir "out" -swaggerJsonPath "http://localhost:8081/swagger/_doc.json" ``` * java 使用api client [bsf nacos配置](https://gitee.com/chejiangyi/free-bsf-all/blob/3.4-SNAPSHOT/free-bsf-nacos/README.md) ``` /*使用nacos获取配置;注意要开启bsf nacos配置*/ //ExampleClient exampleClient = ExampleClient.Instance.refreshHostPorts(NacosUtils.getServiceList("demo")); /*使用本地服务列表或统一网关服务地址*/ ExampleClient exampleClient = ExampleClient.Instance.refreshHostPorts(new String[]{"localhost:8081"}); val r= exampleClient.apiGetTestInfo(new ExampleClient.GetTestInfoRequest().setId("").setName("a").setPageIndex(1L).setPageSize(10L)); LogUtils.info(EmptyApplication.class,"", JsonUtils.serialize(r)); ``` #### 生成结果案例 按照目录隔离: 一个目录一个api client文件
![img.png](img.png) #### 目前协议底层http通讯 sdk sdk 目前支持随机负载均衡策略,故障转移,重试机制,http连接池,开发人员可扩展并替换其他组件实现。 * [bsf java版本](https://gitee.com/chejiangyi/free-bsf-all) * [bsf go版本](https://gitee.com/chejiangyi/bsfgo) by 车江毅