SpringCloud-服务注册与发现

搭建服务注册中心

创建一个SpringBoot工程,命名为eureka-server,在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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.lzc</groupId>
    <artifactId>eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>eureka-server</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR1</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>

在启动类加上@EnableEurekaServer注解

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

默认设置下,服务注册中心也会将自己作为客户端尝试注册它自己,所以我们需要禁用它的客户端注册行为,application.properties配置文件如下

server.port=8080
spring.application.name=eureka-server

#由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己
eureka.client.register-with-eureka=false

#指示该客户端是否应从eureka服务器获取eureka注册表信息,
# 由于注册中心的职责是维护服务实例,并不需要检索服务,所以也设置为false
eureka.client.fetch-registry=false

完成配置后启动应用程序,在浏览器中输入http://localhost:8080/。可以看到如下页面。可以发现Instances currently registered with Eureka这里是空的,因为此时还没有应用注册上来。

注册服务提供者

前面已经完成了服务注册中心,接下来我们写一个SpringBoot应用,将其作为微服务应用注册到注册中心。

创建一个SpringBoot工程,命名为eureka-client,在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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.lzc</groupId>
    <artifactId>eureka-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>eureka-client</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR1</spring-cloud.version>
    </properties>

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

        <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-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>

在启动类加上@EnableDiscoveryClient注解

@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientApplication {

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

application.properties配置文件如下

server.port=8081
spring.application.name=eureka-client

#指定服务注册中心的地址,这样才能将我们的应用注册到服务注册中心
eureka.client.serviceUrl.defaultZone: http://localhost:8080/eureka/

接下来分别启动服务注册中心eureka-server与该应用,启动完成后在浏览器中输入http://localhost:8080/。可以看到如下页面,在Instances currently registered with Eureka下面可以发现eureka-client已经注册上来了。

扫描二维码关注公众号,回复: 3236228 查看本文章

高可用注册中心

Eureka Server高可用就是将自己作为服务向其他服务注册中心注册自己,这样就可以形成一组服务注册中心的集群。我们在服务注册中心的基础上新建两个项目,命名为eureka-server1和eureka-server2,配置文件如下:

eureka-server1:

server.port=8080
spring.application.name=eureka-server
eureka.client.serviceUrl.defaultZone: http://localhost:8090/eureka/

#注册中心关闭自我保护机制
eureka.server.enable-self-preservation=false

eureka-server2:

server.port=8090
spring.application.name=eureka-server
eureka.client.serviceUrl.defaultZone: http://localhost:8080/eureka/

#注册中心关闭自我保护机制
eureka.server.enable-self-preservation=false

启动这两个应用,在浏览器输入http://localhost:8080/http://localhost:8090/会看到如下页面

在设置了多个节点的服务注册中心后,服务提供方还需要做一些配置才能将服务注册到Eureka Server集群中,修改配置文件即可

server.port=8081
spring.application.name=eureka-client

#指定服务注册中心的地址,这样才能将我们的应用注册到服务注册中心
eureka.client.serviceUrl.defaultZone: http://localhost:8080/eureka/,http://localhost:8090/eureka/

启动服务,在浏览器输入http://localhost:8080/http://localhost:8090/可以看到eureka-client服务注册到了eureka-server1和eureka-server2

Spring Cloud Eureka的自我保护机制

自我保护模式正是一种针对网络异常波动的安全保护措施,使用自我保护模式能使Eureka集群更加的健壮、稳定的运行。

自我保护机制的工作机制是如果在15分钟内超过85%的客户端节点都没有正常的心跳,那么Eureka就认为客户端与注册中心出现了网络故障,Eureka Server自动进入自我保护机制,此时会出现以下几种情况:

1、Eureka Server不再从注册列表中移除因为长时间没收到心跳而应该过期的服务。

2、Eureka Server仍然能够接受新服务的注册和查询请求,但是不会被同步到其它节点上,保证当前节点依然可用。

3、当网络稳定时,当前Eureka Server新的注册信息会被同步到其它节点中。

Eureka自我保护机制,通过配置 eureka.server.enable-self-preservation为true打开/false禁用自我保护机制,默认打开状态,建议生产环境打开此配置。开发环境中如果要实现服务失效能自动移除则关闭。

代码地址:https://github.com/923226145/SpringCloud-learning

猜你喜欢

转载自blog.csdn.net/lizc_lizc/article/details/82284350