转载

Spring Cloud 服务注册与发现项目创建(Greenwich SR3版)

创建一个Maven 项目spring-cloud,作为父项目,删除src 目录。

Spring Cloud 服务注册与发现项目创建(Greenwich SR3版)

1.2 创建子模块eureka-server 和eureka-client

创建两个子模块项目,Spring Initializr Module,服务注册与服务发现。

服务注册中心eureka-server,选中Eureka Server

Spring Cloud 服务注册与发现项目创建(Greenwich SR3版)

服务发现eureka-client,选中Eureka Discovery client

Spring Cloud 服务注册与发现项目创建(Greenwich SR3版)

1.3 创建后的项目结构图

Spring Cloud 服务注册与发现项目创建(Greenwich SR3版)

2. 配置服务注册中心eureka-server

2.1 EurekaServerApplication.java

启动类添加注解@EnableEurekaServer 声明为服务注册中心。

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

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

}
复制代码

2.2 application.yml

  • eureka.instance.hostname      主机IP,访问实例时需要通过主机IP+端口号访问,主机名+端口号访问不到
  • fetch-registry     检索服务选项,当设置为True(默认值)时,会进行服务检索,注册中心不负责检索服务。
  • register-with-eureka     服务注册中心也会将自己作为客户端来尝试注册自己,为true(默认)时自动生效
  • eureka.client.serviceUrl.defaultZone     是一个默认的注册中心地址。配置该选项后,可以在服务中心进行注册。

为什么不建议使用default-zone 替代defaultZone?     service-url 的Value 支持Map 键值对;     zone 属性会被Eureka Client 解析当成客户端默认当成zone 处理;     default-zone 被解析时会被认为客户端没有有效的配置service-url;     defaultZone 会正常处理为默认的service-url 为http://192.168.15.1:8761/eureka/     192.168.15.1 是我这里本机ipv4 地址,请忽略

server:
  port: 8761
eureka:
  instance:
    hostname: 192.168.15.1
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
复制代码

2.3 pom.xml

添加依赖spring-cloud-starter-netflix-eureka-server

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>pers.niaonao</groupId>
    <artifactId>eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-server</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR3</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
复制代码

3 配置服务发现eureka-client

3.1 EurekaClientApplication.java

启动类添加注解@EnableEurekaClient 声明为服务发现。

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

@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {

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

}
复制代码

3.2 application.yml

此处192.168.15.1 是本机ipv4。     配置的application name,当存在多个服务时,相互间的服务调用需要通过该名称。     配置serviceUrl,指定服务注册中心地址。

server:
  port: 8080
spring:
  application:
    name: eureka-client
eureka:
  client:
    serviceUrl:
      defaultZone: http://192.168.15.1:8761/eureka/
复制代码

3.3 pom.xml

添加依赖spring-cloud-starter-netflix-eureka-client 和spring-boot-starter-web     spring-boot-starter-web 防止出现启动服务时会报:Completed shut down of DiscoveryClient 问题。

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>pers.niaonao</groupId>
    <artifactId>eureka-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-client</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR3</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
复制代码

4. 运行服务

启动eureka-server,再启动eureka-client 服务。访问http://192.168.15.1:8761 或 http://localhost:8761/ 可以看到DS Replicas 下已注册成功的客户端实例。

Spring Cloud 服务注册与发现项目创建(Greenwich SR3版)

5. 遇见问题

5.1 Completed shut down of DiscoveryClient

Spring Cloud 服务注册与发现项目创建(Greenwich SR3版)

启动eureka-client 报Completed shut down of DiscoveryClient,可以通过在eureka-client 添加pring-boot-starter-web 依赖后重启服务处理。另外没有web 依赖,怎么进行后续的服务开发。

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
复制代码

5.2 红色提示信息EMERGENCY!

Spring Cloud 服务注册与发现项目创建(Greenwich SR3版)

Eurake有一个配置参数eureka.server.renewalPercentThreshold,定义了renews 和renews threshold的比值,默认值为0.85。当server在15分钟内,比值低于percent,即少了15%的微服务心跳,server会进入自我保护状态,Self-Preservation。在此状态下,server不会删除注册信息,这就有可能导致在调用微服务时,实际上服务并不存在。     这种保护状态实际上是考虑了client和server之间的心跳是因为网络问题,而非服务本身问题。     建议:

  • 在生产上可以开自注册,部署两个server
  • 在本机器上测试的时候,可以把比值调低,比如0.49
  • 关闭自我保护模式:eureka.server.enableSelfPreservation=false

THE END

原文  https://juejin.im/post/5da7e01ff265da5bbe2a4110
正文到此结束
Loading...