1.RestTemplate realizes remote call
If a system is split, for example, an e-commerce system can be split into order module, user module and commodity module, which can query data from the order table, user table and commodity table in the database respectively, then when querying the order record according to the order id, the order needs to query the user information according to the userId, so when querying the order, To query the user information together, we need to use RestTemplate for cross service query The steps are as follows:
- Register an instance of RestTemplate to the Spring container
- Remote call based on RestTemplate object
1.1 register RestTemplate
Register the RestTemplate instance in the configuration class
@Bean public RestTemplate restTemplate() { return new RestTemplate(); }
1.2 realize remote call
The remote call is realized through the getForObject method of restTemplate object. Two parameters are passed in, one is the url to be called remotely, and the other is the returned data type
private RestTemplate restTemplate; public Order queryOrderById(Long orderId) { // 1. Query order Order order = orderMapper.findById(orderId); // 2. Use RestTemplate to initiate http request and query users // 2.1.url path String url = "http://localhost:8081/user/" + order.getUserId(); // 2.2. Send http request to realize remote call User user = restTemplate.getForObject(url, User.class); // 3. Encapsulate user to Order order.setUser(user); // 4. Return return order; }
There are two different roles in this service invocation process
Service provider: a service called by other micro services in a business. (provide interfaces to other micro services)
Service consumer: Service in one business calling other micro services. (call interfaces provided by other microservices)
2.Eureka registry
2.1 Eureka two major components
- Eureka Server provides service registration
- Eureka Client is accessed through the registry
2.2 setting up Eureka server
Create a maven project of Eureka server
2.2.1 introduction of eureka dependency
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
2.2.2 write startup class
@SpringBootApplication @EnableEurekaServer public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } }
2.2.3 preparation of configuration files
Create application YML file
server: port: 10086 spring: application: name: eureka-server eureka: client: service-url: defaultZone: http://127.0.0.1:10086/eureka
Next, start the micro service, and then you can access it in the browser: http://127.0.0.1:10086
2.3 service registration
If we need to register the user service with Eureka server, we only need to introduce dependency and modify the configuration file
2.3.1 introducing dependencies
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
2.3.2 preparation of configuration files
Create application YML file, add service name and eureka address
server: port: 8081 spring: application: name: userservice eureka: client: service-url: defaultZone: http://127.0.0.1:10086/eureka
In order to simulate the scenario that a service has multiple instances, we can create another user-service2 and modify its port number to 8082. The other configurations are exactly the same as user service
2.4 service discovery
2.4.1 introducing dependencies
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
2.4.2 preparation of configuration files
server: port: 8080 spring: application: name: orderservice eureka: client: service-url: defaultZone: http://127.0.0.1:10086/eureka
2.5 service pull and load balancing
Finally, we need to pull the instance list of user service from Eureka server and realize load balancing.
Load balancing capability given by @ dbalancetemplate
@Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); }
Modify the url address of the service call
@Autowired private RestTemplate restTemplate; public Order queryOrderById(Long orderId) { // 1. Query order Order order = orderMapper.findById(orderId); // 2. Use RestTemplate to initiate http request and query users // 2.1.url path String url = "http://userservice/user/" + order.getUserId(); // 2.2. Send http request to realize remote call User user = restTemplate.getForObject(url, User.class); // 3. Encapsulate user to Order order.setUser(user); // 4. Return return order; }
3.Eureka self-protection
By default, Eureka client will send a heartbeat to Eureka server within 30 seconds to prove that it is still alive. If Eureka server does not receive the heartbeat from Eureka client within a certain time (90 seconds by default), Eureka server will eliminate Eureka client service. However, if Eureka server loses a large number of service instance heartbeats in a short time, Eureka server will turn on the self-protection mechanism, Eureka client service will not be eliminated immediately, but will be put into self-protection mode. When it sends heartbeat again, the node will return to normal
Bottom line: when a micro service is unavailable at a certain time, Eureka will not clean it immediately, but will still save the information of the micro service