JSONP全局异样解决机制
1.1问题阐明
当后端服务器执行出错时,会执行全局异样的解决.然而JSONP的申请的调用要求 返回值类型 callback(JSON)构造,所以须要重构全局异样解决的返回值构造类型.
1.2编辑全局异样解决机制
package com.jt.aop;import com.fasterxml.jackson.databind.util.JSONPObject;import com.jt.vo.SysResult;import org.springframework.util.StringUtils;import org.springframework.web.bind.annotation.ControllerAdvice;import org.springframework.web.bind.annotation.ExceptionHandler;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.bind.annotation.RestControllerAdvice;import javax.servlet.http.HttpServletRequest;import java.sql.SQLException;//@ControllerAdvice //拦挡controller层//@ResponseBody@RestControllerAdvice //定义全局异样的解决类 AOP=异样告诉public class SystemAOP { /** * 定义全局异样的办法 当遇到了什么异样时,程序开始执行 参数个别class类型 * 如果一旦产生异样,则应该输入异样的信息,之后返回谬误数据即可. * * 解决跨域全局异样解决的规定: 京淘我的项目的跨域都是应用JSONP. http://xxxx?callback=xxxxx * 如果申请中携带了callback参数 则认为是JSONP跨域申请. * 难点: 如何获取callback参数呢??/ */ @ExceptionHandler({RuntimeException.class}) public Object systemAop(Exception e, HttpServletRequest request){ e.printStackTrace(); String callback = request.getParameter("callback"); if(StringUtils.isEmpty(callback)){ //惯例办法调用形式 return SysResult.fail(); }else{ //证实是jsonp跨域申请 return new JSONPObject(callback, SysResult.fail()); } }}
2.HttpClient申请
2.1业务需要
业务阐明:当做某些操作时,可能会对数据进行业务加工,之后由服务器和服务器之间造成通信.
2.2HttpClient介绍
Http协定可能是当初Interent上应用最多 最重要的协定了.越来也多的java应用程序须要间接通过Http协定来拜访网络资源.尽管在JDK的java net包中曾经提供了拜访HTTP协定的基本功能,然而对于大部分应用程序来说,JDK库自身提供的性能还不够丰盛和灵便.HttpClient是Apache Jakarta Common 下的子项目,用来提供高效的、最新的、功能丰富的反对 HTTP 协定的客户端编程工具包,并且它反对 HTTP 协定最新的版本和倡议。HttpClient 曾经利用在很多的我的项目中,比方 Apache Jakarta 上很驰名的另外两个开源我的项目 Cactus 和 HTMLUnit 都应用了 HttpClient。当初HttpClient最新版本为 HttpClient 4.5 .6(2015-09-11).
总结:在java代理外部能够应用httpClient发动http申请拜访服务器获取资源.(工具API)
2.3HttpClient入门案例
2.3.1引入jar包
<!--增加httpClient jar包 --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> </dependency>
2.3.2编辑测试API
package com.jt;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.client.methods.HttpPut;import org.apache.http.impl.client.HttpClients;import org.apache.http.util.EntityUtils;import org.junit.jupiter.api.Test;import java.io.IOException;public class HttpClientTest { /** * 要求:在java代码外部,获取百度的页面. * 实现步骤: * 1.确定指标地址: https://www.baidu.com/ * 2.创立httpClient客户端对象 * 3.创立申请类型 * 4.发动http申请.并且获取响应的后果.之后判断状态码是否为200 如果等于200则申请正确 * 5.如果申请正确则动静获取响应值信息.之后进行数据的再次加工.... * */ @Test public void testGet() throws IOException { String url = "https://www.jd.com/"; HttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(url); HttpResponse httpResponse = httpClient.execute(httpGet); if(httpResponse.getStatusLine().getStatusCode() == 200) { //示意用户申请正确 //获取返回值数据 HttpEntity httpEntity = httpResponse.getEntity(); String result = EntityUtils.toString(httpEntity, "UTF-8"); System.out.println(result); } }}
2.4HttpClient增强案例
2.4.1案例要求
用户通过网址 http://www.jt.com/getItems 要求采纳httpClient形式,获取jt-manage中的商品信息 之后json串的模式展示.jt-web服务器拜访jt-manage时的网址http://manage.jt.com/getItems.
2.4.2编辑前台HttpClientController
package com.jt.controller;import com.jt.pojo.Item;import com.jt.service.HttpClientService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestControllerpublic class HttpClientController { @Autowired private HttpClientService httpClientService; /** * 获取后端manage中的商品数据信息 */ @RequestMapping("/getItems") public List<Item> getItems(){ return httpClientService.getItems(); }}
2.4.2编辑前台HttpClientService
package com.jt.service;import com.jt.pojo.Item;import com.jt.util.ObjectMapperUtil;import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.HttpClients;import org.apache.http.util.EntityUtils;import org.springframework.stereotype.Service;import org.springframework.util.StringUtils;import java.io.IOException;import java.util.ArrayList;import java.util.List;@Servicepublic class HttpClientServiceImpl implements HttpClientService{ @Override public List<Item> getItems() { List<Item> itemList = new ArrayList<>(); //1.定义近程拜访网址 String url = "http://manage.jt.com/getItems"; HttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(url); try { HttpResponse httpResponse = httpClient.execute(httpGet); if(httpResponse.getStatusLine().getStatusCode() == 200){ String result = EntityUtils.toString(httpResponse.getEntity(), "UTF-8"); //result是jt-manage为jt-web返回的List<Item>的JSON串 if(!StringUtils.isEmpty(result)){ itemList = ObjectMapperUtil.toObj(result, itemList.getClass()); } } } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } return itemList; }}
2.4.3编辑后盾HttpClientController
package com.jt.web.controller;import com.jt.pojo.Item;import com.jt.service.ItemService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestControllerpublic class HttpClientController { @Autowired private ItemService itemService; /** * url申请地址: http://manage.jt.com/getItems */ @RequestMapping("/getItems") public List<Item> getItems(){ return itemService.getItems(); } }
2.4.4编辑后盾HttpClientService
@Override public List<Item> getItems() { return itemMapper.selectList(null); }
2.4.5页面展示成果
3.SOA思维(微服务代理编辑的规范)
面向服务的架构(SOA)是一个组件模型,它将应用程序的不同性能单元(称为服务)进行拆分,并通过这些服务之间定义良好的接口和协定分割起来。接口是采纳中立的形式进行定义的,它应该独立于实现服务的硬件平台、操作系统和编程语言。这使得构建在各种各样的零碎中的服务能够以一种对立和通用的形式进行交互。
4.RPC思维介绍
RPC是近程过程调用(Remote Procedure Call)的缩写模式。
总结:
1.当实现业务时本人没有方法间接实现时,须要通过第三方帮忙能力实现的业务.
2.应用RPC时"感觉"上就是在调用本人的办法实现业务.
5.微服务思维
外围:1.分布式思维(拆)2.自动化(HA,自动化)
5.1传统我的项目问题
1.如果采纳nginx形式实现负载平衡,当服务数量扭转时,都必须手动的批改nginx.conf配置文件.不够智能.
2.所有的申请都会通过nginx服务器作为直达.如果nginx服务器一旦宕,则间接影响整个零碎.nginx最好只做简略的反向代理即可.
传统形式不够智能
5.2微服务的调用形式介绍
调用步骤
1.将服务信息写入到注册核心(1.服务名称 2.服务IP地址 3.端口)
2.注册核心接管到服务器信息会动静保护服务列表数据.
3.消费者启动时会链接注册核心,目标是获取服务列表数据.
4.注册核心会将服务列表数据同步给消费者,并且保留到消费者本地,当前不便调用.
5.当消费者开始业务调用时,会依据已知的服务信息进行负载平衡操作,拜访服务提供者.
6.当服务提供者宕机时,因为注册核心有心跳检测机制,所以会动静保护服务列表.
7.当注册核心的服务列表变动时,则会全网进行播送,告诉所有消费者更新本地服务列表.
5.3Zookeeper注册核心介绍
ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件。它是一个为分布式应用提供一致性服务的软件,提供的性能包含:配置保护、域名服务、分布式同步、组服务等。
ZooKeeper的指标就是封装好简单易出错的要害服务,将简略易用的接口和性能高效、性能稳固的零碎提供给用户。
ZooKeeper蕴含一个简略的原语集,提供Java和C的接口。
ZooKeeper代码版本中,提供了分布式独享锁、选举、队列的接口,代码在$zookeeper_homesrcrecipes。其中散布锁和队列有Java和C两个版本,选举只有Java版本。
概括:ZK次要的工作是服务的调度,提供一致性性能.
5.4对于集群的常识介绍
5.4.1最小的集群单位几台
公式:存活节点的数量 > N/2集群能够创立
1台: 1-1<1/2 不能创立
2台: 2-1=2/2 不能创立
3台: 3-1>3/2 能够创立
4台: 4-1>4/2 能够创立
论断:搭建集群的最小单位3台.
5.4.2为什么集群个别都是奇数
准则:myid最大值优先 myid值越大的越容易入选主机 超半数批准即入选主机.
题目:问1,2,3,4,5,6,7顺次启动 问谁能入选主机 谁永远入选不了主机?
当投票进行到4时,4得票过半 入选主机.
因为1,2,3的得票永远不可能超过半数 所以1,2,3永远入选不了主机.