springcloud一些概念知识
1.Eureka
1)Eureka服务治理体系支持跨平台
2)三个核心概念:服务注册中心、服务提供者以及服务消费者
3)服务续约:注册完服务之后,服务提供者会维护一个心跳来不停的告诉Eureka Server:“我还在运行”以防止Eureka Server将该服务实例从服务列表中剔除,这个动作称之为服务续约,和服务续约相关的属性有两个,如下:
eureka.instance.lease-expiration-duration-in-seconds=90
eureka.instance.lease-renewal-interval-in-seconds=30
2.客户端负载均衡,服务端负载均衡
1)目的:是我们处理高并发、缓解网络压力和进行服务端扩容的重要手段之一
2)区别:在于服务清单所存储的位置,所有的客户端节点都有一份自己要访问的服务端清单,这些清单统统都是从Eureka服务注册中心获取的
3)服务端负载均衡:硬件负载均衡,还有一种是软件负载均衡,都会维护一个可用的服务端清单,然后通过心跳机制来删除故障的服务端节点以保证清单中都是可以正常访问的服务端节点
4)客户端负载均衡:上一篇关于springcloud服务的生产与消费中使用的就是客户端负载均衡,开启@LoadBalanced
注解即可实现springcloud客户端负载均衡
3.RestTemplate中几种常见的请求方式
这个类的功能很强大
1)Get请求
i).getForEntity:返回值是一个ResponseEntity<T>
,ResponseEntity<T>
是Spring对HTTP请求响应的封装,包括了几个重要的元素,如响应码、contentType、contentLength、响应消息体等
@RequestMapping("/gethello")
public String getHello() {
ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://HELLO-SERVICE/hello", String.class);
String body = responseEntity.getBody();
HttpStatus statusCode = responseEntity.getStatusCode();
int statusCodeValue = responseEntity.getStatusCodeValue();
HttpHeaders headers = responseEntity.getHeaders();
StringBuffer result = new StringBuffer();
result.append("responseEntity.getBody():").append(body).append("<hr>")
.append("responseEntity.getStatusCode():").append(statusCode).append("<hr>")
.append("responseEntity.getStatusCodeValue():").append(statusCodeValue).append("<hr>")
.append("responseEntity.getHeaders():").append(headers).append("<hr>");
return result.toString();
}
调用服务端接口需要传递参数,编写方式
@RequestMapping("/sayhello")
public String sayHello() {
ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://HELLO-SERVICE/sayhello?name={1}", String.class, "张三");
return responseEntity.getBody();
}
@RequestMapping("/sayhello2")
public String sayHello2() {
Map<String, String> map = new HashMap<>();
map.put("name", "李四");
ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://HELLO-SERVICE/sayhello?name={name}", String.class, map);
return responseEntity.getBody();
}
ii)getForObject:是对getForEntity函数的进一步封装,如果你只关注返回的消息体的内容,对其他信息都不关注,此时可以使用getForObject
@RequestMapping("/book2")
public Book book2() {
Book book = restTemplate.getForObject("http://HELLO-SERVICE/getbook1", Book.class);
return book;
}
2)Post请求
i)postForEntiy:与getForEntity方法差不多
@RequestMapping("/book3")
public Book book3() {
Book book = new Book();
book.setName("红楼梦");
ResponseEntity<Book> responseEntity = restTemplate.postForEntity("http://HELLO-SERVICE/getbook2", book, Book.class);
return responseEntity.getBody();
}
@RequestMapping(value = "/getbook2", method = RequestMethod.POST)
public Book book2(@RequestBody Book book) {
System.out.println(book.getName());
book.setPrice();
book.setAuthor("曹雪芹");
book.setPublisher("人民文学出版社");
return book;
}
ii)postForObject:如果你只关注,返回的消息体,可以直接使用postForObject。用法和getForObject一致
iii)postForLocation:postForLocation的参数和前面两种的参数基本一致,只不过该方法的返回值为Uri,这个只需要服务提供者返回一个Uri即可,该Uri表示新资源的位置
3)Put请求
put方法没有返回值,和post差不多
@RequestMapping("/put")
public void put() {
Book book = new Book();
book.setName("红楼梦");
restTemplate.put("http://HELLO-SERVICE/getbook3/{1}", book, );
}
4)Delete请求
@RequestMapping("/delete")
public void delete() {
restTemplate.delete("http://HELLO-SERVICE/getbook4/{1}", );
}
5)如何具备负载能力
添加@LoadBalanced注解后具备负载能力
实现流程:RestTemplate发起一个请求,这个请求被LoadBalancerInterceptor给拦截了,拦截后将请求的地址中的服务逻辑名转为具体的服务地址,然后继续执行请求
最新文章
- Nonblocking I/O and select()
- Oracle手工建库
- 读书笔记_Effective_C++_条款四十六:需要类型转换时请为模板定义非成员函数
- 续Gulp使用入门三步压缩CSS
- Codeforces Round #201 (Div. 2) - C. Alice and Bob
- BZOJ 1034: [ZJOI2008]泡泡堂BNB( 贪心 )
- SHELL编程笔记(二)之shell流程控制
- 分别使用Hadoop和Spark实现二次排序
- [转] DDD领域驱动设计框架分享
- Docker:测试环境的准备-centos7上安装docker
- Python脱产8期 Day10 2019/4/24
- 微信小程序记账本进度二
- 基础知识系列☞关键字→virtual
- POJ 3233 Matrix Power Series(二分等比求和)
- 使用 restTemplate 实现get/post 请求
- 【树莓派+.NET MF打造视频监控智能车】控制篇(树莓派)
- STM32硬件IIC
- 高可用OpenStack(Queen版)集群-9.Cinder控制节点集群
- iview组件 eslint校验出错 Parsing error: x-invalid-end-tag
- 单源最短路(spfa),删边求和
热门文章
- 【转】开发者应该了解的API技术清单
- 【BZOJ1811】[Ioi2005]mea 乱搞
- SharePoint服务器端对象模型 之 使用CAML进行数据查询(Part 4)
- Python——用正则求时间差
- IDEA 跑spring项目找不到get,set的问题
- Joiner
- Java栈和堆的区别
- java动手动脑解析
- Java FAQ -- ";Exception in thread &#39;main&#39; java.lang.UnsupportedClassVersionError:";
- python——单例模式