首页 > 其他分享 >.net core微服务之服务发现

.net core微服务之服务发现

时间:2024-02-20 10:22:52浏览次数:50  
标签:core 服务 SERVICE 172.0 mysql nacos 8848 MYSQL net

一: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

version: "3.8"
networks:
  caseor_bridge:
    driver: bridge
    ipam:
      config:
        - subnet: 172.0.10.0/24
  
services:

    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

    redis:
        image: redis
        container_name: "redis"
        ports:
            - "6379:6379"
        volumes:
            - ./redis/data:/data
            - ./redis/conf:/usr/local/etc/redis
        networks:
          caseor_bridge:
                ipv4_address: 172.0.10.4

    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

    etcd:
        container_name: etcd
        hostname: etcd
        image: bitnami/etcd
        volumes:
          - ./etcd/data:/bitnami/etcd
        environment:
          ETCD_ENABLE_V2: "true"
          ALLOW_NONE_AUTHENTICATION: "yes"
          ETCD_ADVERTISE_CLIENT_URLS: "http://etcd:2379" #https://github.com/apache/apisix-dashboard/issues/2756 需要更换为host域名不能使用0.0.0.0
          ETCD_LISTEN_CLIENT_URLS: "http://0.0.0.0:2379"
        ports:
          - "2379:2379/tcp"
        networks:
          caseor_bridge:
                ipv4_address: 172.0.10.8

    apisix:
        container_name: apisix
        hostname: apisix
        image: apache/apisix        
        volumes:
          - ./apisix/log:/usr/local/apisix/logs
          - ./apisix/conf/config.yaml:/usr/local/apisix/conf/config.yaml:ro
        depends_on:
          - etcd
        ports:
          - "9088:9088/tcp"
          - "9180:9180/tcp"
          - "127.0.0.1:9090:9090/tcp"
        networks:
          caseor_bridge:
                ipv4_address: 172.0.10.9
    
    apisix-dashboard:
        container_name: apisix-dashboard
        image: apache/apisix-dashboard
        depends_on:
          - etcd
        ports:
          - "9188:9188"
        volumes:
          - ./apisix/conf/dashboard.yaml:/usr/local/apisix-dashboard/conf/conf.yaml
        networks:
          caseor_bridge:
                ipv4_address: 172.0.10.10

    rabbitmq01:
      image: rabbitmq
      container_name: rabbitmq01
      hostname: rabbitmq01
      environment:
        - TZ=Asia/Shanghai
        - RABBITMQ_DEFAULT_USER=root   #自定义登录账号
        - RABBITMQ_DEFAULT_PASS=123456 #自定义登录密码
        - RABBITMQ_ERLANG_COOKIE='secret_cookie'
      ports:
        - "15672:15672"
        - "5672:5672"
      volumes:
        - ./rabbitmq/mq1/data:/var/lib/rabbitmq
        - ./rabbitmq/mq1/conf:/etc/rabbitmq
      command:  bash -c "sleep 10; rabbitmq-server;"
      networks:
        caseor_bridge:
            ipv4_address: 172.0.10.11

    rabbitmq02:
      image: rabbitmq
      container_name: rabbitmq02
      hostname: rabbitmq02
      environment:
        - TZ=Asia/Shanghai
        - RABBITMQ_DEFAULT_USER=root   #自定义登录账号
        - RABBITMQ_DEFAULT_PASS=123456 #自定义登录密码
        - RABBITMQ_ERLANG_COOKIE='secret_cookie'
      ports:
        - "15673:15672"
        - "5673:5672"
      depends_on:
          - rabbitmq01
      volumes:
        - ./rabbitmq/mq2/data:/var/lib/rabbitmq
        - ./rabbitmq/mq2/conf:/etc/rabbitmq
      command:  bash -c "sleep 10; rabbitmq-server;"
      networks:
        caseor_bridge:
            ipv4_address: 172.0.10.12

    rabbitmq03:
      image: rabbitmq
      container_name: rabbitmq03
      hostname: rabbitmq03
      environment:
        - TZ=Asia/Shanghai
        - RABBITMQ_DEFAULT_USER=root   #自定义登录账号
        - RABBITMQ_DEFAULT_PASS=123456 #自定义登录密码
        - RABBITMQ_ERLANG_COOKIE='secret_cookie'
      ports:
        - "15674:15672"
        - "5674:5672"
      depends_on:
          - rabbitmq01
      volumes:
        - ./rabbitmq/mq3/data:/var/lib/rabbitmq
        - ./rabbitmq/mq3/conf:/etc/rabbitmq
      command:  bash -c "sleep 10; rabbitmq-server;"
      networks:
        caseor_bridge:
            ipv4_address: 172.0.10.13

# 开启web管理 
# rabbitmq-plugins enable rabbitmq_management 

# # 加入rabbitmq集群

# # rabbit1
# rabbitmqctl stop_app
# rabbitmqctl reset
# rabbitmqctl start_app

