Feign融合nacos使用https微服务调用
时间: 2023-06-29 18:12:56 浏览: 267
可以通过以下步骤实现Feign与Nacos的https微服务调用:
1. 在Nacos中配置https协议的微服务实例,确保服务已经正常运行。
2. 在Feign客户端中配置https请求的方式,可以参考以下代码:
```java
@Configuration
public class FeignHttpsConfig {
@Bean
public OkHttpClient okHttpClient() {
return new OkHttpClient.Builder()
.sslSocketFactory(createSSLSocketFactory())
.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
})
.build();
}
private SSLSocketFactory createSSLSocketFactory() {
SSLSocketFactory sslSocketFactory = null;
try {
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[]{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];
}
}}, new SecureRandom());
sslSocketFactory = sslContext.getSocketFactory();
} catch (Exception e) {
e.printStackTrace();
}
return sslSocketFactory;
}
}
```
3. 在Feign客户端中使用@FeignClient注解指定微服务名称,并通过@RequestLine注解指定https请求的方式,例如:
```java
@FeignClient(name = "example-service", configuration = FeignHttpsConfig.class)
public interface ExampleFeignClient {
@RequestLine("GET /example")
String getExample();
}
```
4. 在应用启动类中添加@EnableFeignClients注解,以启用Feign客户端。
5. 调用Feign客户端的方法即可实现https微服务调用。
阅读全文