1. 如果使用一个ip地址(适用于单网卡)每个eureka实例使用不同的域名映射到同一个IP
  2. 如果每个eureka实例使用不同的IP(多网卡),要确保这些IP要都表示本地

本文假设使用第一种情况

sudo /etc/hosts

添加如下行

127.0.0.1 registry01
127.0.0.1 registry02

可以输入

ping registry01
ping registry02

来验证是否解析到 127.0.0.1

idea向导如何建立eureka server就不详细写了

我直接扔配置

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.4.2</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.linkanyway</groupId>
  12. <artifactId>registry</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>registry</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. <spring-cloud.version>2020.0.0</spring-cloud.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-devtools</artifactId>
  24. <scope>runtime</scope>
  25. <optional>true</optional>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>spring-boot-configuration-processor</artifactId>
  30. <optional>true</optional>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.cloud</groupId>
  34. <artifactId>spring-cloud-netflix-eureka-server</artifactId>
  35. <version>2.2.5.RELEASE</version>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.testng</groupId>
  39. <artifactId>testng</artifactId>
  40. <version>RELEASE</version>
  41. <scope>test</scope>
  42. </dependency>
  43. <dependency>
  44. <groupId>org.springframework.boot</groupId>
  45. <artifactId>spring-boot-test</artifactId>
  46. <version>2.4.2</version>
  47. <scope>test</scope>
  48. </dependency>
  49. </dependencies>
  50. <dependencyManagement>
  51. <dependencies>
  52. <dependency>
  53. <groupId>org.springframework.cloud</groupId>
  54. <artifactId>spring-cloud-dependencies</artifactId>
  55. <version>${spring-cloud.version}</version>
  56. <type>pom</type>
  57. <scope>import</scope>
  58. </dependency>
  59. </dependencies>
  60. </dependencyManagement>
  61. <build>
  62. <plugins>
  63. <plugin>
  64. <groupId>org.springframework.boot</groupId>
  65. <artifactId>spring-boot-maven-plugin</artifactId>
  66. </plugin>
  67. </plugins>
  68. </build>
  69. <repositories>
  70. <repository>
  71. <id>spring-milestones</id>
  72. <name>Spring Milestones</name>
  73. <url>https://repo.spring.io/milestone</url>
  74. </repository>
  75. </repositories>
  76. </project>

主程序

  1. package com.linkanyway.registry;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  5. /**
  6. * @author linkanyway
  7. */
  8. @EnableEurekaServer
  9. @SpringBootApplication
  10. public class RegistryApplication {
  11. public static void main(String[] args) {
  12. SpringApplication.run (RegistryApplication.class, args);
  13. }
  14. }

application.yml

  1. server:
  2. port: 8080
  3. spring:
  4. application:
  5. name: EurekaCluster # 所有集群内都采用一样Name
  6. eureka:
  7. instance:
  8. hostname: registry01
  9. prefer-ip-address: true # 采用ip地址
  10. ip-address: 127.0.0.1
  11. client:
  12. register-with-eureka: true # 控制当Spring Boot启动服务完成后是否将该服务注册到服务治理服务器上。这里因为服务本身就是服务治理服务器且有集群因此为true,如果未构建任何服务治理集群,需将其设置为false,表示不注册
  13. fetch-registry: true # eureka.client.fetch-registry属性设置表示应用启动后是否需要从服务治理服务器中同步已注册的服务注册列表数据到本地 本处集群服务器成员则需要注册此处为true
  14. service-url:
  15. defaultZone: http://registry02:8081/eureka # 集群需要配置相互复制
  16. server:
  17. wait-time-in-ms-when-sync-empty: 5

pom文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.4.2</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.linkanyway</groupId>
  12. <artifactId>registry02</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>registry02</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. <spring-cloud.version>2020.0.0</spring-cloud.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-web</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.cloud</groupId>
  27. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-devtools</artifactId>
  32. <scope>runtime</scope>
  33. <optional>true</optional>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-configuration-processor</artifactId>
  38. <optional>true</optional>
  39. </dependency>
  40. <dependency>
  41. <groupId>org.projectlombok</groupId>
  42. <artifactId>lombok</artifactId>
  43. <optional>true</optional>
  44. </dependency>
  45. <dependency>
  46. <groupId>org.springframework.boot</groupId>
  47. <artifactId>spring-boot-starter-test</artifactId>
  48. <scope>test</scope>
  49. </dependency>
  50. </dependencies>
  51. <dependencyManagement>
  52. <dependencies>
  53. <dependency>
  54. <groupId>org.springframework.cloud</groupId>
  55. <artifactId>spring-cloud-dependencies</artifactId>
  56. <version>${spring-cloud.version}</version>
  57. <type>pom</type>
  58. <scope>import</scope>
  59. </dependency>
  60. </dependencies>
  61. </dependencyManagement>
  62. <build>
  63. <plugins>
  64. <plugin>
  65. <groupId>org.springframework.boot</groupId>
  66. <artifactId>spring-boot-maven-plugin</artifactId>
  67. <configuration>
  68. <excludes>
  69. <exclude>
  70. <groupId>org.projectlombok</groupId>
  71. <artifactId>lombok</artifactId>
  72. </exclude>
  73. </excludes>
  74. </configuration>
  75. </plugin>
  76. </plugins>
  77. </build>
  78. <repositories>
  79. <repository>
  80. <id>spring-milestones</id>
  81. <name>Spring Milestones</name>
  82. <url>https://repo.spring.io/milestone</url>
  83. </repository>
  84. </repositories>
  85. </project>

