5.2. 使用 circuit breaker 事件的事件处理程序的示例应用
本例演示了如何侦听断路器发出的不同事件类型。在本例中,使用 fetch 函数来请求微服务 URL。根据事件类型,相关消息会输出到 web 控制台。
...
// Import the circuit breaker add-on
const Opossum = require('@redhat/opossum');
...
// Create a new circuit breaker instance and pass the
// protected function call as its first parameter
const circuit = new Opossum(() => $.get(route), circuitOptions);
...
// Listen for success events emitted when the
// circuit breaker action completes successfully
circuit.on('success',
(result) => {
console.log(`SUCCESS: ${JSON.stringify(result)}`);
}
// Listen for timeout events emitted when the timeout
// period for the circuit breaker action expires
circuit.on('timeout',
(result) => {
console.log(`TIMEOUT: ${url} is taking too long to respond.`);
}
// Listen for reject events emitted when the
// circuit breaker has an open or half-open state
circuit.on('reject',
(result) => {
console.log(`REJECTED: The breaker for ${url} is open. Failing fast.`);
}
// Listen for open events emitted when the
// circuit breaker moves to an open state
circuit.on('open',
(result) => {
console.log(`OPEN: The breaker for ${url} just opened.`);
}
// Listen for halfOpen events emitted when the
// circuit breaker moves to a half-open state
circuit.on('halfOpen',
(result) => {
console.log(`HALF_OPEN: The breaker for ${url} is half open.`);
}
// Listen for close events emitted when the
// circuit breaker moves to a closed state
circuit.on('close',
(result) => {
console.log(`CLOSE: The breaker for ${url} has closed. Service OK.`);
}
// Listen for fallback events emitted when
// a fallback function is executed
circuit.on('fallback',
(result) => {
console.log(`FALLBACK: ${JSON.stringify(data)}`);
}
...
// Import the circuit breaker add-on
const Opossum = require('@redhat/opossum');
...
// Create a new circuit breaker instance and pass the
// protected function call as its first parameter
const circuit = new Opossum(() => $.get(route), circuitOptions);
...
// Listen for success events emitted when the
// circuit breaker action completes successfully
circuit.on('success',
(result) => {
console.log(`SUCCESS: ${JSON.stringify(result)}`);
}
// Listen for timeout events emitted when the timeout
// period for the circuit breaker action expires
circuit.on('timeout',
(result) => {
console.log(`TIMEOUT: ${url} is taking too long to respond.`);
}
// Listen for reject events emitted when the
// circuit breaker has an open or half-open state
circuit.on('reject',
(result) => {
console.log(`REJECTED: The breaker for ${url} is open. Failing fast.`);
}
// Listen for open events emitted when the
// circuit breaker moves to an open state
circuit.on('open',
(result) => {
console.log(`OPEN: The breaker for ${url} just opened.`);
}
// Listen for halfOpen events emitted when the
// circuit breaker moves to a half-open state
circuit.on('halfOpen',
(result) => {
console.log(`HALF_OPEN: The breaker for ${url} is half open.`);
}
// Listen for close events emitted when the
// circuit breaker moves to a closed state
circuit.on('close',
(result) => {
console.log(`CLOSE: The breaker for ${url} has closed. Service OK.`);
}
// Listen for fallback events emitted when
// a fallback function is executed
circuit.on('fallback',
(result) => {
console.log(`FALLBACK: ${JSON.stringify(data)}`);
}