{"id":13525385,"url":"https://github.com/ShaoHans/OcelotSample","last_synced_at":"2025-04-01T04:32:03.188Z","repository":{"id":193294523,"uuid":"136409118","full_name":"ShaoHans/OcelotSample","owner":"ShaoHans","description":"Ocelot使用案例，结合IdentityServer4进行鉴权，结合Consul进行服务治理","archived":false,"fork":false,"pushed_at":"2019-01-22T14:54:26.000Z","size":542,"stargazers_count":59,"open_issues_count":0,"forks_count":16,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-08-02T06:17:38.471Z","etag":null,"topics":["apigateway","consul","identityserver4","ocelotsample"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ShaoHans.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2018-06-07T02:10:16.000Z","updated_at":"2023-04-26T03:17:17.000Z","dependencies_parsed_at":"2023-09-07T15:25:31.720Z","dependency_job_id":null,"html_url":"https://github.com/ShaoHans/OcelotSample","commit_stats":null,"previous_names":["shaohans/ocelotsample"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ShaoHans%2FOcelotSample","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ShaoHans%2FOcelotSample/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ShaoHans%2FOcelotSample/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ShaoHans%2FOcelotSample/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ShaoHans","download_url":"https://codeload.github.com/ShaoHans/OcelotSample/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":222698227,"owners_count":17024879,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["apigateway","consul","identityserver4","ocelotsample"],"created_at":"2024-08-01T06:01:18.236Z","updated_at":"2025-04-01T04:32:03.182Z","avatar_url":"https://github.com/ShaoHans.png","language":"C#","funding_links":[],"categories":["Sample Projects"],"sub_categories":[],"readme":"# Ocelot使用案例\n### 解决方案中各项目介绍\n* APIGatewayByOcelot：该项目的作用是API网关，采用 [Ocelot](https://github.com/ThreeMammals/Ocelot) 组件\n* ApiOne：该项目为下游API服务测试项目，需使用 [Consul](https://github.com/hashicorp/consul) 组件注册自己\n* ApiTwo：该项目为下游API服务测试项目，需使用 [Consul](https://github.com/hashicorp/consul) 组件注册自己\n* Ids4Center.Mvc：该项目是服务认证授权中心，使用 [IdentityServer4](https://github.com/IdentityServer/IdentityServer4) 组件\n* AccessApiOne：客户端项目，访问需要授权的API接口\n\n### Consul服务治理\n1. [下载地址](https://www.consul.io/downloads.html)\n2. 解压下载完成之后的压缩包，通过cmd命令行定位到consul.exe所在的目录，运行命令：consul.exe agent --dev\n3. 通过浏览器访问：http://localhost:8500/ui ，此Consul管理端可以查看注册的服务信息\n\n### 创建下游API服务项目\n1. 创建两个Asp.net Core WebApi项目\n2. 下载 Consul nuget包\n3. 在appsettings.json文件中配置Consul服务端信息\n   ```\n\t   /*服务治理第三方组件Consul相关配置参数*/\n\t  \"ServiceDiscovery\": {\n\t\t\"ServiceName\": \"ApiOne\", //本WebApi项目的服务名称，可以随便取名，将显示Consul的管理页面上，届时通过该服务名查找可用的服务站点\n\t\t\"Consul\": {\n\t\t  \"HttpEndpoint\": \"http://127.0.0.1:8500\",\n\t\t  \"TcpEndpoint\": {\n\t\t\t\"Address\": \"127.0.0.1\",\n\t\t\t\"Port\": 8600\n\t\t  }\n\t\t}\n\t  }\n   ```\n4. 编写服务注册代码，向Consul注册本项目提供的API服务，详见 RegisterToConsulExtension.cs 代码\n\n### APIGatewayByOcelot网关项目\n1. 下载 Ocelot nuget包，已经包含Consul的服务发现功能\n2. 添加Ocelot.json配置文件，配置参数详见文件。需要注意的是，如果下游API服务需要授权才能访问，只要添加如下配置参数，并在StartUp类中编写相关代码\n```\n\"AuthenticationOptions\": {\n\t\"AuthenticationProviderKey\": \"TestKey\",\n\t\"AllowedScopes\": []\n  }\n```\n\n### Ids4Center.Mvc认证授权中心\n1. 下载 IdentityServer4 nuget包\n2. 配置客户端和APIResource参数\n\n### AccessApiOne客户端\n1. 下载IdentityModel nuget包\n2. 由于ApiOne应用的API接口是需要授权后才能访问，不能通过浏览器直接访问，所以单独下一个客户端项目访问\n\n### 运行解决方案\n1. 必须先运行Consul，cmd命令：consul.exe agent --dev\n2. 启动项目ApiOne和ApiTwo，你可以打开项目所在目录通过命令行命令，每个项目都可以运行多个实例，如：打开ApiOne项目根目录，直接运行如下命令\t\n\t\u003e dotnet run --ip 127.0.0.1 --port 8000  \n\t\u003e dotnet run --ip 127.0.0.1 --port 8001  \n\t这样就启动了ApiOne项目的两个实例，能接受ip和port参数是因为自己写的代码才支持这样运行，同理可以允许多个ApiTwo项目的实例\n3. 启动Ids4Center.Mvc项目，该项目的端口写死了12345，不需要改动，因为客户端项目AccessApiOne也是写死了这个端口\n4. 启动APIGatewayByOcelot网关项目，端口默认5000，SSL端口是5001。此时可以通过浏览器访问:https://localhost:5001/two/values 可以看到返回结果，\n   但访问 https://localhost:5001/one/values 时并未看到任何结果，通过F12查看请求，可以看到服务端返回401（未授权），若要访问该接口，可以启动\n   客户端AccessApiOne项目查看结果。\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FShaoHans%2FOcelotSample","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FShaoHans%2FOcelotSample","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FShaoHans%2FOcelotSample/lists"}