Go-kit 入门(四)服务调用

调用服务

存在“真空”(即极其独立,与其他任何服务无互相调用的关系)中的服务是罕见的。而我们常见的是,我们需要调用其他的服务。这也是 Go kit 的闪光点 ,我们提供了 传输中间件 机制来解决可能出现的很多问题。

下面我们将实现一个代理功能的中间件,作为一个服务中间件。在这里我们只代理一个方法,Uppercase。

客户端端点

我们已经有了一个跟我们所知道的完全相同的端点,但是我们将使用它来调用一个请求,而不是提供服务。按照这种方式来使用它的时候,我们称它为客户端端点。为了调用客户端端点,我们需要做一些简单的转换。

现在,我们为了构造一个代理中间件,我们将一个代理URL字符串转换为一个端点。加入我们使用 HTTP 协议之上的 JSON,我们可以使用 transport/http 包中的一个辅助(helper)函数。

服务发现和负载均衡

如果我们只使用一个远程的服务就好了。但是实际上,我们往往 会有很多个服务实例。我们希望通过一些服务发现算法来发现它们,然后将我们的负载分散到它们上面,并且如果这些实例中的任何一个变得糟糕,我们能够处理它,并且不影响我们服务的可用性。

Go kit 为不同的服务发现系统提供了适配器,为了获取最新的服务实例集合,暴露端点个体。这些适配器被称为 发布器(publishers)。

在发布器内部,它使用一个私有的工厂函数,将被发现的每一个 host:port 字符串 转换成一个可用的端点。

目前,我们的工程方法,makeUppercaseEndpoint,只是直接请求 URL。但是,在工厂函数中加入一些安全的中间件方法是很重要的,比如 回路断路器 和 限流器。

现在,我们已经有了一系列的端点,我们需要在其中选择一个。负载均衡器包装了 发布器,并且从端点集合中选择其中的某一个。Go kit 提供了一组基本的负载均衡器,并且,如果你希望更多的高级的算法,也可以很容易的自己来写一个。

现在,我们可以根据一下算法来选择端点。我们能够使用它为消费器提供一个单一的、逻辑的可靠的端点。通过重试的策略包装负载均衡器,并且返回一个可用的端点。重试的策略可以将一个失败的请求进行重试,直到达到最大的可重试次数或是达到超时时间。

现在,让我们将最后的代理中间件加入到代码中。为了简洁,我们假设用户会为逗号(,)分隔的多个实例端点指定一个标记。

stringsvc3

截止目前,这个完整的服务是 stringsvc3$ go get github.com/go-kit/kit/examples/stringsvc3 $ stringsvc3 -listen=:8001 & listen=:8001 caller=proxying.go:25 proxy_to=none listen=:8001 caller=main.go:72 msg=HTTP addr=:8001 $ stringsvc3 -listen=:8002 & listen=:8002 caller=proxying.go:25 proxy_to=none listen=:8002 caller=main.go:72 msg=HTTP addr=:8002 $ stringsvc3 -listen=:8003 & listen=:8003 caller=proxying.go:25 proxy_to=none listen=:8003 caller=main.go:72 msg=HTTP addr=:8003 $ stringsvc3 -listen=:8080 -proxy=localhost:8001,localhost:8002,localhost:8003 listen=:8080 caller=proxying.go:29 proxy_to="[localhost:8001 localhost:8002 localhost:8003]" listen=:8080 caller=main.go:72 msg=HTTP addr=:8080

高级话题

线程上下文

上下文对象用来在单个请求中携带那些需要跨越概念性边界的信息。在我们的例子中,我们还没有在业务逻辑中使用线程上下文。但是,这种方式几乎会永远都是一个好方案。它允许我们在业务逻辑和中间件中传递请求范围内的信息,并且对于复杂的任务(比如,分布式系统的细粒度追踪信息)也是很必要的。

具体点来讲,这也意味着你的业务逻辑接口会这样:

 

请求跟踪

一旦你的基础设施增长超过一定的规模,在多个服务中跟踪请求是非常必要的,这样,你就能够分析并解决故障热点。参见 package tracing 获取更多信息。

创建一个客户端软件包

使用 Go kit 为你的服务创建一个客户端软件包是很可能的事情,让你的服务能够很容易对其他的 Go 程序进行调用。实际上,你的客户端package会提供你的服务接口,这个接口会使用指定的传输方式来调用远程服务。参见 package addsvc/client 作为参考例子.

其他例子

addsvc

addsvc 是原来的一个例子。它公开 所有支持的传输方式 的系列操作。它完整地做了日志记录、仪表盘化,并且使用 Zipkin 来跟踪请求。同样,它也示范了如何创建和使用客户端package。它是一个非常棒的例子,为 Go kit 服务提供了完整的功能示例。

profilesvc

profilesvc 示范了如何使用 Go kit 来打造 REST 风格的微服务。

apigateway

apigateway 示范了如何实现一个 API 网关模式,通过 Consul 作为服务发现系统。

shipping

shipping 是一个完整的,真实的应用,由多个微服务组成,基于领域驱动设计原则。

|2|left
此条目发表在MircroService分类目录,贴了, 标签。将固定链接加入收藏夹。

