@HystrixCommand не работает при вызове внутреннего метода - PullRequest
0 голосов
/ 07 июня 2019

У меня есть метод @Service аннотированный класс, который вызывает удаленный сервис. когда я добавляю @HystrixCommand к этому методу, он работает нормально. но когда этот удаленный сервис перешел к новому методу и добавил @HystrixCommand ко второму методу, Hystrix перестала работать. этот второй метод также публичный метод в том же классе.

  1. Это ошибка или как дизайн?
  2. как это исправить?

примечание: В рабочем примере я добавил HttpHeaders в качестве параметра, потому что не могу получить доступ к SecurityContextHolder.getContext() с помощью команды hystrix

// это работает нормально

 @HystrixCommand(fallbackMethod = "fetchEmployeesBack")
    public Employee fetchEmployees(Employee employee,HttpHeaders httpHeaders) {
        Optional<Employee> optionalEmployee = employeeRepository.findById(employee.getId());
        if (optionalEmployee.isPresent()) {
            //fetch project allocation
            ResponseEntity<Allocation[]> responseEntity;
            HttpEntity<String> entity = new HttpEntity<>("", httpHeaders);
            responseEntity =
                    restTemplate.exchange("http://allocation-service/rest/allocations/".
                            concat(employee.getId().toString()), HttpMethod.GET, entity,Allocation[].class);
            Employee employee1 = optionalEmployee.get();
            System.out.println(responseEntity.getBody().toString());
            employee1.setAllocations(responseEntity.getBody());
            return employee1;
        } else {
            return null;
        }
    }

// это не работает

    public Employee fetchEmployees(Employee employee) {
        Optional<Employee> optionalEmployee = employeeRepository.findById(employee.getId());
        if (optionalEmployee.isPresent()) {

            Employee employee1 = optionalEmployee.get();
            Allocation[] allocations = fetchEmployeesAllocation(optionalEmployee.get());
            employee1.setAllocations(allocations);
            return employee1;
        } else {
            return null;
        }
    }

    @HystrixCommand(fallbackMethod = "fetchEmployeesAllocationFallback")
    public Allocation[] fetchEmployeesAllocation(Employee employee) {
        //fetch project allocation
        HttpHeaders httpHeaders = new HttpHeaders();
        //extract token from context
        OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails)
                SecurityContextHolder.getContext().getAuthentication().getDetails();
        httpHeaders.add("Authorization", "bearer ".concat(details.getTokenValue()));
        //
        ResponseEntity<Allocation[]> responseEntity;
        HttpEntity<String> entity = new HttpEntity<>("", httpHeaders);

        responseEntity =
                restTemplate.exchange("http://allocation-service/rest/allocations/".
                        concat(employee.getId().toString()), HttpMethod.GET, entity, Allocation[].class);
        return responseEntity.getBody();
    }

в этом не работающем примере это дает исключение Ribbon, говорящее, что ни один экземпляр из службы распределения не запущен. не распознается как команда Hystrix

...