Spring cloud series: 5 Stand alone Eureka registry + cluster Eureka registry

preface

  1. Detailed explanation of Eureka
    1.1 detailed explanation of Eureka 1
    1.2 detailed explanation of Eureka 2
  2. Code demo - stand alone Eureka registry ring
    2.1 code Demonstration - Eureka service registration
    2.2 Eureka's self-protection mechanism (service discovery mechanism for team cooperation))
  3. Eureka cluster environment configuration
  4. Eureka cap principle and comparison Zookeeper

preface

This article mainly explains the detailed explanation of Eureka registry and its code deployment demonstration.

1. Detailed explanation of Eureka

1.1 detailed explanation of Eureka 1

  1. Netlix followed the AP principle when designing Eureka
  2. Eureka is a sub module of Netflix and one of the core modules.
  3. Eureka is a REST based service used to locate services to implement
    Cloud middle tier service discovery and failover, service registration and discovery are very important for microservices. With service discovery and registration, you only need to
    To use the service identifier, you can access the service without modifying the service call configuration file. The function is similar to Dubbo's registry, such as Zookeeper;

1.2 detailed explanation of Eureka 2

  1. Service registration and discovery
    (1) for service registration and discovery, many people may use Zookeeper before. For Zookeeper, it has a client. Therefore, when using ZK, we need to open a client to link it.

  2. Now we use "Eureka" for Service registration and discovery, which is based on CS architecture (including server and client). Eureka's server is equivalent to writing a sub project module of Service business as the server. If we use the original zookeeper server, we need to download the zookeeper server. This is the difference between Eureka and zookeeper.
    (3) Euraka and Zookeeper are almost similar, with little difference. Both implement service registration
    of

  1. In "consumers and providers" of remote procedure service call - "figure - Eureka registry-1".
    (1) compared with the previous architecture implementation of "Dubbo+Zookeeper", we use Zookeeper as the registry, and RPC in Dubbo is used for communication between consumers and providers.
    (2) in Eureka, the registration center is Eureka, and the remote communication between consumers and providers is implemented based on Rest (the Java back end provides it to external or third-party calls by writing the Api of the corresponding Controller interface).

  2. For how to use Eureka, Eureka's server is an Eureka server. We need to manually write an Eureka implementation class as the server. In the previous process of using zookeeper, we downloaded a 'zookeeper' and started zk as the server.
    ----------->This is the difference between Eureka and Zookeeepr.

  3. In Eureka, we need both the "client and server" to connect to the Eureka Server registry. After connecting, a heartbeat will be generated (Eureka, for example, needs the "client and server" to send a message to Eureka every 5 seconds. If it doesn't send it, it will be considered that you are dead). That is to connect and keep the heartbeat.
    (1) in Dubbo+Zookeeper, we used Dubbo admin to monitor. Here, Eureka uses Eureka Server as the registration center, and he can monitor it himself.

  1. Eureka's three roles.
    (1) Eureka Server provides service registration and discovery. Previously, in the implementation of service registration and discovery using "Dubbo+Zooker", you usually double-click zkserver CMD let it be the client (the client that starts Zookeeper). Therefore, the function of this "Eureka Server" is similar to that of zkserver CMD is similar.
    (2) Service Provider: Service Provider: register its own service in Eureka and find it for consumers.
    (3) Service Consumer: obtain services from Eureka.

Figure - Eureka registry-1

2. Code Demonstration - setting up of stand-alone Eureka registry environment

1. Here“ Eureka "Setting up the registry environment" is in the previous chapter"SpringCloud Series: 4. complete Rest Build service providers and consumers"Built on the basis of.

1.1 Next, start the demonstration"Eureka Registration center environment construction",The steps are as follows↓: 
⑴ Create a Eureka client(springcloud-eureka-7001)
--->Step 1: create a sub project pom.xm File, import dependency
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka-server</artifactId>
        <version>1.4.6.RELEASE</version>
    </dependency>
    <!--Hot deployment tool:Code modification automatically updates the code without restarting the project-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>
</dependencies>

⑵ Step 2: in/resources Under the bag, yes application.yml Configuration file
 relevant Eureka Configuration of.
server:
  port: 7001

eureka:
  instance:
    # Declare the local ip address of Eureka registry, which can not be configured
    hostname: localhost
    client:
      # Indicates whether to register yourself with Eureka Registration Center (if the server is itself, you don't need to register yourself. false)
      register-with-eureka: false
      # Indicates that if fetch registry is false, it indicates that it is the registry
      fetch-registry: false
      #The following address indicates the IP address of the Eureka registry. If you create multiple addresses		
      # Eureka registry, where the IP addresses of multiple Eureka registries can be configured
      # The tail "... / eureka" must be configured 
      service-URL:
        defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/


⑶ Step 3: create the main startup class and add a startup class Eureka Notes on the server
 solution"@EnableEurekaServer",Add on startup class@EnableEurekaServer notes
 This annotation indicates that a service registry is started and provided to other applications for dialogue.

/**
 *  -->Create Eureka's main startup class
 *  -->Open Eureka service and add a comment to @ enable Eureka server
 */
@SpringBootApplication
@EnableEurekaServer // The startup class of EnableEurekaServer server can accept others to register,
public class EurekaServer_7001 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServer_7001.class,args);
    }
}

