Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build ocelot Gateway and consul Cluster from scratch

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Today, I will talk to you about how to build ocelot gateways and consul clusters from scratch. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something from this article.

Introduction

There are several key technologies in microservices, among which gateway and service discovery and service registration complement each other.

First, explain a few necessary terms.

Gateway Gateway (API GW / API Gateway), as its name implies, is a unified entrance for enterprise IT to provide external access to internal interface services on the system boundary, which simplifies the tedious configuration of external tasks due to the cooperation of multiple services. Gateway components have Kong,ocelot

Service discovery: access the internal micro-services through the gateway, and the process for the gateway to find the required services is called service discovery.

Service registration: since there is service discovery, the premise is to "enter" the required services in advance, and this entry process is called service registration. Service registration profile (human flesh is not recommended), or service registration components such as Consul or Eureka (recommended)

Set up Consul cluster (Windows)

Download the Consul program on the official website, https://www.consul.io/downloads.html

Download it to an executable file called Consul.exe

There are two proxy modes for Consul, one is server and the other is client. It is recommended that more than 3 Server devices are highly available, but not too many. Too much will cause pressure on data synchronization between clusters, and there is no limit on the number of client.

A leader is selected among multiple server clients, and when the leader of a server goes down, a new leader is selected by "voting" from other server sides.

Practice

Here, server, we use two experiments.

192.168.74.55

192.168.74.54

1 Client

192.168.74.161

There are two ways to start consul, one is the command line, the other is the configuration file.

Start the server side of a consul from the command line

Consul agent-server-ui-bootstrap-expect 2-data-dir opt/consul/data-node ServerMaster-bind 192.168.74.55-client 192.168.74.55

Description of key parameters

-server:server mode startup

-ui: open the ui interface (GUI graphical interface operation is provided inside consul.exe)

-the cluster takes effect when the number of bootstrap-expect 2:server reaches 2

-the file path generated by data-dir:consul (consul itself will generate the location of the data storage)

-node: this node name

-bind: cluster internal communication address. Default is 0.0.0.0.

-client: the mailing address bound by this node

The above are only key parameters, and the following is the complete description of the parameters:

However, command startup is tedious, so the following configuration file is recommended

Create a server.json configuration file under the same folder in consul

To start quickly, create another bat batch file, runconsul.bat

Consul agent-config-dir server.jsonpause

Double-click runconsul.bat to start consul

Open a server on the 192.168.74.54 server to continue the above operation.

Start in command mode

Consul agent-server-ui-data-dir opt/consul/data-node Server01-bind 192.168.74.54-client 192.168.74.54-join=192.168.74.55

-join adds 192.168.74.54 to the 192.168.74.55 server

Configuration file method:

Open a client of consul on the 192.168.74.161 server

Command method:

Consul agent-ui-data-dir opt/consul/data-node ServerSlave-bind 192.168.74.161-client 192.168.74.161-join 192.168.74.55

Configuration file method:

Effect.

A simple Consul cluster has been successfully built here. As long as you visit any of the three servers, you can synchronize data. Demonstrate:

Netcore Integrated Consul Service Registration

Key code for service registration

First, create a new class library for ConsulClient

The components required for ConsulRegister.csproj are as follows:

Create a new .netcore webapi project WebA, and reference the ConsulRegister project

Add the Consul service to the Startup.cs file in the WebA project

Add the following Consul server configuration to the appsettings.json configuration file of the WebA project

Even if the service registration is completed here,

Ocelot Gateway Construction

Next, continue Ocelot to implement service discovery with the help of Consul.

Create a new project Ocelot.Gateway

Add the following dependencies to Ocelot.Gateway.csproj:

Create a new ocelot.json file

Modify the Startup.cs file as follows:

After the release of WebA, two copies are copied and started separately.

Dotnet WebA.dll-urls= "http://0.0.0.0:2001"

Dotnet WebA.dll-urls= "http://0.0.0.0:2002"

Here is equivalent to a simple cluster of 2001 and 2002 programs.

You can find http://192.168.74.161:2002/HealthCheck call information in the log:

This is actually a call made by consul for a health check.

After starting multiple programs, open the browser and open the Consuld interface, you will find that two services are registered.

Here the service registration and discovery of the ocelot gateway and consul are considered preliminary integration.

If the build environment is windows, you can make consul a windwos service.

Sc create "ConsulServer" binPath= "F:\ XXX\ consul.exe agent-config-dir XXX.json"

If the production environment is linux, you can make a daemon with the help of systemd.

After reading the above, do you have any further understanding of how to build ocelot gateways and consul clusters from scratch? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report