Springcloud-Eureka cluster actual combat

1. Introduction to Eureka

  • Eureka is a service discovery framework developed by Netflix. It is a REST-based service and is mainly used to locate middle-tier services running in the AWS domain to achieve load balancing and middle-tier service failover. SpringCloud integrates it in its sub-project spring-cloud-netflix to realize the service discovery function of SpringCloud.
  • Eureka consists of two components: Eureka Server and Eureka Client.
  • Eureka Server provides service registration services. After each node is started, it will be registered in Eureka Server. In this way, the service registry in EurekaServer will store the information of all available service nodes, and the information of service nodes can be intuitively seen in the interface .
  • Eureka Client is a java client used to simplify the interaction with Eureka Server. The client is also a built-in load balancer that uses a round-robin load algorithm.
  • After the application starts, it will send a heartbeat to Eureka Server. The default period is 30 seconds. If Eureka Server does not receive a heartbeat from a node in multiple heartbeat periods, Eureka Server will add this service node from the service registry. Remove (default 90 seconds).
  • Eureka Server completes data synchronization through replication. Eureka also provides a client-side caching mechanism. Even if all Eureka Servers are down, the client can still use the information in the cache to consume APIs for other services. In summary, Eureka ensures the high availability, flexibility and scalability of the system through mechanisms such as heartbeat checking and client caching.
    (Baidu Encyclopedia)

2. Modify the hosts file

In the C: \ Windows \ System32 \ drivers \ etc hosts file and add the following content

127.0.0.1   ybg.eureka1.com
127.0.0.1   ybg.eureka2.com

3. Create a new maven module and name it springcloud-eureka-7001

springcloud-eureka-7001 is one of the registration centers

1. Write the pom.xml file

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springcloud</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springcloud-eureka-7001</artifactId>
    <!--  导包    -->
    <dependencies>

        <!-- eureka -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>

        <!-- 热部署工具 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>

    </dependencies>
</project>

2. Write application.yml

server:
  port: 7001

# Eureka 配置
eureka:
  instance:
    hostname: ybg.eureka1.com     # Eureka 服务端的实例名称
  client:
    fetch-registry: false       # fetch-registry为false表示自己为注册中心
    register-with-eureka: false    # 表示是否向eureka注册中心注册自己
    service-url:        # 监控页面
      # 单机 defaultZone: http://${
    
    eureka.instance.hostname}:${
    
    server.port}/eureka/
      # 集群(关联) 后面有多少个就接多少个,用逗号隔开
      defaultZone: http://ybg.eureka2.com:7002/eureka/

3. Write the startup class

package com.hzxy.springcloud;

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

@SpringBootApplication
@EnableEurekaServer
public class Eureka_7001 {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(Eureka_7001.class,args);
    }
}

4. Start the test

Insert picture description here
success!

4. Create a new maven module and name it springcloud-eureka-7002

springcloud-eureka-7002 is one of the registration centers, and the two together form a cluster

1. Write the pom.xml file

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springcloud</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springcloud-eureka-7002</artifactId>

    <!--  导包    -->
    <dependencies>
        <!-- eureka -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>

        <!-- 热部署工具 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>


</project>

2. Write application.yml

server:
  port: 7002

# Eureka 配置
eureka:
  instance:
    hostname: ybg.eureka2.com     # Eureka 服务端的实例名称
  client:
    fetch-registry: false       # fetch-registry为false表示自己为注册中心
    register-with-eureka: false    # 表示是否向eureka注册中心注册自己
    service-url:        # 监控页面
      # 单机 defaultZone: http://${
    
    eureka.instance.hostname}:${
    
    server.port}/eureka/
      # 集群(关联) 后面有多少个就接多少个,用逗号隔开
      defaultZone: http://ybg.eureka1.com:7001/eureka/

3. Write the startup class

package com.hzxy.springcloud;

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

@SpringBootApplication
@EnableEurekaServer
public class Eureka_7002 {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(Eureka_7002.class,args);
    }
}

4. Start the test

Insert picture description here
success!

5. Modify the springcloud-provider-user-8001 submodule

We use the 8001 port of https://blog.csdn.net/weixin_43520670/article/details/114215777 as the basis

1. Modify pom.xml

After modification

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springcloud</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springcloud-provier-user-8001</artifactId>

    <dependencies>

        <!-- springboot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- eureka -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>


        <!-- 我们需要拿到实体类 ,所以配置api module -->
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>springcloud-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <!-- junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- 数据库驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>


        <!-- mybatis_plus -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
        </dependency>

        <!--druid-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.18</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
        </dependency>

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
        </dependency>

        <!-- jetty -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>

        <!-- 热部署工具 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>

</project>

2. Modify the configuration

After modification

server:
  port: 8001
spring:
  application:
    name: springcloud-provider-user
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=GMT%2B8
    username: root
    password: 520China


mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  global-config:
    db-config:
      logic-delete-value: 1
      logic-not-delete-value: 0

# Eureka 配置
eureka:
  client:
    service-url:
      defaultZone: http://ybg.eureka1.com:7001/eureka/,http://ybg.eureka2.com:7002/eureka/   # 发布在哪个网址的注册中心
  instance:
    instance-id: springcloud-provider-user-8001  #修改Eureka上的描述信息

# info配置
info:
  app.name: ybg-springcloud
  company.name: www.hzxy.com

3. Add annotations to the startup class

Add @EnableEurekaClient annotation to let it automatically register to Eureka

package com.hzxy.springcloud;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@MapperScan("com.hzxy.springcloud.mapper")
@EnableDiscoveryClient       //服务发现
@EnableEurekaClient      //在服务启动后自动注册到Eureka中
@SpringBootApplication
public class Provider_8001 {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(Provider_8001.class,args);
    }
}

6. Test

Start the three services separately,
Insert picture description here
then visit http://ybg.eureka1.com:7001/, as follows,
Insert picture description here
and then visit http://ybg.eureka1.com:7002/, as follows
Insert picture description here
we turn off 7002, and simulate 7002 server failure and abnormality Run
Insert picture description here
and then visit http://ybg.eureka1.com:7002/, as follows, access fails.
Insert picture description here
We are visiting http://ybg.eureka1.com:7001/, as follows, it can continue to run, but there is a red letter, indicating that there is A service is down.
Insert picture description here
So, the test is successful!

Guess you like

Origin blog.csdn.net/weixin_43520670/article/details/114241191