将用Go编写的微服务合并为一个二进制文件。

huangapple go评论82阅读模式
英文:

Merge micro-services written in Go into one binary

问题

我的应用程序由几个使用Golang编写的程序组成,这些程序在Docker/Kubernetes微服务架构中运行。现在,我们希望它在Linux上直接运行,而不使用Docker/Kubernetes。因此,我们考虑将所有程序合并为一个单独的二进制程序。哪种方式最好,因为每个程序都是在cmd/main.go、internal/{aaa,bbb,ccc}/xxx.go中组织的?谢谢!

我现在能想到的方法是将每个服务重新组织在pkg/.../xxx.go中,包括main.go中的所有当前代码。然后,我的all-in-one.go将导入所有main.go的代码。

有什么建议吗?

编辑:

合并的原因更新。1)目标平台是嵌入式系统,我希望尽可能使应用程序变小,并尽可能容易部署。2)我还希望尝试在系统测试中使用all-in-one应用程序,因为与真正的微服务Kubernetes版本相比,在开发者机器上本地运行更轻量级。

英文:

my app consists of several golang written programs that run inside docker/k8s micro-services arch. Now, we want it to run directly on Linux without docker/k8s. So we’re thinking to merge all the programs into a single binary program. Which would be best way since each program is organized in cmd/main.go, internal/{aaa,bbb,ccc}/xxx.go? Thanks!

What I can think of now is to re-organize each service in pkg/.../xxx.go, including all current code in main.go. Then my all-in-one.go will import all the main.go code.

Any suggestions?

Edit:

Update reason for merging. 1) the target platform is embedded system, I'd like to make the app as small as possible, and as easier to deploy as possible. 2) I'd also like to try use the all-in-one app in system test, as it's more light-weight to run locally on developer machine than the real micro-service k8s version.

答案1

得分: 1

每个程序都是在cmd/main.gointernal/{aaa,bbb,ccc}/xxx.go中组织的,哪种方式最好?

你需要重新组织它们。大多数不是非常小或一次性的Go项目都遵循以下某个版本:

  • 为每个可构建的命令在cmd/commandname下创建一个或多个main包,其中几乎没有代码:通常只有一个main()入口函数,它只是调用其他包中的代码。
  • 所有的模型和应用逻辑都在cmd之外的其他包中,这样可以在需要时被多个命令(或其他项目)共享,并且可以轻松进行单元测试。

话虽如此,我建议仔细考虑是否真的有必要或有帮助将它们合并,而不是像现在这样并行运行这些服务。即便如此,重新组织项目可能都会有好处。

英文:

> Which would be best way since each program is organized in cmd/main.go, internal/{aaa,bbb,ccc}/xxx.go?

You'll have to reorganize them. Most Go projects that aren't tiny/disposable follow some version of the following:

  • One or more main packages under cmd/commandname for each buildable command, with very little code inside: generally just a main() entrypoint function that does nothing but call code from some other package(s).
  • All models and application logic in other packages outside cmd, where it can be shared by multiple commands (or other projects) as needed, and easily unit-tested.

That said, I'd suggest looking carefully at whether it's really necessary or helpful to merge these rather than just running the services side by side as they are. Even so, it sounds like reorganizing the projects could be beneficial either way.

答案2

得分: -1

我认为最好的方法是开始一个新项目,并将代码复制/粘贴到新的函数和包中。如果你有多个同名的函数(func main())或同名的文件(main.go),你的代码将无法编译。

我会从一个全新的main.go开始,并在子文件夹中实现代码,例如project1/project1.go。

然后可以通过在main.go中使用import yourpackageurl.com/project1来导入包。

英文:

I think the best way is to start a new project and copy/paste code into new functions and packages. If you have multiple functions with the same name (func main()) or the same file name (main.go) your code won't compile.

I would start of with a brand new main.go and start to implement the code in subfolders f.e. project1/project1.go

Package can be imported then via import yourpackageurl.com/project1 into the main.go

huangapple
  • 本文由 发表于 2022年12月21日 18:02:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/74874283.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定