Distributed and Microservices

1. Distributed and Microservices

Distributed: Split one thing into multiple things, split one project into multiple projects

Microservice: It is distributed. When the project is divided into small enough, the granularity is small, so small that each project has only one function, which is called microservice.

Why split?

1. Distributed is also a means to improve concurrent processing capabilities

2. There are more and more project functions and more complex, effective management after splitting

Distributed case:


Homepage: https://www.jd.com/

Search: https://search.jd.com/Search?keyword=%E8%8B%8F%E6%B3%8A%E5%B0%94%E7%94%B5%E7%A3%81%E7%82 %89&enc=utf-8&spm=a.0.0&pvid=63a88dd4a4ae4e00908eed7147a83d8a

Registration: https://reg.jd.com/reg/person?ReturnUrl=https%3A//www.jd.com/

Login: https://passport.jd.com/new/login.aspx?ReturnUrl=https%3A%2F%2Fwww.jd.com%2F

Each product category is a separate project

Management system:

Student Module news.sstudent.1000phone.net

Office Automation http://oa.1000phone.net/oa.php

Teaching management system: http://jx.1000phone.net/teacher.php

Work order system: http://gd.1000phone.net/work.php/Work/index

[Interview question] How to split the project? What is the principle of splitting?

1. Split by level: The few dao layers used are a single project, the service is split into a project, and the controller is split into a project

  • Separation of front and rear ends

2. Split by business: mainstream split by functional modules, each functional module includes controller+service+dao

Distributed problems:

1. How to call between projects

2. How to build a cluster

3. How to monitor the operation of the service

A distributed framework is needed to solve the above problems

Distributed framework:



3. Dubbo+Zookeeper does not update

2. springcloud-Netflix

1. The microservice architecture is just a style, a style.

2. Split a completed project into multiple modules to develop separately.

3. Each module runs independently in its own container.

4. Each module needs to communicate with each other. Http, RPC, MQ.

5. There is no dependency between each module, and it is deployed separately.

6. Multiple languages ​​can be used to develop different modules.

7. Use MySQL database, Redis, ES to store data, or use multiple MySQL databases.


Divide complex and bloated monolithic applications into fine-grained divisions, and package and deploy each split service separately.

Eureka: service registration and discovery

Feign: Service communication, using http to call between services

Ribbon: Implement load balancing

Hystrix: prevent service avalanche

Config: unified configuration service management

Gateway: route management

zipkin+sleuth : link tracking

3. Eureka Registration Center

springcloud+springboot need to pay attention to the version

The role of the registration center:

1. Service registration discovery: All split projects need to be registered to the Eureka registration center after startup

2. The cluster can be assembled according to the service name

3. Monitor the operation of the service

Build a registration center

1. Create a new maven project, which is the pom type as the parent project

​ Function: Unify the version of springboot springcloud

2. Add the required jar in the parent project

<!--In the parent project, all microservices are uniformly defined springboot version of-->
    <relativePath/> <!-- lookup parent from repository -->

3. Right-click the parent project, new–>module

​ Create a new registration center subproject, which is a springboot project

4. Add the jar dependency of the server in springboot and eureka registration


    <!--Add to springboot reliance on-->
    <!--Add to eureka Registry server dependencies-->

5. Create the startup program of the registration center

package com.qf.cloudhis2204eureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer  //Indicates that the current program is a registration center: receiving registrations for other services
public class MyEurekaApplication {
    public static void main(String[] args) {

6. Create a configuration file for the registration center

  port: 8081
    hostname: localhost  # Domain Names Accessing the Registry
    register-with-eureka: false  #The current registration center is a stand-alone version and does not register with other registration centers
    fetch-registry: false   #Since it is a stand-alone version, there is no need to pull data between registry centers
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ # URL of the registry server

7. Start the service

operation result:

4. Create a new basic data microservice and register it in the registration center

4.1: Create a new jar project to encapsulate the common resources of each microservice

Entity class, tool class

4.2: Create a new basic data microservice – maven project

4, 3: Add jar dependencies

    <!--springboot project-->
    <!--Add to eureka Registry client's jar-->
    <!--Add a common that holds entity classes jar-->

4, 4: Create a startup class

package com.qf.cloudhis2204basedata;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@EnableEurekaClient   //Indicate that the current program is a client of eureka and register with the registration center
public class BaseDataApplication {
    public static void main(String[] args) {

4, 5: Writing configuration files

The role of the service name:

1. To call each other between services, call by service name

2. For each microservice with the same service name, eureka will automatically assemble the cluster according to the service name.

  port: 8082
    name: BASEDATASERVER  #The name of the service registered to the registry
      defaultZone: http://localhost:8081/eureka/ # The address of the registration center where the current service is registered

4, 6: Write controller for testing

package com.qf.cloudhis2204basedata.controller;

import com.qf.cloudhist2204common.pojo.Mechanism;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

public class MechanismController {
    public Mechanism find(){
        Mechanism mechanism=new Mechanism("1001","Union Road Campus","No. 1, Lianhe Road, Zhongshan District");
        return mechanism;

4, 7: Start the registration center first, and then start the basic data microservice for registration

Tags: Java Microservices Distribution

Posted by snpo123 on Sun, 19 Mar 2023 18:11:44 +1030