如何使用Golang微服务?

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

how to use golang microservices?

问题

我的公司使用Go语言构建一些HTTP API服务。我们希望这些服务共享一个HTTP端口。

目前的解决方案是我们创建了一个名为"router"的项目,router导入一些模块,每个请求都通过router传递到它们自己的模块中。
但问题是,如果其中一个模块崩溃了,router也会崩溃。

是否有其他解决方案?

要求:

  1. 一个HTTP端口。
  2. 每个服务都是独立的。

我知道有go-kit和go micro,我也尝试过,但还是不太理解。

英文:

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.

答案1

得分: 1

Go-kitgo-micro是用于编写微服务的工具,它们不能解决你的问题。

你应该在应用程序前面使用一个反向代理,比如Nginx:https://www.nginx.com/resources/admin-guide/reverse-proxy/

以下是一个实现你想要的功能的nginx配置文件示例:

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;
   }
}

这样,你仍然可以将应用程序部署在不同的端口上,但它们只会通过端口80进行访问。

英文:

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.

答案2

得分: 0

你要找的是一个API网关。API网关也是一种反向代理形式。我已经研究了一些完整的解决方案,比如Tyk、Traefik和Zuul,它们都是很好的解决方案。当然,你也可以使用Nginx,但是你可能需要使用Lua脚本来集成一些功能,比如OpenID-Connect/OAuth的客户端用于授权等。还有一些需要考虑的因素包括安全性、日志记录、指标监控、限流(动态路由)等等。我真的不会把我的API网关建立在Nginx上。Nginx-Plus包含一个API网关,但需要付费。

我目前正在一个非常大的项目上工作,发现需要大量的定制工作,比如为上述解决方案编写插件。实际上,对我来说,从头开始编写我的API网关(使用Golang和库)会更容易一些。

既然你提到你已经尝试过go-kit和micro,我建议你尝试一下Traefik,它非常简单设置,并且包含了一个Web界面以方便使用。

英文:

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.

答案3

得分: 0

我使用的一种在Windows上托管Go微服务的方法是简单地运行go install <main-package-file>,这将生成一个Windows的exe文件。然后,我使用SC命令将其创建为一个Windows服务。最后,将其作为普通的Windows服务运行。这样,我就不需要Nginx或任何类似的引擎。

英文:

An approach that I used to host Go microservices in Windows is simply run go install &lt;main-package-file&gt; 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..

huangapple
  • 本文由 发表于 2017年7月15日 15:49:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/45115860.html
匿名

发表评论

匿名网友

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

确定