https://projects.spring.io/spring-cloud/#quick-start

maven

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.0.1.RELEASE</version>
  5. </parent>
  6. <dependencyManagement>
  7. <dependencies>
  8. <dependency>
  9. <groupId>org.springframework.cloud</groupId>
  10. <artifactId>spring-cloud-dependencies</artifactId>
  11. <version>Finchley.SR1</version>
  12. <type>pom</type>
  13. <scope>import</scope>
  14. </dependency>
  15. </dependencies>
  16. </dependencyManagement>
  17. <dependencies>
  18. <dependency>
  19. <groupId>org.springframework.cloud</groupId>
  20. <artifactId>spring-cloud-starter-config</artifactId>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-log4j2</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.cloud</groupId>
  28. <artifactId>spring-cloud-netflix-eureka-server</artifactId>
  29. </dependency>
  30. </dependencies>

Eureka Server

java

  1. @EnableEurekaServer
  2. @SpringBootApplication
  3. public class EurekaserverApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(EurekaserverApplication.class, args);
  6. }
  7. }

配置

  1. server:
  2. port: 8761
  3. eureka:
  4. instance:
  5. hostname: localhost
  6. client:
  7. registerWithEureka: false
  8. fetchRegistry: false
  9. serviceUrl:
  10. defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  1. server.port=8761
  2. spring.application.name=api-eureka
  3. eureka.instance.prefer-ip-address=true
  4. eureka.client.fetch-registry=false
  5. eureka.client.register-with-eureka=false
  6. eureka.server.wait-time-in-ms-when-sync-empty=0
  7. eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

通过eureka.client.registerWithEureka:false和fetchRegistry:false来表明自己是一个eureka server.

Eureka Client

Java

  1. @SpringBootApplication
  2. @EnableDiscoveryClient
  3. @RestController
  4. public class ServiceHiApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(ServiceHiApplication.class, args);
  7. }
  8. @Value("${server.port}")
  9. String port;
  10. @RequestMapping("/hi")
  11. public String home(@RequestParam String name) {
  12. return "hi "+name+",i am from port:" +port;
  13. }
  14. }

配置

  1. eureka:
  2. client:
  3. serviceUrl:
  4. defaultZone: http://localhost:8761/eureka/
  5. server:
  6. port: 8762
  7. spring:
  8. application:
  9. name: service-hi
文档更新时间: 2018-08-03 10:59