服务端
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)