当前位置:网站首页>Nacos+AspnetCore+Ocelot实战编码
Nacos+AspnetCore+Ocelot实战编码
2022-04-23 13:59:00 【流苏1990】
纪录下最近玩的这个方案。
1、AspnetCore(框架NetCore3.1)服务部署2个docker,作为负载均衡的2个节点,通过Nacos自行分配
2、网关Ocelot(框架Net5.0,因Ocelot.Provider.Nacos对接Nacos2.0,低版本访问2.0的Nacos会报错)做转发,配置采用Nacos负载均衡
以下列出部分代码以及配置
A、创建微服务,主要代码如下:
1、注册Nacos(引用Nuget:nacos-sdk-csharp-unofficial.aspnetcore)
public void ConfigureServices(IServiceCollection services)
{
services.AddNacosAspNetCore(Configuration);
}
2、appsetting.json配置Nacos如下:
"nacos": {
"ServerAddresses": [ "" ],//服务器地址
"DefaultTimeOut": 15,
"Namespace": "",//Nacos的命名空间
"ListenInterval": 1000,
"ServiceName": "",//服务名
"Weight": 100
}
3、注册成功后,可以在Nacos 页面找到,如下图:
有个需要注意的地方是 配置docker 启动的时候需要指定IP地址跟Port端口(不然Nacos自动配置了docker容器的内部IP地址,会导致转发后访问不了),所以在配置docker 容器Run 的时候 需要增加ASPNETCORE_URLS参数,具体如下:
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网关项目(Net5.0框架),主要代码以及配置如下:
1、引用的Nuget有如下:
nacos-sdk-csharp.Extensions.Configuration
Ocelot.Provider.Nacos
Ocelot.Provider.Polly
2、配置文件appsettings.json如下:
"GlobalConfiguration": {
"ServiceDiscoveryProvider": {
"Type": "Nacos"//这句话是重要的
}
},
"nacos": {
"Listeners": [
{
"Optional": false,
"DataId": "spcreceive-ocelot.json",//配置中心的DataId,配置信息写的是路由跳转
"Group": "DEFAULT_GROUP"
}
],
"ServerAddresses": [ "http://xxxxx:8848" ],//nacos地址
"ServiceName": "apigateway",//服务名
"DefaultTimeOut": 5000,
"Namespace": "",//自定义Namespace的Id
"GroupName": "DEFAULT_GROUP",
"ClusterName": "DEFAULT",
"ListenInterval": 1000,
"RegisterEnabled": true,
"InstanceEnabled": true,
"LBStrategy": "WeightRoundRobin", //WeightRoundRobin WeightRandom
"NamingUseRpc": true
}
3、主要代码:
public void ConfigureServices(IServiceCollection services)
{
//注册服务发现
services.AddOcelot().AddNacosDiscovery();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseOcelot().Wait();//使用Ocelot服务
}
Program.cs文件
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的配置中心配置项spcreceive-ocelot.json 如下:
{
"Routes": [
{
"DownstreamPathTemplate": "/api/xxxxxbe",
"DownstreamScheme": "http",
"UpstreamPathTemplate": "/spc/Cxxxxxxiable",
"UpstreamHttpMethod": [ "Get", "Post" ],
"ServiceName": "SPCService",
"LoadBalancerOptions": {
"Type": "RoundRobin"
},
// 使用服务发现
"UseServiceDiscovery": true
}
]
}
版权声明
本文为[流苏1990]所创,转载请带上原文链接,感谢
https://blog.csdn.net/fuweiping/article/details/120378986
边栏推荐
- AtomicIntegerArray源码分析与感悟
- elmo(BiLSTM-CRF+elmo)(Conll-2003 命名实体识别NER)
- 第一章 电商秒杀商品回顾
- Oracle alarm log alert Chinese trace and trace files
- Quartus prime hardware experimental development (de2-115 board) experiment 1 CPU instruction calculator design
- SSM project deployed in Alibaba cloud
- Neuron and neural network
- Taobao released the baby prompt "your consumer protection deposit is insufficient, and the expiration protection has been started"
- MySQL [read / write lock + table lock + row lock + mvcc]
- 自动化的艺术
猜你喜欢
33 million IOPs, 39 microsecond delay, carbon footprint certification, who is serious?
3300万IOPS、39微秒延迟、碳足迹认证,谁在认真搞事情?
【vmware】vmware tools 地址
编程旅行之函数
大专的我,闭关苦学 56 天,含泪拿下阿里 offer,五轮面试,六个小时灵魂拷问
Basic SQL query and learning
Decentralized Collaborative Learning Framework for Next POI Recommendation
MySQL [acid + isolation level + redo log + undo log]
Leetcode brush question 𞓜 13 Roman numeral to integer
Leetcode? The first common node of two linked lists
随机推荐
Force deduction brush question 101 Symmetric binary tree
Quartus prime hardware experimental development (de2-115 board) experiment 1 CPU instruction calculator design
联想产品经理林林:天津当地网络运营商网络故障 ZUI系统后台服务器暂时无法正常工作
SQL learning | set operation
Strange bug of cnpm
cnpm的诡异bug
Express②(路由)
JS 力扣刷题 102. 二叉树的层序遍历
专题测试05·二重积分【李艳芳全程班】
Express middleware ③ (custom Middleware)
UNIX final exam summary -- for direct Department
1256:献给阿尔吉侬的花束
Port occupied 1
freeCodeCamp----arithmetic_ Arranger exercise
leetcode--380.O(1) 时间插入、删除和获取随机元素
Express中间件③(自定义中间件)
Using Jupiter notebook in virtual environment
神经元与神经网络
Dynamic subset division problem
Un modèle universel pour la construction d'un modèle d'apprentissage scikit