此处将为大家介绍关于SpringBoot:Apachederby池为空。30秒内无法获取连接的详细内容,并且为您解答有关springboothttp连接池的相关问题,此外,我们还将为您介绍关于Apac
此处将为大家介绍关于Spring Boot:Apache derby池为空。30秒内无法获取连接的详细内容,并且为您解答有关springboot http连接池的相关问题,此外,我们还将为您介绍关于Apache + SSL 代理错误上的 Spring Boot API、Apache Camel Spring Boot、Apache Dubbo 官方发布 Spring 6 & Spring Boot 3 支持、Apache Dubbo 官方正式发布 Spring 6 & Spring Boot 3 支持的有用信息。
本文目录一览:- Spring Boot:Apache derby池为空。30秒内无法获取连接(springboot http连接池)
- Apache + SSL 代理错误上的 Spring Boot API
- Apache Camel Spring Boot
- Apache Dubbo 官方发布 Spring 6 & Spring Boot 3 支持
- Apache Dubbo 官方正式发布 Spring 6 & Spring Boot 3 支持
Spring Boot:Apache derby池为空。30秒内无法获取连接(springboot http连接池)
运行一段时间后,出现此错误。
路径为[]的Servlet [dispatcherServlet]的Servlet.service()抛出异常[请求处理失败;
嵌套的异常是org.springframework.orm.jpa.JpaSystemException:无法获取JDBC连接。嵌套的异常是org.hibernate.exception.GenericJDBCException:无法获取JDBC
Connection,其根本原因是org.apache.tomcat.jdbc.pool.PoolExhaustedException:[http-
nio-8081-exec-5]超时:池为空。30秒内无法获取连接,无可用[size:100; 繁忙:100;空闲:0;
lastwait:30000]。
这是application.properties文件:
spring.datasource.url=jdbc:derby:/spring-boot/db;create=truespring.datasource.username=adminspring.datasource.password=1234spring.datasource.driver=org.apache.derby.jdbc.EmbeddedDriverhibernate.dialect = org.hibernate.dialect.DerbyDialectspring.jpa.generate-ddl=truespring.jpa.hibernate.ddl-auto = updatespring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContextspring.datasource.max-active=100spring.datasource.max-idle=8spring.datasource.min-idle=8
答案1
小编典典我无法重现错误,但是请允许我更正您的属性文件,这样,如果 您使用的是SpringBoot 1.4或更高版本 ,Spring实际上可以读取您的属性。
正如我在帖子中发现的一样的错误,为了解决该问题,您正在尝试增加datasource.max-active
属性和其他属性,对吗?
因此,如果属性不在此默认应用程序属性中,则必须将它们编写为
spring.jpa.properties.*= # Additional native properties to set on the JPA provider.
据此,我建议您将application.properties
文件更改为以下内容:
spring.datasource.url = jdbc:derby:/spring-boot/db;create=true spring.datasource.username = admin spring.datasource.password = 1234 # this one is incorrect # spring.datasource.driver = org.apache.derby.jdbc.EmbeddedDriver # replace with the one below, otherwise Spring will use his own # derby driver spring.datasource.driver-class-name = org.apache.derby.jdbc.EmbeddedDriver # this one is incorrect as well # hibernate.dialect = org.hibernate.dialect.DerbyDialect # use the following way spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.DerbyDialect spring.jpa.generate-ddl = true spring.jpa.hibernate.ddl-auto = update spring.jpa.properties.hibernate.current_session_context_class = org.springframework.orm.hibernate4.SpringSessionContext # and in order for these guys to work you have to know # spring.datasource.max-active=100 # spring.datasource.max-idle=8 # spring.datasource.min-idle=8 # your dbcp and look for documentation # below is the example for Tomcat spring.datasource.tomcat.max-active=100 spring.datasource.tomcat.max-idle=8 spring.datasource.tomcat.min-idle=8 # make sure you check out the logging # this is IMPORTANT to see that your # properties were ACTUALLY picked up logging.level.org.springframework.web = DEBUG logging.level.org.hibernate = DEBUG
最后但并非最不重要的一点是,您必须确保 Spring确实拾取了 您的max-idle
属性。这就是为什么我添加了两个最后的日志记录和调试属性。
如果您仔细查看日志文件,请检查将其设置为哪些值。这是那条线对我的外观
2016-11-17 11:40:27.875 DEBUG 32079 --- [ main] o.hibernate.internal.SessionFactoryImpl : Instantiating session factory with properties: {java.vendor=Oracle Corporation, sun.java.launcher=SUN_STANDARD, catalina.base=/tmp/tomcat.736868414125414090.8080, sun.management.compiler=HotSpot 64-Bit Tiered Compilers, catalina.useNaming=false, hibernate.implicit_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy, os.name=Linux, sun.boot.class.path=/usr/users/nikiforo/src/jdk1.8.0_73/jre/lib/resources.jar:/usr/users/nikiforo/src/jdk1.8.0_73/jre/lib/rt.jar:/usr/users/nikiforo/src/jdk1.8.0_73/jre/lib/sunrsasign.jar:/usr/users/nikiforo/src/jdk1.8.0_73/jre/lib/jsse.jar:/usr/users/nikiforo/src/jdk1.8.0_73/jre/lib/jce.jar:/usr/users/nikiforo/src/jdk1.8.0_73/jre/lib/charsets.jar:/usr/users/nikiforo/src/jdk1.8.0_73/jre/lib/jfr.jar:/usr/users/nikiforo/src/jdk1.8.0_73/jre/classes, hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext, java.vm.specification.vendor=Oracle Corporation, java.runtime.version=1.8.0_73-b02, user.name=nikiforo, javax.persistence.validation.mode=AUTO, user.language=en, sun.boot.library.path=/usr/users/nikiforo/src/jdk1.8.0_73/jre/lib/amd64, PID=32079, java.version=1.8.0_73, user.timezone=Europe/Berlin, sun.arch.data.model=64, java.endorsed.dirs=/usr/users/nikiforo/src/jdk1.8.0_73/jre/lib/endorsed, sun.cpu.isalist=, sun.jnu.encoding=UTF-8, file.encoding.pkg=sun.io, file.separator=/, java.specification.name=Java Platform API Specification, java.class.version=52.0, user.country=US, java.home=/usr/users/nikiforo/src/jdk1.8.0_73/jre, java.vm.info=mixed mode, os.version=4.1.20-11-default, hibernate.boot.CfgXmlAccessService.key=org.hibernate.boot.cfgxml.spi.LoadedConfig@6abb7b7d, hibernate.connection.datasource=org.apache.tomcat.jdbc.pool.DataSource@5c7dfc05{ConnectionPool[defaultAutoCommit=null; defaultReadOnly=null; defaultTransactionIsolation=-1; defaultCatalog=null; driverClassName=org.apache.derby.jdbc.EmbeddedDriver; maxActive=100
这是一条很长的线,但是正如您所看到的,对于我而言,这max-active
已经定了。
如果您想查看我在哪个简单的Spring-Data-REST应用程序中尝试了derby并遵循了以下属性,请检查此GitHub项目:derby-tomcat-
database-error分支
可能有帮助,可能没有帮助,如果我写错了,请告诉我,我将删除帖子。
Apache + SSL 代理错误上的 Spring Boot API
如何解决Apache + SSL 代理错误上的 Spring Boot API?
我正在尝试使用代理在 Apache Web 服务器上部署 Spring boot API。我设法做到了,它对 HTTP 工作正常。但是,当我开始使用 HTTPS 时,它停止工作。
我的网站的 apache conf 是:
<VirtualHost *:80>
ServerAdmin admin@domain.com
ServerName my-site.com
ServerAlias www.my-site.com
DocumentRoot /var/www/my-site.com
Redirect permanent / https://my-site.com/
</VirtualHost>
<VirtualHost *:443>
ServerName my-site.com
ServerAlias www.my-site.com
DocumentRoot /var/www/my-site.com
ProxyPreserveHost On
ProxyPass /api http://localhost:8080/
ProxyPassReverse /api http://localhost:8080/
SSLEngine on
SSLCertificateFile /etc/ssl/...
SSLCertificateKeyFile /etc/ssl/...
SSLCertificateChainFile /etc/ssl/...
</VirtualHost>
当我向 https://my-site.com/api 发送请求时,我的 jar 日志中出现以下错误:
java.lang.IllegalArgumentException: Invalid character found in method name. HTTP method names must be tokens
感谢您的帮助。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
Apache Camel Spring Boot
Camel应用初始化
Apache Camel 采用的是组件化的设计思想,通过Camel Component对接第三方的应用,Camel核心模块会扫描classpath 加载这些Camel Component。 Camel应用在启动的过程中,需要将应用涉及到的路由(Route),节点 (Endpoint),类型转换器(TypeConverter)以及发送接收模板(ProducerTemplate, ConsumerTemplate)加载到 Camel上下文环境(CamelContext)进行组装。
在Camel早期时代,Camel直接提供了一套XML的DSL来描述路由规则,以及配置Camel应用相关模块,这样我们只需要在应用程序入口创建Spring 应用,加载相关的XML配置文件就可以了。 Spring创建ApplicationContext时候会加载对应Camel路由规则,并完成有关CamelContext创建和组装工作。
随着Spring Boot的普及,大家逐步走上了通过标注和在starter依赖中加入很多自动配置模块的方式来配置相关组件的道路, Camel Spring Boot Starter 为Camel提供了一个自动配置组件。 通过自动配置不但将有Spring管理的Camel Routes组装到Camel Context中, 而且可以通过Autowire的方式向业务代码注入其他与Camel 应用运行相关的组件。
如何管理Spring Boot与Apache Camel依赖
一般来说可以通过maven的依赖管理如下方式管理Spring Boot 以及Apache Camel的依赖。
<properties>
<!-- 通过spring-boot 以及 camel的提供的 BOM管理依赖 -->
<camel.version>2.24.1</camel.version>
<spring-boot.version>2.1.6.RELEASE</spring-boot.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-boot-dependencies</artifactId>
<version>${camel.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
以下是典型的Apache Camel 应用的依赖包
<dependencies>
<!-- Camel Spring Boot Starter 包依赖,
包含了Spring Boot以及Apache Camel Core的依赖 -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
</dependency>
<!-- Camel应用涉到其他的Camel组件
大家可以从https://github.com/apache/camel/blob/master/components/readme.adoc
获取有关Camel组件的信息-->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-rabbitmq-starter</artifactId>
</dependency>
</dependencies>
配置Camel Context 以及加载路由规则
添加完相关的starter依赖之后,我们还需要定义相关的Camel路由规则。添加@Component,可以让Camel自动加载相关路由规则。 如果有多个路由规则,你可以通过定义多个RouterBuilder实例,或者在单个RouterBuilder的configure方法中定义多个 from的方式来实现。
package com.example;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;
@Component
public class MyRoute extends RouteBuilder {
@Override
public void configure() throws Exception {
from("timer:foo")
.to("log:bar");
}
}
当然你可以通过@Configuration来对路由实例进行精确控制。
@Configuration
public class MyRouterConfiguration {
@Bean
RoutesBuilder myRouter() {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("jms:invoices")
.to("file:/invoices");
}
};
}
}
这样在Spring Boot 应用启动的时候就自动加载这些定义好的路由规则。 如果想阻塞Spring Boot的主线程的话,你可以通过加入 spring-boot-starter-web依赖,或者在Spring Boot应的配置文件(application.properties, application.yaml) 中添加 camel.springboot.main-run-controller=true。
组装设置CamelContext
Apache Camel 的自动配置模块会提供一个配置好的CamelContext,这个CamelContext实例用camelContext在Spring应用上下文中进行注册,可以通过@Autowired注入到你的应用中。 这样可以基于CamelContext创建相关的服务或者组件。
@Configuration
public class MyAppConfig {
@Autowired
CamelContext camelContext;
@Bean
MyService myService() {
return new DefaultMyService(camelContext);
}
}
如果预先对CamelContext做一些设置的话, 可以通过预先创建CamelContextConfiguration的方式来进行配置。这样在Spring应用启动之前,Spring Boot会自动加载这些预先设置好的CamelContextConfiguration实例,回调相关的配置方法。
@Configuration
public class MyAppConfig {
...
@Bean
CamelContextConfiguration contextConfiguration() {
return new CamelContextConfiguration() {
@Override
void beforeApplicationStart(CamelContext context) {
// 在此编写Spring应用初始化之前,需要设置CamelContext的代码。
}
};
}
设置ConsumerTemplate和ProducerTemplate
Camel支持自动创建以及配置ProducerTemplate以及 ConsumerTemplate。 如下展示的内容,你可以使用平常Spring管理的对象一样通过Autowired方式注入这些对象。
@Component
public class InvoiceProcessor {
@Autowired
private ProducerTemplate producerTemplate;
@Autowired
private ConsumerTemplate consumerTemplate;
public void processNextInvoice() {
Invoice invoice = consumerTemplate.receiveBody("jms:invoices", Invoice.class);
...
producerTemplate.sendBody("netty-http:http://invoicing.com/received/" + invoice.id());
}
}
缺省情况下 ConsumerTemplate 和 ProducerTemplate缺省设置的endpoint的缓存大小是1000。你可以通过修改Spring属性文件的方式设置缓存大小。
camel.springboot.consumerTemplateCacheSize = 100
camel.springboot.producerTemplateCacheSize = 200
编写单元测试
可以通过CamelSpringBootRunner来支持Spring Boot应用的单元测试, 下面是一个具体的例子大家可以参考一下。
@ActiveProfiles("test")
@RunWith(CamelSpringBootRunner.class)
@SpringBootTest
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
@DisableJmx(true)
public class MyRouteTest extends CamelTestSupport {
@Autowired
private CamelContext camelContext;
@Override
protected CamelContext createCamelContext() throws Exception {
return camelContext;
}
@EndpointInject(uri = "direct:myEndpoint")
private ProducerTemplate endpoint;
@Override
public void setUp() throws Exception {
super.setUp();
RouteDefinition definition = context().getRouteDefinitions().get(0);
definition.adviceWith(context(), new RouteBuilder() {
@Override
public void configure() throws Exception {
onException(Exception.class).maximumRedeliveries(0);
}
});
}
@Override
public String isMockEndpointsAndSkip() {
return "myEndpoint:put*";
}
@Test
public void shouldSucceed() throws Exception {
assertNotNull(camelContext);
assertNotNull(endpoint);
String expectedValue = "expectedValue";
MockEndpoint mock = getMockEndpoint("mock:myEndpoint:put");
mock.expectedMessageCount(1);
mock.allMessages().body().isEqualTo(expectedValue);
mock.allMessages().header(MY_HEADER).isEqualTo("testHeader");
endpoint.sendBodyAndHeader("test", MY_HEADER, "testHeader");
mock.assertIsSatisfied();
}
}
参考资料
Camel官方文档
Camel Spring Boot 示例代码
Apache Dubbo 官方发布 Spring 6 & Spring Boot 3 支持
Apache Dubbo 简介
Apache Dubbo 是一款 RPC 服务开发框架,用于解决微服务架构下的服务治理与通信问题,官方提供了 Java、Golang 等多语言 SDK 实现。使用 Dubbo 开发的微服务原生具备相互之间的远程地址发现与通信能力, 利用 Dubbo 提供的丰富服务治理特性,可以实现诸如服务发现、负载均衡、流量调度等服务治理诉求。Dubbo 被设计为高度可扩展,用户可以方便的实现流量拦截、选址的各种定制逻辑。
Spring6 & Spring Boot3 及接入背景
Spring Framework 6.0 于11月16日正式发布 GA 版本,Spring Boot 3.0 也于11月25日正式发布 GA 版本,并且Spring 6 & SpringBoot 3最低支持JDK17,意味着如果升级使用Spring 6 & Spring Boot 3时就必须需要升级使用JDK17。
然而Java 8 目前是国内主流生产环境 Java 版本之一。虽然近几年陆续发布了 Java 11、Java 17 官方 LTS 版本,但是大部分开发者依然本着 “你发任你发,我用Java8” 的看法看待JDK的升级。不过 Java 17 版本在性能上确实做了大量的优化特别是 ZGC 的发布,促进了国内不少企业升级到 Java 17。
而Spring 框架在 Java 生态中的重要程度不言而喻,我们相信在Spring 这波“最低支持JDK17” 推动下,Spring Framework 6.0 & Spring Boot 3.0 一定会在不久的将来被大家接受,并成为主流技术栈。
Dubbo 社区非常重视 Spring 社区的更新迭代,总会积极支持适配,这点在最近Spring 6.0 和 Spring Boot 3.0 发布中同样得到了验证。Dubbo 社区早在Spring 6.0.0-RC4
和 Spring Boot 3.0.0-RC2
时已经做好了大致的兼容适配,但是为了保证Dubbo 能够完全适配 Spring 6 和 Spring Boot 3.0 的正式版,我们一直等到Spring Boot 3.0 GA 后,才选择宣布这个令人高兴的事情。
为什么接入 Spring6 & Spring
首先是,升级到 Spring 6.0 & Spring Boot 3.0 将获得未来很长年限的由官方提供的免费技术支撑。Spring 6 和 Spring Boot 3 是 Spring 下一代技术框架基石,尽管官方当前同时维护了 Spring 5.3 和 Spring Boot 2.6.x 和 Spring Boot 2.7.x,但它们最终都会在 2025 年和 2026 年结束其 OSS support(Open Source Software Support)。
其次是,您将在新一代框架中获得大量新特新,这些新特性都可以在以下链接获得:
Spring Boot 3.0 Release Notes
What''s New in Spring Framework 6.x
最后是,Spring 6.x 和 Spring Boot 3.x 将会最广泛的支持 JDK 17-29,需要额外说明的是 JDK17 作为当前最新的LTS 版本,它提供了一组累积的最新语言、API 和 JVM 增强功能,使其成为更具吸引力的编译版本的升级,这也是为什么最低支持 JDK17 的原因。
在 Dubbo 中体验 Spring6
现在很高兴向大家宣布,Dubbo 已经开始兼容Spring 6 & Spring Boot 3,所以当前Dubbo 3.2.0-beta.2 版本可以同时兼容支持Spring Boot 1.x、2.x、3.x。您现在可以使用dubbo-3.2.0-beta.2
版本体验其兼容性。
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>3.2.0-beta.2</version>
</dependency>
更多关于Spring Boot 3.0 集成 Dubbo 使用示例可参见apache/dubbo-samples:https://github.com/apache/dubbo-samples

接入过程技术总结
我们根据Dubbo 兼容适配Spring 6 & Spring Boot 3 过程中总结的经验整理如下,其他组件维护者也可以参考以下经验进行适配或者升级,更早适配升级到最新版本:
Jakarta EE 9 将所有API包名从javax.*
命名空间变更到了jakarta.*
。而造成这一变化的原因是Oracle拒绝交出相关权益。
因为Jakarta EE 的迁移,对于Web Apps,确保升级使用Tomcat 10, Jetty 11, or Undertow 2.2.19。
以下列出了一系列工具可以帮助你完成这部分的迁移:
OpenRewrite recipes.
The Spring Boot Migrator project.
Migration support in IntelliJ IDEA.
Spring Boot 3.0移除了META-INF/spring.factories
文件对Auto-configuration的支持,为了兼容性,SpringBoot 2.7.x 是最后一个支持的版本。
适配支持按照下面两个步骤即可完成
使用 @AutoConfiguration 注解代替 @Configuration(proxyBeanMethods = false)
@AutoConfiguration 注解是SpringBoot 2.7中的新引入的注解,旨在专门标识Auto-configuraton class name。
依然使用@Configuration
注解标识自动适配类也是可以的,Dubbo 正是基于这个便利点完美支持了Spring Boot 1.x、2.x、3.x所有版本。
使用 AutoConfiguration.imports 文件代替 META-INF/spring.factories 文件
Spring Boot 2.7是最后一个依然兼容使用spring.factories 的版本,SpringBoot 3 以后不再兼容,此时您应该使用META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
文件替换。
在该文件列举你所有的configuration classes,每行一个class name,例如:
com.mycorp.libx.autoconfigure.LibXAutoConfiguration
com.mycorp.libx.autoconfigure.LibXWebAutoConfiguration
原来默认日志日期格式:yyyy-MM-dd HH:mm:ss.SSS
当前默认日志日期格式:yyyy-MM-dd''T''HH:mm:ss.SSSXXX
原来的默认日志日期格式不具有timezone / offset 信息。
yyyy-MM-dd''T''HH:mm:ss.SSSXXX >>> e.g.: 2014-12-03T10:06:04.646+08:00
Spring Boot 官方测试发现YamlJsonParser
并不能很好的解析JSON,Spring Boot 3决定不再支持使用它来作为解析JSON的备选。
YamlJsonParser 封装的是 snakeyaml。
Spring Boot 3 解析JSON 的解析器使用优先级如下:
1)JacksonJsonParser
2)GsonJsonParser
3)BasicJsonParser
移除了spring.session.store-type
配置项,当存在多个可用存储库,将会按照Redis,JDBC,Hazelcast,Mongo 顺序使用。
更新spring data 配置键使其清楚地反应该配置键是否依赖Spring Data
如果存储库(redis、mongo等)相关的配置键不依赖Spring Data存在,则只需要 spring 前缀,否则需要使用 spring.data 前缀。
举例说明:
spring.redis.host >> spring.data.redis.host
spring.redis.port >> spring.data.redis.port
spring.data.cassandra.port >> spring.cassandra.port
根据最新的rfc2616,HTTP Method已经属于不可枚举属性,所以重构HttpMethod enum类为class类。
除了我们熟知的GET, HEAD, PUT, POST等方法,现在还存在了可扩展方法,当前可扩展方法包含了LOCK, COPY, 和 MOVE。这些扩展方法定义在WebDAV。
Spring 6之前,访问 “/resources” 和 “/resources/” 都可以进入resources()方法。
@GetMapping("/resources")
String resources() {
return "Hello from /resources";
}
Spring 6之后,您只能通过看到的path “/resources” 进入mapping 方法。
如果您依然想让“/resources/” 和 “/resources” 进入相同的mapping方法,可以通过其他手段,诸如“反向代理”、“Servlet/Web 过滤器”或“在控制器配置显式重定向”。
Spring 6 介绍了@HttpExchange 注解,基于@HttpExchange注解可以简化HTTP远程调用。
Spring 6 之前,SpringFactoriesLoader 只允许加载"META-INF/spring.factories"文件内容。
Spring 6 之后,SpringFactoriesLoader 可以加载自定义文件或文件名文件,并且可以通过链式编程加载多个文件。
早期兼容JDK19预览版的虚拟线程(virtual threads)
可以在Spring 6 和Spring Boot 3 中使用虚拟线程处理请求来提前体验。
这部分详细说明参见:https://spring.io/blog/2022/10/11/embracing-virtual-threads
Spring 6 以后,Spring MVC 可以使用 application/problem+json media 类型自定义 错误信息响应体,像下面这样:
{
"type": "https://example.org/problems/unknown-project",
"title": "Unknown project",
"status": 404,
"detail": "No project found for id ''spring-unknown''",
"instance": "/projects/spring-unknown"
}
社区合作展望
在云原生时代,Java 的跨平台特性,已经不算是其亮眼特性了,而其 Jar 包体积大、启动慢、占用内存多、需要另装 JVM 是 Java 应用的痛点问题。
而通过使用 GraalVM 可以很好的解决这些问题。并且通过 GraalVM 的 AOT(Ahead-Of-Time)可以将应用编译成单独可执行文件并直接运行。
未来 Dubbo 将会积极地在 Native 方面做一些工作以此能够使应用程序达到下面的目标
支持 Spring & Spring Boot native-image
较小的本地应用程序和容器镜像占用空间
快速启动,快速启动(几十毫秒)
低内存消耗,减少 RSS(驻留集大小),低内存有助于优化需要多个容器的微服务架构部署中的容器密度
快速的第一请求响应,避免 Hotspot 的预热问题
Part.1
扫码加入 “Apache Dubbo 开发者交流群”,如过期请联系以下社区小助手。
Part.2
微信公众号 | ApacheDubbo
钉钉群号 | 21976540
官网 | https://dubbo.apache.org
如您是生产用户,请联系小助手加入 “生产用户支持群”。
更多动态请关注 Apache Dubbo 官方公众号,及时获取最新的消息。
本文分享自微信公众号 - Apache Dubbo(ApacheDubbo)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。
Apache Dubbo 官方正式发布 Spring 6 & Spring Boot 3 支持
Dubbo 简介
Apache Dubbo 是一款 RPC 服务开发框架,用于解决微服务架构下的服务治理与通信问题,官方提供了 Java、Golang 等多语言 SDK 实现。使用 Dubbo 开发的微服务原生具备相互之间的远程地址发现与通信能力, 利用 Dubbo 提供的丰富服务治理特性,可以实现诸如服务发现、负载均衡、流量调度等服务治理诉求。Dubbo 被设计为高度可扩展,用户可以方便的实现流量拦截、选址的各种定制逻辑。
背景
Spring Framework 6.0 于 11 月 16 日正式发布 GA 版本,Spring Boot 3.0 也于 11 月 25 日正式发布 GA 版本,并且 Spring 6 & SpringBoot 3 最低支持 JDK17,意味着如果升级使用 Spring 6 & Spring Boot 3 时就必须需要升级使用 JDK17。
然而 Java 8 目前是国内主流生产环境 Java 版本之一。虽然近几年陆续发布了 Java 11、Java 17 官方 LTS 版本,但是大部分开发者依然本着 “你发任你发,我用 Java8” 的看法看待 JDK 的升级。不过 Java 17 版本在性能上确实做了大量的优化特别是 ZGC 的发布,促进了国内不少企业升级到 Java 17。
而 Spring 框架在 Java 生态中的重要程度不言而喻,我们相信在 Spring 这波 “最低支持 JDK17” 推动下,Spring Framework 6.0 & Spring Boot 3.0 一定会在不久的将来被大家接受,并成为主流技术栈。
Dubbo 社区非常重视 Spring 社区的更新迭代,总会积极支持适配,这点在最近 Spring 6.0 和 Spring Boot 3.0 发布中同样得到了验证。Dubbo 社区早在 Spring 6.0.0-RC4
和 Spring Boot 3.0.0-RC2
时已经做好了大致的兼容适配,但是为了保证 Dubbo 能够完全适配 Spring 6 和 Spring Boot 3.0 的正式版,我们一直等到 Spring Boot 3.0 GA 后,才选择宣布这个令人高兴的事情。
为什么要升级到 Spring 6.0 & Spring Boot 3.0
首先是,升级到 Spring 6.0 & Spring Boot 3.0 将获得未来很长年限的由官方提供的免费技术支撑。Spring 6 和 Spring Boot 3 是 Spring 下一代技术框架基石,尽管官方当前同时维护了 Spring 5.3 和 Spring Boot 2.6.x 和 Spring Boot 2.7.x,但它们最终都会在 2025 年和 2026 年结束其 OSS support(Open Source Software Support)。
其次是,您将在新一代框架中获得大量新特新,这些新特性都可以在 Spring Boot 3.0 Release Notes(https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-3.0-Release-Notes) 和 What''s New in Spring Framework 6.x(https://github.com/spring-projects/spring-framework/wiki/What''s-New-in-Spring-Framework-6.x) 中获得。
最后是,Spring 6.x 和 Spring Boot 3.x 将会最广泛的支持 JDK 17-29,需要额外说明的是 JDK17 作为当前最新的 LTS 版本,它提供了一组累积的最新语言、API 和 JVM 增强功能,使其成为更具吸引力的编译版本的升级,这也是为什么最低支持 JDK17 的原因。
Dubbo 支持 Spring 6 & Spring Boot 3
现在很高兴向大家宣布,Dubbo 已经开始兼容 Spring 6 & Spring Boot 3,所以当前 Dubbo 3.2.0-beta.2 版本可以同时兼容支持 Spring Boot 1.x、2.x、3.x。您现在可以使用 dubbo-3.2.0-beta.2
版本体验其兼容性。
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>3.2.0-beta.2</version>
</dependency>
更多关于 Spring Boot 3.0 集成 Dubbo 使用示例可参见 apache/dubbo-sample:https://github.com/apache/dubbo-samples/tree/master/1-basic
升级总结
我们根据 Dubbo 兼容适配 Spring 6 & Spring Boot 3 过程中总结的经验整理如下,其他组件维护者也可以参考以下经验进行适配或者升级,更早适配升级到最新版本:
Jakarta EE
Jakarta EE 9 将所有 API 包名从 javax.*
命名空间变更到了 jakarta.*
。而造成这一变化的原因是 Oracle 拒绝交出相关权益,详情可以查看:https://www.oschina.net/news/106465/oracle-killed-java-ee。
因为 Jakarta EE 的迁移,对于 Web Apps,确保升级使用 Tomcat 10, Jetty 11, or Undertow 2.2.19。
以下列出了一系列工具可以帮助你完成这部分的迁移:
- OpenRewrite recipes.
- The Spring Boot Migrator project.
- Migration support in IntelliJ IDEA.
移除 META-INF/spring.factories 文件对 Auto-configuration 的支持
Spring Boot 3.0 移除了 META-INF/spring.factories
文件对 Auto-configuration 的支持,为了兼容性,SpringBoot 2.7.x 是最后一个支持的版本。
适配支持按照下面两个步骤即可完成
Step1: [可选] 使用 @AutoConfiguration 注解代替 @Configuration(proxyBeanMethods = false)
@AutoConfiguration 注解是 SpringBoot 2.7 中的新引入的注解,旨在专门标识 Auto-configuraton class name。
依然使用 @Configuration
注解标识自动适配类也是可以的,Dubbo 正是基于这个便利点完美支持了 Spring Boot 1.x、2.x、3.x 所有版本。
Step2: 使用 AutoConfiguration.imports 文件代替 META-INF/spring.factories 文件
Spring Boot 2.7 是最后一个依然兼容使用 spring.factories 的版本,SpringBoot 3 以后不再兼容,此时您应该使用 META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
文件替换。
在该文件列举你所有的 configuration classes,每行一个 class name,例如:
com.mycorp.libx.autoconfigure.LibXAutoConfiguration
com.mycorp.libx.autoconfigure.LibXWebAutoConfiguration
为了对齐 ISO-8601,使用 yyyy-MM-dd''T''HH:mm:ss.SSSXXX 作为默认日志日期格式
原来默认日志日期格式:yyyy-MM-dd HH:mm:ss.SSS 当前默认日志日期格式:yyyy-MM-dd''T''HH:mm:ss.SSSXXX
原来的默认日志日期格式不具有 timezone /offset 信息。
yyyy-MM-dd''T''HH:mm:ss.SSSXXX >>> e.g.: 2014-12-03T10:06:04.646+08:00
移除 YamlJsonParser
Spring Boot 官方测试发现 YamlJsonParser
并不能很好的解析 JSON,Spring Boot 3 决定不再支持使用它来作为解析 JSON 的备选。
YamlJsonParser 封装的是 snakeyaml。
Spring Boot 3 解析 JSON 的解析器使用优先级如下:
- 1)JacksonJsonParser
- 2)GsonJsonParser
- 3)BasicJsonParser
移除 spring.session.store-type 配置键
移除了 spring.session.store-type
配置项,当存在多个可用存储库,将会按照 Redis,JDBC,Hazelcast,Mongo 顺序使用。
更新 spring data 配置键使其清楚地反应该配置键是否依赖 Spring Data
如果存储库(redis、mongo 等)相关的配置键不依赖 Spring Data 存在,则只需要 spring 前缀,否则需要使用 spring.data 前缀。
举例说明:
spring.redis.host >> spring.data.redis.host
spring.redis.port >> spring.data.redis.port
spring.data.cassandra.port >> spring.cassandra.port
重构 HttpMethod 枚举为类
根据最新的 rfc2616,HTTP Method 已经属于不可枚举属性,所以重构 HttpMethod enum 类为 class 类。
除了我们熟知的 GET, HEAD, PUT, POST 等方法,现在还存在了可扩展方法,当前可扩展方法包含了 LOCK, COPY, 和 MOVE。这些扩展方法定义在 WebDAV。
不允许 URI 尾部斜杠匹配
Spring 6 之前,访问 “/resources” 和 “/resources/” 都可以进入 resources () 方法。
@GetMapping("/resources")
String resources() {
return "Hello from /resources";
}
Spring 6 之后,您只能通过看到的 path “/resources” 进入 mapping 方法。
如果您依然想让 “/resources/” 和 “/resources” 进入相同的 mapping 方法,可以通过其他手段,诸如 “反向代理”、“Servlet/Web 过滤器” 或 “在控制器配置显式重定向”。
提供基于 @HttpExchange 服务接口的 HTTP 客户端
Spring 6 介绍了 @HttpExchange 注解,基于 @HttpExchange 注解可以简化 HTTP 远程调用。
增强 Spring SPI 加载器 SpringFactoriesLoader 允许加载多自定义文件
Spring 6 之前,SpringFactoriesLoader 只允许加载 "META-INF/spring.factories" 文件内容。
Spring 6 之后,SpringFactoriesLoader 可以加载自定义文件或文件名文件,并且可以通过链式编程加载多个文件。
早期兼容 JDK19 预览版的虚拟线程(virtual threads)
可以在 Spring 6 和 Spring Boot 3 中使用虚拟线程处理请求来提前体验。
这部分详细说明参见:https://spring.io/blog/2022/10/11/embracing-virtual-threads
支持 RFC 7807 Problem Details
Spring 6 以后,Spring MVC 可以使用 application/problem+json media 类型自定义 错误信息响应体,像下面这样:
{
"type": "https://example.org/problems/unknown-project",
"title": "Unknown project",
"status": 404,
"detail": "No project found for id ''spring-unknown''",
"instance": "/projects/spring-unknown"
}
展望
在云原生时代,Java 的跨平台特性,已经不算是其亮眼特性了,而其 Jar 包体积大、启动慢、占用内存多、需要另装 JVM 是 Java 应用的痛点问题。
而通过使用 GraalVM 可以很好的解决这些问题。并且通过 GraalVM 的 AOT(Ahead-Of-Time)可以将应用编译成单独可执行文件并直接运行。
未来 Dubbo 将会积极地在 Native 方面做一些工作以此能够使应用程序达到下面的目标
- 支持 Spring & Spring Boot native-image
- 较小的本地应用程序和容器镜像占用空间
- 快速启动,快速启动(几十毫秒)
- 低内存消耗,减少 RSS(驻留集大小),低内存有助于优化需要多个容器的微服务架构部署中的容器密度
- 快速的第一请求响应,避免 Hotspot 的预热问题
欢迎在 https://github.com/apache/dubbo 给 Dubbo Star。 搜索关注官方微信公众号:Apache Dubbo,了解更多业界最新动态,掌握大厂面试必备 Dubbo 技能
关于Spring Boot:Apache derby池为空。30秒内无法获取连接和springboot http连接池的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于Apache + SSL 代理错误上的 Spring Boot API、Apache Camel Spring Boot、Apache Dubbo 官方发布 Spring 6 & Spring Boot 3 支持、Apache Dubbo 官方正式发布 Spring 6 & Spring Boot 3 支持等相关内容,可以在本站寻找。
本文标签: