provider and consumer
Service provider: A service called by other microservices in a business. (Provide interfaces to other microservices)
Service consumers: In one business, services that call other microservices. (Call the interface provided by other services)
Thinking : Service A calls service B, and service B calls service C, so what is the role of service B?
Service B is a provider with respect to service A, and service B is a consumer with respect to service C.
summary:
Service call relationship:
- Service provider: expose the interface to other microservice calls
- Service consumers: call interfaces provided by other microservices
- Provider and consumer roles are relative
Table of contents:
- remote call problem
- eureka principle
- Build Eureka Server
- service registration
- service discovery
Problems with service calls
Problems with service calls
- How do service consumers obtain the address information of service providers?
- If there are multiple service providers, how should consumers choose?
- How do consumers know the health status of service providers?
The role of eureka
How do service consumers obtain specific information about service providers?
- The service provider registers its own information with eureka when it starts
- eureka saves this information
- Consumers pull provider information from eureka according to the service name
If there are multiple service providers, how should consumers choose?
- The service consumer uses the load balancing algorithm to select a service from the service list
How do consumers know the health status of service providers?
- The service provider will send a heartbeat request to EurekaServer every 30 seconds to report the health status
- eureka will update the record service list information, and the abnormal heartbeat will be removed
- Consumers can pull the latest news
summary:
In the Eureka architecture, there are two types of microservice roles:
EurekaServer : server, registration center
- Record service information
- Heartbeat monitoring
EurekaClient : client
- Provider: service provider, such as user-service in the case
-
- Register your own information to EurekaServer
-
- Send a heartbeat to EurekaServer every 30 seconds
- consumer: service consumer, such as order-service in the case
-
- Pull the service list from EurekaServier according to the service name
-
- Based on the service list for load balancing, select a microservice to initiate a remote call
Build euraka service
- Build a registration center : build EurekaServer
- Service registration : register user-service and order-service to eureka
- Service discovery : complete service pull in order-service, and then select a service through load balancing to realize remote calling
Build Eureka Server
The steps to build the EurekaServer service are as follows:
- Create a project and introduce the dependency of spring-cscloud-starter-netflix-eureka-server
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cscloud-starter-netflix-eureka-server</artifactId>
</dependency>
- Write the startup class and add @EnableEurekaServer annotation
- Add the application.yml file and write the following configuration:
server:
port: 10086
spring:
application:
name: eurekaserver
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka/
summary:
Build Eureka Server
- Introduce eureka-server dependency
- Add @EnableEurekaServer annotation
- Configure the eureka address in application.yml
service registration
register user-service
The steps to register the user-service service to EurekaServer are as follows:
- Introduce spring-cscloud-starter-netflix-eureka-client dependency in user-service project
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cscloud-starter-netflix-eureka-client</artifactId>
</dependency>
- In the application.yml file, write the following configuration:
spring:
application:
name: userservice
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka/
summary:
1. Service registration
- Introduce eureka-client dependency
- Set eureka address in applicaiton.yml
2. Whether it is a consumer or a provider, after introducing the eureeka-client dependency and knowing the eureka address, the service registration can be completed
service discovery
Complete service pull in order-service
Service pull is to obtain the service list based on the service name, and then perform load balancing on the service list
- Modify the code of OrderService, modify the url path to access, and use the service name instead of ip and port:
String url = "http://userservice/user/" + order.getUserId();
- Add load balancing annotations to RestTemplate in the startup class OrderApplication of the order-service project :
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
OrderApplication.java
@MapperScan("cn.itcast.order.mapper")
@SpringBootApplication
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class);
}
//创建RestTemplate并注入Spring容器
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
OrderService.java
@Service
public class OrderService () {
@Autowired
private OrderMapper orderMapper;
@Autowired
private RestTemplate restTemplate;
public Order queryOrderById(Long orderId) {
//1.查询订单
Order order = orderMapper.findById(orderId);
//2.利用RestTemplate发起http请求,查询用户
//2.1.url路径
String url = "http://userService/user/" + order.getUserId();
//2.2.发送http请求,实现远程调用
User user = restTemplate.getForObject(url, User.class);
//3.封装user到Order
order.setUser(user);
//4.返回
return order;
}
}
Summarize
1. Build Eureka Server
- Introduce eureka-server dependency
- Add @EnableEurekaServer annotation
- Configure the eureka address in application.yml
2. Service Registration
- Introduce eureka-client dependency
- Configure the eureka address in application.yml
3. Service Discovery
- Introduce eureka-client dependency
- Configure the eureka address in application.yml
- Add @LoadBalanced annotation to RestTemplate
- Remote call with the service name of the service provider