首先Spring Boot 3必须使用最新版本的Dubbo 3.2
使用zookeeper可以修改依赖
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper-curator5</artifactId>
<type>pom</type>
</dependency>
Dubbo默认使用了fastjson 2序列化,可能会出现一些小问题,可以修改配置文件,在provider中使用hessian2
dubbo:
registry:
protocol: zookeeper
address: localhost:2181
config-center:
timeout: 100000
protocol:
name: dubbo
port: 20801
telnet: help
payload: 104857600
serialization: hessian2
provider:
timeout: 100000
filter: -exception,dubboExceptionFilter
consumer:
timeout: 100000
check: false
如何使用了基于apache httpclient的RestTemplate,可以修改依赖
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
</dependency>
如果需要自定义RestTemplate,可以参考以下内容
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import org.apache.hc.client5.http.classic.HttpClient;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.apache.hc.client5.http.socket.ConnectionSocketFactory;
import org.apache.hc.client5.http.socket.PlainConnectionSocketFactory;
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
import org.apache.hc.core5.http.config.Registry;
import org.apache.hc.core5.http.config.RegistryBuilder;
import org.apache.hc.core5.util.Timeout;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
@Configuration
public class RestTemplateConfig {
@Value("${spring.jackson.date-format}")
private String dateFormat;
@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
var restTemplate = new RestTemplate(factory);
var converters = restTemplate.getMessageConverters();
var iterator = converters.iterator();
while (iterator.hasNext()) {
var converter = iterator.next();
if (converter instanceof MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter) {
var objectMapper = mappingJackson2HttpMessageConverter.getObjectMapper();
var javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(LocalDateTime.class,
new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateFormat)));
javaTimeModule.addDeserializer(LocalDateTime.class,
new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(dateFormat)));
objectMapper.disable(MapperFeature.IGNORE_DUPLICATE_MODULE_REGISTRATIONS);
objectMapper.registerModule(javaTimeModule);
objectMapper.enable(MapperFeature.IGNORE_DUPLICATE_MODULE_REGISTRATIONS);
}
}
return restTemplate;
}
@Bean
public ClientHttpRequestFactory httpRequestFactory() {
return new HttpComponentsClientHttpRequestFactory(httpClient());
}
public HttpClient httpClient() {
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", SSLConnectionSocketFactory.getSocketFactory()).build();
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
// 设置最大连接池的数量
connectionManager.setMaxTotal(1000);
// 每个主机的最大并发量,route是指域名。--对MaxTotal的细化
connectionManager.setDefaultMaxPerRoute(300);
RequestConfig requestConfig = RequestConfig.custom()
// 数据返回超时时间
.setResponseTimeout(Timeout.ofMilliseconds(60000))
// 连接超时时间
.setConnectTimeout(Timeout.ofMilliseconds(60000))
// 从连接池中获取连接的超时时间
.setConnectionRequestTimeout(Timeout.ofMilliseconds(10000)).build();
return HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).setConnectionManager(connectionManager)
.build();
}
}