Back to Javatutorial

SpringCloudEureka

docs/Spring全家桶/SpringCloud/SpringCloudEureka.md

1.0.028.4 KB
Original Source

Eureka һԴڹϣʻ㣬ǡˡ˼Eureka Netflix ˾һԴķע뷢

Spring Cloud Eureka Netflix еԴ RibbonFeign Լ Hystrix ȣһϽ Spring Cloud Netflix ģУϺȫΪ Spring Cloud Netflix Eureka

Eureka Spring Cloud Netflix ģģ飬 Spring Cloud Netflix Eureka ĶηװҪ Spring Cloud ķע뷢ֹܡ

Spring Cloud ʹ Spring Boot ˼Ϊ Eureka ԶãԱֻҪע⣬ܽ Spring Boot ΢ɵ Eureka ϡ

Eureka

Eureka CSClient/Serverͻ/ ܹ

  • Eureka ServerEureka עģҪṩעṦܡ΢ʱὫԼķעᵽ Eureka ServerEureka Server άһ÷б洢עᵽ Eureka Server Ŀ÷ϢЩ÷ Eureka Server Ĺֱۿ
  • Eureka ClientEureka ͻˣָͨ΢ϵͳи΢Ҫں Eureka Server н΢ӦEureka Client Eureka Server ĬΪ 30 룩 Eureka Server ڶûнյij Eureka Client Eureka Server ӿ÷бƳĬ 90 룩

עָһζʱ͵ԶϢöԷ֪ԼȷӵЧԡ󲿷 CS ܹӦó򶼲ƣ˺Ϳͻ˶Էͨǿͻ˷жϿͻǷߡ

Eureka ע뷢

Eureka ʵַע뷢ֵԭͼʾ

ͼ1Eureka ԭͼ

ͼй漰 3 ɫ

  • עģRegister Serviceһ Eureka Serverṩעͷֹܡ
  • ṩߣProvider Serviceһ Eureka ClientṩԼṩķעᵽעģԹ߷֡
  • ߣConsumer Serviceһ Eureka ClientѷԴӷעĻȡбķ

Eureka ʵַע뷢ֵ£

  1. һ Eureka Server Ϊעģ
  2. ṩ Eureka Client ʱѵǰϢԷspring.application.nameķʽעᵽעģ
  3. Eureka Client ʱҲעע᣻
  4. ߻ȡһݿ÷ббаעᵽעĵķϢṩߺϢ
  5. ڻ˿÷б󣬷ͨ HTTP ϢмԶ̵÷ṩṩķ

עģEureka ServerݵĽɫʮҪǷṩߺͷ֮ṩֻнԼķעᵽעIJſܱߵãҲֻͨעĻȡ÷б󣬲ܵķ

ʾ 1

棬ͨһչʾ Eureka ʵַע뷢ֵġ

1. ̣Maven Project

ڱУ漰 Spring Boot ΢Ϊ˷Dz Maven Ķ Module ṹһ Project Module̡

һΪ spring-cloud-demo2 Maven Ȼڸ̵ pom.xml ʹ dependencyManagement Spring Cloud İ汾¡


<?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">
    <modelVersion>4.0.0</modelVersion>
    <packaging>pom</packaging>
    <modules>
        <module>micro-service-cloud-api</module>
    </modules>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>net.biancheng.c</groupId>
    <artifactId>spring-cloud-demo2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <junit.version>4.12</junit.version>
        <log4j.version>1.2.17</log4j.version>
        <lombok.version>1.16.18</lombok.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <!--ʹ dependencyManagement  Spring Cloud İ汾
            ڵ Module  Spring Cloud ʱͲİ汾Ϣ
            ֤ Spring Cloud һ-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR12</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <finalName>microservicecloud</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <delimiters>
                        <delimit>$</delimit>
                    </delimiters>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

2. ģ飨Maven Module

  1. £һΪ micro-service-cloud-api Maven Modulemicro-service-cloud-api pom.xml ¡
<?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>spring-cloud-demo2</artifactId>
        <groupId>net.biancheng.c</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>micro-service-cloud-api</artifactId>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>
