Advertisement

NATS的使用(1)、NATS介绍

阅读量:

NATS的介绍

NATS官网

nats是一个高性能的消息中间件。

下载地址

运行
复制代码
    单个运行方式
    nats-server -D -p 4222
    
    
      
      
    
    AI写代码
复制代码
    集群运行模式
    A:
    nats-server -D -p 4222 -cluster nats://localhost:6222
    
    B:
    nats-server -D -p 4333 -cluster nats://localhost:6333 -routes nats://localhost:6222
    
    C:
    nats-server -D -p 4444 -cluster nats://localhost:6444 -routes nats://localhost:6222
    
    D:
    nats-server -D -p 4555 -cluster nats://localhost:6555 -routes nats://localhost:6222,nats://localhost:6333
    
    -cluster nats://localhost:6222 表示开启集群,其它nats-server可以通过6222端口与之建立集群
    -routes nats://localhost:6222 连接到开启6222端口的nats-server与其建立集群
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    
    AI写代码
复制代码
    以配置文件运行
    ./nats64 -c ./config.conf
    
    
    config.conf
    # Client port of 4222 on all interfaces
    port: 5225
    
    # HTTP monitoring port
    monitor_port: 9225
    
    # This is for clustering multiple servers together.
    cluster {
    
      # Route connections to be received on any interface on port 6222
      port: 7225
    
      # Routes are protected, so need to use them with --routes flag
      # e.g. --routes=nats-route://ruser:T0pS3cr3t@otherdockerhost:6222
      authorization {
    user: ABC
    password: ABC123456
    timeout: 1
      }
    
      # Routes are actively solicited and connected to from this server.
      # This Docker image has none by default, but you can pass a
      # flag to the nats-server docker image to create one to an existing server.
      #与其它nats-server建立集群,主节点为空
      routes = ["nats://ABC:ABC123456@localhost:7223","nats://ABC:ABC123456@localhost:7224"]
    }
    
    #客户端的连接认证
    #tls: { //tls 认证配置
     # cert_file: "./cert/server/server.crt"
     # key_file: "./cert/server/server_rsa_private.pem.unsecure"
     # verify: true
     # ca_file: "./ca/ca.crt"
      #verify_and_map: true
    #}
    
    #authorization { //token方式的认证
    #    token: "abc123"
    #}
    
    #authorization: {
    #    users: [ //用户密码方式的认证
    #        {user: admin, password: admin},
    #        {user: test, password: test}
    #    ]
    #}
    #authorization: { //OU方式的认证
    #  users: [
    #    {user: "CN=CLIENT,OU=ABC"}
    # 
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    
    AI写代码
客户端连接
复制代码
    	cd := &customDialer{
    		ctx:             ctx,
    		connectTimeout:  10 * time.Second,
    		connectTimeWait: 1 * time.Second,
    	}
    	opts := []nats.Option{
    		nats.SetCustomDialer(cd),
    		nats.ReconnectWait(2 * time.Second),
    		nats.ReconnectHandler(func(c *nats.Conn) {
    			log.Println("Reconnected to", c.ConnectedUrl())
    		}),
    		nats.DisconnectHandler(func(c *nats.Conn) {
    			log.Println("Disconnected from NATS")
    		}),
    		nats.ClosedHandler(func(c *nats.Conn) {
    			log.Println("NATS connection is closed.")
    		}),
    		nats.NoReconnect(),
    		nats.ClientCert("D:\ cert\ client\ client.crt", "D:\ cert\ client\ client_rsa_private.pem.unsecure"),
    		nats.RootCAs("D:\ cert\ ca.crt"),
    		nats.UserInfo("admin", "admin"),
    		//nats.Token("abc123"), //nc, err = nats.Connect("abc123@server:4224", opts...)
    		// nats.UserJWT(),
    		// nats.Dialer(),
    	}
    	nc, err := nats.Connect("server:42242", opts...)
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    
    AI写代码

全部评论 (0)

还没有任何评论哟~