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)的步骤如下: ...
随机推荐
- 2017-2018 ACM-ICPC Southeastern European Regional Programming Contest (SEERC 2017) Solution
A:Concerts 题意:给出一个串T, 一个串S,求串S中有多少个串T,可以重复,但是两个字符间的距离要满足给出的数据要求 思路:先顺序统计第一个T中的字符在S中有多少个,然后对于第二位的以及后面 ...
- 20155203 2016-2017-4 《Java程序设计》第8周学习总结
20155203 2016-2017-4 <Java程序设计>第8周学习总结 教材学习内容总结 1.channel的继承架构 2.position()类似于堆栈操作中的栈顶指针. 3.Pa ...
- 587. Erect the Fence(凸包算法)
问题 给定一群树的坐标点,画个围栏把所有树围起来(凸包). 至少有一棵树,输入和输出没有顺序. Input: [[1,1],[2,2],[2,0],[2,4],[3,3],[4,2]] Output: ...
- Union、Union All、Intersect、Minus用法和区别
假设我们有一个表Student,包括以下字段与数据: [c-sharp] view plain copydrop table student; create table student ( ...
- 【运维技术】Jenkins配置使用教程
Jenkins配置使用教程 单机jenkins启动 软件安装和启动,必须含有java环境 # 安装jdk,参考其他教程,创建文件目录 mkdir -p /app/jenkins cd /app/jen ...
- [微信开发] - UnionID以及微信开放平台
- Python学习札记(二十六) 函数式编程7 修饰器
修饰器 NOTE 1.函数对象有一个__name__属性,可以拿到函数的名字: #!/usr/bin/env python3 def now(): print('2017/2/19') def mai ...
- Codeforces Round #416 (Div. 2) C. Vladik and Memorable Trip
http://codeforces.com/contest/811/problem/C 题意: 给出一行序列,现在要选出一些区间来(不必全部选完),但是相同的数必须出现在同一个区间中,也就是说该数要么 ...
- php中正则表达式的语法规则
- Android res目录结构
所有以drawable开头的文件夹都是用来放图片的 所有以values开头的文件夹都是用来放字符串的 layout 文件夹是用来放布局文件的 menu 文件夹是用来放菜单文件的.之所以有这么多 dra ...