主程序

  1. package com.linkanyway.registry02;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  5. @EnableEurekaServer
  6. @SpringBootApplication
  7. public class Registry02Application {
  8. public static void main(String[] args) {
  9. SpringApplication.run (Registry02Application.class, args);
  10. }
  11. }

application.yml

  1. server:
  2. port: 8081
  3. spring:
  4. application:
  5. name: EurekaCluster # 所有集群内都一样name
  6. eureka:
  7. instance:
  8. hostname: registry02
  9. prefer-ip-address: true # 采用ip地址
  10. ip-address: 127.0.0.1
  11. client:
  12. register-with-eureka: true # 控制当Spring Boot启动服务完成后是否将该服务注册到服务治理服务器上。这里因为服务本身就是服务治理服务器且有集群因此为true,如果未构建任何服务治理集群,需将其设置为false,表示不注册
  13. fetch-registry: true # eureka.client.fetch-registry属性设置表示应用启动后是否需要从服务治理服务器中同步已注册的服务注册列表数据到本地 本处集群服务器成员则需要注册此处为true
  14. service-url:
  15. defaultZone: http://registry01:8080/eureka # 集群需要配置相互复制
  16. server:
  17. wait-time-in-ms-when-sync-empty: 5

因为eureka同步需要时间,稍后会就可以看到下图的效果

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.4.2</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.linkanyway</groupId>
  12. <artifactId>service01</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>service01</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. <spring-cloud.version>2020.0.0</spring-cloud.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-web</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.cloud</groupId>
  27. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-devtools</artifactId>
  32. <scope>runtime</scope>
  33. <optional>true</optional>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-configuration-processor</artifactId>
  38. <optional>true</optional>
  39. </dependency>
  40. <dependency>
  41. <groupId>org.projectlombok</groupId>
  42. <artifactId>lombok</artifactId>
  43. <optional>true</optional>
  44. </dependency>
  45. <dependency>
  46. <groupId>org.springframework.boot</groupId>
  47. <artifactId>spring-boot-starter-test</artifactId>
  48. <scope>test</scope>
  49. </dependency>
  50. </dependencies>
  51. <dependencyManagement>
  52. <dependencies>
  53. <dependency>
  54. <groupId>org.springframework.cloud</groupId>
  55. <artifactId>spring-cloud-dependencies</artifactId>
  56. <version>${spring-cloud.version}</version>
  57. <type>pom</type>
  58. <scope>import</scope>
  59. </dependency>
  60. </dependencies>
  61. </dependencyManagement>
  62. <build>
  63. <plugins>
  64. <plugin>
  65. <groupId>org.springframework.boot</groupId>
  66. <artifactId>spring-boot-maven-plugin</artifactId>
  67. <configuration>
  68. <excludes>
  69. <exclude>
  70. <groupId>org.projectlombok</groupId>
  71. <artifactId>lombok</artifactId>
  72. </exclude>
  73. </excludes>
  74. </configuration>
  75. </plugin>
  76. </plugins>
  77. </build>
  78. <repositories>
  79. <repository>
  80. <id>spring-milestones</id>
  81. <name>Spring Milestones</name>
  82. <url>https://repo.spring.io/milestone</url>
  83. </repository>
  84. </repositories>
  85. </project>

主程序

  1. package com.linkanyway.service01;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  5. /**
  6. * @author linkanyway
  7. */
  8. @EnableDiscoveryClient
  9. @SpringBootApplication
  10. public class Service01Application {
  11. public static void main(String[] args) {
  12. SpringApplication.run (Service01Application.class, args);
  13. }
  14. }

application.yml

  1. server:
  2. port: 8082
  3. eureka:
  4. client:
  5. fetch-registry: true
  6. register-with-eureka: true
  7. serviceUrl:
  8. defaultZone: http://127.0.0.1:8080/eureka/
  9. instance:
  10. prefer-ip-address: true #强烈推荐在项目中使用IP地址而不是主机名称来注册微服务。
  11. lease-expiration-duration-in-seconds: 90
  12. lease-renewal-interval-in-seconds: 30
  13. ip-address: 127.0.0.1 # 因此,强烈推荐在项目中使用IP地址而不是主机名称来注册微服务。
  14. spring:
  15. application:
  16. name: service01

编写服务(此处简略的只是做一个控制器)

  1. package com.linkanyway.service01.controller;
  2. import org.springframework.web.bind.annotation.GetMapping;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5. /**
  6. * @author linkanyway
  7. * @version 1.0
  8. * @name ServiceController
  9. * @description TODO
  10. * @date 2021/02/01 20:21
  11. */
  12. @RestController
  13. @RequestMapping("service")
  14. public class ServiceController {
  15. @GetMapping("get")
  16. public String get() {
  17. return "Service01";
  18. }
  19. }

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.4.2</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.linkanyway</groupId>
  12. <artifactId>service02</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>service02</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. <spring-cloud.version>2020.0.0</spring-cloud.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-web</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.cloud</groupId>
  27. <artifactId>spring-cloud-starter-feign</artifactId>
  28. <version>1.4.7.RELEASE</version>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework.cloud</groupId>
  32. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.springframework.boot</groupId>
  36. <artifactId>spring-boot-devtools</artifactId>
  37. <scope>runtime</scope>
  38. <optional>true</optional>
  39. </dependency>
  40. <dependency>
  41. <groupId>org.springframework.boot</groupId>
  42. <artifactId>spring-boot-configuration-processor</artifactId>
  43. <optional>true</optional>
  44. </dependency>
  45. <dependency>
  46. <groupId>org.projectlombok</groupId>
  47. <artifactId>lombok</artifactId>
  48. <optional>true</optional>
  49. </dependency>
  50. <dependency>
  51. <groupId>org.springframework.boot</groupId>
  52. <artifactId>spring-boot-starter-test</artifactId>
  53. <scope>test</scope>
  54. </dependency>
  55. </dependencies>
  56. <dependencyManagement>
  57. <dependencies>
  58. <dependency>
  59. <groupId>org.springframework.cloud</groupId>
  60. <artifactId>spring-cloud-dependencies</artifactId>
  61. <version>${spring-cloud.version}</version>
  62. <type>pom</type>
  63. <scope>import</scope>
  64. </dependency>
  65. </dependencies>
  66. </dependencyManagement>
  67. <build>
  68. <plugins>
  69. <plugin>
  70. <groupId>org.springframework.boot</groupId>
  71. <artifactId>spring-boot-maven-plugin</artifactId>
  72. <configuration>
  73. <excludes>
  74. <exclude>
  75. <groupId>org.projectlombok</groupId>
  76. <artifactId>lombok</artifactId>
  77. </exclude>
  78. </excludes>
  79. </configuration>
  80. </plugin>
  81. </plugins>
  82. </build>
  83. <repositories>
  84. <repository>
  85. <id>spring-milestones</id>
  86. <name>Spring Milestones</name>
  87. <url>https://repo.spring.io/milestone</url>
  88. </repository>
  89. </repositories>
  90. </project>

application.yml配置文件

  1. server:
  2. port: 8083
  3. eureka:
  4. client:
  5. fetch-registry: true
  6. register-with-eureka: true
  7. serviceUrl:
  8. defaultZone: http://127.0.0.1:8081/eureka/
  9. instance:
  10. prefer-ip-address: true
  11. ip-address: 127.0.0.1
  12. spring:
  13. application:
  14. name: service02
  15. feign:
  16. client:
  17. default-config:
  18. connectTimeout: 10000
  19. readTimeout: 10000

主程序

  1. package com.linkanyway.service02;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  5. import org.springframework.cloud.openfeign.EnableFeignClients;
  6. /**
  7. * @author linkanyway
  8. */
  9. @EnableDiscoveryClient
  10. @EnableFeignClients
  11. @SpringBootApplication
  12. public class Service02Application {
  13. public static void main(String[] args) {
  14. SpringApplication.run (Service02Application.class, args);
  15. }
  16. }

服务调用的feign定义

  1. package com.linkanyway.service02.feign;
  2. import org.springframework.cloud.openfeign.FeignClient;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. /**
  5. * @author linkanyway
  6. * @version 1.0
  7. * @name Service01
  8. * @description TODO
  9. * @date 2021/02/01 20:31
  10. */
  11. @FeignClient("service01")
  12. public interface Service01 {
  13. /**
  14. * get
  15. * @return
  16. */
  17. @RequestMapping("service/get")
  18. String get();
  19. }

控制器代码

  1. package com.linkanyway.service02.controller;
  2. import com.linkanyway.service02.feign.Service01;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6. /**
  7. * @author linkanyway
  8. * @version 1.0
  9. * @name ServiceController
  10. * @description TODO
  11. * @date 2021/02/01 20:34
  12. */
  13. @RestController
  14. @RequestMapping("service")
  15. public class ServiceController {
  16. final
  17. Service01 service01;
  18. public ServiceController(Service01 service01) {
  19. this.service01 = service01;
  20. }
  21. @RequestMapping("get")
  22. public String get()
  23. {
  24. return service01.get ();
  25. }
  26. }

启动service01,02

可以看到成功注册到注册中心

版权声明:本文为linkanyway原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/linkanyway/p/springcloud-eureka-cluster-single-host.html