加入相关依赖与spring-boot集成
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
RestTemplate连接池超时等配置
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
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.web.client.RestTemplate;
/**
* @author Administrator
* @description
* @date 2022-01-21 9:46
*/
@Configuration
public class RestTemplateConfig {
@Bean
//@LoadBalanced // 开启负载均衡的功能
public RestTemplate restTemplate() {
return new RestTemplate(httpRequestFactory());
}
@Bean
public ClientHttpRequestFactory httpRequestFactory() {
return new HttpComponentsClientHttpRequestFactory(httpClient());
}
/**
* http链接池
* 服务器返回数据(response)的时间,超过该时间抛出read timeout
*
* @return
*/
@Bean
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(200);
// 路由是对maxTotal的细分
connectionManager.setDefaultMaxPerRoute(100);
RequestConfig requestConfig = RequestConfig.custom()
// 服务器返回数据(response)的时间,超过该时间抛出read timeout
.setSocketTimeout(10000)
// 连接上服务器(握手成功)的时间,超出该时间抛出connect timeout
.setConnectTimeout(5000)
// 从连接池中获取连接的超时时间,超过该时间未拿到可用连接
// 会抛出org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection from pool
.setConnectionRequestTimeout(1000)
.build();
return HttpClientBuilder.create()
.setDefaultRequestConfig(requestConfig)
.setConnectionManager(connectionManager)
.build();
}
}
使用restTemplate调用高德地图API
import com.example.socketserver.service.AmapService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.net.URI;
/**
* @author Administrator
* @description 高德地图API
* @date 2022-01-21 14:36
*/
@Service
public class AmapServiceImpl implements AmapService {
@Resource
private RestTemplate restTemplate;
@Value("${amap.api.key}")
private String amapKey;
@Override
public String geo(String address) {
// 获取地理编码(经纬度)
// 结构化地址举例:北京市东城区天安门 转换后经纬度:116.397499,39.908722
String url = "https://restapi.amap.com/v3/geocode/geo?key="+ amapKey + "&address=" + address;
URI uri = URI.create(url);
String response = restTemplate.getForObject(uri,String.class);
return response;
}
@Override
public String regeo(String location) {
// 根据经纬度获取地理位置
// 经纬度:116.397499,39.908722 获取到的formatted_address是:北京市东城区东华门街道天安门
String url = "https://restapi.amap.com/v3/geocode/regeo?key=" + amapKey + "&location=" + location;
URI uri = URI.create(url);
String response = restTemplate.getForObject(uri,String.class);
return response;
}
@Override
public String district(String keywords) {
// 根据关键字查询行政区域
String url = "https://restapi.amap.com/v3/config/district?key=" + amapKey + "&keywords=" + keywords;
URI uri = URI.create(url);
String response = restTemplate.getForObject(uri,String.class);
return response;
}
}