Pārlūkot izejas kodu

feat:搭建使用nacos注册中心案例,在父项目中引入父依赖管理进行统一版本管理,在各个微服务模块引入nacos的服务发现依赖,在各个微服务配置nacos注册中心地址,成功注册到nacos注册中心

yang yi 1 gadu atpakaļ
vecāks
revīzija
376a6782c5

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

@@ -16,6 +16,11 @@
         <maven.compiler.target>17</maven.compiler.target>
     </properties>
     <dependencies>
+        <!--引入nacos的服务发现依赖-->
+        <dependency>
+            <groupId>com.alibaba.cloud</groupId>
+            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</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.client.discovery.EnableDiscoveryClient;
 
 /**
  * @ProjectName: distributedSystemLearn
@@ -11,6 +12,7 @@ import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
  * @Data:2025/4/13 20:05
  * @Description:
  */
+@EnableDiscoveryClient
 @SpringBootApplication(exclude= DataSourceAutoConfiguration.class)
 public class MemberConsumerApplication {
     public static void main(String[] args) {

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

@@ -5,3 +5,8 @@ server:
 spring:
   application:
     name: member-consumer
+  cloud:
+    nacos:
+      discovery:
+        #配置nacos服务注册中心的地址
+        server-addr: localhost:8848

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

@@ -16,6 +16,11 @@
         <maven.compiler.target>17</maven.compiler.target>
     </properties>
     <dependencies>
+        <!--引入nacos的服务发现依赖-->
+        <dependency>
+            <groupId>com.alibaba.cloud</groupId>
+            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
+        </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>

+ 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.client.discovery.EnableDiscoveryClient;
 
 /**
  * @ProjectName: distributedSystemLearn
@@ -10,6 +11,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
  * @Data:2025/4/13 18:53
  * @Description:
  */
+@EnableDiscoveryClient
 @SpringBootApplication(scanBasePackages = {"anyi.sapce.common","anyi.space.memberServer"})
 public class MemberServiceApplication {
     public static void main(String[] args) {

+ 6 - 2
member-service-provider_10000/src/main/resources/application.yaml

@@ -1,10 +1,10 @@
 server:
-  application:
-    name: member-service-provider
   port: 10001
   servlet:
     context-path: /member-provider
 spring:
+  application:
+    name: member-service-provider
   datasource:
     type: com.alibaba.druid.pool.DruidDataSource
     druid:
@@ -12,6 +12,10 @@ spring:
       username: root
       password: hsp
       driver-class-name: com.mysql.jdbc.Driver
+  cloud:
+    nacos:
+      discovery:
+        server-addr: localhost:8848
 mybatis-plus:
   configuration:
     log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

+ 8 - 0
pom.xml

@@ -26,6 +26,14 @@
 
     <dependencyManagement>
         <dependencies>
+            <!--以pom依赖的形式导入spring-cloud-alibaba-dependencies依赖-->
+            <dependency>
+                <groupId>com.alibaba.cloud</groupId>
+                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
+                <version>2021.0.5.0</version>
+                <type>pom</type>
+                <scope>import</scope>
+            </dependency>
             <dependency>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-starter-parent</artifactId>