In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to make a Docker image for Ocelot. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
In the micro-service architecture, ApiGateway acts as a link between the past and the future. It can be classified not only according to the client, but also according to the functional business, but also plays a good interface role for service invocation. At present, the function of ApiGateway (payment function) is basically provided in all clouds, which can be configured through SDK or online.
Zuul and Kong are both famous in the Java system.
In the .net system, it is currently more popular (it has been 1000+stars in just 1 year).
Ocelot, this is a very excellent Api gateway open source project based on .net Core. Our team leader also participated in the development. Before the Chinese New year, it was included in the Microsoft eShopMicro Service Architecture Demo project. As its Api gateway, it is currently in the process of integration. If you are interested in paying attention to the new Ocelot Branch in the project.
The basic usage has been written by bloggers in the park, but the content is the previous version, the new version is slightly different, it is recommended that you read the documentation. Through the documentation, we know that Ocelot is configured through a json file, so when in use we only need to modify the json file, each time for different ApiGateway to create a different project is more troublesome, So, today on how to make Ocelot into a Docker image, so that when using only need to enter a docker instruction.
Today's Dockerfile I Fork the Ocelot project in my own Branch, directly is the project reference, this is just an example, you can re-create a special project, through Nuget management to add references to Ocelot.
Create Dockerfile code
Dockerfile is just a text file, each line represents a layer of Docker image, and each line is composed of commands and parameters. By writing simple commands, we can use docker tools to generate docker images.
First of all, you need to create a Dockerfile in the project, please remember to put your Dockerfile in the sln directory, because the build environment of the Dockerfile file is according to the directory of your file, remember that it took the blogger N genius to discover this retarded problem. Docker command is case sensitive to file names.
Go directly to the code:
FROM microsoft/aspnetcore:2.0 AS base # based on asp.net core 2.0 image WORKDIR / appEXPOSE 8 images first use the asp.net core build image, and then copy the project to the / src directory FROM microsoft/aspnetcore-build:2.0 AS buildWORKDIR / srcCOPY *. Sln. / COPY demos/ApiGateway.Web/ApiGateway.Web.csproj demos/ApiGateway.Web/COPY src/Ocelot/Ocelot.csproj src/Ocelot/RUN dotnet restoreCOPY. WORKDIR / src/demos/ApiGateway.WebRUN dotnet add package BuildBundlerMinifier# added support for bundle here, you don't have to use it, because I changed the style, so I added this here. After RUN dotnet restoreRUN dotnet build-c Release-o / app# is compiled, we release it and copy it directly to the app directory FROM build AS publishRUN dotnet publish-c Release-o / app#. Set the app directory to the working directory FROM base AS finalWORKDIR / appCOPY-- from=publish / app. # Mount / app/configurations directory VOLUME / app/ConfigurationsENTRYPOINT ["dotnet", "ApiGateway.Web.dll"]
This is my Dockerfile file. Instead of putting the configuration.json file in the project root, I created a separate Configurations directory so that I can mount the directory and share data in multiple containers.
Generate Docker image
If you have Docker installed on your machine, you can run it from the root directory of the project through the command tool:
Docker build-t myocelot:v1.
Pay attention to what happens after this order. Don't forget.
If Docker is not installed on your machine, it doesn't matter. You can generate it through Docker repository or Aliyun container management. They are all free. Bloggers use Aliyun Container Management to create them, so that every time the code is changed, it will automatically generate a new image.
Run the container
When we have the image, we can run the container. Because the blogger uses Ali Cloud container management, I need to pull the image to the running environment first (I use Aliyun ECS)
Docker pull registry.cn-hangzhou.aliyuncs.com/jamesying/ocelot-demodocker tag registry.cn-hangzhou.aliyuncs.com/jamesying/ocelot-demo myocelot:v1
The blogger pulled the image with the pull command and renamed tagname with the tag command.
Then we create a directory of configuration files for ocelot and create a configurations.json file:
Mkdir / home/ocelottouch / home/ocelot/configurations.json
The following is to configure your own config through the vi tool, and here is the blogger's:
{"ReRoutes": [{"DownstreamPathTemplate": "/ api/values/ {id}", "DownstreamScheme": "http", "DownstreamHostAndPorts": [{"Host": "localhost", "Port": 6002}, {"Host": "localhost", "Port": 6001}], "LoadBalancer": "RoundRobin" "UpstreamPathTemplate": "/ api/v1/values/ {id}", "DownstreamHealthcheckPath": "/ hc?apikey=testapi", "UpstreamHttpMethod": ["GET", "Put", "Delete"]}], "GlobalConfiguration": {}}
Let's run the container later:
Docker run-- name myocelot-p 6008 home/ocelot:/app/configurations 80-v / home/ocelot:/app/configurations-d ocelot:v1
After running successfully, we can access it through port 6008, and you can take a look at Demo through http://ocelot.jcsoft.xyz:6008. The following is a screenshot of the demo:
Why is there this image, because it is easy to generate through the image, and after the configuration file is changed, only docker restart myocelot can be reloaded, which is very easy. Today's content is very simple, but very practical, of course, this image is still a little lacking, because Ocelot has many functions and you need to register some service in Startup.cs before you can use it. Bloggers have an idea to create a special Ocelot Demo that can automatically register the corresponding service through the config file.
Maybe you saw from Demo that the landlord changed the Ocelot, and the landlord added the DownstreamHealthcheckPath attribute, which is to Healthcheck the downstream server, and the landlord PR also gave it to the project manager of Ocelot, but Tom is not very clear about the use of this attribute, so I'm going to make a Demo to show him, this property is very useful, except by trying to check the status of the downstream server. At the same time, invalid servers can be ignored during LoadBalance.
This is the end of the article on "how to make a Docker image for Ocelot". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.