简体   繁体   中英

NatsError: stan: connect request timeout || port forwarding

my publisher.ts file:

import nats from 'node-nats-streaming';

const stan = nats.connect('ticketing', 'abc', {
  url: 'http://localhost:4222',
});

stan.on('connect', () => {
  console.log('Publisher connected to NATS');

  });
});

my port forwarding command:

kubectl port-forward {nats-deployment pod name} 4222:4222

my publish command in my nats-test folder:

"publish": "ts-node-dev --rs --notify false src/publisher.ts"

my nats-depl.yaml file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nats-depl
spec:
  selector:
    matchLabels:
      app: nats
  template:
    metadata:
      labels:
        app: nats
    spec:
      containers:
      - name: nats
        image: nats-streaming:0.17.0
        args: [
          '-p',
          '4222',
          '-m',
          '8222',
          '-hbi',
          '5s',
          '-hbt',
          '5s',
          '-hbf',
          '2',
          '-SD',
          '-cid',
          '-ticketing'
        ]
---
apiVersion: v1
kind: Service
metadata:
  name: nats-srv
spec:
  selector:
    app: nats
  ports:
  - name: client
    protocol: TCP
    port: 4222
    targetPort: 4222
  - name: monitoring
    protocol: TCP
    port: 8222
    targetPort: 8222  

not able to connect to nats client.

The first argument of the connect function is the cluster ID . You can check your NATS cluster's ID in the logs of the container where it is running. The cluster ID is underlined with green in the picture.

在此处输入图像描述

Thus, the connect function should be:

const stan = nats.connect('test-cluster', 'abc', {
  url: 'http://localhost:4222',
});

Instead of:

const stan = nats.connect('testing', 'abc', {
  url: 'http://localhost:4222',
});

In your nats-depl.yaml file, The last entry in your args array is -ticketing, which is your clusterId

remove - from ticketing entry

const stan = nats.connect('ticketing', 'abc', {
  url: 'http://nats-srv:4222', // nats-serv is your nats service name
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM