Spring Cloud (two): stand-alone Eureka construction

1. Basic knowledge of Eureka

1.1 Service governance

Spring Cloud 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 and services, so service governance needs to be used to manage the dependencies between services and services, which can achieve service call, load balancing, fault tolerance, etc. Service discovery and registration.

1.2 Service registration

Eureka adopts the design architecture of CS. Eureka Server serves as the server for the service registration function. It is the service registry. Other microservices in the system use Eureka Client to connect to Eureka Server and maintain a heartbeat so that system maintainers can Eureka Server is used to monitor whether each microservice in the system is running normally.

In the service discovery and registration, there is a registration center. When the server starts, it will register its current server information, such as service address, mailing address, etc., to the registration center by alias, and the other party (consumer | service provider) (Or), use the alias to go to the registry to obtain the actual service communication address, and then implement local RPC calls and RPC remote calls.

Insert picture description here

1.3 Eureka two components

Contains two components: Eureka Server and Eureka Client.

Eureka Server: Provides service registration services; after
each microservice node is started through configuration, it will be registered in Eureka Server, so that the service registry in Eureka Server will store the information of all available service nodes, and the information of service nodes can be Intuitively seen in the interface.

Eureka Client: Access 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 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 automatically remove this node from the service registry Cull out (90 seconds).

2. Single machine Eureka construction

2.1 Build module

  • cloud-eureka-server7001
    Insert picture description here

2.2 Revised 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-demo</artifactId>
        <groupId>com.lele.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>com.lele.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <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.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <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>

2.3 application.yml

server:
  port: 7001

eureka:
  instance:
    hostname: localhost #Eureka 服务端实例名称
  client:
    register-with-eureka: false  #false表示不向注册中心注册自己
    fetch-registry: false  #false表示自己就是注册中心,职责就是未付服务实例,并不需要检索服务
    service-url:
      #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
      defaultZone: http://${
    
    eureka.instance.hostname}:${
    
    server.port}/eureka/

2.4 Startup

package com.lele.springcloud;

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

/**
 * @author: lele
 * @date: 2021/3/4 7:21
 * @description:
 */
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7001 {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(EurekaMain7001.class, args);
    }
}

2.5 Testing

  1. Start the current module;
  2. Visit localhost:7001, as shown

Insert picture description here

3. Cloud-provider-payment8001 modification

EurekaClient cloud-provider-payment8001 will be registered in EurekaServer as a service provider provider.

  • New pom file:
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
  • New in yml:
eureka:
  client:
    #表示是否将自己注册进EurekaServer默认为true
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册信息,默认为true,单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:7001/eureka
  • Main start class
package com.lele.springcloud;

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

/**
 * @author: lele
 * @date: 2021/2/28 20:52
 * @description:
 */
@SpringBootApplication
@EnableEurekaClient
public class PaymentMain8001 {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(PaymentMain8001.class,args);
    }
}

4. Cloud-consumer-order80 modification

EurekaClient cloud-consumer-order80 will be registered in EurekaServer to become a service consumer consumer.

  • New pom file:
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
  • yml new:
spring:
  application:
    name: cloud-order-service
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:7001/eureka
  • Main start:
package com.lele.springcloud;

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

/**
 * @author: lele
 * @date: 2021/3/1 21:19
 * @description:
 */
@SpringBootApplication
@EnableEurekaClient
public class OrderMain80 {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(OrderMain80.class, args);
    }
}

5. Testing

Start Eureka, provider, consumer modules:
Insert picture description here

Browser access: http://localhost/consumer/payment/get/1

Guess you like

Origin blog.csdn.net/houwanle/article/details/114295279