马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

.net core使用ocelot---第七篇 服务发现

[复制链接]
查看2390 | 回复0 | 2019-8-13 18:14:11 | 显示全部楼层 |阅读模式

简介

  .net core使用ocelot---第一篇 简单使用
  .net core使用ocelot---第二篇 身份验证使用
  .net core使用ocelot---第三篇 日志记录
  .net core使用ocelot---第四篇 限流熔断
  .net core使用ocelot---第五篇 服务质量
  .net core使用ocelot---第六篇 负载均衡

本文我们介绍用Spring Cloud Eureka Server介绍Ocelot的服务发现模块。

什么是服务发现

服务发现是自动检测这些设备在盘算机网络上提供的设备和服务。服务发现协议(SDP)是帮助完成服务发现的网络协议。服务发现旨在减少用户的配置工作。

在Ocelot中我们可以使用许多的服务发现,比如Consul, Eureka等等。本文我使用Eureka进行介绍。Ocelot使用Steeltoe与Eureka进行通信,Eureka是一个开源项目,使.NET开发人员可以或许在云上构建弹性微服务时能实现满足行业尺度的最佳实践。

我将使用Ocelot的7.1.0-unstable0011版本向您展示此功能。

Step1

首先创建两个API服务,一个是默认的ASP.NET Core Web API项目。在API网关发现我们的API服务之前,我们需要在Eureka服务上注册。

在appsettings.json添加一些配置

  1. 1. "spring": {
  2. 2. "application": {
  3. 3. "name": "service-a"
  4. 4. }
  5. 5. },
  6. 6. "eureka": {
  7. 7. "client": {
  8. 8. "serviceUrl": "http://192.168.0.107:8761/eureka/",
  9. 9. "shouldFetchRegistry": true,
  10. 10. "validateCertificates": false
  11. 11. },
  12. 12. "instance": {
  13. 13. "port": 9001,
  14. 14. "instanceId": "192.168.0.103:9001",
  15. 15. "hostName": "192.168.0.103",
  16. 16. "healthCheckUrlPath": "/api/values/healthcheck",
  17. 17. "statusPageUrlPath": "/api/values/info"
  18. 18. }
  19. 19. }
复制代码

注意

  1. Service-a 是Ocelot发现服务的重要标志。
  2. ServiceUrl是Eureka Server的端点。
  3. 获得更多信息,看这里

  添加服务发现必要的代码

  1. public class Startup
  2. {
  3. public Startup(IConfiguration configuration)
  4. {
  5. Configuration = configuration;
  6. }
  7. public IConfiguration Configuration { get; }
  8. public void ConfigureServices(IServiceCollection services)
  9. {
  10. services.AddDiscoveryClient(Configuration);
  11. services.AddMvc();
  12. }
  13. public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  14. {
  15. if (env.IsDevelopment())
  16. {
  17. app.UseDeveloperExceptionPage();
  18. }
  19. app.UseDiscoveryClient();
  20. app.UseMvc();
  21. }
  22. }
复制代码

  在这里,我将使用三个API服务来演示,两个API服务名称service-a具有不同的端口(9001和9003),一个API服务名称service-b。

Step2

新建一个APIGateway项目,添加名为ocelot.json的配置文件。

  1. {
  2. "ReRoutes": [
  3. {
  4. "DownstreamPathTemplate": "/api/values",
  5. "DownstreamScheme": "http",
  6. "UpstreamPathTemplate": "/a",
  7. "UseServiceDiscovery": true,
  8. "ServiceName": "service-a",
  9. "UpstreamHttpMethod": [ "Get" ],
  10. "QoSOptions": {
  11. "ExceptionsAllowedBeforeBreaking": 3,
  12. "DurationOfBreak": 1000,
  13. "TimeoutValue": 5000
  14. },
  15. "FileCacheOptions": { "TtlSeconds": 15 },
  16. "LoadBalancerOptions": {
  17. "Type": "RoundRobin"
  18. }
  19. },
  20. {
  21. "DownstreamPathTemplate": "/api/values",
  22. "DownstreamScheme": "http",
  23. "UpstreamPathTemplate": "/b",
  24. "UseServiceDiscovery": true,
  25. "ServiceName": "service-b",
  26. "UpstreamHttpMethod": [ "Get" ],
  27. "QoSOptions": {
  28. "ExceptionsAllowedBeforeBreaking": 3,
  29. "DurationOfBreak": 1000,
  30. "TimeoutValue": 5000
  31. },
  32. "FileCacheOptions": { "TtlSeconds": 15 }
  33. }
  34. ],
  35. "GlobalConfiguration": {
  36. "RequestIdKey": "OcRequestId",
  37. "AdministrationPath": "/administration",
  38. "ServiceDiscoveryProvider": { "Type": "Eureka" }
  39. }
  40. }
