1、Spring Cloud

Spring Cloud是基于Spring Boot的用于管理Spring Boot创建的各个微服务应用,Spring Cloud使用erureka server注册中心管理分布式环境下的各个spring boot微服务,各个在Spring Cloud管理下的Spring boot应用就是需要注册的client,所有需要访问配置文件的应用都作为一个erureka client注册上去。

eureka是一个高可用组件,它没有后端缓存,每一个实例注册之后需要向注册中心发送一个心跳,在默认情况下,一个eureka server也是一个client,必须要指定一个server.

2、创建一个注册中心即eureka server,

(1)创建一个maven工程EurekaServer,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.hxr.erureka.server</groupId>
    <artifactId>EurekaServer</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--maven特有用于拉一些默认基础jar包-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
        <relativePath/>
    </parent>
    <!--编码-->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!--eureka server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <!--spring boot test-->
        <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>Dalston.RC1</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>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

</project>

(2)、适用spring boot创建一个EurekaServerApplication

package com.hxr.erureka.server;

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

/**
 * @Description:eurekaserver启动类
 * @Author:忘川、彼岸
 * @Date:上午9:59 2018/11/7
 */
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
    public static void main (String [] args){

        SpringApplication.run(EurekaServerApplication.class,args);
    }
}

(3)、在resources目录下创建EurekaServer配置文件application.yml(注:同级格式需相同)

server:
    port: 8761

eureka:
    instance:
        hostname: localhost
    client:
        registerWithEureka: false
        fetchRegistry: false
        serviceUrl:
           defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

(4)、启动EurekaServerApplication,访问地址:“http://localhost:8761/” 出现如下图(No instances available标识无client注册)

spring cloud注册到docker nacos spring cloud 注册中心_ci

3、创建一个EurekaClient

(1)、创建一个maven工程EurekaClient,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.hxr.eureka.client</groupId>
    <artifactId>EurekaClient</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
    </parent>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</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>
     <!--开启健康检查依赖-->
     <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.RC1</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>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>

(2)、使用spring boot创建一个Eureka Client

package com.hxr.eureka.client;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description:eurekaClient
 * @Author:忘川、彼岸
 * @Date:上午11:18 2018/11/8
 */
@EnableEurekaClient
@SpringBootApplication
@RestController
public class EurekaClientApplication {

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

    @Value("${server.port}")
    String port;
    @RequestMapping("/")
    public String home(){
        return "eureka-client-test port is"+26;
    }
}

(3)在resources目录下创建EurekaClient配置文件application.yml

eureka:
    client:
         serviceUrl:
             defaultZone: http://localhost:8761/eureka/
server:
     port: 8762
spring:
     application:
         name: EurakaClient

(4)、启动EurekaClientApplication,访问地址:“http://localhost:8762/”

spring cloud注册到docker nacos spring cloud 注册中心_maven_02

(5)、再次访问 http://localhost:8761/,已经将EurekaClient自动注册到server中

spring cloud注册到docker nacos spring cloud 注册中心_spring cloud_03

 注:再次访问server时发现当服务注册后页面报:

 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进入了保护模式,保护模式主要用于一组客户端和Eureka Server之间存在网络分区场景下的保护。一旦进入保护模式,Eureka Server将会尝试保护其服务注册表中的信息,不再删除服务注册表中的数据(也就是不会注销任何微服务)在开发过程中,我们常常希望Eureka Server能够迅速有效地踢出已关停的节点,但是由于Eureka自我保护模式,以及心跳周期长的原因,常常会遇到Eureka Server不踢出已关停的节点的问题。解决方法如下

1)第一种:EurekaServer端application.yml配置文件中关闭自我保护,并按需配置Eureka Server清理无效节点的时间间隔

server:
    port: 8761

eureka:
    instance:
        hostname: localhost
    client:
        registerWithEureka: false
        fetchRegistry: false
        serviceUrl:
           defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
    #设为false,关闭自我保护
    enable-self-preservation: false
    #清理间隔(单位毫秒,默认是60*1000)
    eviction-interval-timer-in-ms: 4000

2)第二种:EurekaClient端application.yml配置文件中开启健康检查,并按需配置续约更新时间和到期时间

eureka:
    client:

        #开启健康检查(需要spring-boot-starter-actuator依赖)
        healthcheck:
           enabled: true
     serviceUrl: 
      defaultZone: http://localhost:8761/eureka/

    instance:
        # 续约更新时间间隔(默认30秒)
        lease-expiration-duration-in-seconds: 30
        # 续约到期时间(默认90秒)
        lease-renewal-interval-in-seconds: 10

server:
     port: 8762
spring:
     application:
      #服务与服务之间相互调用一般都是根据这个name
         name: EurakaClient

(更改Eureka更新频率将打破服务器的自我保护功能,生产环境下不建议自定义这些配置)

效果:

spring cloud注册到docker nacos spring cloud 注册中心_spring_04