Spring Cloud的Eureka注册中心集群搭建

Spring Cloud也包含了许多的子项目 , 下面等下要使用的Eureka只是其中的一个子项目 java

Eureka的功能有点相似于dubbo和zookeeper,它是一个服务治理组件,包含了服务注册中心、服务注册与发现机制。web

Eureka的使用很是简单 spring

咱们首先在idea里建立一个maven项目 , 等下方便咱们进行操做app

服务治理组件  咱们须要弄服务注册中心、服务注册(提供者)与发现机制(消费者)三个Spring Boot项目,我把他们都放在maven里进行管理 ,具体步骤以下:maven

先在maven项目上在去新建一个module ,做为咱们的服务注册中心(eureka)ide

新建的module咱们选择Spring Boot项目 , 勾选以下依赖spring-boot

 

具体依赖以下fetch

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.liy</groupId>
    <artifactId>eureka</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.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-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>

这个服务中心 ,咱们只须要在配置文件中配置一下 , 而后在启动类里使用一个注解 ,而后启动项目便可ui

application.propertiesurl

#端口号
server.port=1111
spring.application.name=eureka
#是否向注册中心(eureka)注册 ,没必要向本身注册 ,除非是搭建一个注册中心的集群,那么这里就要为true了
eureka.client.register-with-eureka=false
#是否容许它去获取其余的注册的服务,由于他是注册中心,因此不须要去获取的
eureka.client.fetch-registry=false

启动类里添加个@EnableEurekaServer

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {

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

}

启动项目看到以下页面 , 即注册中心ok了(下面截图只截取了头部)

 

若是是要搭建这个注册中心的集群的话 ,也是至关简单的 , 

复制这个application.properties配置文件的两份

application-peer1.properties

server.port=1111
spring.application.name=eureka
eureka.instance.hostname=peer1
#搭建eureka集群 ,开启向eureka注册
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=false
#搭建eureka集群,那么须要注册到eureka去,这是注册的eureka的路径
eureka.client.service-url.defaultZone=http://peer2:1112/eureka

application-peer2.properties

server.port=1112
spring.application.name=eureka
eureka.instance.hostname=peer2
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=false
#互相注册,保持消息服务同步
eureka.client.service-url.defaultZone=http://peer1:1111/eureka

还须要在 本机的C:\Windows\System32\drivers\etc  路径下去到 hosts 文件里去加上下面图片上的两行

这是在一个Spring Boot项目里 ,怎么运行不一样的配置文件呢 , 咱们把项目进行打包

选择左边的maven ,而后选择要打包的Spring Boot项目 ,双击如图的package ,就开始打包了  ,以下打包成功

 

而后咱们点击Terminal

进入打包的目录里去

分别启动两个

 

 

若是你只启动了一个 ,或者另外一个尚未启动起来 , 那么那个已经启动了的会一直报错

,

 

你只需把另外一个启动便可  ,两个启动成功

 

这样个人注册中心eureka集群就就搭建好了 , 下一篇咱们讲提供者(provider)以及提供者集群和消费者(consumer)怎么搭建

相关文章
相关标签/搜索