如何使用golang微服务? [关闭]

My company use Go to build some HTTP API services. We want these services share one HTTP port.

So the solution right now is we create a project named router, and router import some modules, every request pass through router to their own modules.
But the question is that if one of these modules process crashed, the router just crash.

Is there any solutions?

Require:

  1. One http port.
  2. Every service is independent.

I know go-kit and go micro, also I have tried, but still not too understand.

Go-kit and go-micro are for writing microservices, it won't solve your problem.

You should use a reverse proxy in front of your applications, for instance Nginx: https://www.nginx.com/resources/admin-guide/reverse-proxy/

Here is an example of a nginx configuration file that does what you want:

server {
   listen 80 default_server;
   server_name your-domain;

   location /app1 {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_pass http://localhost:3000;
   }

   location /app2 {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_pass http://localhost:4000;
   }
}

This way, you still deploy your applications on different ports, but they will only be exposed through the port 80.

What you are looking for is an API-gateway. An API-gateway is a form of reverse-proxy too. I've looked into complete solutions like Tyk, Traefik and Zuul which all are great solutions. Of course one can use Nginx, but then you probably have a lot you have to incorporate using Lua scripting, such as client for OpenID-Connect/OAuth for authorization and such. Another things to take into consideration is security, logging, metrics, rate-limiting, (dynamic routing) and more. I really wouldn't base my API-gateway on Nginx. Nginx-Plus includes an API-gateway, but requires payment.

I'm currently working on a very large project and have found that it requires a lot of customization work in form of plugins for the solutions mentioned above. So much in fact, that it will be a lot easier for me to write my API-gateway from scratch (using Golang and libraries).

Since you mention you have tried go-kit and micro, I would suggest you try Traefik as it is very simple to set up and includes a Web UI for convenience.

An approach that I used to host Go microservices in Windows is simply run go install <main-package-file> which generated an exe file for Windows. Then I simply used SC to create a Windows service out of it. Then just run it as a normal Windows service. This way I did not need Nginx or any such engine..