使用restTemplate(httpClient)实现微服务之间的调用

一:需要调用的微服务, 就和平时写的微服务一样, 能实现数据的增删改查就行。

	@RequestMapping("/add")
    public CommonResult add(Payment payment) {
   
    @RequestMapping("/getPayment")
    public CommonResult getPayment(Long id) {

二:消费者的实现

  1. 将restTemplate注入到容器里面
@Configuration
public class ApplicationConfig {
    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    };
}

在controller里面使用它调用之前的微服务

 @Autowired
    private RestTemplate restTemplate;
    public static final String PAYMENT_URL = "http://localhost:8001" ;

    @GetMapping("/consumer/payment/add")
    public ResponseEntity<CommonResult> add(Payment payment){
        return restTemplate.postForEntity(PAYMENT_URL + "/add", payment, CommonResult.class );
    }

    @GetMapping("/consumer/payment/getPayment")
    public ResponseEntity<CommonResult> getPayment(@RequestParam("id") int id){
        return restTemplate.getForEntity(PAYMENT_URL + "/getPayment?id="+id, CommonResult.class );
    }

此时, 我们使用消费者的端口号去访问

http://localhost/consumer/payment/getPayment?id=1

结果如下
在这里插入图片描述

但是插入会出现问题,显示插入成功但是数据库里面的数据为null
在这里插入图片描述
此时 解决这个问题的方法就是在8001里面添加@RequestBody

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Spring Boot 3.1 中使用 `RestTemplate` 和 `HttpClient 5`,你需要添加以下依赖项到你的 `pom.xml` 文件中: ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>5.0.4</version> </dependency> </dependencies> ``` 然后,你需要创建一个 `RestTemplate` 的 Bean,使用 `HttpClient 5` 作为底层 HTTP 客户端。以下是示例代码: ```java import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; import org.apache.hc.client5.http.impl.classic.HttpClients; import org.apache.hc.core5.http.config.Registry; import org.apache.hc.core5.http.config.RegistryBuilder; import org.apache.hc.core5.http.impl.io.PoolingHttpClientConnectionManager; import org.apache.hc.core5.http.io.SocketConfig; import org.apache.hc.core5.http.protocol.HttpRequestInterceptor; import org.apache.hc.core5.http.protocol.RequestAddCookies; import org.apache.hc.core5.http.protocol.RequestDefaultHeaders; import org.apache.hc.core5.http.protocol.ResponseProcessCookies; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; @Configuration public class RestTemplateConfig { @Bean public RestTemplate restTemplate() { // 创建 HttpClient CloseableHttpClient httpClient = HttpClients.custom() .setDefaultRequestConfig(RequestConfig.custom() .setSocketTimeout(5000) .setConnectTimeout(5000) .build()) .setConnectionManager(new PoolingHttpClientConnectionManager( RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.getSocketFactory()) .register("https", SSLConnectionSocketFactory.getSocketFactory()) .build())) .setDefaultSocketConfig(SocketConfig.custom() .setTcpNoDelay(true) .build()) .addInterceptorFirst((HttpRequestInterceptor) new RequestDefaultHeaders( Arrays.asList(new BasicHeader("User-Agent", "MyRestClient")))) // 设置 User-Agent .addInterceptorFirst(new RequestAddCookies()) .addInterceptorLast(new ResponseProcessCookies()) .build(); // 创建 RestTemplate HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(); factory.setHttpClient(httpClient); RestTemplate restTemplate = new RestTemplate(factory); return restTemplate; } } ``` 这里我们创建了一个 `RestTemplate` 的 Bean,使用 `HttpClient 5` 作为底层 HTTP 客户端。我们还可以设置一些默认的请求配置和拦截器,例如设置 `User-Agent`、添加 Cookie 等。 现在你可以在你的代码中使用 `RestTemplate` 进行 HTTP 请求了: ```java @Autowired private RestTemplate restTemplate; public void doRequest() { String url = "http://example.com/api"; ResponseEntity<String> response = restTemplate.getForEntity(url, String.class); String body = response.getBody(); // 处理响应数据 } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值