简体   繁体   中英

Microservice : How can you deploy your Microservices project using an API gateway on windows server

I have created two projects one the API Gateway that uses this JSON file:

{
  "Routes": [
    {
      "DownstreamPathTemplate": "/api/post",
      "DownstreamScheme": "https",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 44309
        }
      ],
      "UpstreamPathTemplate": "/gateway/post",
      "UpstreamHttpMethod": [
        "POST",
        "PUT",
        "GET"
      ]
    }
  ]
}

And on the Microservice side i have created a PostController that inherits Controllerbase, basically an ApiController:

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PostService.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

        namespace PostService.Controllers
        {
            [Route("api/[controller]")]
            [ApiController]
            public class PostController : ControllerBase
            {
                private readonly IPostRepository _postRepository;

                public PostController(IPostRepository postRepository)
                {
                    _postRepository = postRepository;
                }

                [HttpGet]
                public IActionResult Get()
                {
                    var posts = _postRepository.GetPosts();
                    return new OkObjectResult(posts);
                }

            }
        }

When I run the project Two browsers open, the first which is the ApiGateway and the other browser I run the in the address bar:

https://localhost:44342/gateway/post

The great thing is that the Get Method in my PostController get called and returns the data correctly.

But if I wanted to run or deploy these projects on windows server, how would this work on a windows server. What would I need to change in my ocelot.json file or does this remain the same or do O need to change these values to the remote IP and port:

    "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 44309
        }
      ],

So could someone point me in the right direction on how to deploy this on a windows server so that maybe a web or mobileapp can access the APIGateway?

To deploy it on a Windows Server you need to create two web sites on IIS, one for the API Gateway and one for the microservice. The API gateway site should listen on https port 443 and any IP address. The microservice can listen in any port of your choice, but there is no need to configure it for https because the communication between the gateway and the microservice is local to the server. The microservice should listen only on 127.0.0.1 and [::1] IP addresses because the microservice should only be accessed though the API gateway. So your ocelot.json can be:

{
  "Routes": [
    {
      "DownstreamPathTemplate": "/api/post",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 44309
        }
      ],
      "UpstreamPathTemplate": "/gateway/post",
      "UpstreamHttpMethod": [
        "POST",
        "PUT",
        "GET"
      ]
    }
  ]
}

Don't forget to configure the microservice site bindings for http on port 44309 or the port of your choice

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM