eureka服务单节点搭建以及集群的搭建

x33g5p2x  于2022-08-17 转载在 其他  
字(5.7k)|赞(0)|评价(0)|浏览(551)

1.单节点搭建

1.1 依赖

  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.7.2</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.yl</groupId>
  12. <artifactId>eureka</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>eureka</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>11</java.version>
  18. <spring-cloud.version>2021.0.3</spring-cloud.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.cloud</groupId>
  23. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-test</artifactId>
  28. <scope>test</scope>
  29. </dependency>
  30. </dependencies>
  31. <dependencyManagement>
  32. <dependencies>
  33. <dependency>
  34. <groupId>org.springframework.cloud</groupId>
  35. <artifactId>spring-cloud-dependencies</artifactId>
  36. <version>${spring-cloud.version}</version>
  37. <type>pom</type>
  38. <scope>import</scope>
  39. </dependency>
  40. </dependencies>
  41. </dependencyManagement>
  42. <build>
  43. <plugins>
  44. <plugin>
  45. <groupId>org.springframework.boot</groupId>
  46. <artifactId>spring-boot-maven-plugin</artifactId>
  47. </plugin>
  48. </plugins>
  49. </build>
  50. </project>

1.2 application.properties

  1. # 服务名称
  2. spring.application.name=eureka
  3. # 端口号
  4. server.port=1111
  5. # 其本身即是一个服务,也是一个注册中心,这里设置不把自己注册到注册中心去
  6. eureka.client.register-with-eureka=false
  7. # 从注册中心获取注册信息
  8. eureka.client.fetch-registry=false

1.3 开启eureka服务的功能

  1. package com.yl.eureka;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  5. @SpringBootApplication
  6. @EnableEurekaServer // 开启eureka服务的功能
  7. public class EurekaApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(EurekaApplication.class, args);
  10. }
  11. }

1.4 查看eureka注册中心相关信息

2.集群搭建

2.1 eureka集群概念

eureka集群可以理解成是启动了多个eureka实例,然后这几个实例之间相互注册,以达到同步数据的目的!

2.2 先去这个目录下C:\Windows\System32\drivers\etc,找到host文件并做修改

2.3 依赖

  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.7.2</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.yl</groupId>
  12. <artifactId>eureka</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>eureka</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>11</java.version>
  18. <spring-cloud.version>2021.0.3</spring-cloud.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.cloud</groupId>
  23. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-test</artifactId>
  28. <scope>test</scope>
  29. </dependency>
  30. </dependencies>
  31. <dependencyManagement>
  32. <dependencies>
  33. <dependency>
  34. <groupId>org.springframework.cloud</groupId>
  35. <artifactId>spring-cloud-dependencies</artifactId>
  36. <version>${spring-cloud.version}</version>
  37. <type>pom</type>
  38. <scope>import</scope>
  39. </dependency>
  40. </dependencies>
  41. </dependencyManagement>
  42. <build>
  43. <plugins>
  44. <plugin>
  45. <groupId>org.springframework.boot</groupId>
  46. <artifactId>spring-boot-maven-plugin</artifactId>
  47. </plugin>
  48. </plugins>
  49. </build>
  50. </project>

2.4 application-a.properties

  1. # 服务名称
  2. spring.application.name=eurekaA
  3. # 端口号
  4. server.port=1112
  5. # 实例名称
  6. eureka.instance.hostname=eurekaA
  7. # 其本身即是一个服务,也是一个注册中心,这里设置把自己注册到注册中心去
  8. eureka.client.register-with-eureka=true
  9. # 从注册中心获取注册信息
  10. eureka.client.fetch-registry=true
  11. # A服务注册到B服务中
  12. eureka.client.service-url.defaultZone=http://eurekaB:1113/eureka

2.5 application-b.properties

  1. # 服务名称
  2. spring.application.name=eurekaB
  3. # 端口号
  4. server.port=1113
  5. # 实例名称
  6. eureka.instance.hostname=eurekaB
  7. # 其本身即是一个服务,也是一个注册中心,这里设置把自己注册到注册中心去
  8. eureka.client.register-with-eureka=true
  9. # 从注册中心获取注册信息
  10. eureka.client.fetch-registry=true
  11. # B服务注册到A服务中
  12. eureka.client.service-url.defaultZone=http://eurekaA:1112/eureka

2.6 开启eureka服务功能

  1. package com.yl.eureka;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  5. @SpringBootApplication
  6. @EnableEurekaServer // 开启eureka服务的功能
  7. public class EurekaApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(EurekaApplication.class, args);
  10. }
  11. }

2.7 项目打包,以java-jar命令启动两个实例

2.8 访问eurekaA注册中心

2.9 访问eurekaB注册中心

相关文章