简介
.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. "spring": {
- 2. "application": {
- 3. "name": "service-a"
- 4. }
- 5. },
- 6. "eureka": {
- 7. "client": {
- 8. "serviceUrl": "http://192.168.0.107:8761/eureka/",
- 9. "shouldFetchRegistry": true,
- 10. "validateCertificates": false
- 11. },
- 12. "instance": {
- 13. "port": 9001,
- 14. "instanceId": "192.168.0.103:9001",
- 15. "hostName": "192.168.0.103",
- 16. "healthCheckUrlPath": "/api/values/healthcheck",
- 17. "statusPageUrlPath": "/api/values/info"
- 18. }
- 19. }
复制代码
注意
- Service-a 是Ocelot发现服务的重要标志。
- ServiceUrl是Eureka Server的端点。
- 获得更多信息,看这里
添加服务发现必要的代码
- public class Startup
- {
- public Startup(IConfiguration configuration)
- {
- Configuration = configuration;
- }
-
- public IConfiguration Configuration { get; }
-
- public void ConfigureServices(IServiceCollection services)
- {
- services.AddDiscoveryClient(Configuration);
- services.AddMvc();
- }
-
- public void Configure(IApplicationBuilder app, IHostingEnvironment env)
- {
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- }
-
- app.UseDiscoveryClient();
- app.UseMvc();
- }
- }
复制代码
在这里,我将使用三个API服务来演示,两个API服务名称service-a具有不同的端口(9001和9003),一个API服务名称service-b。
Step2
新建一个APIGateway项目,添加名为ocelot.json的配置文件。
- {
- "ReRoutes": [
- {
- "DownstreamPathTemplate": "/api/values",
- "DownstreamScheme": "http",
- "UpstreamPathTemplate": "/a",
- "UseServiceDiscovery": true,
- "ServiceName": "service-a",
- "UpstreamHttpMethod": [ "Get" ],
- "QoSOptions": {
- "ExceptionsAllowedBeforeBreaking": 3,
- "DurationOfBreak": 1000,
- "TimeoutValue": 5000
- },
- "FileCacheOptions": { "TtlSeconds": 15 },
- "LoadBalancerOptions": {
- "Type": "RoundRobin"
- }
- },
- {
- "DownstreamPathTemplate": "/api/values",
- "DownstreamScheme": "http",
- "UpstreamPathTemplate": "/b",
- "UseServiceDiscovery": true,
- "ServiceName": "service-b",
- "UpstreamHttpMethod": [ "Get" ],
- "QoSOptions": {
- "ExceptionsAllowedBeforeBreaking": 3,
- "DurationOfBreak": 1000,
- "TimeoutValue": 5000
- },
- "FileCacheOptions": { "TtlSeconds": 15 }
- }
- ],
- "GlobalConfiguration": {
- "RequestIdKey": "OcRequestId",
- "AdministrationPath": "/administration",
- "ServiceDiscoveryProvider": { "Type": "Eureka" }
- }
- }
复制代码
这里有几点需要注意。
对于ReRoutes
- 将UseServiceDiscovery设为true。
- 将ServiceName值设为在API服务里定义的服务名称。
- 不用指明DownstreamHostAndPorts的值。
- 将LoadBalancerOptions设成RoundRobin。
对于GlobalConfiguration
设置ServiceDiscoveryProvider的Type为Eureka。这是使用Eureka至关重要的配置。
回到Program.cs,我们需要让Ocelot可以使用。
- public class Program
- {
- public static void Main(string[] args)
- {
- BuildWebHost(args).Run();
- }
-
- public static IWebHost BuildWebHost(string[] args) =>
- WebHost.CreateDefaultBuilder(args)
- .UseUrls("http://*:9000")
- .ConfigureAppConfiguration((hostingContext, config) =>
- {
- config
- .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
- .AddJsonFile("ocelot.json")
- .AddEnvironmentVariables();
- })
- .ConfigureServices(s =>
- {
- s.AddOcelot();
- })
- .Configure(a =>
- {
- a.UseOcelot().Wait();
- })
- .Build();
- }
复制代码
Step3
让Eureka跑起来。
如你所见,Eureka服务启动起来了,但是没有可以使用的实例。
注意
为了在你的电脑上跑Eureka服务,你可以按照下面的步骤实验。
- 安装Java 8 的JDK
- 安装Maven3.X
- 复制Spring Cloud Samples Eureka 代码库(https://github.com/spring-cloud-samples/eureka.git)
- 转到eureka服务的目次(eureka)并使用mvn spring-boot:run启动它
Step4
我们如何注册我们的服务?只需运行我们的项目,我们就会得到service-a实例。
当运行我们的API服务,你就会发现service-a 运行在Eureka 服务里了
好了,启动我们的APIGateway,不幸的是,当我们运行起项目。
查看非常信息,我们忘了在APIGateway项目中配置Eureka。在appsettings.json添加下面的配置。
- "spring": {
- "application": {
- "name": "service-gw"
- }
- },
- "eureka": {
- "client": {
- "serviceUrl": "http://192.168.0.107:8761/eureka/",
- "shouldRegisterWithEureka": false,
- "validateCertificates": false
- },
- "instance": {
- "port": 9000,
- "instanceId": "192.168.0.103:9000",
- "hostName": "192.168.0.103"
- }
- }
复制代码
重新启动APIGateway,终于正常了。
通过APIGat访问service-a
通过APIGateway访问未启动的service-b
毫无疑问我们不可能访问service-b
启动后
再次启动
最后注册另一个service-a
访问service-a你得到的效果有时来自9001,有时来自9003 。因为我们负载均衡算法设为RoundRobin。
当我们制止service-a的一个服务,我们依然可以访问,但是停止的那个服务不能访问。
源码在此
总结
本文介绍了Ocelot使用Eureka实现服务发现的简单示例。当让另有consul也可以实现服务发现,下篇介绍。 来源:https://www.cnblogs.com/xlxr45/p/11321786.html 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |