|
3 | 3 | [](https://github.com/thangchung/GrpcJsonTranscoder/blob/master/LICENSE)
|
4 | 4 | [](https://www.nuget.org/packages?q=GrpcJsonTranscoder)
|
5 | 5 |
|
6 |
| -This is a filter that allows a RESTful JSON API client (Ocelot Gateway) to send requests to .NET Web API (Aggregation Service) over HTTP and get proxied to a gRPC service (on behind). |
| 6 | +This is a filter that allows a RESTful JSON API client (Ocelot Gateway) to send requests to .NET Web API (Aggregator Service) over HTTP and get proxied to a gRPC service (on the downstream). |
7 | 7 |
|
8 | 8 | This project is inspired by [grpc-gateway](https://github.com/grpc-ecosystem/grpc-gateway) which is totally for golang, [grpc-dynamic-gateway](https://github.com/konsumer/grpc-dynamic-gateway) is for nodejs. And especially, [Envoy gRPC-JSON transcoder](https://www.envoyproxy.io/docs/envoy/latest/configuration/http_filters/grpc_json_transcoder_filter) is the best of transcoding in this area, but it is only on the infrastructure level. You also can use it just like my project used at [coolstore-microservices](https://github.com/vietnam-devs/coolstore-microservices/blob/master/deploys/dockers/envoy-proxy/envoy.yaml).
|
9 | 9 |
|
@@ -33,10 +33,15 @@ $ start.sh # I haven't done it yet :p
|
33 | 33 |
|
34 | 34 | > In the mean time, we open up the visual studio, run multiple projects included OcelotGateway, AggregationRestApi, ProductCatalogGrpcServer and GreatGrpcServer
|
35 | 35 |
|
36 |
| -- OcelotGateway (.NET Core 2.2): http://localhost:5000 |
37 |
| -- AggregationRestApi (.NET Core 3.0): http://localhost:5001 |
38 |
| -- ProductCatalogGrpcServer (.NET Core 3.0): http://localhost:5002 |
39 |
| -- GreatGrpcServer (.NET Core 3.0): http://localhost:5003 |
| 36 | +- OcelotGateway (.NET Core 3.1): |
| 37 | + - REST: http://localhost:5000 |
| 38 | +- WeatherServer (.NET Core 3.1): |
| 39 | + - REST: http://localhost:5001 |
| 40 | +- ProductCatalogGrpcServer (.NET Core 3.1): |
| 41 | + - REST: http://localhost:5002 |
| 42 | + - gRPC: http://localhost:50022 |
| 43 | +- GreatGrpcServer (.NET Core 3.1): |
| 44 | + - gRPC: http://localhost:5003 |
40 | 45 |
|
41 | 46 | Test it as below:
|
42 | 47 |
|
@@ -64,12 +69,14 @@ $ curl -X GET -H 'content-type: application/json' -k http://localhost:5000/weath
|
64 | 69 | $ [{"date":"2019-08-17T18:34:41.1090164+07:00","temperatureC":-6,"temperatureF":22,"summary":"Sweltering"},{"date":"2019-08-18T18:34:41.1090371+07:00","temperatureC":27,"temperatureF":80,"summary":"Hot"},{"date":"2019-08-19T18:34:41.1090499+07:00","temperatureC":33,"temperatureF":91,"summary":"Balmy"},{"date":"2019-08-20T18:34:41.1090617+07:00","temperatureC":-14,"temperatureF":7,"summary":"Chilly"},{"date":"2019-08-21T18:34:41.1090743+07:00","temperatureC":22,"temperatureF":71,"summary":"Hot"}]
|
65 | 70 | ```
|
66 | 71 |
|
| 72 | +**Notes**: |
| 73 | +- REST method: `content-type: application/json` is for REST method on the downstream services. |
| 74 | +- gRPC method: `content-type: application/grpc` is really important if you call to `gRPC endpoint` on the downstream services. |
| 75 | + |
67 | 76 | ## How to understand it!
|
68 | 77 |
|
69 | 78 | The project aims to .NET community and its ecosystem which leverage the power of [Ocelot Gateway](https://github.com/ThreeMammals/Ocelot) which is very powerful in the gateway components were used by various of companies and sample source code when we try to adopt the microservices architecture project.
|
70 | 79 |
|
71 |
| -- Option 1: Use directly with Ocelot |
72 |
| - |
73 | 80 | 
|
74 | 81 |
|
75 | 82 | That's quite simple with only a few steps to make it work :)
|
@@ -146,123 +153,6 @@ var configuration = new OcelotPipelineConfiguration
|
146 | 153 | app.UseOcelot(configuration).Wait();
|
147 | 154 | ```
|
148 | 155 |
|
149 |
| -- Option 2: Use via an aggregation layer |
150 |
| - |
151 |
| - |
152 |
| - |
153 |
| -We will normally use Ocelot configuration for the transcode process, the main parser and transformation processes are only happening at aggregation service level so that you will easy to upgrade Ocelot in case we need, but not affect to the grpc-json transcode seats in the aggregation service. |
154 |
| - |
155 |
| -```json |
156 |
| -// ocelot.json |
157 |
| -{ |
158 |
| - "ReRoutes": [ |
159 |
| - { |
160 |
| - "UpstreamPathTemplate": "/say/{name}", |
161 |
| - "UpstreamHttpMethod": [ "Get" ], |
162 |
| - "DownstreamPathTemplate": "/Greet.Greeter/SayHello", |
163 |
| - "DownstreamScheme": "http", |
164 |
| - "DownstreamHostAndPorts": [ |
165 |
| - { |
166 |
| - "Host": "localhost", |
167 |
| - "Port": 5001 |
168 |
| - } |
169 |
| - ] |
170 |
| - }, |
171 |
| - { |
172 |
| - "UpstreamPathTemplate": "/products", |
173 |
| - "UpstreamHttpMethod": [ "Get" ], |
174 |
| - "DownstreamPathTemplate": "/ProductCatalog.Product/GetProducts", |
175 |
| - "DownstreamScheme": "http", |
176 |
| - "DownstreamHostAndPorts": [ |
177 |
| - { |
178 |
| - "Host": "localhost", |
179 |
| - "Port": 5001 |
180 |
| - } |
181 |
| - ] |
182 |
| - }, |
183 |
| - { |
184 |
| - "UpstreamPathTemplate": "/products", |
185 |
| - "UpstreamHttpMethod": [ "Post" ], |
186 |
| - "DownstreamPathTemplate": "/ProductCatalog.Product/CreateProduct", |
187 |
| - "DownstreamScheme": "http", |
188 |
| - "DownstreamHostAndPorts": [ |
189 |
| - { |
190 |
| - "Host": "localhost", |
191 |
| - "Port": 5001 |
192 |
| - } |
193 |
| - ] |
194 |
| - } |
195 |
| - ], |
196 |
| - "GlobalConfiguration": { |
197 |
| - "BaseUrl": "http://localhost:5000" |
198 |
| - } |
199 |
| -} |
200 |
| -``` |
201 |
| - |
202 |
| -and, |
203 |
| - |
204 |
| -```csharp |
205 |
| -// Program.cs |
206 |
| -var configuration = new OcelotPipelineConfiguration |
207 |
| -{ |
208 |
| - PreQueryStringBuilderMiddleware = async (ctx, next) => |
209 |
| - { |
210 |
| - var routes = ctx.TemplatePlaceholderNameAndValues; |
211 |
| - ctx.DownstreamRequest.Headers.Add( |
212 |
| - "x-grpc-routes", |
213 |
| - JsonConvert.SerializeObject(routes.Select(x => new NameAndValue { Name = x.Name, Value = x.Value }))); |
214 |
| - await next.Invoke(); |
215 |
| - } |
216 |
| -}; |
217 |
| - |
218 |
| -app.UseOcelot(configuration).Wait(); |
219 |
| -``` |
220 |
| - |
221 |
| -More at https://github.com/thangchung/GrpcJsonTranscoder/tree/master/samples/OcelotGateway |
222 |
| - |
223 |
| -Then we only put some of the json configurations into `appsettings.json` inside aggregation service to point it to other gRPC services we need. |
224 |
| - |
225 |
| -```json |
226 |
| -// appsettings.json |
227 |
| -"GrpcJsonTranscoder": { |
228 |
| - "GrpcMappers": [ |
229 |
| - { |
230 |
| - "GrpcMethod": "/Greet.Greeter/SayHello", |
231 |
| - "GrpcHost": "127.0.0.1:5003" |
232 |
| - }, |
233 |
| - { |
234 |
| - "GrpcMethod": "/ProductCatalog.Product/GetProducts", |
235 |
| - "GrpcHost": "127.0.0.1:5002" |
236 |
| - }, |
237 |
| - { |
238 |
| - "GrpcMethod": "/ProductCatalog.Product/CreateProduct", |
239 |
| - "GrpcHost": "127.0.0.1:5002" |
240 |
| - } |
241 |
| - ] |
242 |
| -} |
243 |
| -``` |
244 |
| - |
245 |
| -```csharp |
246 |
| -// Startup.cs |
247 |
| -public void ConfigureServices(IServiceCollection services) |
248 |
| -{ |
249 |
| - ... |
250 |
| - |
251 |
| - services.AddGrpcJsonTranscoder(() => new GrpcAssemblyResolver().ConfigGrpcAssembly(typeof(Greeter.GreeterClient).Assembly)); |
252 |
| -} |
253 |
| - |
254 |
| -public void Configure(IApplicationBuilder app, IWebHostEnvironment env) |
255 |
| -{ |
256 |
| - ... |
257 |
| - |
258 |
| - app.UseGrpcJsonTranscoder(); |
259 |
| - |
260 |
| - ... |
261 |
| -} |
262 |
| -``` |
263 |
| - |
264 |
| -More at https://github.com/thangchung/GrpcJsonTranscoder/tree/master/samples/AggregationRestApi |
265 |
| - |
266 | 156 | ### **Don't believe what I said. Try it!**
|
267 | 157 |
|
268 | 158 | > We haven't tested it with stream and full-duplex transport protocols yet. So we feel free to contribute by the .NET community.
|
|
0 commit comments