Introduction to microservices

Differences between single application architecture and microservice architecture

Single application architecture (a project is packaged and deployed to a server)

Disadvantages:

  1. Code complexity is high, and a project may contain millions of code

  2. Difficult to update technology

  3. Low iteration frequency of project version

  4. Low scalability, can only be expanded vertically (upgrade hardware)

  5. Low reliability, a bug causes the whole program to crash

  6. Low performance, too large project package, long request and response time

Microservice architecture (multiple small projects are packaged and deployed to multiple servers)

Advantages:

  1. Reduced complexity of individual projects

  2. The project technology is easy to update

  3. Version iteration frequency increased

  4. Expansibility is improved and can be expanded horizontally (plus machine)

  5. Reliability is improved. If one service fails, others can continue to access it

  6. Improved performance, smaller projects, faster response

Disadvantages:

  1. Higher cost, multiple servers

  2. The complexity of the overall project has greatly increased

  3. Operation and maintenance is more difficult

Spring cloud framework

Architecture for developing microservices

  • zookeeper + dubbo is an early architecture, and many of them need to be built by themselves

  • At present, spring cloud is a mature framework with comprehensive functions and easier development

  • Main components

  • Registration Center, service registration and discovery mechanism, Eureka, Nacos, Zookeeper, etc

  • Configuration center, centralized management of service configuration files, Config, Nacos

  • API Gateway, realizing routing and permission management, Zuul, Gateway

  • Service communication, realizing Restful service call, Openfeign, Dubbo

  • Load balancing, balancing the load of each service, Ribbon

  • Fuse to improve system reliability, Hystrix, Sentinel

  • Distributed transaction, global transaction management, multiple services, Seata

Spring cloud (based on SpringBoot)

Main framework system:

  1. Neflix (Netflix, the world's largest streaming media Internet enterprise)

    Eureka,Ribbon,Hystrix,Zuul

  2. Alibaba

    Nacos,Sentinel,Seata,RocketMQ

Registration Center

What problems does the registry solve:

Because microservices communicate with each other through Ip and port, and Ip and port are written on the program code, problems will occur. First, when the Ip and port of the service change, there will be errors in calling, and then cluster deployment cannot be realized.

Registration Center Eureka

Both the service provider and the service consumer register in the registry (save the IP and port). When calling the service provider, the service consumer finds the IP and port of the other server through the registry for communication

1. The provider registers the IP and port with the registry

2. The registry and the provider send heartbeat packets at regular intervals (60s)

3. If no heartbeat is received for a period of time (90s), the registration center will reject the service

4. When consumers need to call the provider, they will search the registry, which provides a service list

5. The consumer calls the provider through the IP and port in the service list

6. The service list shall be updated at regular intervals (30s)

Example: just like consumers' usual didi taxi, they get the data of drivers and users through a third-party platform. First, the registration center sends a heartbeat package to the driver to see if the driver is still working. If it is normal, they will get feedback. If it exceeds the time, it will be feedback, It will be rejected by the platform (judged as no longer working), and when consumers need to use Didi, the platform will provide the service list of drivers to call the drivers, and finally the platform will observe the update of the service list.

Set up Eureka server

1. Inherit parent item

2. Introduce the dependency of eureka server

          <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
          </dependency>

3. Add @ EnableEurekaServer to the startup class

4. Prepare configuration file

server.port=8888
        spring.application.name=eureka-server
        eureka.instance.hostname=127.0.0.1
        # Whether to register with Eureka, false is the server itself
        eureka.client.register-with-eureka=false
        # Whether to pull the service list, false
        eureka.client.fetch-registry=false
        # Address configuration of the service
        eureka.client.serviceUrl.defaultZone=http:
//${eureka.instance.hostname}:${server.port}/eureka/

Configure Eureka client

1. Import dependency

     <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
     </dependency>

2. Add @ EnableEurekaClient to the startup class

3. Configuration file

        server.port=8002
        # Service name
        spring.application.name=order-service
        # Register with Eureka
        eureka.client.register-with-eureka=true
        # Whether to pull the service list, false
        eureka.client.fetch-registry=true
        # Address configuration of the service
        eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8888/eureka/

4. Modify the configuration of RestTemplate

@Configuration
public class RestTemplateConfig {

    @LoadBalanced
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

5. Modify the code calling the service

restTemplate.getForEntity("http://product-service/product/8", Product.class);

Eureka's automatic protection mechanism

Eureka starts the automatic protection mechanism by default to avoid a large number of services being rejected due to a large area of service dropping in a short time (85% services within 15 minutes). After the network is restored, the service cannot be called normally. The automatic protection mechanism will protect the service list and will not be rejected. It will be judged as its own responsibility, so it will be protected and will not be rejected.

It is recommended to use the self-protection mechanism in the production environment, which can be turned off during development

Cluster configuration of Eureka

Advantages: improve the availability of the registration center. One hangs up and the other can be used

Practical effect: add Eureka servers, let them register with each other, and all clients register with all registration centers

Configuration of two services:

application-server1.properties

server.port=8888
spring.application.name=eureka-server
eureka.instance.hostname=127.0.0.1
# Whether to register with Eureka. true: the server registers with other Eureka
eureka.client.register-with-eureka=true
# Whether to pull the service list, true
eureka.client.fetch-registry=true
# Address configuration of the service
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:7777/eureka/
# Self protection mechanism false off
eureka.server.enable-self-preservation=false







application-server2.properties

server.port=7777
spring.application.name=eureka-server
eureka.instance.hostname=127.0.0.1
# Register with Eureka
eureka.client.register-with-eureka=true
# Pull service list
eureka.client.fetch-registry=true
# Address configuration of the service
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8888/eureka/
# Self protection mechanism false off
eureka.server.enable-self-preservation=false

Note: create two boot configurations

Client registration: Eureka client. serviceUrl. defaultZone=http:

//127.0.0.1:8888/eureka/,http://127.0.0.1:7777/eureka/

Keywords: Java Spring Cloud Microservices

Added by srinivas6203 on Mon, 13 Dec 2021 13:31:46 +0200