复制代码

  这里有几点需要注意。

  对于ReRoutes

  1. 将UseServiceDiscovery设为true。
  2. 将ServiceName值设为在API服务里定义的服务名称。
  3. 不用指明DownstreamHostAndPorts的值。
  4. 将LoadBalancerOptions设成RoundRobin。

  对于GlobalConfiguration

设置ServiceDiscoveryProvider的Type为Eureka。这是使用Eureka至关重要的配置。

回到Program.cs,我们需要让Ocelot可以使用。

  1. public class Program
  2. {
  3. public static void Main(string[] args)
  4. {
  5. BuildWebHost(args).Run();
  6. }
  7. public static IWebHost BuildWebHost(string[] args) =>
  8. WebHost.CreateDefaultBuilder(args)
  9. .UseUrls("http://*:9000")
  10. .ConfigureAppConfiguration((hostingContext, config) =>
  11. {
  12. config
  13. .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
  14. .AddJsonFile("ocelot.json")
  15. .AddEnvironmentVariables();
  16. })
  17. .ConfigureServices(s =>
  18. {
  19. s.AddOcelot();
  20. })
  21. .Configure(a =>
  22. {
  23. a.UseOcelot().Wait();
  24. })
  25. .Build();
  26. }
复制代码

Step3

让Eureka跑起来。

181523moqsh7pw005wig54.png

  如你所见,Eureka服务启动起来了,但是没有可以使用的实例。

注意

为了在你的电脑上跑Eureka服务,你可以按照下面的步骤实验。

  1. 安装Java 8 的JDK
  2. 安装Maven3.X
  3. 复制Spring Cloud Samples Eureka 代码库(https://github.com/spring-cloud-samples/eureka.git)
  4. 转到eureka服务的目次(eureka)并使用mvn spring-boot:run启动它

Step4

我们如何注册我们的服务?只需运行我们的项目,我们就会得到service-a实例。

当运行我们的API服务,你就会发现service-a 运行在Eureka 服务里了

181523ssantot9go7dtlad.png

  好了,启动我们的APIGateway,不幸的是,当我们运行起项目。

181524vbr7a5bru9p520z5.png

  查看非常信息,我们忘了在APIGateway项目中配置Eureka。在appsettings.json添加下面的配置。

  1. "spring": {
  2. "application": {
  3. "name": "service-gw"
  4. }
  5. },
  6. "eureka": {
  7. "client": {
  8. "serviceUrl": "http://192.168.0.107:8761/eureka/",
  9. "shouldRegisterWithEureka": false,
  10. "validateCertificates": false
  11. },
  12. "instance": {
  13. "port": 9000,
  14. "instanceId": "192.168.0.103:9000",
  15. "hostName": "192.168.0.103"
  16. }
  17. }
复制代码

  重新启动APIGateway,终于正常了。

  

181524ou6nozhhqdig60zh.png

  通过APIGat访问service-a

181525twwdf0eu0plp2p4y.jpg

  通过APIGateway访问未启动的service-b

181525k0s02z01sm81se1e.png

  毫无疑问我们不可能访问service-b

  启动后

181526qgpxcpg4odgxd4a0.png

  再次启动

181526g8kdbwey8fhzzzkk.jpg

  最后注册另一个service-a

181527c5ztjlms948y8j19.png

  访问service-a你得到的效果有时来自9001,有时来自9003 。因为我们负载均衡算法设为RoundRobin。

181527co6l5cl7h4h6n4g7.jpg

  当我们制止service-a的一个服务,我们依然可以访问,但是停止的那个服务不能访问。

  源码在此

总结

本文介绍了Ocelot使用Eureka实现服务发现的简单示例。当让另有consul也可以实现服务发现,下篇介绍。


来源:https://www.cnblogs.com/xlxr45/p/11321786.html
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则