Eureka (service registration and discovery) simple entry

Eureka (service registration and discovery)

One, Eureka basic knowledge

1, what services governance?

​ The bottom layer of SpringCloud encapsulates the Eureka module developed by Netflix to implement service governance. In the traditional RPC remote call framework, it is more complicated to manage the dependencies between each service, so service governance is needed. Manage the dependencies between services and services, which can be achievedService invocation, load balancing, fault tolerance, etc.Implement service discovery and registration

2. What service registration and discovery?

​ Eureka adopts the design architecture of CS. Eureka Server serves as the server for the service registration function. It is the service registration center. Other services in the system use Eureka's client to connect to the Eureka Server and maintain a heartbeat connection. In this way, system maintainers can use Eureka Server to monitor whether each microservice in the system is operating normally.

​ In service registration and discovery, there is a registry. When the server starts, it will register the current information of its own server (for example: service address, communication address, etc.) to the registry in the form of an alias. The other party (consumer/service provider) uses the alias to obtain the actual service communication address from the registry, and then implements local RPC calls and remote RPC calls. The core design idea of the framework lies in the registry, because the registry is used to manage the dependencies between each service (Service governance concept). In any RPC remote framework, there will be a registry (used to store service address related information, such as interface address ).

Insert picture description here

3. Two components of Eureka (Eureka Server and Eureka Client)

​ a) Eureka Server (provides service registration)

​ After each microservice node is started through the configuration, it will be registered in EurekaServer, so that the service registry in EurekaServer will store all available service node information, and the information of the service node can be seen directly on the page.

​ b) Eureka Client (accessed through the registry)

​ It is a Java client used to simplify the interaction of Eureka Server. The client also has a built-in load balancer that uses a round-Robin load algorithm. After the application is started, a heartbeat will be sent to the Eureka Server (the default period is 30 seconds). If Eureka Server does not receive a node's heartbeat within multiple heartbeat cycles (90 seconds by default), Eureka Server will remove the service node from the service registry.

Second, build a stand-alone Eureka

1. Create a microservice (cloud-eureka-server7001)

2. Add pom.xml dependency

<?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>cloud2020</artifactId>
        <groupId>com.xuan.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-eureka-server7001</artifactId>


    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
        <!--这里是eureka-server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <!--公共的包依赖管理(如:公共的Entity(Bean)实体类包..)-->
        <dependency>
            <groupId>com.xuan.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web  -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
        <!--开发热部署工具包,方便开发中代码测试(修改Java代码服务器会自动发布重启)-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>

    </dependencies>
</project>

3. Create a yml file for configuration (must be configured in strict accordance with yml syntax)

server:
  port: 7001

eureka:
  instance:
    hostname: eureka7001.com #eureka服务端的实例名称
  client:
    register-with-eureka: false  #false表示不向注册中心注册自己。
    fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    service-url:
        #单机就是7001自己
      defaultZone: http://eureka7001.com:7001/eureka/

4. Start the microservice from the main method of the main startup class, enter the address to access: http://localhost:7001/

Insert picture description here

Three, create a microservice and register it in EurekaServer

1. Create a microservice (cloud-provider-payment8001)

2. Add pom.xml dependency

<?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>cloud2020</artifactId>
        <groupId>com.xuan.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-provider-payment8001</artifactId>

    <dependencies>
        <!--web-->
        <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>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>

        <!--mysql-connector-java-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--jdbc-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>com.xuan.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!--热部署工具-->
        <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>

        <!--这里是eureka-client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

</project>

3. Create a yml file for configuration (must be configured in strict accordance with yml syntax)

server:
  port: 8001


spring:
  application:
    name: cloud-payment-service
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操作类型
    driver-class-name: org.gjt.mm.mysql.Driver              # mysql驱动包
    url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: root


mybatis:
  mapperLocations: classpath:mapper/*.xml
  type-aliases-package: cn.xuan.springcloud.entities    # 所有Entity别名类所在包

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      #单机版
      defaultZone: http://eureka7001.com:7001/eureka
  instance:
    instance-id: payment8001
    #访问路径可以显示IP地址
    prefer-ip-address: true
      #Eureka客户端向服务端发送心跳的时间间隔,单位为秒(默认是30秒)
    lease-renewal-interval-in-seconds: 1
    #Eureka服务端在收到最后一次心跳后等待时间上限,单位为秒(默认是90秒),超时将剔除服务
    lease-expiration-duration-in-seconds: 2

4. Start the microservice from the main method of the main startup class, observe from the registry, and enter the address to access: http://localhost:7001/

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_45496190/article/details/107430171