一、引入starter
1 2 |
//热部署 compile("org.springframework.boot:spring-boot-devtools") |
二、开启自动编译 第一步
1 2 3 |
windows:ctrl + alt + shift + / mac: command + alt + shift + / |
弹出以下界面 第二步 点击Registry,勾选compiler.automake.allow.when.app.running 第三步 勾选 Make/Build project automatically 重新运行项目,随意改一个java类,看看项目是不是重启了^_^ from:https://blog.csdn.net/JonWu0102/article/details/81064776
View Details在引用依赖时经常会有这样的问题:某些间接引用的依赖项是不需要的;产生了依赖冲突。此时需要排除一些依赖。 下面的内容介绍了几种在gradle中排除依赖的方式。 在dependency中排除 1 2 3 4 5 6 7 8 dependencies { compile('com.zhyea:ar4j:1.0') { //excluding a particular transitive dependency: exclude module: 'cglib' //by artifact name exclude group: 'org.jmock' //by group exclude group: 'org.unwanted', module: 'iAmBuggy' //by both name and group } } 这种方式是粒度最细的,也是最为繁琐的。此时可以考虑全局设置。 在全局配置中排除 全局配置是在configuration中完成的。 1 2 3 4 configurations { compile.exclude module: 'cglib' all*.exclude group:’org.unwanted', module: 'iAmBuggy' } 禁用传递依赖 禁用传递依赖需要将属性transitive设置为false。可以在dependency中禁用传递依赖,也可以在configuration中全局禁用: 1 2 3 4 5 6 7 compile('com.zhyea:ar4j:1.0') { transitive = false } configurations.all { transitive = false } 还可以在单个依赖项中使用@jar标识符忽略传递依赖: […]
View Detailsspringboot 中默认的web容器是tomcat。 在maven 的pom 文件中加入如下依赖,便可使用tomcat 容器。
1 2 3 4 |
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> |
如果想使用 jetty 作为 web容器,需要2步操作: 1.排除默认的tomcat 容器
1 2 3 4 5 6 7 8 9 10 |
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> |
2.加入 jetty 的 starter 依赖
1 2 3 4 |
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> |
from:https://www.cnblogs.com/appleat/p/9100822.html
View DetailsSpringWebflux是SpringFramework5.0添加的新功能,WebFlux本身追随当下最火的Reactive Programming而诞生的框架,那么本篇就来简述一下这个框架到底是做什么的 一、关于WebFlux 我们知道传统的Web框架,比如说:struts2,springmvc等都是基于Servlet API与Servlet容器基础之上运行的,在Servlet3.1之后才有了异步非阻塞的支持。而WebFlux是一个典型非阻塞异步的框架,它的核心是基于Reactor的相关API实现的。相对于传统的web框架来说,它可以运行在诸如Netty,Undertow及支持Servlet3.1的容器上,因此它的运行环境的可选择行要比传统web框架多的多。 根据官方的说法,webflux主要在如下两方面体现出独有的优势: 1)非阻塞式 其实在servlet3.1提供了非阻塞的API,WebFlux提供了一种比其更完美的解决方案。使用非阻塞的方式可以利用较小的线程或硬件资源来处理并发进而提高其可伸缩性 2) 函数式编程端点 老生常谈的编程方式了,Spring5必须让你使用java8,那么函数式编程就是java8重要的特点之一,而WebFlux支持函数式编程来定义路由端点处理请求。 二、SpringMVC与SpringWebFlux 我们先来看官网的一张图: 它们都可以用注解式编程模型,都可以运行在tomcat,jetty,undertow等servlet容器当中。但是SpringMVC采用命令式编程方式,代码一句一句的执行,这样更有利于理解与调试,而WebFlux则是基于异步响应式编程,对于初次接触的码农们来说会不习惯。对于这两种框架官方给出的建议是: 1)如果原先使用用SpringMVC好好的话,则没必要迁移。因为命令式编程是编写、理解和调试代码的最简单方法。因为老项目的类库与代码都是基于阻塞式的。 2)如果你的团队打算使用非阻塞式web框架,WebFlux确实是一个可考虑的技术路线,而且它支持类似于SpringMvc的Annotation的方式实现编程模式,也可以在微服务架构中让WebMvc与WebFlux共用Controller,切换使用的成本相当小 3)在SpringMVC项目里如果需要调用远程服务的话,你不妨考虑一下使用WebClient,而且方法的返回值可以考虑使用Reactive Type类型的,当每个调用的延迟时间越长,或者调用之间的相互依赖程度越高,其好处就越大 我个人意见是:官网明确指出,SpringWebFlux并不是让你的程序运行的更快(相对于SpringMVC来说),而是在有限的资源下提高系统的伸缩性,因此当你对响应式编程非常熟练的情况下并将其应用于新的系统中,还是值得考虑的,否则还是老老实实的使用WebMVC吧 三、Reactive Spring Web 在这里定义了最基本的服务端接口:HttpHandler和WebHandler HttpHandler HttpHandler定义了最基本的处理Http请求行为,这个接口主要作用是处理Http请求并将结果做出响应,下面这个表格是说明了Server API的使用方式及何种方式进行响应式流支持的: Server name Server API used Reactive Streams support Netty Netty API Reactor Netty Undertow Undertow API spring-web: Undertow to Reactive Streams bridge Tomcat Servlet 3.1 non-blocking I/O; Tomcat API to read and write ByteBuffers vs byte[] spring-web: Servlet 3.1 non-blocking I/O to Reactive Streams bridge Jetty Servlet 3.1 non-blocking I/O; Jetty API to write ByteBuffers vs byte[] spring-web: Servlet 3.1 […]
View Details
1 2 3 4 5 6 7 8 9 10 11 12 |
public class test1_format { public static void main(String[] args) { BigDecimal decimal = new BigDecimal("1.12345"); System.out.println(decimal); BigDecimal setScale = decimal.setScale(4,BigDecimal.ROUND_HALF_DOWN); System.out.println(setScale); BigDecimal setScale1 = decimal.setScale(4,BigDecimal.ROUND_HALF_UP); System.out.println(setScale1); } } |
参数定义 ROUND_CEILING Rounding mode to round towards positive infinity. 向正无穷方向舍入 ROUND_DOWN Rounding mode to round towards zero. 向零方向舍入 ROUND_FLOOR Rounding mode to round towards negative infinity. 向负无穷方向舍入 ROUND_HALF_DOWN Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round down. 向(距离)最近的一边舍入,除非两边(的距离)是相等,如果是这样,向下舍入, 例如1.55 保留一位小数结果为1.5 ROUND_HALF_EVEN Rounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor. 向(距离)最近的一边舍入,除非两边(的距离)是相等,如果是这样,如果保留位数是奇数,使用ROUND_HALF_UP ,如果是偶数,使用ROUND_HALF_DOWN ROUND_HALF_UP Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in […]
View Details1、Math中四舍五入的方法 Math.ceil(double a)向上舍入,将数值向上舍入为最为接近的整数,返回值是double类型 Math.floor(double a)向下舍入,将数值向下舍入为最为接近的整数,返回值是double类型 Math.round(float a)标准舍入,将数值四舍五入为最为接近的整数,返回值是int类型 Math.round(double a)标准舍入,将数值四舍五入为最为接近的整数,返回值是long类型 2、Math中random生成随机数 Math.random()生成大于等于0,小于1的随机数 3、Random类生成随机数 两种构造方式:第一种使用默认的种子(当前时间作为种子),另一个使用long型整数为种子,Random类可以生成布尔型、浮点类型、整数等类型的随机数,还可以指定生成随机数的范围 4、BigDecimal处理小数 两种构造方式:第一种直接value写数字的值,第二种用String
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
import java.math.BigDecimal; import java.util.Random; import java.util.concurrent.ThreadLocalRandom; public class TestNumber { public static void main(String[] args){ //ceil返回大的值 System.out.println(Math.ceil(-10.1)); //-10.0 System.out.println(Math.ceil(10.7)); //11.0 System.out.println(Math.ceil(-0.7)); //-0.0 System.out.println(Math.ceil(0.0)); //0.0 System.out.println(Math.ceil(-0.0)); //-0.0 System.out.println(Math.ceil(-1.7)); //-1.0 //floor返回小的值 System.out.println(Math.floor(-10.1)); //-11.0 System.out.println(Math.floor(10.7)); //10.0 System.out.println(Math.floor(-0.7)); //-1.0 System.out.println(Math.floor(0.0)); //0.0 System.out.println(Math.floor(-0.0)); //-0.0 System.out.println(Math.floor(-1.7)); //-2.0 //round四舍五入,float返回int,double返回long System.out.println(Math.round(10.5)); //11 System.out.println(Math.round(-10.5)); //-10 //Math生成随机数 System.out.println(Math.random()); //Random类生成随机数 Random rand=new Random(); System.out.println(rand.nextBoolean()); System.out.println(rand.nextDouble()); System.out.println(rand.nextInt()); System.out.println(rand.nextInt(10)); //Random使用当前时间作为Random的种子 Random rand2 = new Random(System.currentTimeMillis()); System.out.println(rand2.nextBoolean()); System.out.println(rand2.nextDouble()); System.out.println(rand2.nextInt()); System.out.println(rand2.nextInt(10)); System.out.println(rand2.nextInt(5)); //ThreadLocalRandom ThreadLocalRandom rand3 = ThreadLocalRandom.current(); System.out.println(rand3.nextInt(5,10)); //BigDecimal System.out.println(0.8 - 0.7); //0.10000000000000009 BigDecimal a1=new BigDecimal(0.1); BigDecimal b1=new BigDecimal(0.9); BigDecimal c1=a1.add(b1); System.out.println("a1.add(b1)="+c1); //a1.add(b1)=1.0000000000000000277555756156289135105907917022705078125 BigDecimal a2=new BigDecimal("0.1"); BigDecimal b2=new BigDecimal("0.9"); BigDecimal c2=a2.add(b2); System.out.println("a2="+a2); //a2=0.1 System.out.println("a2.add(b2)="+c2); //a2.add(b2)=1.0 } } |
from:https://www.cnblogs.com/testerlina/p/11349456.html
View Details能编译通过说明SDK导入正确,但是为啥我们点击每一个Java文件会出现好多红色的下划线 ,并提示idea cant resolve symbol 原因就是可能没有清除原来的历史缓存,导致一些错误,解决方法是 File-Invalidate Caches 然后重启IDEA,OK~困扰多年的问题解决!
View Details1. 基本的RPC模型 主要介绍RPC是什么,基本的RPC代码,RPC与REST的区别,gRPC的使用 1.1 基本概念 RPC(Remote Procedure Call)远程过程调用,简单的理解是一个节点请求另一个节点提供的服务 本地过程调用:如果需要将本地student对象的age+1,可以实现一个addAge()方法,将student对象传入,对年龄进行更新之后返回即可,本地方法调用的函数体通过函数指针来指定。 远程过程调用:上述操作的过程中,如果addAge()这个方法在服务端,执行函数的函数体在远程机器上,如何告诉机器需要调用这个方法呢? 首先客户端需要告诉服务器,需要调用的函数,这里函数和进程ID存在一个映射,客户端远程调用时,需要查一下函数,找到对应的ID,然后执行函数的代码。 客户端需要把本地参数传给远程函数,本地调用的过程中,直接压栈即可,但是在远程调用过程中不再同一个内存里,无法直接传递函数的参数,因此需要客户端把参数转换成字节流,传给服务端,然后服务端将字节流转换成自身能读取的格式,是一个序列化和反序列化的过程。 3.数据准备好了之后,如何进行传输?网络传输层需要把调用的ID和序列化后的参数传给服务端,然后把计算好的结果序列化传给客户端,因此TCP层即可完成上述过程,gRPC中采用的是HTTP2协议。 总结一下上述过程:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// Client端 // Student student = Call(ServerAddr, addAge, student) 1. 将这个调用映射为Call ID。 2. 将Call ID,student(params)序列化,以二进制形式打包 3. 把2中得到的数据包发送给ServerAddr,这需要使用网络传输层 4. 等待服务器返回结果 5. 如果服务器调用成功,那么就将结果反序列化,并赋给student,年龄更新 // Server端 1. 在本地维护一个Call ID到函数指针的映射call_id_map,可以用Map<String, Method> callIdMap 2. 等待服务端请求 3. 得到一个请求后,将其数据包反序列化,得到Call ID 4. 通过在callIdMap中查找,得到相应的函数指针 5. 将student(params)反序列化后,在本地调用addAge()函数,得到结果 6. 将student结果序列化后通过网络返回给Client |
在微服务的设计中,一个服务A如果访问另一个Module下的服务B,可以采用HTTP REST传输数据,并在两个服务之间进行序列化和反序列化操作,服务B把执行结果返回过来。 由于HTTP在应用层中完成,整个通信的代价较高,远程过程调用中直接基于TCP进行远程调用,数据传输在传输层TCP层完成,更适合对效率要求比较高的场景,RPC主要依赖于客户端和服务端之间建立Socket链接进行,底层实现比REST更复杂。 1.2 rpc demo 系统类图 系统调用过程 客户端:
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 |
public class RPCClient<T> { public static <T> T getRemoteProxyObj(final Class<?> serviceInterface, final InetSocketAddress addr) { // 1.将本地的接口调用转换成JDK的动态代理,在动态代理中实现接口的远程调用 return (T) Proxy.newProxyInstance(serviceInterface.getClassLoader(), new Class<?>[]{serviceInterface}, new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Socket socket = null; ObjectOutputStream output = null; ObjectInputStream input = null; try{ // 2.创建Socket客户端,根据指定地址连接远程服务提供者 socket = new Socket(); socket.connect(addr); // 3.将远程服务调用所需的接口类、方法名、参数列表等编码后发送给服务提供者 output = new ObjectOutputStream(socket.getOutputStream()); output.writeUTF(serviceInterface.getName()); output.writeUTF(method.getName()); output.writeObject(method.getParameterTypes()); output.writeObject(args); // 4.同步阻塞等待服务器返回应答,获取应答后返回 input = new ObjectInputStream(socket.getInputStream()); return input.readObject(); }finally { if (socket != null){ socket.close(); } if (output != null){ output.close(); } if (input != null){ input.close(); } } } }); } } |
服务端:
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
public class ServiceCenter implements Server { private static ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); private static final HashMap<String, Class> serviceRegistry = new HashMap<String, Class>(); private static boolean isRunning = false; private static int port; public ServiceCenter(int port){ ServiceCenter.port = port; } @Override public void start() throws IOException { ServerSocket server = new ServerSocket(); server.bind(new InetSocketAddress(port)); System.out.println("Server Start ....."); try{ while(true){ executor.execute(new ServiceTask(server.accept())); } }finally { server.close(); } } @Override public void register(Class serviceInterface, Class impl) { serviceRegistry.put(serviceInterface.getName(), impl); } @Override public boolean isRunning() { return isRunning; } @Override public int getPort() { return port; } @Override public void stop() { isRunning = false; executor.shutdown(); } private static class ServiceTask implements Runnable { Socket client = null; public ServiceTask(Socket client) { this.client = client; } @Override public void run() { ObjectInputStream input = null; ObjectOutputStream output = null; try{ input = new ObjectInputStream(client.getInputStream()); String serviceName = input.readUTF(); String methodName = input.readUTF(); Class<?>[] parameterTypes = (Class<?>[]) input.readObject(); Object[] arguments = (Object[]) input.readObject(); Class serviceClass = serviceRegistry.get(serviceName); if(serviceClass == null){ throw new ClassNotFoundException(serviceName + "not found!"); } Method method = serviceClass.getMethod(methodName, parameterTypes); Object result = method.invoke(serviceClass.newInstance(), arguments); output = new ObjectOutputStream(client.getOutputStream()); output.writeObject(result); }catch (Exception e){ e.printStackTrace(); }finally { if(output!=null){ try{ output.close(); }catch (IOException e){ e.printStackTrace(); } } if (input != null) { try { input.close(); } catch (IOException e) { e.printStackTrace(); } } if (client != null) { try { client.close(); } catch (IOException e) { e.printStackTrace(); } } } } } } |
1 2 3 4 5 6 7 8 |
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ServiceProducerImpl</span> <span class="token keyword">implements</span> <span class="token class-name">ServiceProducer</span><span class="token punctuation">{</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">sendData</span><span class="token punctuation">(</span><span class="token class-name">String</span> data<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token string">"I am service producer!!!, the data is "</span><span class="token operator">+</span> data<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class RPCTest { public static void main(String[] args) throws IOException { new Thread(new Runnable() { @Override public void run() { try { Server serviceServer = new ServiceCenter(8088); serviceServer.register(ServiceProducer.class, ServiceProducerImpl.class); serviceServer.start(); } catch (IOException e) { e.printStackTrace(); } } }).start(); ServiceProducer service = RPCClient.getRemoteProxyObj(ServiceProducer.class, new InetSocketAddress("localhost", 8088)); System.out.println(service.sendData("test")); } } |
1.3 完整源码 RPCdemo 1.4 分析 这里客户端只需要知道Server端的接口ServiceProducer即可,服务端在执行的时候,会根据具体实例调用实际的方法ServiceProducerImpl,符合面向对象过程中父类引用指向子类对象。 2. gRPC的使用 2.1. gRPC与REST REST通常以业务为导向,将业务对象上执行的操作映射到HTTP动词,格式非常简单,可以使用浏览器进行扩展和传输,通过JSON数据完成客户端和服务端之间的消息通信,直接支持请求/响应方式的通信。不需要中间的代理,简化了系统的架构,不同系统之间只需要对JSON进行解析和序列化即可完成数据的传递。 但是REST也存在一些弊端,比如只支持请求/响应这种单一的通信方式,对象和字符串之间的序列化操作也会影响消息传递速度,客户端需要通过服务发现的方式,知道服务实例的位置,在单个请求获取多个资源时存在着挑战,而且有时候很难将所有的动作都映射到HTTP动词。 正是因为REST面临一些问题,因此可以采用gRPC作为一种替代方案,gRPC 是一种基于二进制流的消息协议,可以采用基于Protocol Buffer的IDL定义grpc API,这是Google公司用于序列化结构化数据提供的一套语言中立的序列化机制,客户端和服务端使用HTTP/2以Protocol Buffer格式交换二进制消息。 gRPC的优势是,设计复杂更新操作的API非常简单,具有高效紧凑的进程通信机制,在交换大量消息时效率高,远程过程调用和消息传递时可以采用双向的流式消息方式,同时客户端和服务端支持多种语言编写,互操作性强;不过gRPC的缺点是不方便与JavaScript集成,某些防火墙不支持该协议。 注册中心:当项目中有很多服务时,可以把所有的服务在启动的时候注册到一个注册中心里面,用于维护服务和服务器之间的列表,当注册中心接收到客户端请求时,去找到该服务是否远程可以调用,如果可以调用需要提供服务地址返回给客户端,客户端根据返回的地址和端口,去调用远程服务端的方法,执行完成之后将结果返回给客户端。这样在服务端加新功能的时候,客户端不需要直接感知服务端的方法,服务端将更新之后的结果在注册中心注册即可,而且当修改了服务端某些方法的时候,或者服务降级服务多机部署想实现负载均衡的时候,我们只需要更新注册中心的服务群即可。 RPC调用过程 2.2. gRPC与Spring Boot 这里使用SpringBoot+gRPC的形式实现RPC调用过程 项目结构分为三部分:client、grpc、server 项目结构 2.2.2 grpc pom.xml中引入依赖:
1 2 3 4 5 |
<dependency> <groupId>io.grpc</groupId> <artifactId>grpc-all</artifactId> <version>1.12.0</version> </dependency> |
引入bulid
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 |
<build> <extensions> <extension> <groupId>kr.motd.maven</groupId> <artifactId>os-maven-plugin</artifactId> <version>1.4.1.Final</version> </extension> </extensions> <plugins> <plugin> <groupId>org.xolstice.maven.plugins</groupId> <artifactId>protobuf-maven-plugin</artifactId> <version>0.5.0</version> <configuration> <pluginId>grpc-java</pluginId> <protocArtifact>com.google.protobuf:protoc:3.0.2:exe:${os.detected.classifier}</protocArtifact> <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.2.0:exe:${os.detected.classifier}</pluginArtifact> </configuration> <executions> <execution> <goals> <goal>compile</goal> <goal>compile-custom</goal> </goals> </execution> </executions> </plugin> </plugins> </build> |
创建.proto文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
syntax = "proto3"; // 语法版本 // stub选项 option java_package = "com.shgx.grpc.api"; option java_outer_classname = "RPCDateServiceApi"; option java_multiple_files = true; // 定义包名 package com.shgx.grpc.api; // 服务接口定义,服务端和客户端都要遵守该接口进行通信 service RPCDateService { rpc getDate (RPCDateRequest) returns (RPCDateResponse) {} } // 定义消息(请求) message RPCDateRequest { string userName = 1; } // 定义消息(响应) message RPCDateResponse { string serverDate = 1; } |
mvn complie 生成代码: 2.2.3 client 根据gRPC中的项目配置在client和server两个Module的pom.xml添加依赖 […]
View Details1.服务配置文件
1 2 |
cd /etc/systemd/system touch your_service_name.service |
1 2 3 4 5 6 |
[Unit] Description=zaomianbao [Service] ExecStart= /your_java_path/bin/java -jar /your_app_path/your_app_name.jar [Install] WantedBy=multi-user.target |
2.启动服务
1 |
systemctl start your_service_name.service |
参考: https://blog.csdn.net/weixin_37490221/article/details/80758276
View Details直接用java -jar xxx.jar,当退出或关闭shell时,程序就会停止掉。以下方法可让jar运行后一直在后台运行。 方法一 java -jar xxx.jar & 说明: 在末尾加入 & 符号 方法二 (1)执行java -jar xxx.jar后 (2)ctrl+z 退出到控制台,执行 bg (3)exit 完成以上3步,退出SHELL后,jar服务一直在后台运行。 方法三 nohup java -jar xxxx.jar & 将java -jar xxxx.jar 加入 nohup &中间,也可以实现 from:https://www.cnblogs.com/zsg88/p/9473843.html
View Details