Getting started with microservices demo

For example, to query all orders of a user = > access the order service in the user service.




Order service

public class OrderController {
    //according to user_id Query all orders of a user
    public List<Order> queryOrdersByUserId(@PathVariable Integer user_id){
        //After a series of operations, return to the order list
        return orderList;


To return the return value of the method to the user service, @ ResponseBody needs to be converted to json format. String, List and Map cannot be passed directly.

The service itself may need to call the view to display, or directly return some type of data, not the data in json format, so it needs to write a new method as the service interface to return json data for other services to call.






User service

public class UserController {
    //springboot Between services REST Mutual access, call, springboot Already put REST Encapsulated as RestTemplate Class, injected for use
    private RestTemplate restTemplate;

    public void setRestTemplate(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;

    public List<Order> queryOrdersByUserId(@PathVariable Integer user_id){
        //Address of order service
        String url = "http://localhost:8080/order/user/{user_id}";
        //Parameters: service address, expected return data type( class Object), parameters to pass in
        List<Order> orderList = restTemplate.getForObject(url, List.class,user_id);
        return orderList;




restTemplate.getForObject(url, targetClass,Object...args); / / variable parameters


Parameters refer to the parameters in the url, such as:

String url = "http://localhost:8080/springboot/user/{group}/{user_id}";

restTemplate.getForObject(url, targetClass, argument 1, argument 2) / / the nth argument corresponds to the nth {} in the url



We see the IDEA prompt: we can't auto assemble because we can't find an instance of type RestTemplate.

We need to manually create an instance of resttempalt:

public class App{
    // Automatically created by the specified method Bean(RestTempalte)An instance of spring Container
    public RestTemplate restTemplate(){
        return new RestTemplate();

    public static void main(String[] args) {;


Created in the bootstrap class and available to all classes of this service.






The url of the service is written in the code above. Of course, it can also be written in application.yml. Use @ Value injection:

    url: http://localhost:8080/springboot/order/user/{user_id}
    private String url;



Although it's better to modify the url, the node address is still dead and fixed, and only the specified node will be accessed. What should I do when load balancing is done, or when the service node is increased or decreased?

The traditional way is to use nginx as agent and load balancing. Nginx decides which node to distribute to the service.

Microservices have their own processing methods: spring cloud uses Eureka to register and discover services, Dubbo uses Zookeeper to register and discover services.

Tags: Java SpringBoot JSON REST Spring

Posted on Tue, 11 Feb 2020 01:26:24 -0500 by battlesiege