</project>

עmicro-service-cloud-api ̵Ĺģ飬һЩģ鹲еݣʵࡢࡢȡģҪʹùģеʱֻҪ pom.xml 빫ģɡ

  1. micro-service-cloud-api net.biancheng.c.entity £һΪ Dept ʵ࣬¡

package net.biancheng.c.entity;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;

@NoArgsConstructor //޲ι캯
@Data // ṩgetsetequalshashCodecanEqualtoString 
@Accessors(chain = true)
public class Dept implements Serializable {
    private Integer deptNo;
    private String deptName;
    private String dbSource;
}

3. ע

  1. ´һΪ micro-service-cloud-eureka-7001 Spring Boot Module Ϊעģ pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!--̵̳ POM-->
    <parent>
        <artifactId>spring-cloud-demo2</artifactId>
        <groupId>net.biancheng.c</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>net.biancheng.c</groupId>
    <artifactId>micro-service-cloud-eureka-7001</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>micro-service-cloud-eureka-7001</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--Ϊע Eureka Server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <!--devtools  lombok Ϊģ飬ʵѡ-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
  1. micro-service-cloud-eureka-7001 ·/resouces Ŀ¼£һļ application.yml¡
server:
  port: 7001  # Module Ķ˿ں

eureka:
  instance:
    hostname: localhost #eureka˵ʵƣ

  client:
    register-with-eureka: false #falseʾעעԼ
    fetch-registry: false #falseʾԼ˾עģҵְάʵҪȥ
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #ע
  1. micro-service-cloud-eureka-7001 ʹ @EnableEurekaServer ע⿪עĹܣעᣬ¡

package net.biancheng.c;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer // Eureka server,΢ע
public class MicroServiceCloudEureka7001Application {
    public static void main(String[] args) {
        SpringApplication.run(MicroServiceCloudEureka7001Application.class, args);
    }
}

  1. micro-service-cloud-eureka-7001ʹ Eureka עҳַΪhttp://localhost:7001/ͼ

ͼ2Eureka 7001 ע

4. ṩ

  1. ´һΪ micro-service-cloud-provider-dept-8001 Spring Boot Module pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!--븸pom-->
    <parent>
        <artifactId>spring-cloud-demo2</artifactId>
        <groupId>net.biancheng.c</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <groupId>net.biancheng.c</groupId>
    <artifactId>micro-service-cloud-provider-dept-8001</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>micro-service-cloud-provider-dept-8001</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!--Spring Boot Web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--devtools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <!--Spring Boot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--빫ģ-->
        <dependency>
            <groupId>net.biancheng.c</groupId>
            <artifactId>micro-service-cloud-api</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!--junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <!--mysql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.49</version>
        </dependency>
        <!--logback ־-->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
        </dependency>
        <!-- mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.0</version>
        </dependency>
        <!-- ޸ĺЧȲ -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
            <version>1.2.8.RELEASE</version>
        </dependency>
        <!-- Eureka Client עᵽ Eureka Server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- Spring Boot ģ-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!--mybatisԶɴ-->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.4.0</version>
                <configuration>
                    <configurationFile>src/main/resources/mybatis-generator/generatorConfig.xml</configurationFile>
                    <verbose>true</verbose>
                    <!-- Ƿ񸲸ǣtrueʾ滻ɵJAVAļfalse򲻸 -->
                    <overwrite>true</overwrite>
                </configuration>
                <dependencies>
                    <!--mysql-->
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.49</version>
                    </dependency>
                    <dependency>
                        <groupId>org.mybatis.generator</groupId>
                        <artifactId>mybatis-generator-core</artifactId>
                        <version>1.4.0</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

  1. micro-service-cloud-provider-dept-8001 ·/resources Ŀ¼£ļ application.yml¡

server:
  port: 8001 #˿ں
spring:
  application:
    name: microServiceCloudProviderDept  #΢ƣⱩ©΢ƣʮҪ
