0

Suppose I have four applications:

  1. http-gateway
  2. NATS
  3. Business Logic Client
  4. Business Logic Server
Gateway <--> NATS <---> B.L.

My project structure is as follows:

nats-cluster\
  cmd\
    gateway\gatway.go
    blclient\blclient.go
    blserver\blserver.go
  gateway\
  blclient\
  blserver\

Currently, I have to run four of them separately and only then they can talk with each other. Gateway and BLclient use request-reply pattern while blclient uses grpc to exchange messages with blserver. I wanted to know if there is any method to generate a single binary in Go so that the application can be run through a single binary.

Is there any way to combine multiple app binaries into a single one in Go?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
anisbhsl
  • 149
  • 1
  • 8
  • 1
    What do you mean by combining into one binary? A binary that extracts other binaries, a deployment package, a startup script? What is your business need you want to solve? – Sascha Feb 14 '20 at 06:48
  • so you have four separate applications that talk to each other through some IPC(pipe or something).. ?? i don't think you can club your exe's into a single exe.. – Siva Guru Feb 14 '20 at 07:00
  • @Sascha Yes, I want to make a deployment package so that these four components can be deployed through a single binary. I just wanted to know what are the best industry practices. – anisbhsl Feb 14 '20 at 07:27
  • @SivaGuru yes they use IPC. – anisbhsl Feb 14 '20 at 07:29
  • you could try (https://github.com/sanderhahn/gozip) for a self-extracting binary ... or a self-extracting script (https://www.linuxjournal.com/node/1005818) – Siva Guru Feb 14 '20 at 07:35
  • You can make one binary with all logic in it and start either with a command line switch that changes the behaviour or copy and rename the binary (or create a sym-/hardlink) and ask os.Args[0] for the name it has been called. – topskip Feb 14 '20 at 07:50
  • 1
    The efficient thing to do is: get rid of the individual components and refactor into a monolith. If that is what you want. – Volker Feb 14 '20 at 08:33
  • @Volker The whole point to individual components was to divide application logic. So combining the code into monolith isn't the solution. – anisbhsl Feb 14 '20 at 08:37
  • @SivaGuru the script will just extract the individual binaries and the whole process to run four binaries as individual components stays the same right? Isn't there any way to run just a single binary (with all components in it)? – anisbhsl Feb 14 '20 at 08:50
  • 1
    Then bundling them together makes no sense too. – Volker Feb 14 '20 at 08:53
  • 1
    As @Volker the only possible way is to combine the code and build them into a single binary. I don't think it's possible to take separate binaries -> combine 'em -> and run 'em as separate components. – Siva Guru Feb 14 '20 at 11:32
  • Depending on your target platform you could use some kind of a package. Like a debian package and shipping systemd components. That depends on the target platform – Sascha Feb 17 '20 at 04:51

1 Answers1

0

I embed the NATS server in the Simple IoT application. Here is how I do it:

https://github.com/simpleiot/simpleiot/blob/master/cmd/siot/main.go#L452

https://github.com/simpleiot/simpleiot/blob/master/natsserver/nats-server.go

Works great and super easy to do.

For some applications, it makes sense to bundle everything in one Go application as deployment is much simpler.

cbrake
  • 43
  • 1
  • 1
  • 6