SpringCloudEureka实战:搭建EurekaServer

时间:2024-10-02 07:36:16

d08434266d31ef3a2ca0fb6e1477fab1.jpeg

1、依赖引入

<dependencies>
    <!-- 注册中心 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
</dependencies>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <!-- 如果要用gson,则需要先去除默认的jackson-databind,再加入Gson依赖。-->
    <exclusions>
        <exclusion>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </exclusion>
    </exclusions>
</dependency>


<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Hoxton.SR9</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

2、@EnableEurekaServer修饰启动类

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


}

3、配置数据

application.properties

server.port=8881
spring.application.name=eureka-server


## eureka服务端:禁用自己的客户端注册行为
eureka.instance.hostname=localhost
# 服务端禁用自己的客户端注册行为
eureka.client.registerWithEureka=false
# 服务端只需要维护服务实例,不需要检索服务
eureka.client.fetchRegistry=false
# 服务端设置自己的.properties属性值
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

4、验证效果

http://localhost:8881/

db0bb21b1e50f9642e3c471ca89deb82.png