public static IServiceCollection AddConsulService(this IServiceCollection services)
        {
            //注册服务路由工厂
            services.AddSingleton <IServiceRouteFactory, DefaultServiceRouteFactory>();
            services.AddSingleton <IServiceRouteManager, ConsulServiceRouteManager>();
            services.AddSingleton <IHealthCheckService, DefaultHealthCheckService>();
            services.AddSingleton <IConsulAddressSelector, ConsulRandomAddressSelector>();
            services.AddSingleton <IServiceHeartbeatManager, DefaultServiceHeartbeatManager>();
            services.AddSingleton <ISerializer <string>, JsonSerializer>();
            services.AddSingleton <ISerializer <byte[]>, StringByteArraySerializer>();
            services.AddSingleton <ISerializer <object>, StringObjectSerializer>();
            var provider   = services.BuildServiceProvider();
            var configInfo = new ConfigInfo(null);
            var config     = GetConfigInfo(configInfo);
            var defaultConsulClientProvider = new DefaultConsulClientProvider(config, provider.GetService <IHealthCheckService>(), provider.GetService <IConsulAddressSelector>());

            services.AddSingleton(typeof(IConsulClientProvider), defaultConsulClientProvider);
            var clientWatchManager = new ClientWatchManager(config);

            services.AddSingleton(typeof(IClientWatchManager), clientWatchManager);
            provider = services.BuildServiceProvider();
            var consulServiceRouteManager = new ConsulServiceRouteManager(config, provider.GetService <ISerializer <byte[]> >(), provider.GetService <ISerializer <string> >(), provider.GetService <IClientWatchManager>(), provider.GetService <IServiceRouteFactory>(), provider.GetService <IServiceHeartbeatManager>(), provider.GetService <IConsulClientProvider>());

            services.AddSingleton(typeof(IServiceRouteManager), consulServiceRouteManager);
            return(services);
        }
Ejemplo n.º 2
0
        /**
         * Creates a connection object. The actual network connect doesn't get
         * established until needed. The start() instance method must be called
         * subsequent to construction.
         *
         * @param chrootPath - the chroot of this client. Should be removed from this Class in ZOOKEEPER-838
         * @param hostProvider
         *            the list of ZooKeeper servers to connect to
         * @param sessionTimeout
         *            the timeout for connections.
         * @param zooKeeper
         *            the zookeeper object that this connection is related to.
         * @param watcher watcher for this connection
         * @param clientCnxnSocket
         *            the socket implementation used (e.g. NIO/Netty)
         * @param sessionId
         *            session id if re-establishing session
         * @param sessionPasswd
         *            session passwd if re-establishing session
         * @param canBeReadOnly
         *            whether the connection is allowed to go to read-only mode in
         *            case of partitioning
         * @throws IOException
         */

        internal ClientCnxn(string chrootPath, HostProvider hostProvider, int sessionTimeout, ZooKeeper zooKeeper,
                            ClientWatchManager watcher,
                            long sessionId, byte[] sessionPasswd, bool canBeReadOnly)
        {
            this.zooKeeper      = zooKeeper;
            this.watcher        = watcher;
            this.sessionId      = sessionId;
            this.sessionPasswd  = sessionPasswd;
            this.sessionTimeout = sessionTimeout;
            this.hostProvider   = hostProvider;
            this.chrootPath     = chrootPath;
            readTimeout         = sessionTimeout * 2 / 3;
            readOnly            = canBeReadOnly;
            clientCnxnSocket    = new ClientCnxnSocketNIO(this);
            state.Value         = (int)ZooKeeper.States.CONNECTING;
        }