3.2. greeting-service 示例
greeting-service 导入断路器附加组件。greeting 的调用。
-service 通过将这些调用嵌套在断路器对象中来保护对远程名称服务
greeting-service 会公开以下端点:
-
/api/greeting端点从请求问候的 Web 客户端接收调用。作为处理客户端请求的一部分,/api/greeting端点会向远程name-service中的/api/name端点发送调用。对/api/name端点的调用被嵌套在断路器对象中。如果远程name-service当前可用,/api/greeting端点会向客户端发送问候端点。但是,如果远程name-service当前不可用,/api/greeting端点会向客户端发送错误响应。 -
/api/cb-state端点返回断路器的当前状态。如果设置为open,则断路器目前阻止请求到达失败的服务。如果此值设为closed,则断路器目前允许请求访问服务。
以下代码示例演示了如何开发 greeting-service :
'use strict';
const path = require('path');
const http = require('http');
const express = require('express');
const bodyParser = require('body-parser');
// Import the circuit breaker add-on
const Opossum = require('@redhat/opossum');
const probe = require('kube-probe');
const nameService = require('./lib/name-service-client');
const app = express();
const server = http.createServer(app);
// Add basic health check endpoints
probe(app);
const nameServiceHost = process.env.NAME_SERVICE_HOST || 'http://nodejs-circuit-breaker-redhat-name:8080';
// Set some circuit breaker options
const circuitOptions = {
timeout: 3000, // If name service takes longer than 0.3 seconds,
// trigger a failure
errorThresholdPercentage: 50, // When 50% of requests fail,
// trip the circuit
resetTimeout: 10000 // After 10 seconds, try again.
};
// Create a new circuit breaker instance and pass the remote nameService
// as its first parameter
const circuit = new Opossum(nameService, circuitOptions);
// Create the app with an initial websocket endpoint
require('./lib/web-socket')(server, circuit);
// Serve index.html from the file system
app.use(express.static(path.join(__dirname, 'public')));
// Expose the license.html at http[s]://[host]:[port]/licences/licenses.html
app.use('/licenses', express.static(path.join(__dirname, 'licenses')));
// Send and receive json
app.use(bodyParser.json());
// Greeting API
app.get('/api/greeting', (request, response) => {
// Use the circuit breaker’s fire method to execute the call
// to the name service
circuit.fire(`${nameServiceHost}/api/name`).then(name => {
response.send({ content: `Hello, ${name}`, time: new Date() });
}).catch(console.error);
});
// Circuit breaker state API
app.get('/api/cb-state', (request, response) => {
response.send({ state: circuit.opened ? 'open' : 'closed' });
});
app.get('/api/name-service-host', (request, response) => {
response.send({ host: nameServiceHost });
});
module.exports = server;
'use strict';
const path = require('path');
const http = require('http');
const express = require('express');
const bodyParser = require('body-parser');
// Import the circuit breaker add-on
const Opossum = require('@redhat/opossum');
const probe = require('kube-probe');
const nameService = require('./lib/name-service-client');
const app = express();
const server = http.createServer(app);
// Add basic health check endpoints
probe(app);
const nameServiceHost = process.env.NAME_SERVICE_HOST || 'http://nodejs-circuit-breaker-redhat-name:8080';
// Set some circuit breaker options
const circuitOptions = {
timeout: 3000, // If name service takes longer than 0.3 seconds,
// trigger a failure
errorThresholdPercentage: 50, // When 50% of requests fail,
// trip the circuit
resetTimeout: 10000 // After 10 seconds, try again.
};
// Create a new circuit breaker instance and pass the remote nameService
// as its first parameter
const circuit = new Opossum(nameService, circuitOptions);
// Create the app with an initial websocket endpoint
require('./lib/web-socket')(server, circuit);
// Serve index.html from the file system
app.use(express.static(path.join(__dirname, 'public')));
// Expose the license.html at http[s]://[host]:[port]/licences/licenses.html
app.use('/licenses', express.static(path.join(__dirname, 'licenses')));
// Send and receive json
app.use(bodyParser.json());
// Greeting API
app.get('/api/greeting', (request, response) => {
// Use the circuit breaker’s fire method to execute the call
// to the name service
circuit.fire(`${nameServiceHost}/api/name`).then(name => {
response.send({ content: `Hello, ${name}`, time: new Date() });
}).catch(console.error);
});
// Circuit breaker state API
app.get('/api/cb-state', (request, response) => {
response.send({ state: circuit.opened ? 'open' : 'closed' });
});
app.get('/api/name-service-host', (request, response) => {
response.send({ host: nameServiceHost });
});
module.exports = server;