当前位置:网站首页>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
边栏推荐
- TypeError: set_figure_params() got an unexpected keyword argument ‘figsize‘
- Website_ Collection
- Easyexcel reads the geographical location data in the excel table and sorts them according to Chinese pinyin
- Decimal format decimal / datetime conversion processing
- Nodejs installation and environment configuration
- Real time operation of vim editor
- PostgreSQL列存与行存
- SQL database
- CentOS MySQL multi instance deployment
- 关于 background-image 渐变gradient()那些事!
猜你喜欢
Detailed explanation of file operation (2)
loggie 源码分析 source file 模块主干分析
ByteVCharts可视化图表库,你想要的我都有
Node access to Alipay open platform sandbox to achieve payment function
Dlib of face recognition framework
File upload and download of robot framework
STM32__ 03 - beginner timer
网络安全之渗透靶场实战详解
Nacos detailed explanation, something
Dancenn: overview of byte self-developed 100 billion scale file metadata storage system
随机推荐
Derivation of Σ GL perspective projection matrix
[pimf] openharmony paper Club - what is the experience of wandering in ACM survey
MySQL master-slave configuration under CentOS
RTKLIB 2.4.3源码笔记
伪分布安装spark
The new MySQL table has a self increasing ID of 20 bits. The reason is
STM32__ 03 - beginner timer
True math problems in 1959 college entrance examination
About background image gradient()!
扫码登录的原理你真的了解吗?
Nodejs reads the local JSON file through require. Unexpected token / in JSON at position appears
Do you really understand the principle of code scanning login?
面试百分百问到的进程,你究竟了解多少
Deeply understand the relevant knowledge of 3D model (modeling, material mapping, UV, normal), and the difference between displacement mapping, bump mapping and normal mapping
Website_ Collection
MySQL master-slave replication
Set the color change of interlaced lines in cells in the sail software and the font becomes larger and red when the number is greater than 100
众昂矿业:萤石浮选工艺
∑GL-透视投影矩阵的推导
安装及管理程序