Spring Boot-distributed

One, distributed

1. The basic concept of distributed

A distributed system is a system composed of a group of computer nodes that communicate through a network and coordinate work in order to complete common tasks. The purpose is to use more machines to process more data.

In a distributed system, the zookeeper+dubbo combination is commonly used in China, and Spring Boot recommends using the full-stack Spring, Spring Boot+Spring Cloud.

2. Development of application architecture

Insert picture description here
(1) Single application architecture
When the website traffic is very small, only one application is needed and all functions are deployed together to reduce deployment nodes and costs. At this time, the data access framework (ORM) used to simplify the workload of adding, deleting, modifying and checking is the key.
Insert picture description here

(2) Vertical application architecture
When the number of visits gradually increases, the acceleration caused by the addition of a single application to the machine becomes smaller and smaller. The application is divided into several unrelated applications to improve efficiency. At this time, the web framework (MVC) used to accelerate the development of front-end pages is the key.
Insert picture description here

(3) Distributed service architecture
When there are more and more vertical applications, interaction between applications is inevitable. The core business is extracted as an independent service, and a stable service center is gradually formed, so that front-end applications can respond more quickly to changes. Market demand. At this time, the Distributed Service Framework (RPC) for improving business reuse and integration is the key.
Insert picture description here

(4) Mobile computing architecture
When there are more and more services, capacity evaluation, waste of small service resources and other problems gradually appear, at this time, a dispatch center needs to be added to manage cluster capacity in real time based on access pressure to improve cluster utilization. At this time, the resource scheduling and governance center (SOA) used to improve machine utilization is the key.
Insert picture description here

二、Dubbo——Zookeeper

1. ZooKeeper (Registration Center)

ZooKeeper is a distributed, open source distributed application coordination service. It is a software that provides consistent services for distributed applications. The functions provided include: configuration maintenance, domain name services, distributed synchronization, group services, etc.

2、Dubbo

(1) Dubbo concept
Apache Dubbo is a high-performance, lightweight open source Java RPC framework, which provides three core capabilities: interface-oriented remote method invocation, intelligent fault tolerance and load balancing, and automatic service registration and discovery. From the perspective of the service model, Dubbo adopts a very simple model, either the provider provides the service or the consumer consumes the service, so based on this, the service provider and service consumer can be abstracted (Consumer) Two roles.

(2) RPC:
RPC (Remote Procedure Call) refers to remote procedure call, which is a method of communication between processes. It is a technical idea, not a specification. It allows the program to call a function in another address space (usually on another machine sharing the network). Because it is not in a memory space, it cannot be called directly. The network needs to be used to express the semantics of the call and convey the called data, and call the local The syntax of the method is the same, but the network transmission is required.

Note:
RPC has two core modules: communication and serialization.

(3) The core components of Dubbo
Insert picture description here

  • Service Provider (Provider) : The service provider that exposes the service. When the service provider starts, it registers the service it provides with the registry.
  • Service consumer (Consumer) : The service consumer that calls the remote service. When the service consumer starts, he subscribes to the registry for the service he needs. The service consumer selects from the provider address list based on the soft load balancing algorithm. One provider makes the call, and if the call fails, another call is selected.
  • Registry : The registry returns a list of service provider addresses to consumers. If there are changes, the registry will push the change data to consumers based on the long connection.
  • Monitor : Service consumers and providers accumulate the number of calls and call time in memory, and send statistical data to the monitoring center every minute.

3. Test

(1) Install zookeeper as the registration center

The zookeeper installation help document in Dobbo: https://registry.hub.docker.com/_/zookeeper

1) Pull zookeeper

docker pull ezsfsfyys.mirror.aliyuncs.com/library/zookeeper

2) Run zookeeper

docker run --name ZK01 -p 2181:2181 --restart always -d e1763fd3a0e3

This image includes EXPOSE 2181 2888 3888 8080 (the zookeeper client port, follower port, election port, AdminServer port respectively). Here we don’t do clusters and elections, just expose port 2181.

(2) Write service provider

