1. 程式人生 > >Spring Cloud 整合 euraka-server 和 euraka-client

Spring Cloud 整合 euraka-server 和 euraka-client

euraka-server

1 匯入 依賴

ext {
    springCloudVersion = 'Finchley.M2'
}

// 依賴關係
dependencies {

    // Eureka Server
compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-server') // 該依賴用於測試階段 testCompile('org.springframework.boot:spring-boot-starter-test') } dependencyManagement { imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
} }

2 配置 yml

server:
  port: 8888

eureka:
    instance:
        hostname: localhost
    client:
        registerWithEureka: false
        fetchRegistry: false
        serviceUrl:
            defaultZone: http://localhost:8888

在啟動類上標明 @EnableEurekaServer

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

euraka-client

1 匯入 依賴

ext {
    springCloudVersion = 'Finchley.M2'
}

// 依賴關係
dependencies {

    // Eureka Client
    compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')

    // 該依賴用於測試階段
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

2 配置 yml

spring:
    application:
        name: micro-weather-eureka-client
        # euraka 客戶端例項
eureka:
    client:
        serviceUrl:
            defaultZone: http://localhost:8888/eureka/
            # euraka server路徑

在啟動類上標明 @EnableDiscoveryClient

@SpringBootApplication
@EnableDiscoveryClient
public class Application {

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