⑷ Step 4: start IDEA This subproject, test access-->http://localhost:7001/
--->Will enter a Eureka One of the registry Eureka A default page for the registry.
- As shown in-"chart eureka-1". 

⑸ Step 5: next, we just put the previous 8001"Service provider"Just sign up.
------>there"Service provider 8001"Is a subitem created in the previous article
 order"springcloud-provider-dept-8001",You can look at the last article first
 In the construction process of this chapter, first'Rest Service providers and consumers have built'. 
------>The reference article is named:"SpringCloud Series: 4. complete Rest Build service providers and consumers". 


----------------------------------------------------------


③ At this time, the sub project module we created belongs to Eureka For the service registry, we need to use the previously created(Service provider):"springcloud-provider-dept-8001"Provide services to this"springcloud-eureka-7001"Go to the registration center.

Figure eureka-1

2.1 code Demonstration - Eureka service registration

① In the last article"SpringCloud Series: 4. complete Rest Build service providers and consumers"Based on the code, use the sub projects created inside"springcloud-provider-dept-8001"This service provider provides services to sub modules"springcloud-eureka-7001"this Eureka In the registry.

② stay"springcloud-provider-dept-8001"The operations in are as follows↓
⑴ First step"springcloud-provider-dept-8001"Import in Eureka Dependence.
<!--join EUREKA Dependency, this sub module is a service provider, so import this Eureka Dependence of-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
    <version>1.4.6.RELEASE</version>
</dependency>

⑵ Step 2: in"springcloud-provider-dept-8001"Register the service
 reach Eureka Go to the registry, go to application.yml to configure Eureka Configuration of.

# Where is Eureka's configuration service registered?
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka/
# Modify the default description of accessing Eureka service in localhost: 7001      
  instance:
    instance-id: springcloud-provider-dept8001 
    # In Eureka, you can display or hide the ip address of a service registered in Eureka. true means to display the ip address
    prefer-ip-address: true
    
⑶ Step 3: in"springcloud-provider-dept-8001"Startup class on in Eureka annotation
- Add a comment on the startup class:@EnableEurekaClient

/**
 * 1. @EnableEurekaClient Annotation representation
 *      -->Start Eureka service in "springcloud-provider-dept-8001"
 *      -->And automatically register the services in this sub project to the 7001 Eureka registry.
 * 1.1 Therefore, we only need to add a @ EnableEurekaClient to the startup class
 * It will automatically register with Eureka after the service is started.
 *
 */
@SpringBootApplication
@EnableEurekaClient
public class DeptProvider_8001 {
    public static void main(String[] args) {
        SpringApplication.run(DeptProvider_8001.class,args);
    }
}


⑷ Step 4: test access http://localhost:7001/
- Display the page as shown in the figure-"chart eureka-2"
- This will"springcloud-provider-dept-8001"Services in register to
"springcloud-eureka-7001"this Eureka In the registry.


⑸ Step 5: in"Service provider"(springcloud-provider-dept-8001)In,
Add monitoring information.
- pom.xml Import dependency in
<!--Add monitoring information
actuator Improve monitoring information
-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

- stay"Service provider"(springcloud-provider-dept-8001)Configure the information of the jump page in
# Basic information configuration of info monitoring
info:
  # Project name
  app.name: aaa-springcloud
  # Company name
  company.name: blog.aaa.com

- Test access, as shown in the figure-->
"chart eureka-3": Displays the page.
"chart eureka-4": After the jump, the information we configured is reflected.
"chart eureka-5": Indicates the corresponding of the display service IP Get the address.



Figure eureka-2

Figure eureka-3

Figure eureka-4

Figure eureka-5

2.2 Eureka's self-protection mechanism (service discovery mechanism for team cooperation)

① As shown in the figure-"chart eureka-2"The paragraph above in red is Eureka The embodiment of self-protection mechanism.---> Self protection mechanism-"chart pro-1". 

② stay"Service provider"(springcloud-provider-dept-8001)Medium configuration
controller Interface to display their own configuration information(This configuration team uses more for development)
--->The specific configuration code is as follows:↓: 
▲. Teamwork through us in controller Configured"Service discovery"Mechanism, can get
 Get specific information.

⑴ Step 1: Configure controller Class interface request
package com.lzk.springcloud.springcloud.controller;

/**
 * Created by Linzk on 2020/12
 * <p>
 * -->This Controller class is used to provide restful services
 */

@RestController
public class DeptController {

    @Autowired
    private DiscoveryClient client;

    /**
     * Register the micro service to get some messages related to Eureka!
     * --->This is generally used in team development. You tell others what your configuration information is.
     * --->That is: show your information!!
     *
     *
     */
    @GetMapping("/dept/discovery")
    public Object discovery() {
        //Get the list of micro services
        List<String> services = client.getServices();
        System.out.println("discovery==>services" + services);
        //Get a specific microservice information Get the information through the specific microservice ID -- > ApplicationName
        List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-DEPT");
        for (ServiceInstance instance : instances) {
            System.out.println(
                    instance.getHost() + "\t"+
                    instance.getPort() + "\t"+
                    instance.getUri() + "\t"+
                    instance.getServiceId() + "\t"
            );
        }
        return this.client;
    }
}


⑵ Step 2: start the class and add the annotation of service discovery@EnableDiscoveryClient
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient //Service discovery
public class DeptProvider_8001 {
    public static void main(String[] args) {
        SpringApplication.run(DeptProvider_8001.class,args);
    }
}

⑶ Step 3: test access http://localhost:8001/dept/discovery
- As shown in the figure-"chart info-1"







Figure pro-1
Figure info-1

3. Eureka cluster environment configuration

① Next, the basis of the code is"Above item"Continue to develop on the basis of.
⑴ The above code uses a single Eureka As a registry--->
Next: build clusters,Create multiple Eureka The subproject acts as the registry.

② Create multiple Eureka The cluster environment configuration steps are as follows↓;

⑴ Step 1: create two sub project modules"springcloud-eureka-7002"and
"springcloud-eureka-7003"And previously created"springcloud-eureka-7001"
Three in total Eureka Registration center.
-1 Import the created 7002 and 7003 projects into the dependency
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka-server</artifactId>
        <version>1.4.6.RELEASE</version>
    </dependency>
    <!--Hot deployment tool:Code modification automatically updates the code without restarting the project-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>
</dependencies>

-2 Separate the previously created"springcloud-eureka-7001"Inside
application.yml Copy configuration files to 7002 and 7003 resources Pack and repair
 Change the corresponding port number.

server:
  port: 700X

eureka:
  instance:
    # statement Eureka Server instance name(#Local ip address (optional).
    hostname: localhost
  client:
    # Indicates whether to register yourself with Eureka Registration Center (if the server is itself, you don't need to register yourself. false)
    register-with-eureka: false
    # Indicates that if fetch registry is false, it indicates that it is the registry
    fetch-registry: false
    # Eureka needs to have its own connection address, rewrite and configure it to its own address.
    # Configure the following address as Eureka's monitoring address page
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

⑵ Step 2: create corresponding startup classes under 7002 and 7003 subproject modules respectively

@SpringBootApplication
@EnableEurekaServer // The startup class of EnableEurekaServer server can accept others to register,
public class EurekaServer_700X {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServer_700X.class,args);
    }
}

⑶ Step 3: test.
- Start one of 7002 or 7003 startup classes(Here's a demonstration of 7003)
- http://localhost:7003/
- Visit the page as shown in the figure-"chart eureka7003"


--------------------------------------------------------



⑷ Step 4: we now build three Eureka The registry is to prevent when one of them
 One registration center suddenly hangs up, and other registration centers can still be used, so you need to add these three registration centers to the list
 Hearts are bound to each other to realize the dependency of the three clusters.
------>As shown in the figure-"chart connect". 

----> To make three Eureka The three registration centers need to go to the computer to form a dependency relationship
 Make a domain name mapping.
- Address: C:/Windows/System32/drivers/etc/hosts
- modify hosts The information in the file, realize the mapping relationship, and add three mappings, regardless of access
"eureka7001/7002/7003"Either of them can be mapped and accessed to"127.0.0.1"
- As shown in the figure- "chart eureka127". 



⑸ Step 5: modify"sprinfcloud-eureka-7001",
------>"sprinfcloud-eureka-7001"of application.yml
 The server name of the configuration file corresponds to the above mapping address.
-1 7001application.yml configuration file
server:
  port: 7001

eureka:
  instance:
    # Declare the instance name of Eureka server
    hostname: eureka7001.com
  client:
    # Indicates whether to register yourself with Eureka Registration Center (if the server is itself, you don't need to register yourself. false)
    register-with-eureka: false
    # Indicates that if fetch registry is false, it indicates that it is the registry
    fetch-registry: false
    # Eureka needs to have its own connection address, rewrite and configure it to its own address.
    # Configure the following address as Eureka's monitoring address page

    service-url:
      # The configuration of a single Eureka on a single machine is: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      # The configuration of cluster (associated) Eureka is: I need to mount 7002 and 7003 in 7001. Defaultzone: http: / / ${Eureka. Instance. Hostname}: ${server. Port} / Eureka/
      defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/

-2 7002application.yml configuration file
server:
  port: 7002

eureka:
  instance:
    # Declare the instance name of Eureka server
    hostname: eureka7002.com
  client:
    # Indicates whether to register yourself with Eureka Registration Center (if the server is itself, you don't need to register yourself. false)
    register-with-eureka: false
    # Indicates that if fetch registry is false, it indicates that it is the registry
    fetch-registry: false
    # Eureka needs to have its own connection address, rewrite and configure it to its own address.
    # Configure the following address as Eureka's monitoring address page
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/


-3 7003application.yml configuration file
server:
  port: 7003

eureka:
  instance:
    # Declare the instance name of Eureka server
    hostname: eureka7003.com
  client:
    # Indicates whether to register yourself with Eureka Registration Center (if the server is itself, you don't need to register yourself. false)
    register-with-eureka: false
    # Indicates that if fetch registry is false, it indicates that it is the registry
    fetch-registry: false
    # Eureka needs to have its own connection address, rewrite and configure it to its own address.
    # Configure the following address as Eureka's monitoring address page
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/



⑹ Step 6: there are three Eureka Registration Center, you need to integrate these three registration centers
7001,7002,,7003,Publish and switch to sub project module(Service provider)
"springcloud-provider-dept-8001"Inside application.yml to configure
 The document announced the release of the three registries.
server:
  port: 8001

# mybatis configuration
mybatis:
  # Configure Mybatis to read entity classpath configuration
  type-aliases-package: com.xxx.springcloud.pojo
  # Path of mybatis core configuration file
  config-location: classpath:mybatis/mybatis-config.xml
  # Path of mapper configuration of mybatis
  mapper-locations: classpath:mybatis/mapper/*.xml


# Spring configuration
spring:
  application:
    # The spring service provides the name of the project
    name: springcloud-provider-dept
  # spring data source
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: org.gjt.mm.mysql.Driver
    url: jdbc:mysql://127.0.0.1:3306/db01?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
    username: root
    password: root123

# Where is Eureka's configuration service registered?
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  instance:
    instance-id: springcloud-provider-dept8001 # Modify the default description of accessing Eureka service in localhost: 7001

# Basic information configuration of info monitoring
info:
  # Project name
  app.name: aaa-springcloud
  # Company name
  company.name: blog.aaa.com


⑺ Step 7: start 70017002 respectively,7003,Three sub project module clusters
- Start three clusters(7001,7002,7003)  
- Start a service provider(springcloud-provider-dept-8001)

⑻ Step 8: test first visit localhost:7002,You can see that the 7002 association is mounted
7001 And 7003 Eureka The registry, and there is a registered service below
--->As shown in the figure-"chart DSR1"
- Therefore, if the node 7001 crashes at this time, the other two nodes 7002 and 7003 are the same
- It can be used as a registration center, so as not to collapse all at once. Because one collapsed, we
- Just switch the port interface. This is it. Eureka Benefits of cluster configuration.


Figure connect
Figure Eureka 7003

Figure eureka127
Figure DSR1

4. Eureka cap principle and comparison Zookeeper

1.above Eureka About consumers"springcloud-eureka-consumer"Get service
 Go directly to the registration center to get it! Or use the original address to access.

---------------------------------------------------------

2 CAP Explain in detail-"chart cap1","chart cap2"
⑴ Eureka and Zookeeper Can be used as a registry, and the difference between the two is compared
⑵ Difference 1:"chart cap3"and"chart cap4"

---------------------------------------------------------

3. Usually in the interview will be"Question and answer"Ask in the form of: Zookeeper And Eureka What is the difference or difference between the two registries?
- Can answer"chart cap4"Last sentence:
Eureka It can well deal with the loss of contact of some nodes due to network failure zookeeper That paralyzes the entire registration service.


⑴ zookeeper And Dubbo What's the difference?
- Dubbo It's a RPC Framework to realize the communication of different nodes in the distributed system. Solve communication
 Level issues.
- zookeeper: Provide service registration, so that nodes can find objects with each other.

⑵ zookeeper And Eureka What's the difference?
- Eureka It can well deal with the loss of contact of some nodes due to network failure,
Not like zookeeper That paralyzes the entire registration service. Eureka As simple
 For the service registry zookeeper More "professional" because the registration service is more important
 What we want is availability. We can accept that consistency cannot be achieved in the short term.


⑶ Springcloud And Dubbo Differences between-"chart DS Comparative Example"?
a. To sum up, the use of these two frameworks is metaphorical:
use Dubbo Building a microservice architecture is like assembling a computer. We are free to choose in all links
 The degree is very high, but the final result is likely to be dim due to the poor quality of one memory,
It always makes people uneasy, but if you are a master, these are not problems;
and Spring Cloud Like a brand machine, in Spring Source Under the integration of
 The compatibility test ensures that the machine has higher stability, but if it is to be used
 For things other than original components, you need to have a sufficient understanding of its foundation.


b. Dubbo and Spring Cloud It's not a complete competitive relationship,
The two solve different problem domains: Dubbo Our positioning is always one RPC Frame,
and Spring Cloud The purpose of is a one-stop solution under the microservice architecture.

c. If you have to compare, Dubbo It can be analogized to Netflix OSS Technology stack,
and Spring Cloud Integrated Netflix OSS As a distributed service governance solution,
But beyond that Spring Cloud Also provided are config,stream,security,
sleuth Equal distribution service solution. Current due to RPC Protocol, registry metadata
 When facing the selection of micro service infrastructure framework Dubbo And Spring Cloud only
 You can choose one from the other, which is why the two are always compared.
Dubbo After that, we will actively seek to adapt to Spring Cloud Ecology, such as as SpringCloud
 Binary communication scheme to play Dubbo Performance advantages, or Dubbo Through modularization and
 yes http Support adaptation to Spring Cloud

⑷ http And RPC What's the difference?
a. Whether microservices or distributed services(All SOA,They are all service-oriented programming),Are faced with remote calls between services.

b. So what are the remote invocation methods between services?
Common remote calling methods are as follows: RPC: Remote Produce Call remote procedure call + Http: http In fact, it is a network transmission protocol based on TCP,Data transmission is specified
 Input format.

-----------------------------------------------------

- Summary: comparison RPC and http Differences between
1 RPC Both service providers and service callers are required to use the same technology, or both hessian,Either all dubbo and http There is no need to pay attention to the implementation of the language, just follow the rest gauge
 Fan.
2 RPC There are many development requirements, such as Hessian The framework also requires the server to provide a complete interface generation
 code(Package name.Class name.Method names must be exactly the same),Otherwise, the client cannot run.
3 Hessian Only support POST Request.
4 Hessian Only support JAVA Language.



Figure cap1

Fig. 2

Fig. 3

Fig. 4

Figure DS comparison example

Keywords: Dubbo Zookeeper Spring Cloud rpc http

Added by abhi_10_20 on Sat, 15 Jan 2022 08:19:30 +0200