1. 程式人生 > >Spring2 cloud eureka學習(一)

Spring2 cloud eureka學習(一)

1、建立artifactId為microservice-discovery-eureka的maven工程。(spring boot 為2.0.3)

<parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.0.3.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>

   <dependencies>
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
      </dependency>

      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>
   </dependencies>

   <dependencyManagement>
      <dependencies>
         <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
         </dependency>
      </dependencies>
   </dependencyManagement>

2、編寫啟動類,增加@EnableEurekaServer註解,宣告為eureka server。

@SpringBootApplication
@EnableEurekaServer
public class MicroserviceDiscoveryEurekaApplication {

	public static void main(String[] args) {
		SpringApplication.run(MicroserviceDiscoveryEurekaApplication.class, args);
	}
}

3、配置application.yml。

server:
  port: 8764
eureka:
  client:
    register-with-eureka: false # 是否將應用註冊到Eureka server上,當前應用為server,設定為false
    fetch-registry: false # 從eureka獲取註冊資訊
    service-url:
       defaultZone: http://localhost:8764/eureka/  #defaultZone不能替換為default-zone(本人踩過的坑)

4、執行mvn spring-boot:run執行專案

5、結果如圖

6、GitHub原始碼地址:https://github.com/nieshankun/microservice-discovery-eureka.git