如何解决无法向 Eureka 服务器注册 React 应用程序
错误:
访问获取 'http://127.0.0.1:8761/eureka/apps/auth0-react-sample' 来自原点 'http://localhost:3000' 已被 CORS 策略阻止:响应 预检请求未通过访问控制检查:否 请求中存在“Access-Control-Allow-Origin”标头 资源。如果不透明的响应满足您的需求,请设置请求的 模式为 'no-cors' 以在禁用 CORS 的情况下获取资源。
代码:
Index.js (React 应用)
const PORT = process.env.PORT || 3000;
const eurekaHelper = require('./eureka-helper/eureka-helper');
const Eureka = require('eureka-js-client').Eureka;
// const PORT = 3000;
// const HOST = '0.0.0.0';
// example configuration
const client = new Eureka({
// application instance information
instance: {
app: 'auth0-react-sample',hostName: 'localhost',ipAddr: 'localhost',//statusPageUrl: 'http://localhost:3000/',port: 3000,vipAddress: 'auth0-react-sample',dataCenterInfo: {
name: 'default',},registerWithEureka: true,fetchRegistry: true,eureka: {
// eureka server host / port
host: 'localhost',port: 8761,servicePath: '/eureka/apps',});
// Listen on a specific host via the HOST environment variable
var host = process.env.HOST || '0.0.0.0';
// Listen on a specific port via the PORT environment variable
var port = process.env.PORT || 3000;
const onRedirectCallback = (appState) => {
history.push(
appState && appState.returnTo ? appState.returnTo : window.location.pathname
);
};
// Please see https://auth0.github.io/auth0-react/interfaces/auth0provideroptions.html
// for a full list of the available properties on the provider
const config = getConfig();
const providerConfig = {
domain: config.domain,clientId: config.clientId,...(config.audience ? { audience: config.audience } : null),redirectUri: window.location.origin,onRedirectCallback,};
client.start(error=>{
console.log(error || 'NodeJS Eureka started');
ReactDOM.render(
<Auth0Provider {...providerConfig}>
<App />
</Auth0Provider>,document.getElementById("root")
);
});
eurekaHelper.registerWithEureka('auth0-react-sample',PORT);
serviceWorker.unregister();
eureka-helper.js
const Eureka = require('eureka-js-client').Eureka;
const eurekaHost = (process.env.EUREKA_CLIENT_SERVICEURL_DEFAULTZONE || '127.0.0.1');
const eurekaPort = 8761;
const hostName = (process.env.HOSTNAME || 'localhost')
const ipAddr = '127.0.0.1';
exports.registerWithEureka = function(appName,PORT) {
const client = new Eureka({
instance: {
app: appName,hostName: hostName,ipAddr: ipAddr,port: {
'$': PORT,'@enabled': 'true',vipAddress: appName,dataCenterInfo: {
'@class': 'com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo',name: 'MyOwn',//retry 10 time for 3 minute 20 seconds.
eureka: {
host: eurekaHost,port: eurekaPort,servicePath: '/eureka/apps/',maxRetries: 10,requestRetryDelay: 2000,})
client.logger.level('debug')
client.start( error => {
console.log(error || "user service registered")
});
function exitHandler(options,exitCode) {
if (options.cleanup) {
}
if (exitCode || exitCode === 0) console.log(exitCode);
if (options.exit) {
client.stop();
}
}
client.on('deregistered',() => {
process.exit();
console.log('after deregistered');
})
client.on('started',() => {
console.log("eureka host " + eurekaHost);
})
process.on('SIGINT',exitHandler.bind(null,{exit:true}));
};
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。