服務端
build.gradle配置
dependencies {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}"}
}
bootstrap.yml相關配置
server.port: 8761
management.port: 8762eureka:instance:hostname: localhostclient:registerWithEureka: falsefetchRegistry: falseserviceUrl:defaultZone: http://localhost:8761/eureka/
啟動代碼
package lind.sprindemo.eurekaServer;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaServerApplication {public static void main(String[] args) {SpringApplication.run(EurekaServerApplication.class, args);}
}
客戶端
向我們配置中心的客戶端同時也是eureka的一個客戶端,例如一個訂單服務,它的配置存儲在配置中心,它如果希望公開自己,就需要是eureka的一個客戶端。
例子
graph TD A[訂單服務]-->B(注冊) B-->C(eureka) A-->D(獲取配置) D-->E(clientserver)