ballerina 学习二十二 弹性服务
主要包含断路器模式,负载均衡模式,故障转移,重试
Circuit Breaker
- 参考代码
import ballerina/http;
import ballerina/log;
import ballerina/runtime;
endpoint http:Client backendClientEP {
url: "http://localhost:8080",
circuitBreaker: {
rollingWindow: {
timeWindowMillis: 10000,
bucketSizeMillis: 2000
},
failureThreshold: 0.2,
resetTimeMillis: 10000,
statusCodes: [400, 404, 500]
}, timeoutMillis: 2000
};
@http:ServiceConfig {
basePath: "/cb"
}
service<http:Service> circuitbreaker bind { port: 9090 } {
@http:ResourceConfig {
methods: ["GET"],
path: "/"
}
invokeEndpoint(endpoint caller, http:Request request) {
var backendRes = backendClientEP->forward("/hello", request);
match backendRes { http:Response res => {
caller->respond(res) but {
error e => log:printError("Error sending response", err = e)
};
}
error responseError => {
http:Response response = new;
response.statusCode = 500;
response.setPayload(responseError.message);
caller->respond(response) but {
error e => log:printError("Error sending response", err = e)
};
}
}
}
}
public int counter = 1;
@http:ServiceConfig { basePath: "/hello" }
service<http:Service> helloWorld bind { port: 8080 } {
@http:ResourceConfig {
methods: ["GET"],
path: "/"
}
sayHello(endpoint caller, http:Request req) {
if (counter % 5 == 0) {
runtime:sleep(5000); counter = counter + 1;
http:Response res = new;
res.setPayload("Hello World!!!");
caller->respond(res) but {
error e => log:printError(
"Error sending response from mock service", err = e)
};
} else if (counter % 5 == 3) {
counter = counter + 1;
http:Response res = new;
res.statusCode = 500;
res.setPayload(
"Internal error occurred while processing the request.");
caller->respond(res) but {
error e => log:printError(
"Error sending response from mock service", err = e)
};
} else {
counter = counter + 1;
http:Response res = new;
res.setPayload("Hello World!!!");
caller->respond(res) but {
error e => log:printError(
"Error sending response from mock service", err = e)
};
}
}
}
Load Balancing(http)
- 参考代码
import ballerina/http;
import ballerina/log;
endpoint http:Listener backendEP {
port: 8080
};
endpoint http:LoadBalanceClient lbBackendEP {
targets: [
{ url: "http://localhost:8080/mock1" },
{ url: "http://localhost:8080/mock2" },
{ url: "http://localhost:8080/mock3" }
],
algorithm: http:ROUND_ROBIN,
timeoutMillis: 5000
};
@http:ServiceConfig {
basePath: "/lb"
}
service<http:Service> loadBalancerDemoService bind { port: 9090 } {
@http:ResourceConfig {
path: "/"
}
invokeEndpoint(endpoint caller, http:Request req) {
http:Request outRequest = new;
json requestPayload = { "name": "Ballerina" };
outRequest.setPayload(requestPayload);
var response = lbBackendEP->post("/", request = outRequest);
match response {
http:Response resp => {
caller->respond(resp) but {
error e => log:printError("Error sending response", err = e)
};
}
error responseError => {
http:Response outResponse = new;
outResponse.statusCode = 500;
outResponse.setPayload(responseError.message);
caller->respond(outResponse) but {
error e => log:printError("Error sending response", err = e)
};
}
}
}
}
@http:ServiceConfig { basePath: "/mock1" }
service mock1 bind backendEP {
@http:ResourceConfig {
path: "/"
}
mock1Resource(endpoint caller, http:Request req) {
http:Response outResponse = new;
outResponse.setPayload("Mock1 Resource is invoked.");
caller->respond(outResponse) but {
error e => log:printError(
"Error sending response from mock service", err = e)
};
}
}@http:ServiceConfig { basePath: "/mock2" }
service mock2 bind backendEP {
@http:ResourceConfig {
path: "/"
}
mock2Resource(endpoint caller, http:Request req) {
http:Response outResponse = new;
outResponse.setPayload("Mock2 Resource is Invoked.");
caller->respond(outResponse) but {
error e => log:printError(
"Error sending response from mock service", err = e)
};
}
}@http:ServiceConfig { basePath: "/mock3" }
service mock3 bind backendEP {
@http:ResourceConfig {
path: "/"
}
mock3Resource(endpoint caller, http:Request req) {
http:Response outResponse = new;
outResponse.setPayload("Mock3 Resource is Invoked.");
caller->respond(outResponse) but {
error e => log:printError(
"Error sending response from mock service", err = e)
};
}
}
Failover
- 参考代码
import ballerina/http;
import ballerina/log;
import ballerina/runtime;
endpoint http:Listener backendEP {
port: 8080
};
endpoint http:FailoverClient foBackendEP {
timeoutMillis: 5000,
failoverCodes: [501, 502, 503],
intervalMillis: 5000,
targets: [
{ url: "http://localhost:3000/mock1" },
{ url: "http://localhost:8080/echo" },
{ url: "http://localhost:8080/mock" }
]};
@http:ServiceConfig {
basePath: "/fo"
}
service<http:Service> failoverDemoService bind { port: 9090 } {
@http:ResourceConfig {
methods: ["GET", "POST"],
path: "/"
}
invokeEndpoint(endpoint caller, http:Request request) {
var backendRes = foBackendEP->get("/", request = request);
match backendRes {
http:Response response => {
caller->respond(response) but {
error e => log:printError("Error sending response", err = e)
};
}
error responseError => {
http:Response response = new;
response.statusCode = 500;
response.setPayload(responseError.message);
caller->respond(response) but {
error e => log:printError("Error sending response", err = e)
};
}
}
}
}
@http:ServiceConfig {
basePath: "/echo"
}
service echo bind backendEP {
@http:ResourceConfig {
methods: ["POST", "PUT", "GET"],
path: "/"
}
echoResource(endpoint caller, http:Request req) {
http:Response outResponse = new;
runtime:sleep(30000); outResponse.setPayload("echo Resource is invoked");
caller->respond(outResponse) but {
error e => log:printError(
"Error sending response from mock service", err = e)
};
}
}
@http:ServiceConfig {
basePath: "/mock"
}
service mock bind backendEP {
@http:ResourceConfig {
methods: ["POST", "PUT", "GET"],
path: "/"
}
mockResource(endpoint caller, http:Request req) {
http:Response outResponse = new;
outResponse.setPayload("Mock Resource is Invoked.");
caller->respond(outResponse) but {
error e => log:printError(
"Error sending response from mock service", err = e)
};
}
}
Retry
- 参考代码
import ballerina/http;
import ballerina/log;
import ballerina/runtime;
endpoint http:Client backendClientEP {
url: "http://localhost:8080",
retryConfig: {
interval: 3000,
count: 3,
backOffFactor: 0.5
}, timeoutMillis: 2000
};@http:ServiceConfig {
basePath: "/retry"
}
service<http:Service> retryDemoService bind { port: 9090 } {
@http:ResourceConfig {
methods: ["GET"],
path: "/"
}
invokeEndpoint(endpoint caller, http:Request request) {
var backendResponse = backendClientEP->get("/hello", request = request);
match backendResponse { http:Response response => {
caller->respond(response) but {
error e => log:printError("Error sending response", err = e)
}; }
error responseError => {
http:Response errorResponse = new;
errorResponse.statusCode = 500;
errorResponse.setPayload(responseError.message); caller->respond(errorResponse) but {
error e => log:printError("Error sending response", err = e)
};
}
}
}
}public int counter = 0;@http:ServiceConfig { basePath: "/hello" }
service<http:Service> mockHelloService bind { port: 8080 } {
@http:ResourceConfig {
methods: ["GET"],
path: "/"
}
sayHello(endpoint caller, http:Request req) {
counter = counter + 1;
if (counter % 4 != 0) {
log:printInfo(
"Request received from the client to delayed service.");
runtime:sleep(5000); http:Response res = new;
res.setPayload("Hello World!!!");
caller->respond(res) but {
error e => log:printError(
"Error sending response from mock service", err = e)
};
} else {
log:printInfo(
"Request received from the client to healthy service.");
http:Response res = new;
res.setPayload("Hello World!!!");
caller->respond(res) but {
error e => log:printError(
"Error sending response from mock service", err = e) };
}
}
}
参考资料
https://ballerina.io/learn/by-example/http-failover.html
https://ballerina.io/learn/by-example/http-circuit-breaker.html
https://ballerina.io/learn/by-example/http-load-balancer.html
https://ballerina.io/learn/by-example/http-retry.html
ballerina 学习二十二 弹性服务的更多相关文章
- ballerina 学习 三十二 编写安全的程序
ballerina编译器已经集成了部分安全检测,在编译时可以帮助我们生成错误提示,同时ballerina 标准库 已经对于常见漏洞高发的地方做了很好的处理,当我们编写了有安全隐患的代码,编译器就已 ...
- WCF技术剖析之二十四: ServiceDebugBehavior服务行为是如何实现异常的传播的?
原文:WCF技术剖析之二十四: ServiceDebugBehavior服务行为是如何实现异常的传播的? 服务端只有抛出FaultException异常才能被正常地序列化成Fault消息,并实现向客户 ...
- python3.4学习笔记(二十二) python 在字符串里面插入指定分割符,将list中的字符转为数字
python3.4学习笔记(二十二) python 在字符串里面插入指定分割符,将list中的字符转为数字在字符串里面插入指定分割符的方法,先把字符串变成list然后用join方法变成字符串str=' ...
- python3.4学习笔记(十二) python正则表达式的使用,使用pyspider匹配输出带.html结尾的URL
python3.4学习笔记(十二) python正则表达式的使用,使用pyspider匹配输出带.html结尾的URL实战例子:使用pyspider匹配输出带.html结尾的URL:@config(a ...
- Go语言学习笔记十二: 范围(Range)
Go语言学习笔记十二: 范围(Range) rang这个关键字主要用来遍历数组,切片,通道或Map.在数组和切片中返回索引值,在Map中返回key. 这个特别像python的方式.不过写法上比较怪异使 ...
- Tensorflow深度学习之十二:基础图像处理之二
Tensorflow深度学习之十二:基础图像处理之二 from:https://blog.csdn.net/davincil/article/details/76598474 首先放出原始图像: ...
- 学习笔记:CentOS7学习之二十二: 结构化命令case和for、while循环
目录 学习笔记:CentOS7学习之二十二: 结构化命令case和for.while循环 22.1 流程控制语句:case 22.2 循环语句 22.1.2 for-do-done 22.3 whil ...
- (C/C++学习笔记) 二十二. 标准模板库
二十二. 标准模板库 ● STL基本介绍 标准模板库(STL, standard template library): C++提供的大量的函数模板(通用算法)和类模板. ※ 为什么我们一般不需要自己写 ...
- VMware vSphere 服务器虚拟化之二十二桌面虚拟化之创建View Composer链接克隆的虚拟桌面池
VMware vSphere 服务器虚拟化之二十二桌面虚拟化之创建View Composer链接克隆的虚拟桌面池 在上一节我们创建了完整克隆的自动专有桌面池,在创建过程比较缓慢,这次我们将学习创建Vi ...
- Bootstrap <基础二十二>超大屏幕(Jumbotron)
Bootstrap 支持的另一个特性,超大屏幕(Jumbotron).顾名思义该组件可以增加标题的大小,并为登陆页面内容添加更多的外边距(margin).使用超大屏幕(Jumbotron)的步骤如下: ...
随机推荐
- Python: 字符串中嵌入变量
问题:想创建一个内嵌变量的字符串,变量被它的值替换掉 解决方案: ①Python并没有对在字符串中简单替换变量值提供直接的支持,但是通过字符串的format()方法来解决这个问题 ②如果要被替换的变量 ...
- excel文件与txt文件互转,并且把excel里的数据导入到oracle中
一.excel文件转换成txt文件的步骤 a.首先要把excel文件转换成txt文件 1.Excel另存为中已经包含了TXT格式,所以我们可以直接将Excel表格另存为TXT格式,但是最后的效果好像不 ...
- Core Java 6
p277~p279: 1.使用解耦合的 try/catch 和 try/finally 语句块可以提高代码的清晰度,并且会报告 finally 子句中出现的错误. 2.假设利用 return 语句从 ...
- poj1985 / poj2631(树的直径)
poj1985 Cow Marathon 树的直径裸题 树的直径的一般求法: 任意一点为起点,dfs/bfs找出与它最远的点$u$ 以$u$为起点,dfs/bfs找出与它最远的点$v$ 则$d(u,v ...
- Google发布机器学习平台Tensorflow游乐场~带你玩神经网络(转载)
Google发布机器学习平台Tensorflow游乐场-带你玩神经网络 原文地址:http://f.dataguru.cn/article-9324-1.html> 摘要: 昨天,Google发 ...
- 20145319 《网络渗透》Adobe阅读器渗透攻击
20145319 <网络渗透>Adobe阅读器渗透攻击 一 实验内容 初步掌握平台matesploit的使用 有了初步完成渗透操作的思路 本次攻击对象:windows xp sp3 Ad ...
- 初识PHP(二)常用函数
在此记录一些常用库函数和常用语法以便查阅 一.PHP手册 php手册中文地址 http://php.net/manual/zh 二.一些常用操作 2.1字符串操作 2.1.1 strpos — 查找字 ...
- sed删除包含指定字符串的所有行
1.以删除文件example.txt中包含字符串"=yes"的行为例,example.txt文件有以下内容: dadasdfsadf=yes=sds dsdadasdkfk dsd ...
- SDN前瞻 该来的来了!SDN 软件定义网络
SDDC:Software Defined Data Center 软件定义数据中心,全数据中心软件化. 在我们接触SDN概念之前,服务器虚拟化,软件虚拟化技术已经是非常成熟了.如果网络能够被虚拟化, ...
- 安装基础版的kinetic
没有gui工具 sudo apt-get install ros-kinetic-ros-base