Understand the use and configuration of microservices and spring cloud

Posted by ReVeR on Fri, 14 Jan 2022 12:31:20 +0100

Understanding microservices:

  • Concept: microservice is a design style of system architecture, which divides an originally independent service into multiple small services. Each service runs independently in its own process, and the services communicate through HTTP RESTful API Each small service is built around a highly coupled business in the system. Microservice is a well-designed distributed architecture scheme, and Internet companies around the world are actively trying their own microservice landing scheme. The most striking solution in the java field is the solution provided by spring cloud.

  • Architecture diagram:

  • Microservice architecture features
    3.1 single responsibility: Micro services have smaller granularity, and each service should have a unique business capability to achieve a single responsibility
    3.2 autonomy: team independence, technology independence, data independence, independent deployment and delivery
    3.3 service oriented: the service provides a unified standard interface, which is independent of language and technology
    3.4 strong isolation: service calls shall be isolated, fault-tolerant and degraded to avoid cascading problems

SpringCloud

  • Spring cloud is the most widely used microservice technology stack in China. Official website address: https://spring.io/projects/spring-cloud .

  • SpringCloud integrates various microservice functional components and realizes the automatic assembly of these components based on SpringBoot, thus providing a good out of the box experience:

  • For the version compatibility relationship between SpringCloud and SpringBoot, you can see the GitHub source code;

Summary:
  • Monomer architecture: simple and convenient, highly coupled, poor scalability, suitable for small projects. For example: student management system

  • Distributed architecture: loose coupling and good scalability, but the architecture is complex and difficult. Suitable for large Internet projects, such as Jingdong and Taobao

  • Microservice: a good distributed architecture scheme

  • Advantages: smaller splitting granularity, more independent services and lower coupling

  • Disadvantages: the architecture is very complex, making it more difficult to operate, monitor and deploy

  • Spring cloud: spring cloud is a one-stop solution for microservice architecture, integrating various excellent microservice functional components

Spring cloud: Registry - use of Eureka (closed source)

Eureka's role:


According to the above calling process, there are many problems when consumers call the service provider:
1: How can a service consumer obtain the address information of a service provider?
2: If there are multiple service providers, how should consumers choose?
3: How do consumers know the health status of service providers?

How does the Eureka registry address the above issues?

Eureka working principle:

#1: How can consumers obtain specific information about service providers?
The service provider registers its own information with eureka at startup
eureka saves this information
Consumers pull provider information from eureka according to the service name

#2: If there are multiple service providers, how should consumers choose?
Service consumers use the load balancing algorithm to select one from the service list

#3: How do consumers perceive 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 recorded service list information, and abnormal heartbeat will be eliminated
Consumers can get the latest information

Build EurekaServer

  1. Introduce the dependency of spring cloud starter Netflix Eureka server:
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
  1. Annotate the startup class:
@EnableEurekaServer:open EurekaServer function
  1. Configure the core configuration file application yml:
server:
  port: 8001    #Port number
spring:
  application:
    name: eureka-server # The application name will be used as the service id (serviceId) in Eureka
eureka:
  client:
    register-with-eureka: false   #Register yourself with Eureka
    fetch-registry: false   #Get service information from eureka
    service-url:
      defaultZone: http://localhost:8001/eureka

Registered producer to Eureka Center:

  1. Introduce the dependency of spring cloud starter Netflix Eureka client to the producer:
<!--EurekaClient package-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
  1. Go to application Add corresponding configuration to YML configuration file:
spring:
  application:
    name: user  #This name will be used in the service center
eureka:
  client:
    service-url:
      # Address of EurekaServer
      defaultZone: http://localhost:8001/eureka
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${server.port}:@project.version@
    lease-renewal-interval-in-seconds: 30 #Heartbeat cycle, the default is 30 seconds
    lease-expiration-duration-in-seconds: 90 #The maximum timeout for heartbeat failure is 90 seconds by default
    ip-address: localhost #ip address

effect:

Register consumer to Eureka Center:

  1. Introduce the dependency of spring cloud starter Netflix Eureka client to the producer:
<!--EurekaClient package-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
  1. Go to application Add corresponding configuration to YML configuration file:
spring:
  application:
    name: order  #This name will be used in the service center
eureka:
  client:
    service-url:
      # Address of EurekaServer
      defaultZone: http://localhost:8001/eureka
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${server.port}:@project.version@
    lease-renewal-interval-in-seconds: 30 #Heartbeat cycle, the default is 30 seconds
    lease-expiration-duration-in-seconds: 90 #The maximum timeout for heartbeat failure is 90 seconds by default
    ip-address: localhost #ip address

The effect is the same as that of the producer.
Consumers can also be producers, and producers can also be consumers

Remote call (code implementation)

Previous consumer call producer URL path:

//Fixed and single address
String url = "http://localhost:18081/user/" + orderInfo.getUserName();

Now the consumer calls the producer URL path:

//The first user is the user address and address of the service center
//Spring in the configuration file application:. name: order
String url = "http://user/user/" + orderInfo.getUserName();

Note: load balancing needs to be turned on
The RestTemplate method implementation can use the annotation: @ LoadBalanced / / enable load balancing
Or turn on global load balancing

Topics: Java Spring Cloud Microservices