Go-kit 入门(四)服务调用》有 6,423 条评论

  1. cialis 20mg说:

    Amazing YouTube videos posted at this website, I am going to subscribe for daily updates, as I don’t would like to miss this series.

  2. Hi there colleagues, is there any other good weblog related to JavaScript articles or reviews, while this one is nice in support of PHP programming.

  3. I know this web site offers quality based articles or reviews and other material, is there any other web page which offers these kinds of things in quality?

  4. cialis 20说:

    Please add new video lessons related to cooking if you have, since I wish for to learn more and more about all recipes of cooking.

  5. Actually movie is the presentation of some one’s feelings; it presents the lesson to the viewers.

  6. wengeaw说:

    india generic cialis
    cheap cialis
    order cialis online 37.5
    =http://cialissknrx.com/#”>cheap cialis online

  7. Preeple说:

    cialis 5mg
    buy cialis online
    price cialis 20mg nurofen plus
    =http://cialissknrx.com/#”>generic cialis online

  8. What’s up dear, me plus my mother are also watch funny video clips except after I completed my homework

  9. cialis uk说:

    This piece of writing will help the internet users for building up new web site or even a weblog from start to end.

  10. Hi friends, is there any other pleasant website related to JavaScript content, while this one is nice designed for PHP programming.

  11. What a information of un-ambiguity and preserveness of precious know-how regarding unexpected feelings.

  12. I am genuinely thankful to the owner of this web site who has shared this impressive article at at this time.

  13. I like to work on PHP rather than .NET, though .NET gives the ability of drag and drop elements, except I like Personal home pages a lot.

  14. WilliamFUH说:

    Всем привет и пока.

  15. leona j说:

    I am sure this piece of writing has touched all the internet viewers, its really really nice post on building up new
    weblog.

  16. Hi there it’s me Fiona, I am also visiting this website on a regular basis, this site is in fact fastidious and the people are really sharing pleasant thoughts.

  17. cialis cost说:

    What’s up friends, how is all, and what you would like to say about this post, in my view its in fact remarkable for me.

  18. It is the happiest time of my life so far, when I am watching these} funny movies at this place, because after whole day working I was so tired and now feeling perfectly.

  19. cialis cost说:

    There are also so many video uploading sites, and these also provide facility for sharing their videos, however I think YouTube is the most excellent.

  20. cheap cialis说:

    No one can reject from the quality of this video posted at this web site, pleasant work, keep it all the time.

  21. Because the admin of this site is working, no doubt very shortly it will be famous, due to its quality contents.

  22. Hello, it’s understandable piece of writing along with this YouTube video; I can’t believe that one can not understand this simple piece of writing having with video demonstration.

  23. driedge说:

    vicodin pill picture cialis generic
    cialis online
    cialis online pharmacy drugs
    =http://cialissknrx.com/#”>buy cialis

  24. For the reason that the YouTube video tutorials are posted here same like I also embed YouTube video code at my own website, for the reason that it is easy to obtain embedded code.

  25. I all the time download a complete show in parts, that’s always present at YouTube, for the reason that my net connection is extremely slow and YouTube fulfils my desires.

  26. cialis 20说:

    Downloading stuff from this site is as trouble-free |as clicking the mouse rather than other web sites which move me here and there on the webpages.

  27. 20 mg cialis说:

    Yes, you are correct buddy, regularly updating website is genuinely essential for SEO. Good discussion keeps it up.

  28. Yup, you are correct Google is the best for blogging, Google’s web site also come up to rapidly in search engines too.

  29. If you would like to grow your know-how just keep visiting this website and be updated with the newest gossip posted here.

  30. MattV0813说:

    It is the best time to make some plans for the long run and
    it’s time to be happy. I’ve learn this submit and if I may
    I want to suggest you some attention-grabbing things or advice.
    Maybe you can write next articles relating to this article.

    I desire to read even more things approximately it!

  31. Hi there everyone, it’s my first pay a quick visit at this site, and article is truly fruitful for me, keep up posting these articles or reviews.

  32. If you are interested to learn Web optimization techniques then you must read this article, I am sure you will take much more from this article concerning SEO.

  33. It’s very easy to find out any topic on net as compared to textbooks, as I fount this post at this web page.

  34. Hello, everything is going well here and ofcourse every one is sharing information, that’s in fact excellent, keep up writing.

  35. Hello to every body, it’s my first pay a quick visit of this blog; this website consists of remarkable and actually excellent material in support of visitors.

  36. Hi, I would like to subscribe for this blog to get most up-to-date updates, so where can i do it please assist.

  37. daync说:

    Аккаунты игры,социальных сетей,музыкальных сайтов

    зайти в аккаунт ps4

  38. cialis说:

    Its extremely good YouTube video in terms of quality, in fact good, its quality is in fact appreciable.

  39. No matter if some one searches for his necessary thing, so he/she wants to be available that in detail, thus that thing is maintained over here.

  40. Hi there colleagues, nice article and pleasant arguments commented at this place, I am genuinely enjoying by these.

  41. It’s very straightforward to find out any matter on web as compared to books, as I fount this article at this website.

  42. Can you please forward me the code for this script or please let know me in detail about this script?

  43. I read this post fully on the topic of the comparison of most up-to-date and preceding technologies, it’s amazing article.

  44. Wow, what a video it is! Actually pleasant feature video, the lesson given in this video is truly informative.

  45. I am in fact glad to read this blog posts which carries lots of valuable data, thanks for providing such information.

  46. This is my first time visit at here and i am in fact happy to read everthing at alone place.

发表评论

电子邮件地址不会被公开。 必填项已用*标注