微服务通过feign返回的对象 如何获得
时间: 2023-11-08 22:14:23 浏览: 76
如果你使用 Feign 来进行微服务调用,Feign 会将服务接口封装成一个代理对象,通过这个代理对象来实现远程服务的调用。在 Feign 的默认配置下,Feign 会将服务接口的返回值转换成字符串进行返回,而不是直接返回对象。
如果你想要获得返回的对象,需要进行一些配置。具体来说,你需要在启动类上添加 `@EnableFeignClients` 注解,并且在接口方法上添加 `@RequestMapping` 注解,同时设置 `consumes` 和 `produces` 属性。例如:
```java
@FeignClient(name = "service-provider")
public interface UserFeignClient {
@RequestMapping(value = "/user/{id}", method = RequestMethod.GET,
consumes = "application/json", produces = "application/json")
User getUserById(@PathVariable("id") Long id);
}
```
在这个例子中,`consumes` 和 `produces` 属性都被设置成了 `application/json`,表示接口方法返回的是 JSON 格式的数据。这样,当你调用 `getUserById` 方法时,Feign 就会将返回的 JSON 数据转换成 `User` 对象,然后返回给你。
相关问题
微服务 feign 调用https
微服务之间的调用,也可以使用Feign来进行,同样需要在Feign的配置中启用HTTPS支持。具体步骤如下:
1. 引入Feign的依赖
```
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
```
2. 配置Feign的HTTPS支持
```
@Configuration
public class FeignConfig {
@Autowired
private ObjectFactory<HttpMessageConverters> messageConverters;
@Bean
public Client feignClient() {
return new Client.Default(getSSLSocketFactory(), null);
}
private SSLSocketFactory getSSLSocketFactory() {
try {
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[] { new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {}
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {}
public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; }
} }, new SecureRandom());
return sslContext.getSocketFactory();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Bean
public Decoder feignDecoder() {
return new ResponseEntityDecoder(new SpringDecoder(messageConverters));
}
}
```
其中,`getSSLSocketFactory`方法返回一个`SSLSocketFactory`对象,用于创建HTTPS连接。这里采用了忽略证书的方式,实际生产环境中应该使用可信的证书。
3. 在Feign的接口中使用`https`协议
```
@FeignClient(name = "example", url = "https://example.com")
public interface ExampleClient {
@GetMapping("/api")
String get();
}
```
其中,`url`参数指定了HTTPS协议的地址。
4. 在微服务中使用Feign进行调用
```
@RestController
public class ExampleController {
@Autowired
private ExampleClient exampleClient;
@GetMapping("/example")
public String get() {
return exampleClient.get();
}
}
```
这样就可以在微服务中使用Feign调用HTTPS接口了。
微服务 feign 负载均衡 https
微服务之间的调用,如果需要进行负载均衡,可以使用Ribbon和Feign的结合。同样需要在Feign的配置中启用HTTPS支持,并且配置Ribbon的负载均衡策略。具体步骤如下:
1. 引入Ribbon和Feign的依赖
```
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
```
2. 配置Feign和Ribbon的HTTPS支持和负载均衡策略
```
@Configuration
public class FeignConfig {
@Autowired
private ObjectFactory<HttpMessageConverters> messageConverters;
@Bean
public Client feignClient() {
return new Client.Default(getSSLSocketFactory(), null);
}
private SSLSocketFactory getSSLSocketFactory() {
try {
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[] { new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {}
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {}
public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; }
} }, new SecureRandom());
return sslContext.getSocketFactory();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Bean
public Decoder feignDecoder() {
return new ResponseEntityDecoder(new SpringDecoder(messageConverters));
}
@Bean
public IRule ribbonRule() {
return new RandomRule();
}
@Bean
public IPing ribbonPing() {
return new PingUrl(false, "/health");
}
@Bean
public ServerList<Server> ribbonServerList() {
return new ConfigurationBasedServerList();
}
@Bean
public OkHttpClient ribbonOkHttpClient() {
return new OkHttpClient.Builder().sslSocketFactory(getSSLSocketFactory(), new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}).build();
}
@Bean
public RibbonClientConfiguration ribbonClientConfiguration(IClientConfig config, ILoadBalancer loadBalancer, ServerList<Server> serverList, ServerListFilter<Server> serverListFilter, IRule rule, IPing ping, RetryHandler retryHandler, OkHttpClient ribbonOkHttpClient) {
return new RibbonClientConfiguration(config, loadBalancer, serverList, serverListFilter, rule, ping, retryHandler, ribbonOkHttpClient);
}
}
```
其中,`ribbonRule`方法配置了Ribbon的负载均衡策略,这里采用了随机策略,可以根据实际需求进行调整。
3. 在Feign的接口中使用`https`协议和负载均衡地址
```
@FeignClient(name = "example", configuration = ExampleConfig.class)
public interface ExampleClient {
@GetMapping("/api")
String get();
}
```
其中,`configuration`参数指定了配置类,用于配置Ribbon相关的设置。
4. 在配置类中配置Ribbon的信息
```
@Configuration
public class ExampleConfig {
@Bean
public IRule ribbonRule() {
return new RandomRule();
}
@Bean
public IPing ribbonPing() {
return new PingUrl(false, "/health");
}
@Bean
public ServerList<Server> ribbonServerList(IClientConfig config) {
return new ConfigurationBasedServerList();
}
}
```
其中,`ribbonRule`方法和`ribbonPing`方法分别配置了Ribbon的负载均衡策略和健康检查的地址,`ribbonServerList`方法返回一个`ServerList`对象,用于配置可用的服务列表。
5. 在微服务中使用Feign进行调用
```
@RestController
public class ExampleController {
@Autowired
private ExampleClient exampleClient;
@GetMapping("/example")
public String get() {
return exampleClient.get();
}
}
```
这样就可以在微服务中使用Feign调用HTTPS接口,并且进行负载均衡了。
阅读全文