dubbo-SpringBoot official: https://github.com/apache/dubbo-spring-boot-project

1) Introduce dependencies:

 <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.3</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>

        <!-- 引入zookeeper -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>2.12.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.14</version>
            <!--排除这个slf4j-log4j12-->
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

Note:
There is a log conflict here, you need to exclude slf4j-log4j12.

2) Configure application.properties

dubbo.application.name=provider-ticket
dubbo.registry.address=zookeeper://44.92.240.49:2181
dubbo.scan.base-packages=com.glp.service

dubbo.config-center.timeout=10000

dubbo.registry.address: configure the address of the registration center
dubbo.scan.base-packages=com.glp.service: configure the scan package
dubbo.config-center.timeout: configure the connection timeout

3) Publishing service:
Insert picture description here

import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;

@Component
@Service //将服务发布出去
public class TicketServiceImpl implements TicketService {
    
    
    @Override
    public String getTicket() {
    
    
        return "《我爱我的祖国》";
    }
}

@Service: Publish the service

(3) Write service consumers

1) Introduce dependency: same as provider

2) Configure the address of the registration center

dubbo.application.name=consumer-user
dubbo.registry.address=zookeeper://46.78.236.59 

3) The
Insert picture description here
normal procedure for referencing the service is to package the interface of the service provider and import it with the pom file. Here we use a simple method to directly take the interface of the service. The path must be correct, that is, the same as the service provider;

import com.glp.service.TicketService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    
    

    @Reference
    TicketService ticketService;
    public void getTicket(){
    
    
        System.out.println("买到票:"+ticketService.getTicket());
    }
}

(4) Test

 @Autowired
    UserService userService;
    @Test
    void contextLoads() {
    
    
        userService.getTicket();
    }

I finally bought the ticket, and I kept reporting errors in the early stage, mainly because of the version problem in the pom.
Insert picture description here

The complete code of this test: dubbo——zookeeper

三、Spring Cloud

Source code of this example: SpringCloud exercise

1. Basic concepts of Spring Cloud

Spring Cloud is a distributed overall solution. Spring Cloud provides developers with tools to quickly build in distributed systems (configuration management, service discovery, fuse, routing, micro-agent, control bus, one-time token, global trivia, leader election, distributed session, cluster status) , Developers who use Spring Cloud can quickly start services or build applications, and can quickly connect with cloud platform resources.

Five common components of SpringCloud distributed development

  • Service discovery-Netflix Eureka
  • Client load balancing-Netflix Ribbon
  • Circuit Breaker-Netflix Hystrix
  • Service Gateway-Netflix Zuul
  • Distributed configuration-Spring Cloud Config

2. Registration Center

(1) Arrangement eureka

server:
  port: 8761
eureka:
  instance:
    hostname: eureka-server
  client:
    register-with-eureka: false  #不将自己注册到eureka上
    fetch-registry: false  #不从eureka上获取服务的注册信息
    service-url:
      defaultZone: http://localhost:8761/eureka/

(2)@EnableEurekaServer
Insert picture description here

(3) Visit http://localhost:8761/
Insert picture description here

3. Service provider

(1) Arrangement eureka

server:
  port: 8001
spring:
  application:
    name: provider-ticket
eureka:
  instance:
    prefer-ip-address: true  #注册服务的时候使用服务的ip地址进行注册
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

(2) Write service
Insert picture description here
(3) Run service in jar package. This service
can be packaged with multiple jar packages with different port numbers and run at the same time.
Insert picture description here

4. Service consumers

(1) Arrangement eureka

spring:
  application:
    name: consumer-user
server:
  port: 8200

eureka:
  instance:
    prefer-ip-address: true  #注册服务的时候使用服务的ip地址进行注册
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

(2) @EnableDiscoveryClient opens the service discovery function
Insert picture description here
(3) Configure RestTemplate
Insert picture description here
(4) Get service
Insert picture description here

Insert picture description here
(5) Access test
Insert picture description here

Guess you like

Origin blog.csdn.net/glpghz/article/details/112552961