一 创建一个Spring Boot工程,命名为eureka-server,并在pom. 中引入必要的依赖,代码如下。

    <parent>        <groupId>org.spring work.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.3.7.RELEASE</version>        <relativePath/>    </parent>     <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <java.version>1.8</java.version>    </properties>     <dependencies>        <dependency>            <groupId>org.spring work.cloud</groupId>            <artifactId>spring-cloud-starter-eureka-server</artifactId>        </dependency>         <!--<dependency>-->            <!--<groupId>org.spring work.boot</groupId>-->            <!--<artifactId>spring-boot-starter-actuator</artifactId>-->        <!--</dependency>-->    </dependencies>     <dependencyManagement>        <dependencies>            <dependency>                <groupId>org.spring work.cloud</groupId>                <artifactId>spring-cloud-dependencies</artifactId>                <version>Brixton.SR5</version>                <type>pom</type>                <scope>import</scope>            </dependency>        </dependencies>    </dependencyManagement>

二 通过@EnableEurekaServer注解启动一个服务注册中心提供给其他应用程序进行对话,只需要在Spring  Boot应用中添加下面这个注解就能开启此功能。

@EnableEurekaServer@SpringBootApplicationpublic class Application {     public static void main(String[] args) {        new SpringApplicationBuilder(Application.class).web(true).run(args);    } }

三 在默认情况下,服务注册中也会将自己作为客户端来尝试注册它自己,所以需要禁用它的客户端行为。

application.properties中增加如下配置。

spring.application.name=eureka-serverserver.port=1111 eureka.instance.hostname=localhost # 关闭保护机制#eureka.server.enable-self-preservation=false eureka.client.register-with-eureka=falseeureka.client.fetch-registry=falseeureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/ logging.file=${spring.application.name}.log

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

eureka.client.fetch-registry:由于注册中心的职责就是维护服务实例,它并不需要去检索服务,所以也设置为false。

收藏 打印