I need to integrate .net core gateway with rabbit mq.
Iam using angular 10 as client.
The architecture is=> Client(Angular 10) -> Gateway(Ocelot) -> Rabbitmq with easy netq -> .Net core Console app.
.Net core gateway route structure is given below
{
"Routes": [
{
"DownstreamPathTemplate": "/api/product",
"DownstreamScheme": "https",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 44337
}
],
"UpstreamPathTemplate": "/gateway/product",
"UpstreamHttpMethod": [ "POST", "PUT", "GET" ]
},
{
"DownstreamPathTemplate": "/api/product/{id}",
"DownstreamScheme": "https",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 44337
}
],
"UpstreamPathTemplate": "/gateway/product/{id}",
"UpstreamHttpMethod": [ "GET", "DELETE" ]
}
],
"GlobalConfiguration": {
"BaseUrl": "http://localhost:44382"
}
}
We know that rabbit mq pattern is messaging with publish and subscribe method.
My question is do i need to add the code of consumer(subscribe) section of rabbit mq in gatway api and publish code in console app?
Related
I have ASP.NET Web API microservices and currently I am implementing API gateway for routing and authentication.
From my 3 APIs, one of them is for User (crud) operations, so I decided to use it for a gateway, as well. And while I was implementing the routing I came across a strange problem.
With this (test) configuration (again, using my User API running on port 7268)
{
"Routes": [
//User API
{
"UpstreamPathTemplate": "/User/Login",
"UpstreamHttpMethod": [ "Post" ],
"DownstreamScheme": "https",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 7124
}
],
"DownstreamPathTemplate": "/api/Product",
"DownstreamHttpMethod": "Get"
}
],
"GlobalConfiguration": {
"BaseUrl": "https://localhost:7268"
}
}
... I am able to get list of products from the Product API on port 7124, when I navigate to /User/Login.
Now if I change the configuration to access any endpoint of the same User API (which matches the BaseUrl path), it does not work. Example:
{
"Routes": [
//User API
{
"UpstreamPathTemplate": "/User/Login",
"UpstreamHttpMethod": [ "Post" ],
"DownstreamScheme": "https",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 7268
}
],
"DownstreamPathTemplate": "/api/User/Login",
"DownstreamHttpMethod": "Post"
}
],
"GlobalConfiguration": {
"BaseUrl": "https://localhost:7268"
}
}
It gives me 404 Not Found and the following message in the console:
Is this because I am using an existing API to re-route itself and it is expected behavior, or I am doing something wrong?
{
"DownstreamPathTemplate": "/ProceedToBuy/PostWishList",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 8003
}
],
"UpstreamPathTemplate": "/AddToWishlist",
"UpstreamHttpMethod": [ "POST" ]
}
I want to send json body with this route request.
Actually you don't have to route json the request will automatically route to the downstream path, we can just directly make request to upstream path with json and it'll work!
I'm creating a project with microservices and now I want to be able to test the API gateway that I created as an orchestrator with ocelot, but I can't test it through the browser, the API can't access it, can someone help?
ocelot.json:
{
"Routes": [
// Billing.API
{
"DownstreamPathTemplate": "/api/v1/Billing",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "billingapi",
"Port": "80"
}
],
"UpstreamPathTemplate": "/Billing",
"UpstreamHttpMethod": [ "GET" ]
}
]
}
When I run the project and in the browser URL I pass /api/v1/Billing
as I passed the DownstreamPathTemplate property.
Nothing is returned to me.
I would suggest to first try general template and see if it works:
{
"DownstreamPathTemplate": "/{everything}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "billingapi",
"Port": 80,
}
],
"UpstreamPathTemplate": "/billingapi/{everything}",
"UpstreamHttpMethod": [ "Get", "Post" ]
}
From the Ocelot documentation about Downstream:
The DownstreamPathTemplate, DownstreamScheme and DownstreamHostAndPorts define the URL that a request will be forwarded to.
And for the Upstream:
The UpstreamPathTemplate is the URL that Ocelot will use to identify which DownstreamPathTemplate to use for a given request.
You should pass UpstreamPathTemplate URL on the browser to route billing API. In your case, the request HTTP://<gateway.address>/Billings will be routed to HTTP://billingapi/api/v1/Billing.
I have implemented Ocelot API gateway in my project which was working fine for 'GET' requests but when I tried to send 'POST' request I am getting the error:
previousRequestId: no previous request id, message: Error Code: UnableToFindDownstreamRouteError Message: Failed to match ReRoute configuration for upstream path: /api/patient/CreateAppointment, verb: POST. errors found in ResponderMiddleware. Setting error response for request path:/api/patient/CreateAppointment, request method: POST
Following is my ocelot.json:
{
"Routes": [
{
"DownstreamPathTemplate": "/api/patient/{everything}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "patientservice",
"Port": 81
}
],
"UpstreamPathTemplate": "/api/patient/{everything}",
"UpstreamHttpMethod": [ "GET", "POST" ],
"UpstreamHost": "*"
},
{
"DownstreamPathTemplate": "/api/actor",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "postgresqldapper",
"Port": 5001
}
],
"UpstreamPathTemplate": "/api/actor",
"UpstreamHttpMethod": [ "GET" ]
},
{
"DownstreamPathTemplate": "/api/product/{everything}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "productservice",
"Port": 80
}
],
"UpstreamPathTemplate": "/api/product/{everything}",
"UpstreamHttpMethod": [ "GET" ]
},
{
"DownstreamPathTemplate": "/api/weatherforecast",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "postgresqldapper",
"Port": 5001
}
],
"UpstreamPathTemplate": "/api/weatherforecast",
"UpstreamHttpMethod": [ "GET" ]
},
{
"DownstreamPathTemplate": "/api/user",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "loginservicedapr",
"Port": 80
}
],
"UpstreamPathTemplate": "/api/user",
"UpstreamHttpMethod": [ "GET" ]
},
{
"DownstreamPathTemplate": "/api/user/{everything}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "loginservicedapr",
"Port": 5001
}
],
"UpstreamPathTemplate": "/api/user/{id}",
"UpstreamHttpMethod": [ "GET" ]
}
],
"GlobalConfiguration": {
"RequestIdKey": "OcRequestId",
"AdministrationPath": "/administration"
}
}
The API to which I am doing the 'POST' request is working fine for the 'POST' request when invoked directly from Swagger or Postman.
Please let me know what I should change in ocelot.json file so that the 'POST' request can through?
Your error is effectively saying that Ocelot is unable to route requests to
POST /api/patient/CreateAppointment
In your screenshot, your curl command (which is working) is a request to:
POST /api/patient
Your /{everything} path suffix is telling Ocelot that whatever suffix you call into the gateway with will be present on the downstream service.
My theory is that you have not defined a CreateAppointment endpoint operation on your downstream patient service API. Once you define this path on your service, your /api/patient/{everything} route mapping should work fine.
I'm using MMLib.SwaggerForOcelot for gateway. in .Net core
I got this error. Any idea?
My Upstream and DownStream:
"SwaggerEndPoints": [
{
"Key": "skIndustry",
"Config": [
{
"Name": "Industry API",
"Version": "v1",
"Url": "http://industryapi:80/swagger/v1/swagger.json"
}
]
}
]
My ReRoutes:
"ReRoutes": [
{
"DownstreamPathTemplate": "/{everything}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "industryapi/",
"Port": 80
}
],
"UpstreamPathTemplate": "/Industry/{everything}",
"UpstreamHttpMethod": [ "POST", "PUT", "GET" ],
"SwaggerKey": "skIndustry"
}
]
Thanks,
Croos.
Please try new version 1.3.1.
This PR probably solve similar problem.