Как передать параметры запроса как есть между вызовами службы REST в приложении служб Spring Boot? - PullRequest
0 голосов
/ 21 марта 2019

Мы выполняем архитектурный рефакторинг для преобразования монолитного приложения J2EE EJB в сервисы Spring. Для этого я создаю сервисы, разбивая приложение на стыки его домена. В настоящее время у меня их три, и каждый из них вызывает другую службу через Rest.

В этом проекте нашей конечной целью является преобразование приложения в микросервисы, но поскольку облачная инфраструктура не ясна и, вероятно, не будет возможна, мы решили сделать это таким образом и подумали, что, поскольку службы используют Rest, оно будет легко сделать трансформацию в будущем.

Имеет ли наш подход смысл? Мой вопрос проистекает из этого.


Я отправляю запрос в UserService с параметром заголовка userName от Почтальона.

GET http://localhost:8087/users/userId?userName=12345

UserService вызывает другую службу, которая вызывает другую. Порядок звонков на отдых между службами таков:

UserService ---REST--> CustomerService ---REST--> AlarmService

Поскольку я сейчас выполняю работу по переносу общих параметров запроса, как это, мне нужно установить общие параметры заголовка в каждом методе, который выполняет запросы Rest, перенося их из входящего запроса в исходящий запрос:

@RequestMapping(value="/users/userId", method = RequestMethod.GET)
public ResponseEntity<Long> getUserId(@RequestHeader("userName") String userName) {
    ...
        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(Collections.singletonList
(MediaType.APPLICATION_JSON));

        headers.set("userName", userName);

        HttpEntity<String> entity = new HttpEntity<>("parameters", headers);
        HttpEntity<Long> response =
                restTemplate.exchange(CUSTOMER_REST_SERVICE_URI,
                HttpMethod.GET, entity, Long.class);
     ...
 }

UserService:

package com.xxx.userservice.impl;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Collections;
import java.util.Map;

@RestController
public class UserController  extends AbstractService{

    Logger logger = Logger.getLogger(UserController.class.getName());

    @Autowired
    private RestTemplate restTemplate;

    private final String CUSTOMER_REST_SERVICE_HOST = "http://localhost:8085";
    private final String CUSTOMER_REST_SERVICE_URI = CUSTOMER_REST_SERVICE_HOST + "/customers/userId";

    @RequestMapping(value="/users/userId", method = RequestMethod.GET)
    public ResponseEntity<Long> getUserId(@RequestHeader("userName") String userName) {
        logger.info(""user service is calling customer service..."");
        try {

            //do the internal customer service logic

            //call other service.
            HttpHeaders headers = new HttpHeaders();
            headers.setAccept(Collections.singletonList
(MediaType.APPLICATION_JSON));
            headers.set("userName", userName);
            HttpEntity<String> entity = new HttpEntity<>("parameters", headers);

            HttpEntity<Long> response =
                    restTemplate.exchange(CUSTOMER_REST_SERVICE_URI,
                    HttpMethod.GET, entity, Long.class);

            return ResponseEntity.ok(response.getBody());
        } catch (Exception e) {
            logger.error("user service could not call customer service: ", e);
            throw new RuntimeException(e);
        }
        finally {
            logger.info("customer service called...");
        }
    }

}

CustomerService:

package com.xxxx.customerservice.impl;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.xxx.interf.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CustomerController  extends AbstractService{

    private final String ALARM_REST_SERVICE_HOST = "http://localhost:8086";
    private final String ALARM_REST_SERVICE_URI = ALARM_REST_SERVICE_HOST + "/alarms/maxAlarmCount";

    @Autowired
    private CustomerService customerService;

    @Autowired
    private RestTemplate restTemplate;

    ...

    @GetMapping(path="/customers/userId", produces = "application/json")
    public long getUserId(@RequestHeader(value="Accept") String acceptType) throws RemoteException {

        //customer service internal logic.
        customerService.getUserId();

        //customer service calling alarm service.
        return restTemplate.getForObject(ALARM_REST_SERVICE_URI, Long.class);

    }

}

AlarmService:

package com.xxx.alarmservice.impl;

import com.xxx.interf.AlarmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PriceAlarmController extends AbstractService{

    @Autowired
    private AlarmService priceAlarmService;

    @RequestMapping("/alarms/maxAlarmCount")
    public long getMaxAlarmsPerUser() {

        // alarm service internal logic.
        return priceAlarmService.getMaxAlarmsPerUser();
    }

}

Я пробовал эти файлы конфигурации и перехватчика, но я могу использовать их только для регистрации и не могу передать параметры заголовка, используя их. Вероятно, потому что они есть у каждого сервиса. Кроме того, этот перехватчик работает только в UserService, который сначала использует RestTemplate для отправки запроса. Вызываемый сервис и первый запрос, поступающий от Postman, не работают с ним, потому что они не печатают сообщения журнала, как это делает UserService.

CommonModule:

package com.xxx.common.config;

import com.xxx.common.util.HeaderRequestInterceptor;
import org.apache.cxf.common.util.CollectionUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class RestTemplateConfig {

    @Bean
    public RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate();

        List<ClientHttpRequestInterceptor> interceptors
                = restTemplate.getInterceptors();
        if (CollectionUtils.isEmpty(interceptors)) {
            interceptors = new ArrayList<>();
        }
        interceptors.add(new HeaderRequestInterceptor());
        restTemplate.setInterceptors(interceptors);
        return restTemplate;
    }
}

ClientHttpRequestInterceptor:


package com.xxx.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpRequest;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.util.StreamUtils;

import java.io.IOException;
import java.nio.charset.Charset;

public class HeaderRequestInterceptor implements ClientHttpRequestInterceptor {

    private final Logger log = LoggerFactory.getLogger(this.getClass());

    @Override
    public ClientHttpResponse intercept(
            HttpRequest request,
            byte[] body,
            ClientHttpRequestExecution execution) throws IOException
    {
        log.info("HeaderRequestInterceptor....");
        logRequest(request, body);
        request.getHeaders().set("Accept", MediaType.APPLICATION_JSON_VALUE);

        ClientHttpResponse response = execution.execute(request, body);
        logResponse(response);

        return response;
    }

    private void logRequest(HttpRequest request, byte[] body) throws IOException
    {
        log.info("==========request begin=======================");
    }

    private void logResponse(ClientHttpResponse response) throws IOException
    {
        log.info("==========response begin=============");
    }

}

Как я могу управлять передачей информации общего заголовка, такой как userName, используя какие-то перехватчики или другой механизм в одном месте?

1 Ответ

0 голосов
/ 24 марта 2019

В методе перехвата вашего HeaderRequestInterceptor вы можете получить доступ к текущему http-запросу и его заголовкам (userId в вашем случае) следующим образом:

@Override
public ClientHttpResponse intercept(HttpRequest request..
...
   HttpServletRequest httpServletRequest = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
   String userId = httpServletRequest.getHeader("userId");
   request.getHeaders().set("userId", userId);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...