예제 #1
0
        public AuthTest(ITestOutputHelper output)
        {
            _output = output;

            IServiceCollection services = new ServiceCollection();

            services.AddNacosV2Config(x =>
            {
                x.ServerAddresses = new System.Collections.Generic.List <string> {
                    "http://localhost:8848/"
                };
                x.EndPoint  = "";
                x.Namespace = "cs-test";

                x.UserName = "******";
                x.Password = "******";

                // swich to use http or rpc
                x.ConfigUseRpc = true;
                x.NamingUseRpc = true;
            });

            services.AddNacosV2Naming(x =>
            {
            });

            services.AddLogging(builder => { builder.AddConsole(); });

            IServiceProvider serviceProvider = services.BuildServiceProvider();

            _configSvc = serviceProvider.GetService <INacosConfigService>();
            _namingSvc = serviceProvider.GetService <INacosNamingService>();
        }
예제 #2
0
        static async Task RegisterInstance(INacosNamingService svc, int port = 9999)
        {
            await Task.Delay(500);

            var instace = new Nacos.V2.Naming.Dtos.Instance
            {
                ServiceName = "demo-svc1",
                ClusterName = Nacos.V2.Common.Constants.DEFAULT_CLUSTER_NAME,
                Ip          = "127.0.0.1",
                Port        = port,
                Enabled     = true,
                Ephemeral   = true,
                Healthy     = true,
                Weight      = 100,
                InstanceId  = $"demo-svc1-127.0.0.1-{port}",
                Metadata    = new System.Collections.Generic.Dictionary <string, string>
                {
                    { "m1", "v1" },
                    { "m2", "v2" },
                }
            };

            // 注册实例有很多重载,选适合自己的即可。
            await svc.RegisterInstance(instace.ServiceName, Nacos.V2.Common.Constants.DEFAULT_GROUP, instace);

            Console.WriteLine($"======================注册实例成功");
        }
예제 #3
0
        public Nacos(string serviceName, INacosNamingService service)
        {
            var groupedName = serviceName.Split(Constants.SERVICE_INFO_SPLITER);

            _serviceName = groupedName.Length == 1 ? groupedName[0] : groupedName[1];
            _groupName   = groupedName.Length == 2 ? groupedName[0] : Constants.DEFAULT_GROUP;
            _service     = service;
        }
예제 #4
0
파일: Nacos.cs 프로젝트: wht526/Blog.Core
 public Nacos(string serviceName, INacosNamingService client, IOptions <NacosAspNetOptions> options)
 {
     _serviceName = serviceName;
     _client      = client;
     _groupName   = string.IsNullOrWhiteSpace(options.Value.GroupName) ?
                    NacosConstants.DEFAULT_GROUP : options.Value.GroupName;
     _clusters = (string.IsNullOrWhiteSpace(options.Value.ClusterName) ? NacosConstants.DEFAULT_CLUSTER_NAME : options.Value.ClusterName).Split(",").ToList();
 }
 public CustomJwtTokenAuthMiddleware(INacosNamingService serv, RequestDelegate next, IAuthenticationSchemeProvider schemes, Appsettings appset, ICaching cache)
 {
     NacosServClient = serv;
     _cache          = cache;
     _next           = next;
     Schemes         = schemes;
     List <PermissionItem> Permissions = _cache.Cof_AsyncGetICaching <List <PermissionItem> >("Permissions", GetPermitionData, 10).GetAwaiter().GetResult();
 }
예제 #6
0
        static async Task DeregisterInstance(INacosNamingService svc)
        {
            await Task.Delay(500);

            // 注销实例有很多重载,选适合自己的即可。
            await svc.DeregisterInstance("demo-svc1", Nacos.V2.Common.Constants.DEFAULT_GROUP, "127.0.0.1", 9999);

            Console.WriteLine($"======================注销实例成功");
        }
예제 #7
0
        static async Task GetAllInstances(INacosNamingService svc)
        {
            await Task.Delay(500);

            // 获取全部实例有很多重载,选适合自己的即可。最后一个参数表明要不要订阅这个服务
            // SelectInstances, SelectOneHealthyInstance 是另外的方法可以获取服务信息。
            var list = await svc.GetAllInstances("demo-svc1", Nacos.V2.Common.Constants.DEFAULT_GROUP, false);

            Console.WriteLine($"======================获取实例成功,{Newtonsoft.Json.JsonConvert.SerializeObject(list)}");
        }