# # rabbit2
# rabbitmqctl stop_app
# rabbitmqctl reset
# rabbitmqctl join_cluster --ram rabbit@rabbit1
# rabbitmqctl start_app

# # rabbit3
# rabbitmqctl stop_app
# rabbitmqctl reset
# rabbitmqctl join_cluster --ram rabbit@rabbit1
# rabbitmqctl start_app
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());

 }

 

标签:core,服务,SERVICE,172.0,mysql,nacos,8848,MYSQL,net
From: https://www.cnblogs.com/AsprosL/p/18021312

相关文章

  • NET8下生成二维码
    NET8下生成二维码按网上搜索的总是多少有些问题,得总搜索好几次才能解决的,现把自己用过的可以生成的代码放上来,以备后用2024年02月20日在VS2022,NET8,MVC项目上使用通过引入NUGET:ZXing.Net.Bindings.ZKWeb.System.Drawing控制器代码: usingMicrosoft.AspNetCore.Mvc;us......
  • .NET配置文件大揭秘:轻松读取JSON、XML、INI和环境变量
     概述:.NET中的IConfiguration接口提供了一种多源读取配置信息的灵活机制,包括JSON、XML、INI文件和环境变量。通过示例,清晰演示了从这些不同源中读取配置的方法,使配置获取变得方便且易于扩展。这种方式适用于不同场景,如API密钥、数据库连接等,为应用提供了高度可配置性。在.NET......
  • 探索MVVM Toolkit:简化.NET应用开发,构建高效MVVM架构
     概述:MVVMToolkit是.NET平台的强大工具包,旨在简化MVVM应用程序开发。提供基础功能如ViewModelBase和RelayCommand,支持数据绑定和命令绑定,通过Messenger实现消息订阅发布。其高级功能包括ObservableObject和WeakEventListener,助力开发人员构建可维护、高性能的MVVM应用,提升用......
  • zabbix服务端一键安装初始配置脚本
    zabbix服务端一键安装初始配置脚本简易脚本,初始化数据库可以改,centos7.9亲测完美执行#!/bin/bash#关闭SELinux、暂停防火墙setenforce0systemctlstopfirewalld#中文乱码修正yuminstall-ywqy-microhei-fonts\cp-f/usr/share/fonts/wqy-microhei/wqy-microhe......
  • zabbix服务端一键安装初始配置脚本
    zabbix服务端一键安装初始配置脚本简易脚本,初始化数据库可以改,centos7.9亲测完美执行#!/bin/bash#关闭SELinux、暂停防火墙setenforce0systemctlstopfirewalld#中文乱码修正yuminstall-ywqy-microhei-fonts\cp-f/usr/share/fonts/wqy-microhei/wqy-microhe......
  • .NET周刊【2月第2期 2024-02-11】
    国内文章C#/.NET该如何自学入门?https://www.cnblogs.com/Can-daydayup/p/18006914随着DotNetGuide技术社区交流群的扩大,很多新成员希望知道如何自学C#/.NET。本文提出了自学建议:首先要了解语言特点与发展,然后制定详细学习计划,以微软官方文档为学习起点,并结合动手实践与其他资源......
  • 腾讯云-阿里云服务器搭建幻兽帕鲁,终极傻瓜式部署教程指南!
    腾讯云-阿里云服务器搭建幻兽帕鲁,终极傻瓜式部署教程指南!腾讯云与阿里云作为国内领先的云计算服务提供商,提供了《幻兽帕鲁》这款热门网络游戏高效的一键部署方案,玩家可以通过腾讯云或阿里云轻量应用服务器,在短短数分钟内搭建起属于自己的游戏世界。本文将详细介绍如何利用这两家云......
  • 幻兽帕鲁部署教程,阿里云服务器快速搭建幻兽帕鲁
    本文更新阿里云服务器部署幻兽帕鲁保姆级教程,傻瓜式指南,阿里云作为全球领先的云计算服务提供商,凭借其强大的弹性计算能力、高可用性网络架构和一站式解决方案,为《幻兽帕鲁》等网络游戏提供了卓越的服务器支持。针对游戏场景,阿里云特别设计了一系列便捷高效的部署工具和服务模板,让......
  • 幻兽帕鲁服务器搭建部署手把手简单教程
    幻兽帕鲁服务器搭建部署手把手简单教程,本文将更新详细的腾讯轻量云服务器搭建幻兽帕鲁的部署教程。在云服务器领域,腾讯云以其卓越的技术实力与丰富的应用模板为各类用户提供了一站式的解决方案。针对广大游戏玩家的需求,腾讯云现推出了极具吸引力的第三方游戏应用模板——幻兽帕鲁W......
  • 使用VS Code Remote SSH连接上服务器实现远程开发
    1下载VSCODE,Windows版本https://code.visualstudio.com/updates/v1_852安装插件3 配置SSH密钥,上传公钥到服务器4连接成功,直接操作远程目录和文件   远程开发https://code.visualstudio.com/docs/remote/remote-overview https://www.jianshu.com/p/37bbec3788......