Diese Seite …

Zuletzt geändert 1 min Lesezeit (171 Wörter)

OpenFeign - Eureka

Feing means to fake to hide to obscure so you don’t have to remeber HTTP Verbs, port/address to it.

Feign generates based on anotations at runtime an HTTP class so you don’t have to code it by hand.

Eureka

image

Create a new book

Service 01 Changes

Add to the MainController.java:

    @PostMapping("/api/books")
    public Book addNewBook(@RequestBody Book book) {
        return booksClient.createBook(book);
    }

And to BooksClient which is the actual call to the service-02:

    @PostMapping("/books")
    Book createBook(@RequestBody Book book);

Service-02 Changes

The RESTBookController.java is the implementation for new endpoints:

    @PostMapping("books")
    public Book addBook(@RequestBody Book book) {
        int lastId = books.stream()
                .mapToInt(Book::getId)
                .max()
                .orElse(1);
        book.setId(lastId + 1);
        books.add(book);
        return book;
    }

Create a new book

#!/usr/bin/env sh

PORT=8081
curl -X POST "http://localhost:${PORT}/api/books" \
  -H "Content-Type: application/json" \
  -d '{"id": 5, "title": "The Left Hand of Darkness", "author": "Ursula K. Le Guin"}'

Verify with, should show 5 elements:

curl "http://localhost:8081/api/books"

Sources: