哪些微服务代码在https://dzone.com/articles/spring-boot-creating project?

时间:2016-09-13 17:13:29

标签: spring-boot microservices spring-rest

这些天我听到很多关于微服务的消息,并想知道创建POC (Proof of Concept)以便在我的项目中应用,这将从划痕开始。我读了很多关于mocroservices的博客,发现了这个链接:https://dzone.com/articles/spring-boot-creating。我能够成功运行链接中提到的代码。但我觉得它很简单Spring Boot REST example,我不确定microservices在这个例子中的作用是什么?我现在从技术的角度来理解这一点,因为我理论上理解它是什么。请指导。

请参阅以下代码以供参考:

curl -vvv "http://localhost:8080/order?idCustomer=2&idProduct=3&amount=4"

Trying 127.0.0.1...
Connected to localhost (127.0.0.1) port 8080 (#0) 
> GET /order?idCustomer=2&idProduct=3&amount=4 HTTP/1.1 
> Host: localhost:8080 > User-Agent: curl/7.46.0 
> Accept: / > < HTTP/1.1 200 < Content-Type: application/json < Content-Length: 173 
< Date: Fri, 09 Sep 2016 07:22:02 GMT 
< {"id":1,"amount":4,"orderDate":1473405722862,"customer":{"id":2,"name":"Customer 2","email":"Customer2@gmail.com"},"product":{"id":3,"sku":"abcd3","description":"Product3"}}* Connection #0 to host localhost left intact

Application.java

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

ApplicationConfig.java

@Configuration
public class ApplicationConfig {
    @Named
    static class JerseyConfig extends ResourceConfig {
        public JerseyConfig() {
            this.packages("br.com.alexandreesl.handson.rest");
        }
    }
    @Bean
    public RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        return restTemplate;
    }
}

Customer.java

public class Customer {
    private long id;
    private String name;
    private String email;
    // setters and getters
}

CustomerRest.java

@Named
@Path("/customer")
public class CustomerRest {
    private static List<Customer> customers = new ArrayList<Customer>();

    static {
        Customer customer1 = new Customer();
        customer1.setId(1);
        customer1.setName("Customer 1");
        customer1.setEmail("customer1@gmail.com");

        Customer customer2 = new Customer();
        customer2.setId(2);
        customer2.setName("Customer 2");
        customer2.setEmail("Customer2@gmail.com");

        Customer customer3 = new Customer();
        customer3.setId(3);
        customer3.setName("Customer 3");
        customer3.setEmail("Customer3@gmail.com");

        Customer customer4 = new Customer();
        customer4.setId(4);
        customer4.setName("Customer 4");
        customer4.setEmail("Customer4@gmail.com");

        Customer customer5 = new Customer();
        customer5.setId(5);
        customer5.setName("Customer 5");
        customer5.setEmail("Customer5@gmail.com");

        customers.add(customer1);
        customers.add(customer2);
        customers.add(customer3);
        customers.add(customer4);
        customers.add(customer5);
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<Customer> getCustomers() {
        return customers;
    }

    @GET
    @Path("/getCustomer")
    @Produces(MediaType.APPLICATION_JSON)
    public Customer getCustomer(@QueryParam("id") long id) {
        Customer cli = null;
        for (Customer c : customers) {
            if (c.getId() == id)
                cli = c;
        }
        return cli;
    }
}

Order.java

public class Order {
    private long id;
    private long amount;
    private Date orderDate;
    private Customer customer;
    private Product product;
      // setters and getters
}

OrderRest.java

@Named
@Path("/")
public class OrderRest {
    private long id = 1;

    @Inject
    private RestTemplate restTemplate;

    @GET
    @Path("order")
    @Produces(MediaType.APPLICATION_JSON)
    public Order submitOrder(@QueryParam("idCustomer") long idCustomer,
            @QueryParam("idProduct") long idProduct,
            @QueryParam("amount") long amount) {

        Customer customer = restTemplate.getForObject("http://localhost:8080/customer/getCustomer?id={id}", Customer.class, idCustomer);
        Product product = restTemplate.getForObject("http://localhost:8080/product/getProduct?id={id}", Product.class,idProduct);

        Order order = new Order();
        order.setCustomer(customer);
        order.setProduct(product);
        order.setId(id);
        order.setAmount(amount);
        order.setOrderDate(new Date());
        id++;
        return order;
    }
}

Product.java

public class Product {
    private long id;
    private String sku;
    private String description;
        // setters and getters
}

ProductRest.java

@Named
@Path("/product")
public class ProductRest {
    private static List<Product> products = new ArrayList<Product>();

    static {
        Product product1 = new Product();
        product1.setId(1);
        product1.setSku("abcd1");
        product1.setDescription("Product1");
        Product product2 = new Product();
        product2.setId(2);
        product2.setSku("abcd2");
        product2.setDescription("Product2");
        Product product3 = new Product();
        product3.setId(3);
        product3.setSku("abcd3");
        product3.setDescription("Product3");
        Product product4 = new Product();
        product4.setId(4);
        product4.setSku("abcd4");
        product4.setDescription("Product4");
        products.add(product1);
        products.add(product2);
        products.add(product3);
        products.add(product4);
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<Product> getProducts() {
        return products;
    }

    @GET
    @Path("/getProduct")
    @Produces(MediaType.APPLICATION_JSON)
    public Product getProduct(@QueryParam("id") long id) {
        Product prod = null;
        for (Product p : products) {
            if (p.getId() == id)
                prod = p;
        }
        return prod;
    }
}

0 个答案:

没有答案