首页 > 编程语言 > 详细

SpringCloudEureka入门

时间:2019-09-07 20:38:32      阅读:87      评论:0      收藏:0      [点我收藏+]
  • 说明
    • 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

SpringCloudEureka入门

原文:https://www.cnblogs.com/idoljames/p/11482670.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!