################################################## JDBC ͨ  ##########################################
  datasource:
    username: root        #ݿ½û
    password: root        #ݿ½
    url: jdbc:mysql://127.0.0.1:3306/bianchengbang_jdbc       #ݿurl
    driver-class-name: com.mysql.jdbc.Driver                  #ݿ

###############################  spring.config.import=configserver:##################
#  cloud:
#    config:
#      enabled: false
###################################### MyBatis  ######################################
mybatis:
  # ָ mapper.xml λ
  mapper-locations: classpath:mybatis/mapper/*.xml
  #ɨʵλ,ڴ˴ָɨʵİ mapper.xml оͿԲдʵȫ·
  type-aliases-package: net.biancheng.c.entity
  configuration:
    #ĬϿշԲø
    map-underscore-to-camel-case: true
########################################### Spring cloud Զƺ ip ַ###############################################
eureka:
  client: #ͻעᵽ eureka б
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka  #ַ 7001ע application.yml б¶עַ 棩

  instance:
    instance-id: spring-cloud-provider-8001 #ԶϢ
    prefer-ip-address: true  #ʾ· ip ַ
########################################## spring cloud ʹ Spring Boot actuator Ϣ###################################
# Spring Boot 2.50 actuator ˴Ľڵ㣬ֻ¶ heath ڵ㣬ã*Ϊ˿еĽڵ
management:
  endpoints:
    web:
      exposure:
        include: "*"   # * yaml ļڹؼ֣Ҫ
info:
  app.name: micro-service-cloud-provider-dept
  company.name: c.biancheng.net
  build.aetifactId: @project.artifactId@
  build.version: @project.version@
  1. net.biancheng.c.mapper ´һΪ DeptMapper Ľӿڣ¡

package net.biancheng.c.mapper;
import net.biancheng.c.entity.Dept;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface DeptMapper {
    //ȡ
    Dept selectByPrimaryKey(Integer deptNo);
    //ȡеȫ
    List<Dept> GetAll();
}

  1. resources/mybatis/mapper/ Ŀ¼£һΪ DeptMapper.xml MyBatis ӳļ¡

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="net.biancheng.c.mapper.DeptMapper">
    <resultMap id="BaseResultMap" type="net.biancheng.c.entity.Dept">
        <id column="dept_no" jdbcType="INTEGER" property="deptNo"/>
        <result column="dept_name" jdbcType="VARCHAR" property="deptName"/>
        <result column="db_source" jdbcType="VARCHAR" property="dbSource"/>
    </resultMap>

    <sql id="Base_Column_List">
        dept_no
        , dept_name, db_source
    </sql>

    <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from dept
        where dept_no = #{deptNo,jdbcType=INTEGER}
    </select>
    <select id="GetAll" resultType="net.biancheng.c.entity.Dept">
        select *
        from dept;
    </select>
</mapper>

  1. net.biancheng.c.service ´һΪ DeptService Ľӿڣ¡


package net.biancheng.c.service;
import net.biancheng.c.entity.Dept;
import java.util.List;
public interface DeptService {
    Dept get(Integer deptNo);
    List<Dept> selectAll();
}

  1. net.biancheng.c.service.impl ´ DeptService ӿڵʵ DeptServiceImpl¡
package net.biancheng.c.service.impl;

import net.biancheng.c.entity.Dept;
import net.biancheng.c.mapper.DeptMapper;
import net.biancheng.c.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("deptService")
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;

    @Override
    public Dept get(Integer deptNo) {
        return deptMapper.selectByPrimaryKey(deptNo);
    }

    @Override
    public List<Dept> selectAll() {
        return deptMapper.GetAll();
    }
}

  1. net.biancheng.c.controller ´һΪ DeptController Controller ࣬¡

package net.biancheng.c.controller;

import lombok.extern.slf4j.Slf4j;
import net.biancheng.c.entity.Dept;
import net.biancheng.c.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* ṩߵĿƲ
* author:c c.biancheng.net
*/
@RestController
@Slf4j
public class DeptController {
    @Autowired
    private DeptService deptService;
  
    @Value("${server.port}")
    private String serverPort;

    @RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
    public Dept get(@PathVariable("id") int id) {
        return deptService.get(id);
    }

    @RequestMapping(value = "/dept/list", method = RequestMethod.GET)
    public List<Dept> list() {
        return deptService.selectAll();
    }
}
  1. micro-service-cloud-provider-dept-8001 ϣʹ @EnableEurekaClient ע⿪ Eureka ͻ˹ܣעᵽעģEureka Server¡


package net.biancheng.c;

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

@SpringBootApplication
@EnableEurekaClient // Spring cloud Eureka ͻˣԶעᵽ Eureka Server ע
public class MicroServiceCloudProviderDept8001Application {

    public static void main(String[] args) {
        SpringApplication.run(MicroServiceCloudProviderDept8001Application.class, args);
    }
}

  1. micro-service-cloud-eureka-7001 micro-service-cloud-provider-dept-8001ʹٴ Eureka עҳhttp://localhost:7001/ͼ

ͼ3ṩעᵽע

ͼ 3 Կ Instances currently registered with Eureka עᵽ Eureka Server ʵѡѾһϢѾзעᵽ Eureka Server ˡ

Instances currently registered with Eureka ѡаݣ

  • ApplicationMICROSERVICECLOUDPROVIDERDEPTȡֵΪ micro-service-cloud-provider-dept-8001 ļ application.yml spring.application.name ȡֵ
  • Status UP (1) - spring-cloud-provider-8001UP ʾߣ (1) ʾмȺзspring-cloud-provider-8001 micro-service-cloud-provider-dept-8001 ļ application.yml eureka.instance.instance-id ȡֵ
  1. MySQL bianchengbang_jdbc ݿִ SQL׼ݡ
