1. Create three new ones first. NET Core 3.1 API project ApigetWay: will be used as a gateway. You can remove unnecessary Controller folders and entity classes

ApiserviceOne: ServiceOne, which will be used by the API

Apiservicetwo: Service 2, for API use

Add a cross-domain configuration to starup. cs for each of the three projects

public void ConfigureServices(IServiceCollection services)

{ services.AddControllers(); // Configure cross-domain processing to allow all sources:  services.AddCors(options => { options.AddPolicy("all", Builder => {Builder.allowAnyOrigin () // Allows host access from any source.AllowAnyMethod().AllowAnyHeader(); }); }); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } #region app.useCors ("all"); #endregion app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }

1. The Ocelot 15.06 package is downloaded from NuGet. The version is too new and may be unstable

2. Then add some configuration to the project and create a configuration.json file in the root directory

Delete the two curly braces and copy and paste the following code

{

“ReRoutes”: [

//{// "UpStreamPathTemplate ": "/ API /{controller}/{action}", // request path template //" UpStreamHttpMethod ": ["Get", "POST"], // request method array // "downStreamPathTemplate ": "/ API /{controller}/{action}", // Downstream request address template // "DownstreamScheme": // "DownstreamHostAndPorts": [// downstream address and port // {// "host": "localhost", // "port": 5003 // }, // { // "host": "localhost", // "port": 5001 // } // ], // "LoadBalancerOptions": {// Load balancing RoundRobin(polling)/ leastConnection (minimum number of connections)/CookieStickySessions(same Sessions or cookies sent to same address)/NoLoadBalancer(no load used)/ / "Type": "RoundRobin" //} //}, {// ApiserviceOne service "downStreamPathTemplate ": "/api/{Controller}/{action}", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 5196 } ], "UpstreamPathTemplate": "/One/{Controller}/{action}", "UpstreamHttpMethod": ["GET", "POST"]}, {// ApiserviceTwo service "downstreamPathTemplate ": "/ API /{controller}/{action}"," downstreamScheme ": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 5197 } ], "UpstreamPathTemplate": "/Two/{controller}/{action}", "UpstreamHttpMethod": [ "GET", "POST" ] }

]}

Note that in the configuration, the ports for service 1 and service 2 are 5196 and 5197, respectively, so you need to modify the other two projects. Change the startup ports in the ApiserviceOne and ApiserviceTwo projects, in launchSettings.json

Remove all other unnecessary configuration from the three project configuration, leaving only the startup configuration on the line, configure the service port in turn, then configure the gateway project port, you can configure any configuration, here I give 5195.

You then need to modify the route in the controller for both services according to the template for the downstream path in the configuration.json file

3. To register and use Ocelot in the gateway project’s starup. cs, add the following code and import the appropriate namespace when you add it

public void ConfigureServices(IServiceCollection services)

{ services.AddControllers(); services.AddOcelot(new ConfigurationBuilder().AddJsonFile("configuration.json", true, true).Build()); // Configure cross-domain processing to allow all sources:  services.AddCors(options => { options.AddPolicy("all", Builder => {Builder.allowAnyOrigin () // Allows host access from any source.AllowAnyMethod().AllowAnyHeader(); }); }); }

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

{ if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } #region app.useCors ("all"); #endregion app.UseRouting(); app.UseOcelot(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }

4. The postman test

Start all three projects first

The Postman test is the gateway’s address port, 5195. I deleted the gateway’s controller in the previous step, so the test succeeds. The gateway successfully forwards the request to service 1. You can write a test interface by yourself.

I added a test action to each of the two services and tested it again

Well, the test proved that there is no problem, the article involved in the knowledge that do not understand, you need to own Baidu, here will not be introduced.

Original link: Original link