简单搭建Eureka注册中心

1.依赖

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

   <parent>
        <groupId>org.springframework.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.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </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>Brixton.SR5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

2.启动类上加注解

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

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

3.application.properties中增加如下配置

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

spring.application.name=eureka-server
server.port=0001
 
eureka.instance.hostname=localhost
 
# 关闭保护机制
#eureka.server.enable-self-preservation=false
 
#由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己。
eureka.client.register-with-eureka=false 

#由于注册中心的职责就是维护服务实例,它并不需要去检索服务,所以也设置为false。
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
 
logging.file=${spring.application.name}.log

4.测试 启动应用----本地并访问 http://localhost:0001(端口)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值