阿里云虚拟主机怎么做网站,附近电商培训班,网站设计包括什么,个人网站注册概述Swagger 是一个规范和完整的框架#xff0c;用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。在Ocelot网关中#xff0c;我们提供给前端的直接是Swagger#xff0c;如果Swagger分布在各个API中#xff0c;前端查看Swagger的时候非常不便#xff0c;Ocelot与Sw… 概述Swagger 是一个规范和完整的框架用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。在Ocelot网关中我们提供给前端的直接是Swagger如果Swagger分布在各个API中前端查看Swagger的时候非常不便Ocelot与Swagger的集成,方便在网关项目中统一查看各个服务的api文档。所以下面我们尝试把各个项目集合起来。主要代码实现1、客户端项目安装Swashbuckle.AspNetCore2、ConfigureServices配置 services.AddSwaggerGen(c {c.SwaggerDoc(v1, new OpenApiInfo { Title 客户端1 API, Version v1, Description # 客户端1 service api... });// Set the comments path for the Swagger JSON and UI.var xmlFile ${Assembly.GetExecutingAssembly().GetName().Name}.xml;var xmlPath Path.Combine(AppContext.BaseDirectory, xmlFile);c.IncludeXmlComments(xmlPath);
3、Configure配置 app.UseSwagger();app.UseSwaggerUI(c {c.SwaggerEndpoint(/swagger/v1/swagger.json, 客户端1 API V1);});
4、项目运行起来http://localhost:5000/swagger/index.html5、接下去是网关项目安装如下6、ConfigureServices配置 services.AddSwaggerGen(c {c.SwaggerDoc(v1,new OpenApiInfo { Title 网关test API, Version v1, Description # 网关test api... });});services.AddOcelot(Configuration).AddConsul();
7、Configure配置 app.UseSwagger();app.UseSwaggerUI(c {c.SwaggerEndpoint(/swagger/v1/swagger.json, 客户端1 API V1);// c.SwaggerEndpoint(/product/swagger/v1/swagger.json, Product API V1);});8、ocelot.json添加路由{DownstreamPathTemplate: /swagger/v1/swagger.json,DownstreamScheme: http,UpstreamPathTemplate: /swagger/v1/swagger.json,UpstreamHttpMethod: [ Get ],ServiceName: ProductService,LoadBalancerOptions: {Type: RoundRobin}},
9、最后项目运行起来 http://localhost:5003/swagger/index.html总结1、在运行过程的时候会报错如下内部异常 1:
Exception: Unable to start Ocelot, errors are: Unable to start Ocelot, errors are: Unable to start Ocelot because either a ReRoute or GlobalConfiguration are using ServiceDiscoveryOptions but no ServiceDiscoveryFinderDelegate has been registered in dependency injection container. Are you missing a package like Ocelot.Provider.Consul and services.AddConsul() or Ocelot.Provider.Eureka and services.AddEureka()?,Unable to start Ocelot, errors are: Unable to start Ocelot because either a ReRoute or GlobalConfiguration are using ServiceDiscoveryOptions but no ServiceDiscoveryFinderDelegate has been registered in dependency injection container. Are you missing a package like Ocelot.Provider.Consul and services.AddConsul() or Ocelot.Provider.Eureka and services.AddEureka()?
问题原因: 容器中缺少相应的服务
解决办法:install-package Ocelot.Provider.ConsulConfigureServices服务注册中修改为 : services.AddOcelot(Configuration).AddConsul();2、开源地址https://gitee.com/conanOpenSource_admin/Example