【发布时间】:2022-01-26 02:15:16
【问题描述】:
我创建了两个项目,一个是使用此 JSON 文件的 API 网关:
{
"Routes": [
{
"DownstreamPathTemplate": "/api/post",
"DownstreamScheme": "https",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 44309
}
],
"UpstreamPathTemplate": "/gateway/post",
"UpstreamHttpMethod": [
"POST",
"PUT",
"GET"
]
}
]
}
在微服务方面,我创建了一个继承 Controllerbase 的 PostController,基本上是一个 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);
}
}
}
当我运行项目时,打开了两个浏览器,第一个是 ApiGateway,另一个浏览器是微服务在 localhost 的 44309 端口上运行的地方。我在 api 网关的地址栏中运行它:
https://localhost:44342/gateway/post
很棒的是我的 PostController 中的 Get 方法被调用并正确返回数据。
但是,如果我想在 windows 服务器上运行或部署这些项目,这将如何在 windows 服务器上工作。我需要在我的 ocelot.json 文件中更改什么,或者保持不变还是需要将这些值更改为远程 IP 和端口:
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 44309
}
],
那么有人可以为我指出如何在 Windows 服务器上部署它以便 Web 或移动应用程序可以访问 APIGateway 的正确方向吗?
【问题讨论】:
标签: c# microservices webapi