首页 文章 精选 留言 我的

精选列表

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

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--; // 还原 }}

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

java支付宝开发-异常-01-"sub_code":"isv.invalid-app-id","sub_msg":"无效的AppID参数"

一、现象 无论请求哪个接口都报这个错误 二、异常原因 后来检查了一下,发现是因为 我支付宝网关写错了。沙箱环境和正式环境 的支付宝网关不同,如下 //支付宝网关名-正式环境 //public static final String OPEN_API_DOMAIN="https://openapi.alipay.com/gateway.do"; //支付宝网关名-沙箱环境 public static final String OPEN_API_DOMAIN="https://openapi.alipaydev.com/gateway.do"; 二、参考资料 1.蚂蚁金服沙箱测试:无效的AppID参数 2.invalid-app-id(无效的AppID)参数问题自查方案

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

Java中,成员内部类的常见修饰符及应用 && 成员内部类不是静态的,访问的格式

成员内部类的常见修饰符及应用: private 为了保证数据的安全性 static 为了方便访问数据 注意:静态的内部类访问外部类的数据时,外部类的数据必须用静态修饰。 成员内部类不是静态的,访问的格式: 我的GitHub地址: https://github.com/heizemingjun 我的博客园地址: http://www.cnblogs.com/chenmingjun 我的蚂蚁笔记博客地址: http://blog.leanote.com/chenmingjun Copyright ©2018 黑泽明军 【转载文章务必保留出处和署名,谢谢!】

资源下载

更多资源
腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

Sublime Text

Sublime Text

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

用户登录
用户注册