Paul Paul Public Builder & 全栈工程师
search
arrow_back 返回标签拓扑

Springboot

共 4 篇。

newest_tech 2022-08-03 · 约 1 分钟

Dubbo3.0-SpringBoot+Triple

前言 前面讲过使用Dubbo原生API的方式使用Triple,我们发现这个方式不太方便,实际开发中可能并不是很常见这种使用方式,今天我们讲讲在如何在日常项目中使用Triple协议。 快速搭建 环境准备 操作系统:Windowns JDK版本:1.8 编辑工具:IntelliJ IDEA Community Edition 2021.1.2 x64 Zookeeper:3.5.6(3.4或3.5以上的版本都行) 创建项目 项目有三个模块,分别是api、consumer、provider。proto文件都放在api中,consumer放消费端的代码,provider放服务提供者的代码。 api 依赖 xml content_copy 复制代码 <dependencies> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>3.0.9</version> </dependency> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-all</artifactId> <version>1.44.1</version> </dependency> </dependencies> proto插件 这里也是定义在pom中 xml content_copy 复制代码 <build> <extensions> <extension> <groupId>kr.motd.maven</groupId> <artifactId>os-maven-plugin</artifactId> <version>1.6.1</version> </extension> </extensions> <plugins> <plugin> <groupId>org.xolstice.maven.plugins</groupId> <artifactId>protobuf-maven-plugin</artifactId> <version>0.6.1</version> <configuration> <protocArtifact>com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier} </protocArtifact> <pluginId>grpc-java</pluginId> <pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier} </pluginArtifact> <protocPlugins> <protocPlugin> <id>dubbo</id> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-compiler</artifactId> <version>0.0.4.1</version> <mainClass>org.apache.dubbo.gen.tri.Dubbo3TripleGenerator</mainClass> </protocPlugin> </protocPlugins> </configuration> <executions> <execution> <goals> <goal>compile</goal> <goal>test-compile</goal> <goal>compile-custom</goal> <goal>test-compile-custom</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${maven-compiler-plugin.version}</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> proto文件 地址:看这里

阅读全文 arrow_forward
2022-03-20 · 约 2 分钟

SpringBoot外部化配置

前言 在前面的《SpringBoot可执行jar原理》里面我提到过SpringBoot的几个核心功能,而今天的主角外部化配置(Externalized Configuration)正是其中之一。 Spring Boot lets you externalize your configuration so that you can work with the same application code in different environments. You can use a variety of external configuration sources, include Java properties files, YAML files, environment variables, and command-line arguments. Spring官方给的解释是“外部化”你的配置文件,能让同一套的程序在不同的环境下工作。官方还给出properties、YAML、环境变量、还有command-line参数这些例子。 首先,这个外部化的配置肯定不是指我们自定义配置文件,而是指SpringBoot的系统配置文件。也就是我们常说的application.properties(或spring.yaml、spring.yml)。但是,“外部化”究竟是什么意思?SpringFramework和SpringBoot的配置到底有何差异? 差别对别 文本内容 SpringFramework xml content_copy 复制代码 <?xml version="1.0" encoding="UTF-8" standalone="no"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:websocket="http://www.springframework.org/schema/websocket" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket-4.0.xsd"> <!-- <mvc:annotation-driven /> 会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,是spring MVC为@Controllers分发请求所必须的。它提供了数据绑定支持,读取json的支持 --> <mvc:annotation-driven /> <!-- 配置jsp视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="jspViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value=""/> <property name="suffix" value=".jsp"/> </bean> </beans> 这里我们看到了一些熟悉的配置,注解驱动、Componenet扫描、jsp相关配置等等。这些是我们在使用SpringFramework时经常看到的配置,可是在SpringBoot中我们似乎看不到了(忽略掉配置的内容,这里只是举个例子,想告诉你springFramework是通过这种方式来配置一个bean)。

阅读全文 arrow_forward
2022-03-20 · 约 5 分钟

SpringBoot可执行jar原理