예제 #8
0
 public RegSvcBgTask(
     ILoggerFactory loggerFactory,
     INacosNamingService svc,
     IServer server,
     IOptionsMonitor <NacosAspNetOptions> optionsAccs)
 {
     _logger   = loggerFactory.CreateLogger <RegSvcBgTask>();
     _svc      = svc;
     _options  = optionsAccs.CurrentValue;
     _features = server.Features;
 }
예제 #9
0
        static async Task Subscribe(INacosNamingService svc, IEventListener listener)
        {
            // 订阅服务变化
            await svc.Subscribe("demo-svc1", Nacos.V2.Common.Constants.DEFAULT_GROUP, listener);

            // 模拟服务变化,listener会收到变更信息
            await RegisterInstance(svc, 9997);

            await Task.Delay(3000);

            // 取消订阅
            await svc.Unsubscribe("demo-svc1", Nacos.V2.Common.Constants.DEFAULT_GROUP, listener);

            // 服务变化后,listener不会收到变更信息
            await RegisterInstance(svc);

            await Task.Delay(1000);
        }
예제 #10
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="serv"></param>
        /// <param name="configClient"></param>
        /// <param name="serviceProvider"></param>
        /// <param name="_internalConfigurationRepo"></param>
        /// <param name="_internalConfigurationCreator"></param>
        public OcelotConfigurationTask(INacosNamingService serv, INacosConfigService configClient, IServiceProvider serviceProvider, IInternalConfigurationRepository _internalConfigurationRepo, IInternalConfigurationCreator _internalConfigurationCreator)
        {
            _configClient = configClient;
            _servClient   = serv;
            nacosConfigListener.internalConfigurationRepo    = _internalConfigurationRepo;
            nacosConfigListener.internalConfigurationCreator = _internalConfigurationCreator;
            OcelotConfig      = Appsettings.GetValue("ApiGateWay:OcelotConfig");
            OcelotConfigGroup = Appsettings.GetValue("ApiGateWay:OcelotConfigGroup");
            AppConfig         = Appsettings.GetValue("ApiGateWay:AppConfig");
            AppConfigGroup    = Appsettings.GetValue("ApiGateWay:AppConfigGroup");



            string OcelotCfg = configClient.GetConfig(OcelotConfig, OcelotConfigGroup, 10000).GetAwaiter().GetResult();

            nacosConfigListener.ReceiveConfigInfo(OcelotCfg);
            string AppCfg = configClient.GetConfig(AppConfig, AppConfigGroup, 10000).GetAwaiter().GetResult();

            AppConfigListener.ReceiveConfigInfo(AppCfg);
            //string sss = serv.Cof_NaoceGet("fld-cloud-datax", "DEFAULT_GROUP", "/api/base/deviceList?limit=10&page=1").GetAwaiter().GetResult();
        }
예제 #11
0
        static async Task Main(string[] args)
        {
            var serviceProvider = InitServiceProvider();

            INacosConfigService configSvc = serviceProvider.GetService <INacosConfigService>();
            INacosNamingService namingSvc = serviceProvider.GetService <INacosNamingService>();

            DemoConfigListener listener = new DemoConfigListener();

            await PublishConfig(configSvc);
            await GetConfig(configSvc);
            await RemoveConfig(configSvc);
            await ListenConfig(configSvc, listener);

            DemoEventListener eventListener = new DemoEventListener();

            await RegisterInstance(namingSvc);
            await GetAllInstances(namingSvc);
            await DeregisterInstance(namingSvc);
            await Subscribe(namingSvc, eventListener);

            Console.ReadKey();
        }
예제 #12
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="nacosNamingService"></param>
 /// <param name="serviceProvider"></param>
 /// <param name="configuration"></param>
 public NacosListenNamingTask(INacosNamingService nacosNamingService)
 {
     _nacosNamingService = nacosNamingService;
 }
예제 #13
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="nacosNamingService"></param>
 public NacosController(INacosNamingService nacosNamingService)
 {
     NacosNamingService = nacosNamingService;
 }
예제 #14
0
 public Nacos(string serviceName, INacosNamingService client)
 {
     _client      = client;
     _serviceName = serviceName;
 }