Parcourir la source

feat:完成Eureka 服务提供方和服务消费方的客户端搭建并注册到注册中心

yang yi il y a 1 an
Parent
commit
cc1c191830

+ 5 - 0
member-service-consumer/pom.xml

@@ -16,6 +16,11 @@
         <maven.compiler.target>17</maven.compiler.target>
     </properties>
     <dependencies>
+        <!--引入eureka-client依赖-->
+        <dependency>
+            <groupId>org.springframework.cloud</groupId>
+            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
+        </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>

+ 2 - 0
member-service-consumer/src/main/java/anyi/space/memberConsumer/MemberConsumerApplication.java

@@ -3,6 +3,7 @@ package anyi.space.memberConsumer;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
+import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
 
 /**
  * @ProjectName: distributedSystemLearn
@@ -11,6 +12,7 @@ import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
  * @Data:2025/4/13 20:05
  * @Description:
  */
+@EnableEurekaClient
 @SpringBootApplication(exclude= DataSourceAutoConfiguration.class)
 public class MemberConsumerApplication {
     public static void main(String[] args) {

+ 12 - 0
member-service-consumer/src/main/resources/application.yaml

@@ -5,3 +5,15 @@ server:
 spring:
   application:
     name: member-consumer
+eureka:
+  instance:
+    #配置注册到Eureka Server中的名称
+    appname: member-consumer
+  client:
+    #是否将自己注册到Eureka Server中
+    register-with-eureka: true
+    #是否从Eureka Server中获取注册信息,单节点不拉取也可以,集群部署时,需要拉取服务注册信息才能实现负载均衡
+    fetch-registry: true
+    #Eureka Server的地址
+    service-url:
+      defaultZone: http://localhost:9001/eureka/

+ 9 - 0
member-service-provider_10000/pom.xml

@@ -16,6 +16,11 @@
         <maven.compiler.target>17</maven.compiler.target>
     </properties>
     <dependencies>
+        <!--引入eureka-client依赖-->
+        <dependency>
+            <groupId>org.springframework.cloud</groupId>
+            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
+        </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
@@ -41,6 +46,10 @@
             <artifactId>common</artifactId>
             <version>${project.version}</version>
         </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-actuator</artifactId>
+        </dependency>
     </dependencies>
 
 </project>

+ 2 - 0
member-service-provider_10000/src/main/java/anyi/space/memberServer/MemberServiceApplication.java

@@ -2,6 +2,7 @@ package anyi.space.memberServer;
 
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
 
 /**
  * @ProjectName: distributedSystemLearn
@@ -10,6 +11,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
  * @Data:2025/4/13 18:53
  * @Description:
  */
+@EnableEurekaClient
 @SpringBootApplication(scanBasePackages = {"anyi.sapce.common","anyi.space.memberServer"})
 public class MemberServiceApplication {
     public static void main(String[] args) {

+ 12 - 0
member-service-provider_10000/src/main/resources/application.yaml

@@ -15,3 +15,15 @@ spring:
 mybatis-plus:
   configuration:
     log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
+eureka:
+  instance:
+    #配置注册到Eureka Server中的名称
+    appname: member-service-provider
+  client:
+    #是否将自己注册到Eureka Server中
+    register-with-eureka: true
+    #是否从Eureka Server中获取注册信息,单节点不拉取也可以,集群部署时,需要拉取服务注册信息才能实现负载均衡
+    fetch-registry: true
+    #Eureka Server的地址
+    service-url:
+      defaultZone: http://localhost:9001/eureka/