Location>code7788 >text

Getting Started with SpringCloud (II) Inter-Service Calls and Cases

Popularity:860 ℃/2024-09-19 11:51:00

I. Microservice splitting considerations
Microservice Splitting Considerations:
1. Single responsibility: different microservices, do not repeat the development of the same business
2. Data independence: do not access the database of other microservices
3. Service-oriented: expose their business as an interface for other microservice calls

1. Microservices need to be split according to the business module, to achieve a single responsibility, do not repeat the development of the same business
2. Microservices can expose the business as an interface for other microservices to use
3. Different microservices should have their own independent database

II. Orders and user service call cases
Two separate services for orders and users; two separate databases.

 

 

-Requirements:According to the order id query order at the same time, the order belongs to the user information together with the return

Do not duplicate the development of the business, you can not directly check the database, the service is independent of the invisible to others database.
Order to initiate a remote call to the user; how to complete the remote call; remote call method analysis: initiating http request. The figure below shows how to do this:

 

 

Use the RestTemplate http request provided by spring; annotated as a spring object via bean;

Based on RestTemplate initiated http request to achieve remote call. http request to do remote call is language-independent call, as long as you know the other side of the ip, port, interface path, request parameters can be.

So we need to make an http request to user-service in order-service calling the interface http://localhost:8081/user/{userId}.

Steps to use:

The steps are this:

Step 1, register an instance of RestTemplate to the Spring container

To register RestTemplate, first, we register the RestTemplate instance in the OrderApplication startup class in the order-service service:

@MapperScan("")
@SpringBootApplication
public class OrderApplication {
 
    public static void main(String[] args) {
        (OrderApplication.class, args);
    }
 
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

 


Step 2: Modify the queryOrderById method in the OrderService class of the order-service service to query User based on the userId in the Order object.

Modify the queryOrderById method in the OrderService class under the package in the order-service service:

Step 3: Populate the query User with Order objects and return them together

 

@Service
public class OrderService {
 
    @Autowired
    private OrderMapper orderMapper;
 
 
    @Autowired
    private RestTemplate restTemplate;
 
    public Order queryOrderById(Long orderId) {
        // 1. Inquiry order
        Order order = (orderId);
        // 2. Use RestTemplate to initiate http requests to query users
// 2. Path
        String url = "http://userservice/user/" + ();
        // 2.2.Send http request to realize remote call
        User user = (url, User.class);
        // 3. Encapsulate user to Order
        (user);
        // 4. Return
        return order;
    }
    
   
}

 

III. Concepts of providers and consumers of services
-Providers and consumers

Service provider: a service that is called by other microservices in a single operation. (Providing interfaces to other microservices)
Service Consumer: a service that calls other microservices in a single operation. (calling interfaces provided by other microservices)

-Service call relationship:

Service provider: exposing interfaces for other microservices to invoke
Service Consumers: Calling interfaces provided by other microservices
Provider and consumer roles are actually relative
A service can be both a service provider and a service consumer at the same time

However, the roles of service provider and service consumer are not absolute, but relative to the business.

If Service A calls Service B, and Service B calls Service C, what is the role of Service B?

- For operations where A calls B: A is the service consumer and B is the service provider
- For operations where B calls C: B is the service consumer and C is the service provider

Thus, Service B can be both a service provider and a service consumer.