SpringBoot配置okhttp3的操作1. Maven 添加依赖
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.10.0</version>
</dependency>
2. application.properties 配置⽂件
t-timeout=30
ad-timeout=30
ok.http.write-timeout=30
# 连接池中整体的空闲连接的最⼤数量
ok.http.max-idle-connections=200
# 连接空闲时间最多为 300 秒
ok.http.keep-alive-duration=300
3. OkHttpConfiguration 配置类
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import org.springframework.beans.factory.annotation.Value;
import t.annotation.Bean;
import t.annotation.Configuration;
import javax.ssl.*;
import java.security.*;
import CertificateException;
import X509Certificate;
import urrent.TimeUnit;
/**
* @author Answer.AI.L
* @date 2019-04-09
*/
@Configuration
public class OkHttpConfiguration {
@Value("${t-timeout}")
private Integer connectTimeout;
@Value("${ad-timeout}")
private Integer readTimeout;
@Value("${ok.http.write-timeout}")
private Integer writeTimeout;
@Value("${ok.http.max-idle-connections}")
private Integer maxIdleConnections;
@Value("${ok.http.keep-alive-duration}")
private Long keepAliveDuration;
@Bean
public OkHttpClient okHttpClient() {
return new OkHttpClient.Builder()
.
sslSocketFactory(sslSocketFactory(), x509TrustManager())
// 是否开启缓存
.retryOnConnectionFailure(false)
.connectionPool(pool())
.connectTimeout(connectTimeout, TimeUnit.SECONDS)
.readTimeout(readTimeout, TimeUnit.SECONDS)
.writeTimeout(writeTimeout,TimeUnit.SECONDS)
.hostnameVerifier((hostname, session) -> true)
// 设置代理
// .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 8888)))
//
/
/ .addInterceptor()
.build();
}
@Bean
public X509TrustManager x509TrustManager() {
return new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
};
}
@Bean
public SSLSocketFactory sslSocketFactory() {
try {
// 信任任何链接
SSLContext sslContext = Instance("TLS");
sslContext.init(null, new TrustManager[]{x509TrustManager()}, new SecureRandom());
SocketFactory();
} catch (NoSuchAlgorithmException | KeyManagementException e) {
e.printStackTrace();
}
return null;
}
@Bean
public ConnectionPool pool() {
return new ConnectionPool(maxIdleConnections, keepAliveDuration, TimeUnit.SECONDS);
}
}
4. OkHttp 类
slf4j.Slf4j;
import okhttp3.*;
import org.ption.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Map;
/
**
* @author Answer.AI.L
* @date 2019-04-09
*/
@Slf4j
@Component
public class OkHttpCli {
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); private static final MediaType XML = MediaType.parse("application/xml; charset=utf-8"); @Autowired
private OkHttpClient okHttpClient;
/**
* get 请求
* @param url 请求url地址
* @return string
* */
public String doGet(String url) {
return doGet(url, null, null);
}
/**
* get 请求
* @param url 请求url地址
* @param params 请求参数 map
* @return string
* */
public String doGet(String url, Map<String, String> params) {
return doGet(url, params, null);
}
/**
* get 请求
* @param url 请求url地址
* @param headers 请求头字段 {k1, v1 k2, v2, ...}
* @return string
* */
public String doGet(String url, String[] headers) {
return doGet(url, null, headers);
}
/**
* get 请求
* @param url 请求url地址
* @param params 请求参数 map
* @param headers 请求头字段 {k1, v1 k2, v2, ...}
* @return string
* */
public String doGet(String url, Map<String, String> params, String[] headers) {
StringBuilder sb = new StringBuilder(url);
if (params != null && params.keySet().size() > 0) {
boolean firstFlag = true;
for (String key : params.keySet()) {
if (firstFlag) {
sb.append("?").append(key).append("=").(key));
firstFlag = false;
} else {
sb.append("&").append(key).append("=").(key));
}
}
}
Request.Builder builder = new Request.Builder();
if (headers != null && headers.length > 0) {
if (headers.length % 2 == 0) {
for (int i = 0; i < headers.length; i = i + 2) {
builder.addHeader(headers[i], headers[i + 1]);
}
} else {
log.warn("headers's length[{}] is error.", headers.length);
}
}
Request request = builder.String()).build();
log.info("do get request and url[{}]", sb.toString());
return execute(request);
}
/**
* post 请求
* @param url 请求url地址
* @param params 请求参数 map
* @return string
*/
public String doPost(String url, Map<String, String> params) {
FormBody.Builder builder = new FormBody.Builder();
if (params != null && params.keySet().size() > 0) {
for (String key : params.keySet()) {
builder.add(key, (key));
}
}
Request request = new Request.Builder().url(url).post(builder.build()).build();
log.info("do post request and url[{}]", url);
return execute(request);
}
/**
* post 请求, 请求数据为 json 的字符串
* @param url 请求url地址
* @param json 请求数据, json 字符串
* @return string
*/
public String doPostJson(String url, String json) {
log.info("do post request and url[{}]", url);
return exectePost(url, json, JSON);
}
/**
* post 请求, 请求数据为 xml 的字符串
* @param url 请求url地址
* @param xml 请求数据, xml 字符串
* @return string
*/
public String doPostXml(String url, String xml) {
log.info("do post request and url[{}]", url);
return exectePost(url, xml, XML);
}
private String exectePost(String url, String data, MediaType contentType) {
RequestBody requestBody = ate(contentType, data);
Request request = new Request.Builder().url(url).post(requestBody).build();
return execute(request);
}
private String execute(Request request) {
Response response = null;
try {
response = wCall(request).execute();
if (response.isSuccessful()) {
return response.body().string();
}
} catch (Exception e) {
<(StackTrace(e));
springboot和过滤器} finally {
if (response != null) {
response.close();
}
}
return "";
}
}
5. 使⽤验证
@RestController
public class AnswerController {
@Autowired
private OkHttpCli okHttpCli;
@RequestMapping(value = "show", method = RequestMethod.GET)
public String show() {
String url = "www.baidu/";
String message = okHttpCli.doGet(url);
return message;
}
}
6. 双向认证(待证)
@Bean
public SSLSocketFactory sslSocketFactory() {
String certPath = "";
String caPath = "";
String certPwd = "";
String caPwd = "";
try {
ClassPathResource selfcertPath = new ClassPathResource(certPath);
ClassPathResource trustcaPath = new ClassPathResource(caPath);
KeyStore selfCert = Instance("pkcs12");
selfCert.InputStream(), CharArray());
KeyManagerFactory kmf = Instance("sunx509");
kmf.init(selfCert, CharArray());
KeyStore caCert = Instance("jks");
caCert.InputStream(), CharArray());
TrustManagerFactory tmf = Instance("sunx509");
tmf.init(caCert);
SSLContext sslContext = Instance("TLS");
sslContext.KeyManagers(), TrustManagers(), null);
SocketFactory();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
补充:Spring Cloud Feign 总结问题,注意点,性能调优,切换okhttp3 Feign常见问题总结
FeignClient接⼝如使⽤@PathVariable ,必须指定value属性
//在⼀些早期版本中, @PathVariable("id") 中的 "id" ,也就是value属性,必须指定,不能省略。@FeignClient("microservice-provider-user")
public interface UserFeignClient {
@RequestMapping(value = "/simple/{id}", method = RequestMethod.GET)
public User findById(@PathVariable("id") Long id);
...
}
Java代码⾃定义Feign Client的注意点与坑
@FeignClient(name = "microservice-provider-user", configuration = UserFeignConfig.class) public interface UserFeignClient {
@GetMapping("/users/{id}")
User findById(@PathVariable("id") Long id);
}
/**
* 该Feign Client的配置类,注意:
* 1. 该类可以独⽴出去;
* 2. 该类上也可添加@Configuration声明是⼀个配置类;
* 配置类上也可添加@Configuration注解,声明这是⼀个配置类;
* 但此时千万别将该放置在主应⽤程序上下⽂@ComponentScan所扫描的包中,
* 否则,该配置将会被所有Feign Client共享,⽆法实现细粒度配置!
* 个⼈建议:像我⼀样,不加@Configuration注解
*
* @author zhouli
*/
class UserFeignConfig {
@Bean
public Logger.Level logger() {
return Logger.Level.FULL;
}
}
配置类上也可添加@Configuraiton 注解,声明这是⼀个配置类;但此时千万别将该放置在主应⽤程序上
下⽂@ComponentScan 所扫描的包中,否则,该配置将会被所有Feign Client 共享(相当于变成了通⽤配置,其实本质还是Spring⽗⼦上下⽂扫描包重叠导致的问题),⽆法实现细粒度配置!
个⼈建议:像我⼀样,不加@Configuration注解,省得进坑。
最佳实践:尽量⽤配置属性⾃定义Feign的配置
@FeignClient 注解属性
//@FeignClient(name = "microservice-provider-user")
//在早期的Spring Cloud版本中,⽆需提供name属性,从Brixton版开始,@FeignClient必须提供name属性,否则应⽤将⽆法正常启动!
//另外,name、url等属性⽀持占位符。例如:
@FeignClient(name = "${feign.name}", url = "${feign.url}")
类级别的@RequestMapping会被Spring MVC加载
@RequestMapping("/users")
@FeignClient(name = "microservice-user")
public class TestFeignClient {
// ...
}
类上的@RequestMapping 注解也会被Spring MVC加载。该问题现已经被解决,早期的版本有两种解决⽅案:⽅案1:不在类上加@RequestMapping 注解;⽅案2:添加如下代码:
@Configuration
@ConditionalOnClass({ Feign.class })
public class FeignMappingDefaultConfiguration {
@Bean
public WebMvcRegistrations feignWebRegistrations() {
return new WebMvcRegistrationsAdapter() {
@Override
public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
return new FeignFilterRequestMappingHandlerMapping();
}
};
}
private static class FeignFilterRequestMappingHandlerMapping extends RequestMappingHandlerMapping {
@Override
protected boolean isHandler(Class<?> beanType) {
return super.isHandler(beanType) && !beanType.isInterface();
}
}
}
⾸次请求失败Ribbon的饥饿加载(eager-load)模式
如需产⽣Hystrix Stream监控信息,需要做⼀些额外操作Feign本⾝已经整合了Hystrix,可直接使⽤@FeignClient(value = "microservice-provider-user", fallback = XXX.class) 来指定fallback类,fallback类继承@FeignClient所标注的接⼝即可。
第⼀步:添加依赖,⽰例:
<!-- 整合hystrix,其实feign中⾃带了hystrix,引⼊该依赖主要是为了使⽤其中的hystrix-metrics-event-stream,⽤于dashboard -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
第⼆步:在启动类上添加@EnableCircuitBreaker 注解,⽰例:
@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
@EnableCircuitBreaker
public class MovieFeignHystrixApplication {
public static void main(String[] args) {
SpringApplication.run(MovieFeignHystrixApplication.class, args);
}
}
第三步:在l中添加如下内容,暴露hystrix.stream端点:
management:
endpoints:
web:
exposure:
include: 'hystrix.stream'
Feign 上传⽂件
加依赖
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form-spring</artifactId>
<version>3.0.3</version>
</dependency>
编写Feign Client
@FeignClient(name = "ms-content-sample", configuration = UploadFeignClient.MultipartSupportConfig.class)
public interface UploadFeignClient {
@RequestMapping(value = "/upload", method = RequestMethod.POST,
produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},
consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@ResponseBody
String handleFileUpload(@RequestPart(value = "file") MultipartFile file);
class MultipartSupportConfig {
@Bean
public Encoder feignFormEncoder() {
return new SpringFormEncoder();
}
}
}
如代码所⽰,在这个Feign Client中,我们引⽤了配置类MultipartSupportConfig ,在MultipartSupportConfig 中,我们实例化了SpringFormEncoder 。这样这个Feign Client就能够上传啦。
注意点
//RequestMapping注解中的produeces 、consumes 不能少;
@RequestMapping(value = "/upload", method = RequestMethod.POST,
produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},
consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
接⼝定义中的注解@RequestPart(value = "file") 不能写成@RequestParam(value = "file")。
最好将Hystrix的超时时间设长⼀点,例如5秒,否则可能⽂件还没上传完,Hystrix就超时了,从⽽导致客户端侧的报错。
Feign实现Form表单提交
添加依赖:
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form-spring</artifactId>
<version>3.2.2</version>
</dependency>
Feign Client⽰例:
@FeignClient(name = "xxx", url = "www.itmuch/", configuration = TestFeignClient.FormSupportConfig.class)
public interface TestFeignClient {
@PostMapping(value = "/test",
consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE},
produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}
)
void post(Map<String, ?> queryParam);
class FormSupportConfig {
@Autowired
private ObjectFactory<HttpMessageConverters> messageConverters;
// new⼀个form编码器,实现⽀持form表单提交
@Bean
public Encoder feignFormEncoder() {
return new SpringFormEncoder(new SpringEncoder(messageConverters));
}
// 开启Feign的⽇志
@Bean
public Logger.Level logger() {
return Logger.Level.FULL;
}
}
}
调⽤⽰例:
@GetMapping("/user/{id}")
public User findById(@PathVariable Long id) {
HashMap<String, String> param = wHashMap();
param.put("username","zhangsan");
param.put("password","pwd");
return new User();
}
⽇志:
...[TestFeignClient#post] ---> POST www.baidu/test HTTP/1.1
.
..[TestFeignClient#post] Accept: application/json;charset=UTF-8
...[TestFeignClient#post] Content-Type: application/x-www-form-urlencoded; charset=UTF-8
...[TestFeignClient#post] Content-Length: 30
...[TestFeignClient#post]
...[TestFeignClient#post] password=pwd&username=zhangsan
...[TestFeignClient#post] ---> END HTTP (30-byte body)
由⽇志可知,此时Feign已能使⽤Form表单⽅式提交数据。
Feign GET请求如何构造多参数
@FeignClient("microservice-provider-user")
public interface UserFeignClient {
@RequestMapping(value = "/get", method = RequestMethod.GET)
public User get0(User user);
}
然⽽,这种写法并不正确,控制台会输出类似如下的异常。
feign.FeignException: status 405 reading UserFeignClient#get0(User); content:
{"timestamp":1482676142940,"status":405,"error":"Method Not Allowed","exception":"org.springframework.web.HttpRequestMethodNotSupportedException","message":"Request method 'POST' not supported","path":"/get"}由异常可知,尽管我们指定了GET⽅法,Feign依然会使⽤POST⽅法发送请求。于是导致了异常。正确写法如下
⽅法⼀[推荐]注意:使⽤该⽅法⽆法使⽤Fegin的继承模式
@FeignClient("microservice-provider-user")
public interface UserFeignClient {
@GetMapping("/get")
public User get0(@SpringQueryMap User user);
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论