前言 随着SpringBoot的流行,越来越多的企业开始使用SpingBoot。我认为之所以这个框架越来越流行,社区越来越活跃,跟这个框架为开发者带来的便利性有关。它不仅改变了原来我们的编程习惯,简化了业务编程模型,而且还大大的降低了搭建一个应用程序的复杂度。 但随着对这个框架的使用的深入,我发现这个框架远非其表面那么简单,日常所用的SpringBoot的功能特性仅仅是其冰山一角,我不禁有了疑问。SpringBoot到底为我们带来了哪些便利?它与Spring Framework的差别是什么?由于篇幅有限,本文着重讲解SpringBoot的可执行jar,其他特性也会顺带聊一聊。 要了解一个框架,首先要知道这个框架的功能特性,以及框架设计的目的,正所谓“名不正则言不顺”。我第一时间想到的是它的官方文档,官方总结的SpringBoot的特性包括如下: Create stand-alone Spring applications Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files) Provide opinionated ‘starter’ dependencies to simplify your build configuration Automatically configure Spring and 3rd party libraries whenever possible Provide production-ready features such as metrics, health checks, and externalized configuration Absolutely no code generation and no requirement for XML configuration https://spring.io/projects/spring-boot#overview 根据官方文档再结合个人经验总结后的SpringBoot的特性如下: 创建独立运行的Spring应用 — 可执行jar、SpringApplication 嵌入式web容器 —SpringApplication中的一部分 提供固化的starter,简化构建配置 —starter特性 自动装配Spring和第三方的依赖 —自动装配 提供Production-Ready特性(如Metrics、健康检查、外部化配置) 完全的无中间代码生成,也不再需要XML配置 —注解驱动 从上面可以看到SpringBoot可执行jar正是其提供的六大核心功能之一。不知大家是否还记得,在使用SpringMVC构建应用时想要运行自己的应用,必须要先将应用代码打包成一个WAR,再借助一个独立部署的Web容器(Tomcat、Jetty、UnderTow等等)才能运行。可是,当使用SpringBoot时,既不需要将应用打包成WAR,也不需要独立部署的Web容器,反而是直接将应用打包成一个JAR,然后直接通过java -jar命令就能启动。大家是否和我一样有疑问,为什么直接打成JAR就能运行?下面我将揭开它的神秘面纱。(可执行jar只是指jar的运行,本文不讨论嵌入式Web容器相关内容)

阅读全文 arrow_forward
2022-03-20 · 约 2 分钟

Enable模块驱动

前言 很多同学在使用Springboot时都会看到一个注解“@EnableXXX”,该注解用于显示的装配指定的模块,如@EnableScheduling用来装配spring的定时任务模块、@EnableCaching用于激活缓存等等。但是,不知道大家有没有思考过其背后的原理?本篇文章会从代码层面来解释Enable模块驱动的原理,同时也会讲解如何编写自定义的Enable注解来实现Enable模块驱动。 在正式开始讲解之前先讲解“Enable的前世今生”,便于让更加清楚的了解其背后的设计理念。 自定义Enable模块驱动 后面的例子都是围绕CustomServer这个接口来展开,该接口的功能很简单,就是启动和停止“自定义服务器”这两个功能。下面展示的是CustomServer接口的定义,同时也展示了两个该接口的具体实现。 java content_copy 复制代码 public interface CustomServer { void start(); void stop(); } java content_copy 复制代码 public class ServletCustomServer implements CustomServer{ @Override public void start() { System.out.println("servlet custom server start."); } @Override public void stop() { System.out.println("servlet custom server stop."); } } java content_copy 复制代码 public class ReactCustomServer implements CustomServer{ @Override public void start() { System.out.println("react custom server start."); } @Override public void stop() { System.out.println("react custom server stop."); } } 至此,CustomServer模块的功能已经实现完毕,那么接下来的工作就是要通过Enable模块驱动来将CustomServer模块集成到Spring应用中了。

阅读全文 arrow_forward