DROP TABLE IF EXISTS `dept`;
CREATE TABLE `dept` (
  `dept_no` int NOT NULL AUTO_INCREMENT,
  `dept_name` varchar(255) DEFAULT NULL,
  `db_source` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`dept_no`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;


INSERT INTO `dept` VALUES ('1', '', 'bianchengbang_jdbc');
INSERT INTO `dept` VALUES ('2', '²', 'bianchengbang_jdbc');
INSERT INTO `dept` VALUES ('3', '', 'bianchengbang_jdbc');
INSERT INTO `dept` VALUES ('4', 'г', 'bianchengbang_jdbc');
INSERT INTO `dept` VALUES ('5', 'ά', 'bianchengbang_jdbc');
  1. ʹʡhttp://localhost:8001/dept/listͼ

ͼ4ṩṩݿ

Eureka Server Ⱥ

΢ܹУһϵͳʮʮɣЩȫעᵽͬһ Eureka Server Уͼпܵ Eureka Server 򲻿ظյϵͳֱ̱ӵİ취Dz Eureka Server Ⱥ

֪ Eureka ʵַע뷢ʱһ漰 3 ɫעġṩԼߣɫֹȷ˾ְʵ Eureka Уз񶼼ǷҲǷṩߣע Eureka Server Ҳ⡣

ڴעʱ application.yml 漰ã



eureka:
  client:
    register-with-eureka: false  #false ʾעעԼ
    fetch-registry: false  #falseʾԼ˾עģְάʵҪȥ

õԭ micro-service-cloud-eureka-7001 ԼǷעģעDzܽԼעᵽԼϵģעǿԽԼΪķעעԼġ

ٸӣ Eureka Server ֱΪ A BȻ A ܽԼעᵽ A ϣB ҲܽԼעᵽ B ϣ A ǿΪһԼעᵽ B ϵģͬ B ҲԽԼעᵽ A ϡ

Ϳγһ黥ע Eureka Server Ⱥṩ߷ע Eureka Server ʱEureka Server ὫתȺ֮ Eureka Server ϣʵ Eureka Server ֮ķͬ

ͨͬ߿ڼȺеһ̨ Eureka Server ϻȡṩṩķʹȺеijעķϣȻԴӼȺе Eureka Server лȡϢãᵼϵͳ̱ Eureka Server Ⱥĸ߿ԡ

ʾ 2

ʾ 1 ĻϽչһӵ 3 Eureka Server ʵļȺ

1. micro-service-cloud-eureka-7001 Ĵ̣ٴ Eureka Servermicro-service-cloud-eureka-7002 micro-service-cloud-eureka-7003ʱ 3 Eureka Server Maven 뻹öһģһġ

2. ޸ micro-service-cloud-eureka-7001micro-service-cloud-eureka-7002micro-service-cloud-eureka-7003 application.yml ã

micro-service-cloud-eureka-7001 application.yml ¡

server:
  port: 7001  #˿ں
eureka:
  instance:
    hostname: eureka7001.com #eureka˵ʵ
  client:
    register-with-eureka: false #false ʾעעԼ
    fetch-registry: false #false ʾԼ˾עģҵְάʵҪȥ
    service-url:
      #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #
      defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/ #Ⱥ ǰ Eureka Server עᵽ 7003  7003 ϣγһ黥ע Eureka Server Ⱥ

micro-service-cloud-eureka-7002 application.yml ¡


server:
  port: 7002 #˿ں

eureka:
  instance:
    hostname: eureka7002.com #Eureka Server ʵ

  client:
    register-with-eureka: false #false ʾעעԼ
    fetch-registry: false  #false ʾԼ˾עģҵְάʵҪȥ
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/ # Eureka Server עᵽ 7001  7003 

micro-service-cloud-eureka-7003 application.yml ¡


server:
  port: 7003 #˿ں

eureka:
  instance:
    hostname: eureka7003.com #Eureka Server ʵ

  client:
    register-with-eureka: false #false ʾעעԼ
    fetch-registry: false  #false ʾԼ˾עģҵְάʵҪȥ
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/ # Eureka Server עᵽ 7001  7002 

3. ڱش Eureka Server ȺҪ޸ıص host ļWindows ϵͳĵ C:/Windows/System/drivers/etc/hosts ޸ģMac ϵͳĵҪ vim/etc/hosts ޸ģ޸¡

#Spring Cloud eureka Ⱥ
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
127.0.0.1 eureka7003.com

4. ޸ micro-service-cloud-provider-dept-8001ṩߣļ application.yml eureka.client.service-url.defaultZone ȡֵעᵽ Eureka Server Ⱥϣ¡


eureka:
  client: #ͻעᵽ eureka б
    service-url:
      #defaultZone: http://eureka7001.com:7001/eureka  #ַ 7001 ע application.yml б¶עַ 棩
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/  #עᵽ Eureka Server Ⱥ

5. micro-service-cloud-eureka-7001ʹʡhttp://eureka7001.com:7001/ͼ

ͼ5Eureka Server Ⱥ -7001

ͼԿ ṩߣmicro-service-cloud-provider-dept-8001ķѾעᵽ Eureka Server 7001 DS Replicas ѡҲʾ˼Ⱥе Eureka ServerEureka Server 7002 Eureka Server 7003

6. micro-service-cloud-eureka-7002ʹʡhttp://eureka7002.com:7002/ͼ

ͼ6Eureka Server Ⱥ -7002

ͼԿ ṩߣmicro-service-cloud-provider-dept-8001ṩķѾעᵽ Eureka Server 7002 DS Replicas ѡҲʾ˼Ⱥе Eureka ServerEureka Server 7001 Eureka Server 7003

  1. micro-service-cloud-eureka-7003ʹʡhttp://eureka7003.com:7003/ͼ

ͼ7Eureka Server Ⱥ -7003

ͼԿ ṩߣmicro-service-cloud-provider-dept-8001ṩķѾעᵽ Eureka Server 7003 DS Replicas ѡҲʾ˼Ⱥе Eureka ServerEureka Server 7001 Eureka Server 7002

ԴǾ Eureka Server ȺĴʹá

Eureka ұ

ڱصԻ Eureka ijʱEureka עĺпܻͼʾĺɫ档

ͼ8Eureka ұʾ

ʵϣǴ Eureka ұƶֵġĬ£ Eureka Server һʱڣĬΪ 90 룩ûнյijṩߣEureka ClientͻὫṩṩķӷעƳ ߾Ҳ޷ӷעлȡˣ޷ø÷

ʵʵķֲʽ΢ϵͳУķEureka ClientҲпܻϣӳ١١ӵԭ򣩶޷ Eureka Server ͨѶʱ Eureka Server Ϊûн󽫽ķӷбƳȻDzġ Eureka ұƾġ

ν Eureka ұơ˼ǡš Eureka Server һʱûнյ Eureka Client ô Eureka Server ͻῪұģʽе Eureka Client עϢֱӴӷעƳһָЩ Eureka Client ṩķ񻹿Լѡ

ϣEureka ұһӦ쳣İȫʩļܹѧǣͬʱ΢񣨽ķͲķ񶼻ᱣҲäĿƳκνķͨ Eureka ұƣ Eureka Server ȺӵĽ׳ȶ

Eureka ұҲڱ׶ˡ Eureka ұƴڼ䣬ṩṩķ⣬ô߾ͺ׻ȡѾڵķֵʧܵʱǿͨͻ˵ݴ⣬ο Spring Cloud Netflix Ribbon Spring Cloud Netflix Hystrix

Ĭ£Eureka ұǿģҪرգҪļá



eureka:
server:
enable-self-preservation: false # false ر Eureka ұƣĬǿ,һ㲻޸

ʾ 3

ͨһʵ֤ Eureka ұơ

1. micro-service-cloud-eureka-7001 ļ application.yml ãر Eureka ұơ



eureka:
server:
enable-self-preservation: false # false ر Eureka ұƣĬǿ,һ㲻޸

2. Ⱥе micro-service-cloud-eureka-7002 micro-service-cloud-eureka-7002 κ޸ģǵұǿġ

3. Eureka Server ȺԼ micro-service-cloud-provider-dept-8001ʹʡhttp://eureka7001.com:7001/ͼ

ͼ9Eureka رұ

ͼ 8 Կݣ

  • DS Replicas ѡ˺ɫϢTHE SELF PRESERVATION MODE IS TURNED OFF. THIS MAY NOT PROTECT INSTANCE EXPIRY IN CASE OF NETWORK/OTHER PROBLEMS.ָϢʾ Eureka ұģʽѹرա
  • micro-service-cloud-provider-dept-8001 ṩķѾעᵽ Eureka Server С

4. ʹʡhttp://eureka7002.com:7002/ͼ

ͼ10Eureka ұ

ͼ 9 Կmicro-service-cloud-provider-dept-8001 ṩķҲѾעᵽǰ Eureka Server У DS Replicas ѡϷûκξʾ

5. ر micro-service-cloud-provider-dept-8001ȴӣٴηʡhttp://eureka7001.com:7001/ͼ

ͼ11Eureka رұ-2

ͼ 10 ǿԿݣ

  • DS Replicas ѡ˺ɫϢRENEWALS ARE LESSER THAN THE THRESHOLD. THE SELF PRESERVATION MODE IS TURNED OFF. THIS MAY NOT PROTECT INSTANCE EXPIRY IN CASE OF NETWORK/OTHER PROBLEMS.ָϢʾ Eureka ұģʽѹرգѾзƳ
  • micro-service-cloud-provider-dept-8001 ṩķѾӷбƳ

6. ٴηʡhttp://eureka7002.com:7002/ͼ

ͼ12Eureka ұЧ

ͼ 11 Կݣ

  • DS Replicas ѡ˺ɫϢEMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.ָϢ Eureka ұƴڿ״̬Ѿ
  • micro-service-cloud-provider-dept-8001 ķϢȻ Eureka Server עУδƳ

ο

https://lijunyi.xyz/docs/SpringCloud/SpringCloud.html#_2-2-x-%E5%88%86%E6%94%AF https://mp.weixin.qq.com/s/2jeovmj77O9Ux96v3A0NtA https://juejin.cn/post/6931922457741770760 https://github.com/D2C-Cai/herring http://c.biancheng.net/springcloud https://github.com/macrozheng/springcloud-learning