通过Feign和Eureka @PostMapping到其他微服务

时间:2019-03-21 14:53:47

标签: java spring microservices netflix-eureka feign

我正在使用Spring Framework,Feign和Eureka构建微服务架构。

  • Shop充当API网关,通过Zuul(无数据库)实现
  • 每个微服务都有自己的h2数据库。
  • CustomerService可以使用Feign调用购物车映射

示例:我有一个“商店”,并希望通过一种服务本身的方法来创建带有购物车的客户。

如何在ShopService中调用方法来管理其他服务,而又不与其他服务建立关系?如果需要代码示例,请告诉我。

CustomerController.java:

@ComponentScan
@RestController
public class CustomerController {

    final Customer2CartConnectorRequester customer2CartConnectorRequester;
    final Customer2OrderConnectorRequester customer2OrderConnectorRequester;

    @Autowired
    private CustomerJpaRepository customerJpaRepository;

    @Autowired
    public CustomerController(Customer2CartConnectorRequester customer2CartConnectorRequester,
            Customer2OrderConnectorRequester customer2OrderConnectorRequester) {
        this.customer2CartConnectorRequester = customer2CartConnectorRequester;
        this.customer2OrderConnectorRequester = customer2OrderConnectorRequester;

    }

    @GetMapping("/list")
    @ResponseBody
    public List<CustomerEntity> getCustomers() {

        return customerJpaRepository.findAll();
    }


    @RequestMapping("/{customerId}")
    @ResponseBody
    public Optional<CustomerEntity> getCustomer(@PathVariable("customerId") int customerId) {

        return customerJpaRepository.findById(customerId);
    }

    // Client to Server
    // POST über z.B. Postman Client
    @PostMapping("/customer")
    public CustomerEntity addCustomer(@RequestBody CustomerEntity customerEntity) {

        customerJpaRepository.save(customerEntity);

        return customerEntity;
    }

    @DeleteMapping("customer/{customerId}")
    public String deleteCustomer(@PathVariable int customerId) {

        CustomerEntity a = customerJpaRepository.getOne(customerId);
        customerJpaRepository.delete(a);

        return "deleted";
    }

    @PutMapping("/customer")
    public CustomerEntity updateCustomer(@RequestBody CustomerEntity customerEntity) {

        customerJpaRepository.save(customerEntity);

        return customerEntity;
    }

    /// CONNECTOR REQUESTER ///

    @GetMapping("/cart")
    public List<?> getCart() {
        return customer2CartConnectorRequester.getCart();
    }

    @GetMapping("/orders")
    public List<?> getOrders() {
        return customer2OrderConnectorRequester.getOrders();
    }

}

更新: 我尝试使用POST方法通过Postman Client这样发送数据,但似乎仍然存在问题:

Shop2CustomerConnectorRequester:

@FeignClient("customermicroservice")
public interface Shop2CustomerConnectorRequester {

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ResponseEntity<String> createCustomer(Map<String, ?> queryMap);
}

ShopController:

@ComponentScan
@RestController
public class ShopController {

    final Shop2CustomerConnectorRequester shop2CustomerConnectorRequester;

    @Autowired
    private ShopJpaRepository shopJpaRepository;

    @Autowired
    public ShopController(Shop2CustomerConnectorRequester shop2CustomerConnectorRequester) {
        this.shop2CustomerConnectorRequester = shop2CustomerConnectorRequester;

    }

    @GetMapping("/customer")
    public  ResponseEntity<String> createCustomer(Map<String, ?> queryMap) {
        return shop2CustomerConnectorRequester.createCustomer(queryMap);
    }


}

0 个答案:

没有答案