• .Net Core Ocelot网关使用 一


    首先建 立一个.net Core WebAPi 项目

    安装下面两个东西,注意版本

    配置项目Startup.cs文件

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Logging;
    using Microsoft.Extensions.Options;
    using Ocelot.DependencyInjection;
    using Ocelot.Middleware;
    using Ocelot.Provider.Consul;
    
    namespace WebOclot
    {
        public class Startup
        {
            public Startup(IConfiguration configuration)
            {
                Configuration = configuration;
            }
    
            public IConfiguration Configuration { get; }
    
            // This method gets called by the runtime. Use this method to add services to the container.
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddOcelot().AddConsul();
                // 删除掉此处所有默认的配置
    
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IHostingEnvironment env)
            {
                app.UseOcelot();
                // 删除掉此处所有默认的配置
            }
        }
    }

    配置Ocelot配置文件

    {
      "ReRoutes": [
        {
          "DownstreamPathTemplate": "/{url}",   // 服务地址--URL变量
          "DownstreamScheme": "http",
          "UpstreamPathTemplate": "/sopweb/{url}",     // 网关地址--url变量
          "UpstreamHttpMethod": [ "Get", "Post" ],
          "UseServiceDiscovery": true,   // 是否使用服务发现
          "ServiceName": "sopweb",     // consul 服务名称
          "LoadBalancerOptions": {
            "Type": "RoundRobin"     // 轮询 方式   LeastConnection  最少的连接数服务器   NoLoadBalance  不负载均衡
          }
        }
      ],
      "GlobalConfiguration": {
        "BaseUrl": "http://localhost:1140",   // 网关对外地址
        "ServiceDiscoveryProvider": {
          "Host": "47.92.27.244",   // 微服务主节点地址
          "Port": 8500,             // 微服务主节点端口号  
          "Type": "Consul"          // 由Consul提供服务发现,每次请求Consul
        }
      }
    
    }

    在项目中加载文件Program.cs  

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.Logging;
    
    namespace WebOclot
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                CreateWebHostBuilder(args)
                .UseUrls("http://*:1140")
                .Build().Run();
            }
    
            public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
                WebHost.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration(conf=> {
                    conf.AddJsonFile("OcelotSetting.json", optional: false,
                       reloadOnChange: true);
                })
                    // .UseUrls("http://*:1140")  // 用上面的设置端口吧
                    .UseStartup<Startup>();
        }
    }

    然后就可以访问服务sopweb了,只不过是用本地 http://localhost:1140访问的喔

  • 相关阅读:
    30 Day Challenge Day 20 | Leetcode 938. Range Sum of BST
    30 Day Challenge Day 20 | Leetcode 124. Binary Tree Maximum Path Sum
    30 Day Challenge Day 20 | Leetcode 94. Binary Tree Inorder Traversal
    idea2019版本破解
    xml文件时第一行无缘无故报错
    activeMQ的基本使用
    activeMQ的安装(Linux下)
    redis的基本用法
    redis安装(Linux下)
    redis安装(window下)
  • 原文地址:https://www.cnblogs.com/yingger/p/13543927.html
Copyright © 2020-2023  润新知