当前位置:网站首页>Nacos + aspnetcore + Ocelot actual combat code
Nacos + aspnetcore + Ocelot actual combat code
2022-04-23 16:54:00 【Tassel 1990】
Record the program you've played recently .
1、AspnetCore( frame NetCore3.1) Service deployment 2 individual docker, As load balancing 2 Nodes , adopt Nacos Self distribution
2、 gateway Ocelot( frame Net5.0, because Ocelot.Provider.Nacos docking Nacos2.0, Low version access 2.0 Of Nacos Will report a mistake ) Do the forwarding , Configuration uses Nacos Load balancing
Some codes and configurations are listed below
A、 Create microservices , The main codes are as follows :
1、 register Nacos( quote Nuget:nacos-sdk-csharp-unofficial.aspnetcore)
public void ConfigureServices(IServiceCollection services)
{
services.AddNacosAspNetCore(Configuration);
}
2、appsetting.json To configure Nacos as follows :
"nacos": {
"ServerAddresses": [ "" ],// Server address
"DefaultTimeOut": 15,
"Namespace": "",//Nacos The namespace of
"ListenInterval": 1000,
"ServiceName": "",// service name
"Weight": 100
}
3、 After successful registration , Can be in Nacos Page to find , Here's the picture :

One thing to note is that To configure docker You need to specify IP Address with Port port ( Otherwise Nacos Automatically configured docker Inside the container IP Address , It will lead to inaccessibility after forwarding ), So in configuration docker Containers Run When Need to increase the ASPNETCORE_URLS Parameters , As follows :
docker run --name=umsspc1 --restart=always -d -p 85:9002 -e "ASPNETCORE_URLS=http://xxxxx:85" --link umsreports:nameasreporttest -v /usr/pm/umsspccollect/plug:/spccore/plug -v /usr/pm/umsspccollect/jsonConfig:/spccore/jsonConfig umsspcapi
B 、Ocelot Gateway project (Net5.0 frame ), The main code and configuration are as follows :
1、 Refer to the Nuget There are the following :
nacos-sdk-csharp.Extensions.Configuration
Ocelot.Provider.Nacos
Ocelot.Provider.Polly
2、 The configuration file appsettings.json as follows :
"GlobalConfiguration": {
"ServiceDiscoveryProvider": {
"Type": "Nacos"// This sentence is important
}
},
"nacos": {
"Listeners": [
{
"Optional": false,
"DataId": "spcreceive-ocelot.json",// Configuration center DataId, The configuration information is written as route jump
"Group": "DEFAULT_GROUP"
}
],
"ServerAddresses": [ "http://xxxxx:8848" ],//nacos Address
"ServiceName": "apigateway",// service name
"DefaultTimeOut": 5000,
"Namespace": "",// Customize Namespace Of Id
"GroupName": "DEFAULT_GROUP",
"ClusterName": "DEFAULT",
"ListenInterval": 1000,
"RegisterEnabled": true,
"InstanceEnabled": true,
"LBStrategy": "WeightRoundRobin", //WeightRoundRobin WeightRandom
"NamingUseRpc": true
}
3、 Main code :
public void ConfigureServices(IServiceCollection services)
{
// Registration service discovery
services.AddOcelot().AddNacosDiscovery();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseOcelot().Wait();// Use Ocelot service
}
Program.cs file
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, builder) =>
{
var c = builder.Build();
builder.AddNacosV2Configuration(c.GetSection("nacos"));
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
4、Nacos Configuration center configuration item for spcreceive-ocelot.json as follows :
{
"Routes": [
{
"DownstreamPathTemplate": "/api/xxxxxbe",
"DownstreamScheme": "http",
"UpstreamPathTemplate": "/spc/Cxxxxxxiable",
"UpstreamHttpMethod": [ "Get", "Post" ],
"ServiceName": "SPCService",
"LoadBalancerOptions": {
"Type": "RoundRobin"
},
// Use service discovery
"UseServiceDiscovery": true
}
]
}
版权声明
本文为[Tassel 1990]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231359253446.html
边栏推荐
- Change the password after installing MySQL in Linux
- Server log analysis tool (identify, extract, merge, and count exception information)
- ◰ GL shadow map core steps
- 1959年高考数学真题
- ◰GL-阴影贴图核心步骤
- Paging the list collection
- 博士申请 | 厦门大学信息学院郭诗辉老师团队招收全奖博士/博后/实习生
- feign报400处理
- 深入了解3D模型相关知识(建模、材质贴图、UV、法线),置换贴图、凹凸贴图与法线贴图的区别
- MySQL restores data through binlog file
猜你喜欢

Node access to Alipay open platform sandbox to achieve payment function

JMeter installation tutorial and solutions to the problems I encountered

Use case execution of robot framework

DanceNN:字节自研千亿级规模文件元数据存储系统概述

详解牛客----手套

批量制造测试数据的思路,附源码

Nodejs installation and environment configuration

如何建立 TikTok用户信任并拉动粉丝增长

Mock test using postman

磁盘管理与文件系统
随机推荐
PyTorch:train模式与eval模式的那些坑
Paging SQL
Phpstudy V8, a commonly used software for station construction 1 graphic installation tutorial (Windows version) super detailed
How much do you know about the process of the interview
Selenium IDE and XPath installation of chrome plug-in
◰ GL shadow map core steps
PostgreSQL column storage and row storage
Encapsulating the logging module
PHP高效读大文件处理数据
英语 | Day15、16 x 句句真研每日一句(从句断开、修饰)
MySQL master-slave configuration under CentOS
VLAN advanced technology, VLAN aggregation, super VLAN, sub VLAN
[pimf] openharmony paper Club - what is the experience of wandering in ACM survey
Creation of RAID disk array and RAID5
SQL: How to parse Microsoft Transact-SQL Statements in C# and to match the column aliases of a view
True math problems in 1959 college entrance examination
MySQL master-slave synchronization pit avoidance version tutorial
面试百分百问到的进程,你究竟了解多少
How to build tiktok user trust and drive fan growth
Modify the test case name generated by DDT