Kubernetes 上 Node.js 容器中的 Axios 正在返回 "ECONNREFUSED 127.0.0.1:30561"?
Axios in a Node.js container on Kubernetes is returning "ECONNREFUSED 127.0.0.1:30561"?
完整错误信息:connect ECONNREFUSED 127.0.0.1:30561 at TCPConnectWrap.afterConnect
axios 请求在 Node.js 环境中 运行ning (Next.js),这是错误发生的地方,st运行gely axios 请求完美运行当它在浏览器中 运行 时很好。
调用 axios 的组件(运行ning in Node.js):
import axios from 'axios'
import Router from 'next/router'
import React, { Component } from 'react'
import { initializeStore } from '~/reducers'
import { authenticate } from '~/actions/auth'
import { getCookieByName } from '~/helpers/cookie'
const isServer = typeof window === 'undefined'
const __NEXT_REDUX_STORE__ = '__NEXT_REDUX_STORE__'
function getOrCreateStore(initialState) {
// Always make a new store if server, otherwise state is shared between requests
if (isServer) {
return initializeStore(initialState)
}
// Create store if unavailable on the client and set it on the window object
if (!window[__NEXT_REDUX_STORE__]) {
window[__NEXT_REDUX_STORE__] = initializeStore(initialState)
}
return window[__NEXT_REDUX_STORE__]
}
export default App => {
return class AppWithRedux extends Component {
static async getInitialProps(appContext) {
const reduxStore = getOrCreateStore()
appContext.ctx.reduxStore = reduxStore
let appProps = {}
if (typeof App.getInitialProps === 'function') {
appProps = await App.getInitialProps(appContext)
}
const JWT = (isServer ? getCookieByName('JWT', appContext.ctx.req.headers.cookie) : getCookieByName('JWT', document.cookie))
const pathname = appContext.ctx.pathname
//set axios baseURL
axios.defaults.baseURL = (isServer ? `${appContext.ctx.req.headers['x-forwarded-proto']}://${appContext.ctx.req.headers.host}` : window.location.origin)
//if user has a JWT
if(JWT){
axios.defaults.headers.common['Authorization'] = `Bearer ${JWT}`
//get user from API layer
reduxStore.dispatch(authenticate())
}
return {
...appProps,
initialReduxState: reduxStore.getState()
}
}
constructor(props) {
super(props)
this.reduxStore = getOrCreateStore(props.initialReduxState)
}
render() {
return <App {...this.props} reduxStore={this.reduxStore} />
}
}
}
具体来说reduxStore.dispatch(authenticate())
我实际的 axios 调用(使用 redux thunk),查看 authenticate
方法:
import axios from 'axios'
import { setCookieByName } from '~/helpers/cookie'
const BASE_URL = '/api/auth'
export const TYPE_REGISTER = 'TYPE_REGISTER'
export const TYPE_AUTHENTICATE = 'TYPE_AUTHENTICATE'
export const register = values => (dispatch) => {
return axios.post(`${BASE_URL}/register`, values)
.then(function({data: {token, user}}){
setCookieByName('JWT', token, 365)
dispatch({
type: TYPE_REGISTER,
payload: user
})
})
}
export const authenticate = () => (dispatch) => {
return axios.post(`${BASE_URL}/me`)
.then(function({data: {user}}){
dispatch({
type: TYPE_AUTHENTICATE,
payload: user
})
})
.catch(function(err){
console.log(err)
dispatch({
type: TYPE_AUTHENTICATE,
payload: {}
})
})
}
我正在 运行使用 Docker 为 Mac 连接我的本地 Kubernetes 集群,我的 Ingress 控制器正在 http://kludge.info:30561
上被访问。我的域是从 127.0.0.1 kludge.info
本地映射的,以允许 Ingress 控制器访问容器。我的理论是,当我向 http://kludge.info:30561/api/auth/me
发送请求时,docker 容器 运行 宁 Node.js 应用程序认为这是对本地主机的请求(在容器内) ,并导致连接错误。请注意,容器内的 Node.js 应用在 http://localhost:8080
上 运行ning。基本上,我在我的机器上 运行ning localhost,在 Node 实例中使用 localhost。我如何向 http://kludge.info:30561/
外部发送请求,其中 Ingress 控制器是 运行ning.
我也配置了baseURL
in axios,但是并没有解决问题。我的入口控制器有一个路径 /api
将指向一个 PHP 实例,所以我需要我的 Node.js axios 调用来命中它的容器。任何帮助将不胜感激。
当我 运行 我的 K8 集群在 Minikube 上时我没有遇到这个问题,但是 minikube 确实为您提供了 VM 的 IP,而 Docker 桌面使用 localhost
直接在您的机器上。
如果我理解正确的话,我看到您在 lcoalhost(127.0.0.1)
上打开了一个套接字,因此它只能在本地访问。如果您希望它可以从外部访问,您需要将它绑定到 0.0.0.0
,即 "all interfaces"。
在 0.0.0.0
上收听意味着可以从可以访问此计算机的网络的任何地方收听。例如,从这台计算机、本地网络或 Internet。在 127.0.0.1 上监听意味着只能从这台电脑上监听。
如果有帮助,请告诉我。或者如果我误解了你。
完整错误信息:connect ECONNREFUSED 127.0.0.1:30561 at TCPConnectWrap.afterConnect
axios 请求在 Node.js 环境中 运行ning (Next.js),这是错误发生的地方,st运行gely axios 请求完美运行当它在浏览器中 运行 时很好。
调用 axios 的组件(运行ning in Node.js):
import axios from 'axios'
import Router from 'next/router'
import React, { Component } from 'react'
import { initializeStore } from '~/reducers'
import { authenticate } from '~/actions/auth'
import { getCookieByName } from '~/helpers/cookie'
const isServer = typeof window === 'undefined'
const __NEXT_REDUX_STORE__ = '__NEXT_REDUX_STORE__'
function getOrCreateStore(initialState) {
// Always make a new store if server, otherwise state is shared between requests
if (isServer) {
return initializeStore(initialState)
}
// Create store if unavailable on the client and set it on the window object
if (!window[__NEXT_REDUX_STORE__]) {
window[__NEXT_REDUX_STORE__] = initializeStore(initialState)
}
return window[__NEXT_REDUX_STORE__]
}
export default App => {
return class AppWithRedux extends Component {
static async getInitialProps(appContext) {
const reduxStore = getOrCreateStore()
appContext.ctx.reduxStore = reduxStore
let appProps = {}
if (typeof App.getInitialProps === 'function') {
appProps = await App.getInitialProps(appContext)
}
const JWT = (isServer ? getCookieByName('JWT', appContext.ctx.req.headers.cookie) : getCookieByName('JWT', document.cookie))
const pathname = appContext.ctx.pathname
//set axios baseURL
axios.defaults.baseURL = (isServer ? `${appContext.ctx.req.headers['x-forwarded-proto']}://${appContext.ctx.req.headers.host}` : window.location.origin)
//if user has a JWT
if(JWT){
axios.defaults.headers.common['Authorization'] = `Bearer ${JWT}`
//get user from API layer
reduxStore.dispatch(authenticate())
}
return {
...appProps,
initialReduxState: reduxStore.getState()
}
}
constructor(props) {
super(props)
this.reduxStore = getOrCreateStore(props.initialReduxState)
}
render() {
return <App {...this.props} reduxStore={this.reduxStore} />
}
}
}
具体来说reduxStore.dispatch(authenticate())
我实际的 axios 调用(使用 redux thunk),查看 authenticate
方法:
import axios from 'axios'
import { setCookieByName } from '~/helpers/cookie'
const BASE_URL = '/api/auth'
export const TYPE_REGISTER = 'TYPE_REGISTER'
export const TYPE_AUTHENTICATE = 'TYPE_AUTHENTICATE'
export const register = values => (dispatch) => {
return axios.post(`${BASE_URL}/register`, values)
.then(function({data: {token, user}}){
setCookieByName('JWT', token, 365)
dispatch({
type: TYPE_REGISTER,
payload: user
})
})
}
export const authenticate = () => (dispatch) => {
return axios.post(`${BASE_URL}/me`)
.then(function({data: {user}}){
dispatch({
type: TYPE_AUTHENTICATE,
payload: user
})
})
.catch(function(err){
console.log(err)
dispatch({
type: TYPE_AUTHENTICATE,
payload: {}
})
})
}
我正在 运行使用 Docker 为 Mac 连接我的本地 Kubernetes 集群,我的 Ingress 控制器正在 http://kludge.info:30561
上被访问。我的域是从 127.0.0.1 kludge.info
本地映射的,以允许 Ingress 控制器访问容器。我的理论是,当我向 http://kludge.info:30561/api/auth/me
发送请求时,docker 容器 运行 宁 Node.js 应用程序认为这是对本地主机的请求(在容器内) ,并导致连接错误。请注意,容器内的 Node.js 应用在 http://localhost:8080
上 运行ning。基本上,我在我的机器上 运行ning localhost,在 Node 实例中使用 localhost。我如何向 http://kludge.info:30561/
外部发送请求,其中 Ingress 控制器是 运行ning.
我也配置了baseURL
in axios,但是并没有解决问题。我的入口控制器有一个路径 /api
将指向一个 PHP 实例,所以我需要我的 Node.js axios 调用来命中它的容器。任何帮助将不胜感激。
当我 运行 我的 K8 集群在 Minikube 上时我没有遇到这个问题,但是 minikube 确实为您提供了 VM 的 IP,而 Docker 桌面使用 localhost
直接在您的机器上。
如果我理解正确的话,我看到您在 lcoalhost(127.0.0.1)
上打开了一个套接字,因此它只能在本地访问。如果您希望它可以从外部访问,您需要将它绑定到 0.0.0.0
,即 "all interfaces"。
在 0.0.0.0
上收听意味着可以从可以访问此计算机的网络的任何地方收听。例如,从这台计算机、本地网络或 Internet。在 127.0.0.1 上监听意味着只能从这台电脑上监听。
如果有帮助,请告诉我。或者如果我误解了你。