serviceMesh Collection(网络请求与网格加载)与开源工具
serviceMesh Collection(网络请求与网格加载)与开源工具
2017年04月17日 08:08:51 wenwenxiong
个人分类: serviceMesh
版权声明:本文为博主原创文章,未经博主允许不得转载。
http ????/192.168.100.26.json;
serviceMesh(服务网格)
Dog qing(GZD 的CE
在这里插入图片描述
O)给出的Service Mesh定义:
服务网格是一个用于处理服务间通信的基础设施层,它负责为构建复杂的云原生应用传递可靠的网络请求。在实践中,服务网格通常实现为一组和应用程序部署在一起的轻量级的网络代理,但对应用程序来说是透明的。
理解服务网格。
对于单个服务调用,服务网格表现为sidecar(类似kubernetes中pod的Sidecar容器)。
导入afnetworking
servicemesh01
VC
#import "ViewController.h"
#import "FooterCollectionReusableView.h"
#import "MyCollectionViewCell.h"
#import "HeaderCollectionReusableView.h"
#import "NewViewController.h"
#import "AFNetworking/AFNetworking.h"
#define TEST_URL @""
以及协议
UICollectionViewDataSource,UICollectionViewDelegate
首先定义一个字典
NSDictionary *_dic;
end前的变量
@property(nonatomic,strong)UICollectionView *myCollection;
重写方法
-(UICollectionView *)myCollection{
if (!_myCollection) {
//初始化
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
//设置大小
layout.itemSize = CGSizeMake(100, 100);
//设置滚动方向
// layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
//设置最小行间距
layout.minimumLineSpacing = 10;
//设置最小列间距
layout.minimumInteritemSpacing = 5;
//设置头视图的大小 自动适配 (如果是垂直滚动的话 宽自动是屏幕的宽,可以设置高度。反之如果是水平滚动的话 高自动是屏幕的高,可以设置宽度。)
layout.headerReferenceSize = CGSizeMake(100, 30);
//设置尾视图的大小
layout.footerReferenceSize = CGSizeMake(100, 30);
//设置每个模块中 上 左 下 右的间距(模块的边界和模块内容的距离)
layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
// _myCollection = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];
_myCollection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout];
//设置代理
_myCollection.delegate = self;
_myCollection.dataSource = self;
//注册单元格
[_myCollection registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"collectionCellId"];
//头视图单元格
[_myCollection registerClass:[HeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
[_myCollection registerClass:[FooterCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
//背景颜色
_myCollection.backgroundColor = [UIColor lightGrayColor];
}
return _myCollection;
}
viewdidload里面的解析
[self.view addSubview:self.myCollection];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [[AFJSONResponseSerializer alloc]init];
NSURL *url = [NSURL URLWithString:TEST_URL];
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
NSURLSessionDataTask *task = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
self->_dic = responseObject;
[self->_myCollection reloadData];
}];
[task resume];
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return _dic.count;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
NSString *str = _dic.allKeys[section];
return [[_dic objectForKey:str] count];
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellId [email protected]"collectionCellId";
MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
NSString *str = _dic.allKeys[indexPath.section];
NSString *imgstr = [[[_dic objectForKey:str]objectAtIndex:indexPath.row]objectForKey:@"img"];
NSURL *urlimg = [NSURL URLWithString:imgstr];
NSData *data = [NSData dataWithContentsOfURL:urlimg];
cell.imageView.image = [[UIImage alloc]initWithData:data];
cell.label.text =[[[_dic objectForKey:str]objectAtIndex:indexPath.row]objectForKey:@"name"];
cell.backgroundColor = [UIColor redColor];
return cell;
}
//设置头视图 和尾视图
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
if([kind isEqualToString:UICollectionElementKindSectionHeader]){
static NSString *cellId = @"header";
HeaderCollectionReusableView *headerCell = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:cellId forIndexPath:indexPath];
headerCell.label.text = [NSString stringWithFormat:@"第%ld组",indexPath.section+1];
headerCell.backgroundColor = [UIColor yellowColor];
return headerCell;
}else{
static NSString *cellId [email protected]"footer";
FooterCollectionReusableView *footerCell = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:cellId forIndexPath:indexPath];
footerCell.label.text = @"结尾";
footerCell.backgroundColor = [UIColor greenColor];
return footerCell;
}
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"第%ld分区---第%ld行",indexPath.section,indexPath.row);
NewViewController *new = [[NewViewController alloc]init];
NSString *str = _dic.allKeys[indexPath.section];
NSString *imgstr = [[[_dic objectForKey:str]objectAtIndex:indexPath.row]objectForKey:@"img"];
NSURL *urlimg = [NSURL URLWithString:imgstr];
NSData *data = [NSData dataWithContentsOfURL:urlimg];
new.img = [[UIImage alloc]initWithData:data];
new.name= [[[_dic objectForKey:str]objectAtIndex:indexPath.row]objectForKey:@"name"];
[self presentViewController:new animated:YES completion:nil];
}
@end
有多个服务调用,服务网格表现为服务间通讯专用基础设施层。在这种情况下,服务不再负责传递请求的具体逻辑,只负责完成业务处理。服务间通讯的环节就从应用里面剥离出来,呈现出一个抽象层。
servicemesh02
有大量服务,服务网格表现为服务网格形状,Sidecar之间的连接形成网格。首先第一个,服务网格是抽象的,实际上是抽象出了一个基础设施层,在应用之外。其次,功能是实现请求的可靠传递。部署上体现为轻量级的网络代理。最后一个关键词是,对应用程序透明。
servicemesh03
2.NewVC.h
这里是引用 |
@property(nonatomic,strong)UIImage *img;
@property(nonatomic,strong)NSString *name;`| |
|–|
.m
#import "NewViewController.h"
@interface NewViewController ()
@property(nonatomic,strong)UIImageView *imageView;
@property(nonatomic,strong)UILabel *label;
@end
@implementation NewViewController
-(UIImageView *)imageView{
if (!_imageView) {
_imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-50)];
}
return _imageView;
}
-(UILabel *)label{
if (!_label) {
_label = [[UILabel alloc]initWithFrame:CGRectMake(200, self.view.frame.size.height-40, 200, 30)];
}
return _label;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.imageView];
[self.view addSubview:self.label];
self.imageView.image = self.img;
self.label.text = self.name;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
定义关键点
抽象:基础设施层
功能:实现请求的可靠传递
部署:轻量级网络代理
关键:对应用程序透明
3.MyCollectionViewCell.h
@property(nonatomic,strong)UIImageView *imageView;
@property(nonatomic,strong)UILabel *label;
#import "MyCollectionViewCell.h"
@implementation MyCollectionViewCell
-(UIImageView *)imageView{
if (!_imageView) {
_imageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 10, 60, 50)];
}
return _imageView;
}
-(UILabel *)label{
if (!_label) {
_label = [[UILabel alloc]initWithFrame:CGRectMake(30, 70, 80, 20)];
}
return _label;
}
-(instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
[self addSubview:self.imageView];
[self addSubview:self.label];
}
return self;
}
@end
Service Mesh定义当中一个.非常重要的关键点,和Sidecar不相同的地方:不再将代理视为单独的组件,而是强调由这些代理连接而形成的网络。在Service Mesh里面非常强调代理连接组成的网络,而不像Sidecar那样看待个体。
servicemesh04
servicemesh开源工具
4.HeaderCollectionReusableView.h
@property(nonatomic,strong) UILabel *label;
.m
#import "HeaderCollectionReusableView.h"
@implementation HeaderCollectionReusableView
-(UILabel *)label{
if (!_label) {
_label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
}
return _label;
}
-(instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
[self addSubview:self.label];
}
return self;
}
@end
istio
Istio是由Google、IBM和Lyft开源的微服务管理、保护和监控框架。
使用istio可以很简单的创建具有负载均衡、服务间认证、监控等功能的服务网络,而不需要对服务的代码进行任何修改。只需要在部署环境中,例如Kubernetes的pod里注入一个特别的sidecar proxy来增加对istio的支持,用来截获微服务之间的网络流量。
5.FooterCollectionReusableView.h
@property(nonatomic,strong)UILabel *label;
.m
#import "FooterCollectionReusableView.h"
@implementation FooterCollectionReusableView
-(UILabel *)label{
if (!_label) {
_label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
}
return _label;
}
-(instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
[self addSubview:self.label];
}
return self;
}
@end
linkerd
linkerd由Buoyant开源,也是业界第一个Service Mesh项目。
Linkerd 是一个提供弹性云端原生应用服务网格的开源项目。其核心是一个透明代理,可以用它来实现一个专用的基础设施层以提供服务间的通信,进而为软件应用提供服务发现、路由、错误处理以及服务可见性等功能,而无需侵入应用内部本身的实现。
conduit
Buoyant在Linkerd不敌Istio的恶劣情况下,绝地反击,推出全新设计的 Conduit 作为对抗 Istio 的武器。
Conduit是一款针对Kubernetes的超轻量级的service mesh。可以透明得管理服务运行时之间的通信,使得在Kubernetes上运行服务更加安全和可靠;还具有不用修改任何应用程序代码即可改进应用程序的可观测性、可靠性及安全性等方面的特性。
serviceMesh开源工具对比
Feature Istio Linkerd Conduit
功能支持 负载均衡,服务TLS认证,服务调用监控,熔断,动态请求路由,服务发现,服务间流量管理,服务间访问策略管理 负载均衡,服务TLS认证,服务调用监控,熔断,动态请求路由,服务发现 官方文档暂未列出,发展还不完善
第三方插件集成 分布式调用链跟踪Zipkin、监控套件Prometheus与Grafana、日志套件EFK、服务图展示ServiceGraph 分布式调用链跟踪Zipkin、监控套件Prometheus,InfluxDB,StatsD 监控套件Prometheus
部署架构 Envoy/Sidecar DaemonSets sidecar
易用性 复杂 简单 适中
支持平台 kuberentes kubernetes/mesos/Istio/local kuberentes
当前版本 0.7 1.3.7 0.3
是否已有生产部署 否 是 否
评分 ★★★★☆ ★★☆☆☆ ★★☆☆☆