首页 文章 精选 留言 我的

精选列表

搜索[java],共10000篇文章
优秀的个人博客,低调大师

Java版Spring Cloud B2B2C o2o社交电商-客户端负载均衡策略

一、负载均衡介绍 负载均衡(Load Balance):建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽、增加吞吐量、加强网络数据处理能力、提高网络的灵活性和可用性。其意思就是分摊到多个操作单元上进行执行,例如Web服务器、FTP服务器、企业关键应用服务器和其它关键任务服务器等,从而共同完成工作任务。 1、服务端负载均衡:客户端请求到负载均衡服务器,负载均衡服务器根据自身的算法将该请求转给某台真正提供业务的服务器,该服务器将响应数据给负载均衡服务器,负载均衡服务器最后将数据返回给客服端。(nginx) 2、客服端负载均衡:基于客户端的负载均衡,简单的说就是在客户端程序里面,自己设定一个调度算法,在向服务器发起请求的时候,先执行调度算法计算出向哪台服务器发起请求,然后再发起请求给服务器。 二、负载均衡策略介绍 (1) AbstractLoadBalancerRule AbstractLoadBalancerRule是一个抽象类,里边主要定义了一个ILoadBalancer,定义它的目的主要是辅助负责均衡策略选取合适的服务端实例。 (2) RandomRule该负载均衡策略就是随机选择一个服务实例,看源码我们知道,在RandomRule的无参构造方法中初始化了一个Random对象,然后在它重写的choose方法又调用了choose(ILoadBalancer lb, Object key)这个重载的choose方法,在这个重载的choose方法中,每次利用random对象生成一个不大于服务实例总数的随机数,并将该数作为下标所以获取一个服务实例。 (3) RoundRobinRule RoundRobinRule这种负载均衡策略叫做线性负载均衡策略,也就是我们在上文所说的BaseLoadBalancer负载均衡器中默认采用的负载均衡策略。这个类的choose(ILoadBalancer lb, Object key)函数整体逻辑是这样的:开启一个计数器count,在while循环中遍历服务清单,获取清单之前先通过incrementAndGetModulo方法获取一个下标,这个下标是一个不断自增长的数先加1然后和服务清单总数取模之后获取到的(所以这个下标从来不会越界),拿着下标再去服务清单列表中取服务,每次循环计数器都会加1,如果连续10次都没有取到服务,则会报一个警告No available alive servers after 10 tries from load balancer: XXXX。 (4) RetryRule 看名字就知道这种负载均衡策略带有重试功能。首先RetryRule中又定义了一个subRule,它的实现类是RoundRobinRule,然后在RetryRule的choose(ILoadBalancer lb, Object key)方法中,每次还是采用RoundRobinRule中的choose规则来选择一个服务实例,如果选到的实例正常就返回,如果选择的服务实例为null或者已经失效,则在失效时间deadline之前不断的进行重试(重试时获取服务的策略还是RoundRobinRule中定义的策略),如果超过了deadline还是没取到则会返回一个null。 (5) WeightedResponseTimeRule WeightedResponseTimeRule是RoundRobinRule的一个子类,在WeightedResponseTimeRule中对RoundRobinRule的功能进行了扩展,WeightedResponseTimeRule中会根据每一个实例的运行情况来给计算出该实例的一个权重,然后在挑选实例的时候则根据权重进行挑选,这样能够实现更优的实例调用。WeightedResponseTimeRule中有一个名叫DynamicServerWeightTask的定时任务,默认情况下每隔30秒会计算一次各个服务实例的权重,权重的计算规则也很简单,如果一个服务的平均响应时间越短则权重越大,那么该服务实例被选中执行任务的概率也就越大。 (6) ClientConfigEnabledRoundRobinRule ClientConfigEnabledRoundRobinRule选择策略的实现很简单,内部定义了RoundRobinRule,choose方法还是采用了RoundRobinRule的choose方法,所以它的选择策略和RoundRobinRule的选择策略一致,不赘述。 (7) BestAvailableRule BestAvailableRule继承自ClientConfigEnabledRoundRobinRule,它在ClientConfigEnabledRoundRobinRule的基础上主要增加了根据loadBalancerStats中保存的服务实例的状态信息来过滤掉失效的服务实例的功能,然后顺便找出并发请求最小的服务实例来使用。然而loadBalancerStats有可能为null,如果loadBalancerStats为null,则BestAvailableRule将采用它的父类即ClientConfigEnabledRoundRobinRule的服务选取策略(线性轮询)。 (8) PredicateBasedRule PredicateBasedRule是ClientConfigEnabledRoundRobinRule的一个子类,它先通过内部定义的一个过滤器过滤出一部分服务实例清单,然后再采用线性轮询的方式从过滤出来的结果中选取一个服务实例。 (9) ZoneAvoidanceRule ZoneAvoidanceRule是PredicateBasedRule的一个实现类,只不过这里多一个过滤条件,ZoneAvoidanceRule中的过滤条件是以ZoneAvoidancePredicate为主过滤条件和以AvailabilityPredicate为次过滤条件组成的一个叫做CompositePredicate的组合过滤条件,过滤成功之后,继续采用线性轮询的方式从过滤结果中选择一个出来。使用ZoneAvoidancePredicate和AvailabilityPredicate来判断是否选择某个server,前一个判断判定一个zone的运行性能是否可用,剔除不可用的zone(的所有server),AvailabilityPredicate用于过滤掉连接数过多的Server。

