关于spring-cloud:9java-Spring-Cloud企业快速开发架构之SpringCloud搭建Eureka服务注册中心

首先创立一个 Maven 我的项目,取名为 eureka-server,在 pom.xml 中配置 Eureka 的依赖信息,代码如下所示。

须要框架源码请看我个人简介

​
<!-- Spring Boot -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.6.RELEASE</version>
    <relativePath />
</parent>

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

<!-- Spring Cloud -->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Finchley.SR2</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

​

创立一个启动类 EurekaServerApplication,代码如下所示。

​
@EnableEurekaServer
@SpringBootApplication
    public static void main(String[] args) {
        SpringApplication.run(EurekaServer Application.class, args);
    }
}

​

这里所说的启动类,跟咱们之前讲的 Spring Boot 简直齐全一样,只是多了一个 @EnableEurekaServer 注解,示意开启 Eureka Server。

接下来在 src/main/resources 上面创立一个 application.properties 属性文件,减少上面的配置:

​
spring.application.name=eureka-server
server.port=8761
# 因为该利用为注册核心, 所以设置为false, 代表不向注册核心注册本人
eureka.client.register-with-eureka=false
# 因为注册核心的职责就是保护服务实例, 它并不需要去检索服务, 所以也设置为 false
eureka.client.fetch-registry=false

​

创立一个启动类 EurekaServerApplication,代码如下所示。

​
@EnableEurekaServer
@SpringBootApplication
    public static void main(String[] args) {
        SpringApplication.run(EurekaServer Application.class, args);
    }
}

​

这里所说的启动类,跟咱们之前讲的 Spring Boot 简直齐全一样,只是多了一个 @EnableEurekaServer 注解,示意开启 Eureka Server。

接下来在 src/main/resources 上面创立一个 application.properties 属性文件,减少上面的配置:

​
spring.application.name=eureka-server
server.port=8761
# 因为该利用为注册核心, 所以设置为false, 代表不向注册核心注册本人
eureka.client.register-with-eureka=false
# 因为注册核心的职责就是保护服务实例, 它并不需要去检索服务, 所以也设置为 false
eureka.client.fetch-registry=false

​

eureka.client.register-with-eureka 肯定要配置为 false,不然启动时会把本人当作客户端向本人注册,会报错。

接下来间接运行 EurekaServerApplication 就能够启动咱们的注册核心服务了。咱们在 application.properties 配置的端口是 8761,则能够间接通http://localhost:8761/ 去浏览器中拜访,而后便会看到 Eureka 提供的 Web 控制台。

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理