|
一:nacos
https://nacos.io/docs/latest/what-is-nacos/
https://github.com/alibaba/nacos
二:consul
https://developer.hashicorp.com/consul/docs?product_intent=consul
https://github.com/hashicorp/consul
服务发现的框架常用的还有zookeeper eureka等,这里准备使用nacos
前置条件准备 docker,yaml
View Code使用docker启动mysql
- mysql:
- container_name: mysql
- image: mysql
- privileged: true
- command: --character-set-server=utf8mb4 --collation-server=utf8mb4_general_ci --max_connections=2000 --max_allowed_packet=64M
- environment:
- - TZ=Asia/Shanghai
- - MYSQL_ROOT_PASSWORD=123456
- volumes:
- - ./mysql:/var/lib/mysql
- ports:
- - "3306:3306"
- healthcheck:
- test: [ "CMD", "mysqladmin" ,"ping", "-h", "localhost" ]
- interval: 5s
- timeout: 10s
- retries: 10
- networks:
- caseor_bridge:
- ipv4_address: 172.0.10.3
复制代码 View Code在数据库创建nacos数据库- https://github.com/alibaba/nacos/blob/master/config/src/main/resources/META-INF/nacos-db.sql
复制代码 准备三个nacos- nacos1:
- container_name: nacos1
- hostname: nacos1
- image: nacos/nacos-server
- environment:
- - MODE=cluster
- - PREFER_HOST_MODE=hostname
- - NACOS_SERVERS=nacos1:8848 nacos2:8848 nacos3:8848
- - SPRING_DATASOURCE_PLATFORM=mysql
- - MYSQL_SERVICE_HOST=172.0.10.3
- - MYSQL_SERVICE_PORT=3306
- - MYSQL_SERVICE_USER=root
- - MYSQL_SERVICE_PASSWORD=123456
- - MYSQL_SERVICE_DB_NAME=nacos
- - MYSQL_SERVICE_DB_PARAM=characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
- - JVM_XMS=128m
- - JVM_XMX=128m
- - JVM_XMN=128m
- volumes:
- - ./nacos/cluster-logs/nacos1:/home/nacos/logs
- - ./nacos/init.d:/home/nacos/init.d
- ports:
- - 8850:8848
- - 7850:7848
- - 9870:9848
- - 9852:9849
- depends_on:
- - mysql
- networks:
- caseor_bridge:
- ipv4_address: 172.0.10.5
- nacos2:
- container_name: nacos2
- hostname: nacos2
- image: nacos/nacos-server
- environment:
- - MODE=cluster
- - PREFER_HOST_MODE=hostname
- - NACOS_SERVERS=nacos1:8848 nacos2:8848 nacos3:8848
- - SPRING_DATASOURCE_PLATFORM=mysql
- - MYSQL_SERVICE_HOST=172.0.10.3
- - MYSQL_SERVICE_PORT=3306
- - MYSQL_SERVICE_USER=root
- - MYSQL_SERVICE_PASSWORD=123456
- - MYSQL_SERVICE_DB_NAME=nacos
- - MYSQL_SERVICE_DB_PARAM=characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
- - JVM_XMS=128m
- - JVM_XMX=128m
- - JVM_XMN=128m
- volumes:
- - ./nacos/cluster-logs/nacos2:/home/nacos/logs
- - ./nacos/init.d:/home/nacos/init.d
- ports:
- - 8849:8848
- - 7849:7848
- - 9869:9848
- - 9851:9849
- depends_on:
- - mysql
- networks:
- caseor_bridge:
- ipv4_address: 172.0.10.6
- nacos3:
- container_name: nacos3
- hostname: nacos3
- image: nacos/nacos-server
- environment:
- - MODE=cluster
- - PREFER_HOST_MODE=hostname
- - NACOS_SERVERS=nacos1:8848 nacos2:8848 nacos3:8848
- - SPRING_DATASOURCE_PLATFORM=mysql
- - MYSQL_SERVICE_HOST=172.0.10.3
- - MYSQL_SERVICE_PORT=3306
- - MYSQL_SERVICE_USER=root
- - MYSQL_SERVICE_PASSWORD=123456
- - MYSQL_SERVICE_DB_NAME=nacos
- - MYSQL_SERVICE_DB_PARAM=characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
- - JVM_XMS=128m
- - JVM_XMX=128m
- - JVM_XMN=128m
- volumes:
- - ./nacos/cluster-logs/nacos3:/home/nacos/logs
- - ./nacos/init.d:/home/nacos/init.d
- ports:
- - 8848:8848
- - 7848:7848
- - 9848:9848
- - 9849:9849
- depends_on:
- - mysql
- networks:
- caseor_bridge:
- ipv4_address: 172.0.10.7
复制代码 具体配置可以参考官网
查看nacos 控制台 http://127.0.0.1:8848/nacos/,http://127.0.0.1:8849/nacos/,http://127.0.0.1:8850/nacos/
可以看到集群已经搭建完成了
在.net core中使用nacos
https://github.com/nacos-group/nacos-sdk-csharp- nacos-sdk-csharp.AspNetCore
- nacos-sdk-csharp.Extensions.Configuration
复制代码 在appsettings.json中新增Nacos配置- "Nacos": {
- "ServerAddresses": [ "http://192.168.110.39:8848" ],
- //命名空间GUID,public默认没有
- "Namespace": "779857e5-b517-437c-9026-f04c98d4bac1",
- // 配置中心
- "Listeners": [
- {
- "Group": "DEFAULT_GROUP",
- "DataId": "appsettings.json",
- "Optional": false
- }
- ],
- // 服务发现
- "ServiceName": "saas-system",
- "GroupName": "DEFAULT_GROUP"
- }
复制代码
在program下新增- //读取nacos配置文件
- builder.Host.UseNacosConfig("Nacos");
- //注册服务到nacos
- builder.Services.AddNacosAspNet(builder.Configuration, "Nacos");
复制代码 在program下继续读取appsettings的配置信息,首选需要在nacos上的appsettings配置信息
启动该服务- dotnet run --urls=http://*:8083
- dotnet run --urls=http://*:8084
- dotnet run --urls=http://*:8085
复制代码
在其他服务中调用当前服务接口- [HttpGet("nacos.test")]
- public async Task<IActionResult> TestNacos()
- {
- var instance= await _namingService.SelectOneHealthyInstance("saas-system", "DEFAULT_GROUP");
- var host = $"{instance.Ip}:{instance.Port}";
- var baseUrl = instance.Metadata.TryGetValue("secure", out _)
- ? $"https://{host}"
- : $"http://{host}";
- var url = $"{baseUrl}/system/tenant.package.query.list";
- using HttpClient client = new();
- var result = await client.GetAsync(url);
- return Ok(await result.Content.ReadAsStringAsync());
- }
复制代码
来源:https://www.cnblogs.com/AsprosL/p/18021312
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作! |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
x
|