Docker-compose 使2个微服务(前端+后端)通过http请求相互通信
Docker-compose make 2 microservices (frontend+backend) communicate to each other with http requests
我有 2 个微服务:前端 next.js 和后端 node.js 从那里我通过 REST-APIs 从前端获取数据。
我现在遇到了问题,我的 2 个服务似乎没有直接相互通信,问题是,当我在开始时使用 getinitialProps() 方法和 fetch-API。我的服务器端前端通过其服务名称找到后端。但是,当我执行从客户端到后端的 http 请求时(例如,通过浏览器表单输入)。它找不到后端了?这是为什么?
这是我的 docker-compose.yml:
version: '3'
services:
dcbackend:
container_name: dcbackend
build:
context: ./dcbackend
dockerfile: Dockerfile
image: dcbackend
hostname: dcbackend
ports:
- '7766:7766'
dcfrontend:
container_name: dcfrontend
build:
context: ./dcfrontend
dockerfile: Dockerfile
image: dcfrontend
volumes:
- /app/node_modules
- ./dcfrontend:/app
hostname: dcfrontend
ports:
- '6677:6677'
这是我的浏览器客户端发送数据到后端的方法之一(通过浏览器,我的 url 是 http:dcbackend...所以通常它应该找到另一个 docker 后端所在的环境,但它不...
if (environment == 'dev') {
url_link = `http://localhost:${port}`;
} else {
url_link = `http://dcbackend:${port}`;
}
let doublettenListe_link = `${url_link}/doubletten/`;
finishDocumentHandler = (anzeige,index) => {
let thisDocumentID = anzeige.id;
const requestOptions = {
method: 'PUT'
};
fetch(doublettenListe_link + thisDocumentID, requestOptions)
.then((response) => {
this.setState({finishSuccess: 'Dubletten in Datenbank eintragen erfolgreich!'});
this.setState({finishFail: ''});
this.processDocumentArray(index);
console.log(response);
})
.catch((error) => {
this.setState({finishSuccess: ''});
this.setState({finishFail : `Error beim Erzeugen des Eintrags! Eintrag wurde nicht in Datenbank gespeichert. Bitte prüfen, ob der Server läuft. ${error}`});
});
}
网络选项卡对我的请求的响应是:
Request URL: http://dcbackend:7766/doubletten/304699981
Referrer Policy: no-referrer-when-downgrade
Provisional headers are shown
Access-Control-Request-Method: PUT
Origin: http://localhost:6677
Referer: http://localhost:6677/
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36
它是否与 docker-configuration、CORS() 或其他任何东西有关?我无法向后端发出客户端 http 请求,但是,从后端获取一些数据的初始提取工作正常...
您必须将服务器端和客户端请求分开。您需要为客户端请求使用您的主机地址(例如 http://localhost:7766),因为您的浏览器将无法通过 docker 别名到达后端。
您可以使用 next.config.js
.
定义仅服务器和 public 运行时配置
例如:
// next.config.js
module.exports = {
serverRuntimeConfig: {
// Will only be available on the server side
apiUrl: 'http://dcbackend:7766'
},
publicRuntimeConfig: {
// Will be available on both server and client
apiUrl: 'http://localhost:7766'
}
}
然后你需要使用 getConfig()
从 nextjs 获取 apiUrl
// pages/index.js
import getConfig from 'next/config';
const { serverRuntimeConfig, publicRuntimeConfig } = getConfig();
const apiUrl = serverRuntimeConfig.apiUrl || publicRuntimeConfig.apiUrl;
const Index = ({ json }) => <div>Index</div>;
Index.getInitialProps = async () => {
try {
const res = await fetch(`${apiUrl}/doubletten/304699981`);
const json = await res.json();
return { json };
} catch(e) {
console.log('Failed to fetch', e);
return { json: null };
}
}
export default Index;
我有 2 个微服务:前端 next.js 和后端 node.js 从那里我通过 REST-APIs 从前端获取数据。
我现在遇到了问题,我的 2 个服务似乎没有直接相互通信,问题是,当我在开始时使用 getinitialProps() 方法和 fetch-API。我的服务器端前端通过其服务名称找到后端。但是,当我执行从客户端到后端的 http 请求时(例如,通过浏览器表单输入)。它找不到后端了?这是为什么?
这是我的 docker-compose.yml:
version: '3'
services:
dcbackend:
container_name: dcbackend
build:
context: ./dcbackend
dockerfile: Dockerfile
image: dcbackend
hostname: dcbackend
ports:
- '7766:7766'
dcfrontend:
container_name: dcfrontend
build:
context: ./dcfrontend
dockerfile: Dockerfile
image: dcfrontend
volumes:
- /app/node_modules
- ./dcfrontend:/app
hostname: dcfrontend
ports:
- '6677:6677'
这是我的浏览器客户端发送数据到后端的方法之一(通过浏览器,我的 url 是 http:dcbackend...所以通常它应该找到另一个 docker 后端所在的环境,但它不...
if (environment == 'dev') {
url_link = `http://localhost:${port}`;
} else {
url_link = `http://dcbackend:${port}`;
}
let doublettenListe_link = `${url_link}/doubletten/`;
finishDocumentHandler = (anzeige,index) => {
let thisDocumentID = anzeige.id;
const requestOptions = {
method: 'PUT'
};
fetch(doublettenListe_link + thisDocumentID, requestOptions)
.then((response) => {
this.setState({finishSuccess: 'Dubletten in Datenbank eintragen erfolgreich!'});
this.setState({finishFail: ''});
this.processDocumentArray(index);
console.log(response);
})
.catch((error) => {
this.setState({finishSuccess: ''});
this.setState({finishFail : `Error beim Erzeugen des Eintrags! Eintrag wurde nicht in Datenbank gespeichert. Bitte prüfen, ob der Server läuft. ${error}`});
});
}
网络选项卡对我的请求的响应是:
Request URL: http://dcbackend:7766/doubletten/304699981
Referrer Policy: no-referrer-when-downgrade
Provisional headers are shown
Access-Control-Request-Method: PUT
Origin: http://localhost:6677
Referer: http://localhost:6677/
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36
它是否与 docker-configuration、CORS() 或其他任何东西有关?我无法向后端发出客户端 http 请求,但是,从后端获取一些数据的初始提取工作正常...
您必须将服务器端和客户端请求分开。您需要为客户端请求使用您的主机地址(例如 http://localhost:7766),因为您的浏览器将无法通过 docker 别名到达后端。
您可以使用 next.config.js
.
例如:
// next.config.js
module.exports = {
serverRuntimeConfig: {
// Will only be available on the server side
apiUrl: 'http://dcbackend:7766'
},
publicRuntimeConfig: {
// Will be available on both server and client
apiUrl: 'http://localhost:7766'
}
}
然后你需要使用 getConfig()
apiUrl
// pages/index.js
import getConfig from 'next/config';
const { serverRuntimeConfig, publicRuntimeConfig } = getConfig();
const apiUrl = serverRuntimeConfig.apiUrl || publicRuntimeConfig.apiUrl;
const Index = ({ json }) => <div>Index</div>;
Index.getInitialProps = async () => {
try {
const res = await fetch(`${apiUrl}/doubletten/304699981`);
const json = await res.json();
return { json };
} catch(e) {
console.log('Failed to fetch', e);
return { json: null };
}
}
export default Index;