优秀的个人博客,低调大师

java B2B2C电子商务平台分析之九--配置中心服务化和高可用

在前两篇的介绍中,客户端都是直接调用配置中心的server端来获取配置文件信息。这样就存在了一个问题,客户端和服务端的耦合性太高,如果server端要做集群,客户端只能通过原始的方式来路由,server端改变IP地址的时候,客户端也需要修改配置,不符合springcloud服务治理的理念。springcloud提供了这样的解决方案,我们只需要将server端当做一个服务注册到eureka中,client端去eureka中去获取配置中心server端的服务既可。愿意了解源码的朋友直接求求交流分享技术:二一四七七七五六三三 server端改造1、添加依赖 <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> </dependencies> 需要多引入spring-cloud-starter-eureka包,来添加对eureka的支持。 2、配置文件 server: server: port: 8001 spring: application: name: spring-cloud-config-server cloud: config: server: git: uri: https://github.com/ityouknow/spring-cloud-starter/ # 配置git仓库的地址 search-paths: config-repo # git仓库地址下的相对地址,可以配置多个,用,分割。 username: username # git仓库的账号 password: password # git仓库的密码 eureka: client: serviceUrl: defaultZone: http://localhost:8000/eureka/ ## 注册中心eurka地址 增加了eureka注册中心的配置 3、启动类启动类添加@EnableDiscoveryClient激活对配置中心的支持 @EnableDiscoveryClient @EnableConfigServer @SpringBootApplication public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } } 这样server端的改造就完成了。先启动eureka注册中心,在启动server端,在浏览器中访问:http://localhost:8000/就会看到server端已经注册了到注册中心了。 按照上篇的测试步骤对server端进行测试服务正常。 客户端改造1、添加依赖 <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> 需要多引入spring-cloud-starter-eureka包,来添加对eureka的支持。 2、配置文件 spring.application.name=spring-cloud-config-client server.port=8002 spring.cloud.config.name=neo-config spring.cloud.config.profile=dev spring.cloud.config.label=master spring.cloud.config.discovery.enabled=true spring.cloud.config.discovery.serviceId=spring-cloud-config-server eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/ 主要是去掉了spring.cloud.config.uri直接指向server端地址的配置,增加了最后的三个配置: spring.cloud.config.discovery.enabled:开启Config服务发现支持spring.cloud.config.discovery.serviceId:指定server端的name,也就是server端spring.application.name的值eureka.client.serviceUrl.defaultZone:指向配置中心的地址这三个配置文件都需要放到bootstrap.properties的配置中 3、启动类启动类添加@EnableDiscoveryClient激活对配置中心的支持 @EnableDiscoveryClient @SpringBootApplication public class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } } 启动client端,在浏览器中访问:http://localhost:8000/就会看到server端和client端都已经注册了到注册中心了。 高可用为了模拟生产集群环境,我们改动server端的端口为8003,再启动一个server端来做服务的负载,提供高可用的server端支持。 如上图就可发现会有两个server端同时提供配置中心的服务,防止某一台down掉之后影响整个系统的使用。 我们先单独测试服务端,分别访问:http://localhost:8001/neo-config/dev、http://localhost:8003/neo-config/dev返回信息: { "name": "neo-config", "profiles": [ "dev" ], "label": null, "version": null, "state": null, "propertySources": [ { "name": "https://github.com/ityouknow/spring-cloud-starter/config-repo/neo-config-dev.properties", "source": { "neo.hello": "hello im dev" } } ] } 说明两个server端都正常读取到了配置信息。 再次访问:http://localhost:8002/hello, 返回:hello im dev update。说明客户端已经读取到了server端的内容,我们随机停掉一台server端的服务,再次访问http://localhost:8002/hello, 返回:hello im dev update,说明达到了高可用的目的。 整体代码结构如下: 资料和源码来源

优秀的个人博客,低调大师

java实现判断一个经纬度坐标是否在一个多边形内(经自己亲测)

