首页 > 技术文章 > Eureka【入门】

idoljames 2019-09-07 20:00 原文

  • 说明
    • SpringBoot版本 2.1.7.RELEASE
    • SpringCloud版本 Greenwich.SR2
  1. 创建eureka server工程

    1. 加入pom依赖
      <dependencies>
              <dependency>
                  <groupId>org.springframework.cloud</groupId>
                  <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
              </dependency>
          </dependencies>
      
          <dependencyManagement>
              <dependencies>
                  <dependency>
                      <groupId>org.springframework.cloud</groupId>
                      <artifactId>spring-cloud-dependencies</artifactId>
                      <version>Greenwich.SR2</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.yml文件中添加配置
      server:
        port: 8761
      
      
      spring:
        application:
          name: servers-register
      
      
      eureka:
        instance:
          hostname: localhost
        client:
          register-with-eureka: false
          fetch-registry: false
          service-url:
            defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
        server:
          wait-time-in-ms-when-sync-empty: 0
          enable-self-preservation: false

      注意:这里是单机版配置

  2. 创建eureka client工程

    1. 加入pom依赖
      <dependencies>
              <dependency>
                  <groupId>org.springframework.cloud</groupId>
                  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
              </dependency>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter</artifactId>
              </dependency>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-web</artifactId>
              </dependency>
          </dependencies>
      
          <dependencyManagement>
              <dependencies>
                  <dependency>
                      <groupId>org.springframework.cloud</groupId>
                      <artifactId>spring-cloud-dependencies</artifactId>
                      <version>Greenwich.SR2</version>
                      <type>pom</type>
                      <scope>import</scope>
                  </dependency>
              </dependencies>
          </dependencyManagement>
    2. 启动类添加@EnableDiscoveryClient注解
      @EnableDiscoveryClient
      @SpringBootApplication
      public class UserServerApplication {
      
          public static void main(String[] args) {
              SpringApplication.run(UserServerApplication.class, args);
          }
      
      }
    3. application.yml添加配置
      server:
        port: 8081
      
      eureka: client: service
      -url: defaultZone: 'http://localhost:8761/eureka/'
      spring: application: name: user
      -server

      注意:这里的spring.application.name服务自定义名称,如果不定义的话,在eureka server的界面中展示的是UNKNOWN

  3. 启动server和client项目,访问localhost:8761

     

    提示:这里的USER-SERVER名称就是user-client工程配置文件中的 spring.application.name

推荐阅读