1.在高德地图上绘制的多边形;经纬度逗号分隔格式;上面是用来方便存坐标的对象;下面是方法测试;直接复制代码即可运行 public class Point { private Double x; private Double y; public Point (Double x , Double y) { this.x = x; this.y = y; } public Double getX() { return x; } public void setX(Double x) { this.x = x; } public Double getY() { return y; } public void setY(Double y) { this.y = y; } } public class Test01 { public static void main(String[] args) { //114.331951,30.64091#114.341049,30.610185#114.331436,30.588058#114.312038,30.56393#114.293498,30.558609#114.267922,30.563784#114.231185,30.57945#114.212303,30.601616#114.235649,30.626878#114.280624,30.646818# Map [] map=new Map[]{}; Point[] ps = new Point[] { new Point(114.309914,30.599556),//114.309914,30.599556 new Point(114.295688,30.592879),//114.295688,30.592879 new Point(114.292812,30.587726), //114.292812,30.587726 new Point(114.292812,30.587726), //114.292812,30.587726 new Point(114.30058,30.580318),//114.30058,30.580318 new Point(114.303606,30.586959),//114.303606,30.586959 new Point(114.304534,30.594751),//114.304534,30.594751 new Point(114.30838,30.590131),//114.30838,30.590131 new Point(114.308651,30.584182),//114.308651,30.584182 new Point(114.304495,30.584015),//114.304495,30.584015 new Point(114.301301,30.578759),//114.301301,30.578759 new Point(114.309437,30.578528),//114.309437,30.578528 new Point(114.323282,30.592786)};//114.323282,30.592786 Point n1 = new Point(114.303217,30.583553); Point n2 = new Point(114.307336,30.597592); Point n3 = new Point(114.286565,30.590056); Point y1 = new Point(114.227342,30.587987); Point y2 = new Point(120.1866 , 30.2672); Point y4 = new Point(120.1869 , 30.2718); System.out.println( "n1:" + isPtInPoly(n1.getX() , n1.getY() , ps)); System.out.println( "n2:" + isPtInPoly(n2.getX() , n2.getY() , ps)); System.out.println( "n3:" + isPtInPoly(n3.getX() , n3.getY() , ps)); System.out.println( "y1:" + isPtInPoly(y1.getX() , y1.getY() , ps)); System.out.println( "y2:" + isPtInPoly(y2.getX() , y2.getY() , ps)); System.out.println( "y4:" + isPtInPoly(y4.getX() , y4.getY() , ps)); } public static boolean isPtInPoly (double ALon , double ALat , Point[] ps) { int iSum, iCount, iIndex; double dLon1 = 0, dLon2 = 0, dLat1 = 0, dLat2 = 0, dLon; if (ps.length < 3) { return false; } iSum = 0; iCount = ps.length; for (iIndex = 0; iIndex<iCount;iIndex++) { if (iIndex == iCount - 1) { dLon1 = ps[iIndex].getX(); dLat1 = ps[iIndex].getY(); dLon2 = ps[0].getX(); dLat2 = ps[0].getY(); } else { dLon1 = ps[iIndex].getX(); dLat1 = ps[iIndex].getY(); dLon2 = ps[iIndex + 1].getX(); dLat2 = ps[iIndex + 1].getY(); } // 以下语句判断A点是否在边的两端点的水平平行线之间,在则可能有交点,开始判断交点是否在左射线上 if (((ALat >= dLat1) && (ALat < dLat2)) || ((ALat >= dLat2) && (ALat < dLat1))) { if (Math.abs(dLat1 - dLat2) > 0) { //得到 A点向左射线与边的交点的x坐标: dLon = dLon1 - ((dLon1 - dLon2) * (dLat1 - ALat) ) / (dLat1 - dLat2); // 如果交点在A点左侧(说明是做射线与 边的交点),则射线与边的全部交点数加一: if (dLon < ALon) { iSum++; } } } } if ((iSum % 2) != 0) { return true; } return false; } }

优秀的个人博客,低调大师

【LeetCode-面试算法经典-Java实现】【111-Minimum Depth of Binary Tree(二叉树的最小深度)】

原题 Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 题目大意 给定一棵两叉树求树的最小深度。 解题思路 遍历法,对整个树进行遍历,找出最小的深度。 代码实现 树结果定义 public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } 1 2 3 4 5 6 算法实现类 public class Solution { private int min = Integer.MAX_VALUE; // 记录树的最小深度 private int cur = 0; // i当前处理的树的尝试 public int minDepth(TreeNode root) { depth(root); return min; } /** * 计算树的深度 * * @param node 当前结点 */ private void depth(TreeNode node) { if (node == null) { min = cur; return; } cur++; // 当前处理的层次加1 // 如果是叶节点,并且路径比记录的最小还小 if (node.left == null && node.right == null && cur < min) { min = cur; // 更新最小值 } // 处理左子树 if (node.left != null) { depth(node.left); } // 处理右子树 if (node.right != null) { depth(node.right); } cur--; // 还原 } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 /** * 计算树的深度 * * @param node 当前结点 */ private void depth (TreeNode node) { if (node == null ) { min = cur; return ; } cur++; // 当前处理的层次加1 // 如果是叶节点,并且路径比记录的最小还小 if (node.left == null && node.right == null && cur < min) { min = cur; // 更新最小值 } // 处理左子树 if (node.left != null ) { depth(node.left); } // 处理右子树 if (node.right != null ) { depth(node.right); } cur--; // 还原 }}

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册