Переглянути джерело

feat:模块ms-user-api定义服务接口,模块ms-user-web提供服务,模块ms-marketing使用服务,通过手动配置的方式使用第三方服务

yang yi 1 місяць тому
батько
коміт
b960b3da26

+ 2 - 0
build.gradle.kts

@@ -23,6 +23,8 @@ allprojects {
     tasks.withType<Test> {
         systemProperty("file.encoding", "UTF-8")
     }
+    //所有子项目使用的插件
+    apply(plugin = "java-library")
 }
 
 //指定使用的jdk版本

+ 6 - 0
ms-marketing/build.gradle.kts

@@ -11,6 +11,12 @@ repositories {
 }
 
 dependencies {
+    implementation(project(":ms-user-api"))
+    implementation(platform("org.springframework.cloud:spring-cloud-dependencies:2021.0.3"))
+    implementation(platform("org.springframework.boot:spring-boot-dependencies:2.7.0"))
+    implementation("org.springframework.cloud:spring-cloud-starter")
+    implementation("org.springframework.cloud:spring-cloud-starter-openfeign")
+    implementation("org.springframework.boot:spring-boot-starter-web")
     testImplementation("org.junit.jupiter:junit-jupiter-api:5.6.0")
     testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
 }

+ 21 - 0
ms-marketing/src/main/java/space/anyi/markting/Application.java

@@ -0,0 +1,21 @@
+package space.anyi.markting;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.cloud.openfeign.EnableFeignClients;
+
+/**
+ * @ProjectName: springboot-autoconfiguration-learn
+ * @FileName: Application
+ * @Author: 杨逸
+ * @Data:2025/10/13 22:01
+ * @Description:
+ */
+@SpringBootApplication
+//需要手动配置
+@EnableFeignClients(basePackages = "space.anyi.user.service")
+public class Application {
+    public static void main(String[] args) {
+        SpringApplication.run(Application.class, args);
+    }
+}

+ 31 - 0
ms-marketing/src/main/java/space/anyi/markting/controller/GreetingController.java

@@ -0,0 +1,31 @@
+package space.anyi.markting.controller;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RestController;
+import space.anyi.user.model.User;
+import space.anyi.user.service.UserService;
+
+/**
+ * @ProjectName: springboot-autoconfiguration-learn
+ * @FileName: GreetingController
+ * @Author: 杨逸
+ * @Data:2025/10/13 22:03
+ * @Description:
+ */
+@RestController
+public class GreetingController {
+    private final UserService userService;
+
+    @Autowired
+    public GreetingController(UserService userService) {
+        this.userService = userService;
+    }
+
+    @GetMapping("/hello/{userId}")
+    public String hello(@PathVariable("userId") Long userId){
+        User user = userService.getUserById(userId);
+        return "Hello " + user.getName();
+    }
+}

+ 5 - 0
ms-marketing/src/main/resources/application.yaml

@@ -0,0 +1,5 @@
+server:
+  port: 10001
+spring:
+  application:
+    name: ms-marketing

+ 23 - 0
ms-user-api/build.gradle.kts

@@ -0,0 +1,23 @@
+plugins {
+    `java-library`
+}
+
+group = "space.anyi"
+version = "1.0-SNAPSHOT"
+
+repositories {
+    mavenCentral()
+}
+
+dependencies {
+//    implementation(project(":ms-user"))
+    implementation(platform("org.springframework.cloud:spring-cloud-dependencies:2021.0.3"))
+    implementation("org.springframework.cloud:spring-cloud-starter")
+    implementation("org.springframework.cloud:spring-cloud-starter-openfeign")
+    testImplementation("org.junit.jupiter:junit-jupiter-api:5.6.0")
+    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
+}
+
+tasks.getByName<Test>("test") {
+    useJUnitPlatform()
+}

+ 37 - 0
ms-user-api/src/main/java/space/anyi/user/model/User.java

@@ -0,0 +1,37 @@
+package space.anyi.user.model;
+
+/**
+ * @ProjectName: springboot-autoconfiguration-learn
+ * @FileName: User
+ * @Author: 杨逸
+ * @Data:2025/10/13 21:04
+ * @Description:
+ */
+public class User {
+    private Long id;
+    private String name;
+
+    public User(long id, String name) {
+        this.id = id;
+        this.name = name;
+    }
+
+    public User() {
+    }
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+}

+ 37 - 0
ms-user-api/src/main/java/space/anyi/user/service/UserService.java

@@ -0,0 +1,37 @@
+package space.anyi.user.service;
+
+import org.springframework.cloud.openfeign.FallbackFactory;
+import org.springframework.cloud.openfeign.FeignClient;
+import org.springframework.stereotype.Component;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.ResponseBody;
+import space.anyi.user.model.User;
+
+/**
+ * @ProjectName: springboot-autoconfiguration-learn
+ * @FileName: UserService
+ * @Author: 杨逸
+ * @Data:2025/10/13 21:04
+ * @Description:
+ */
+@FeignClient(name = "ms-user-web",url = "${ms-user.url:127.0.0.1:8080}",fallbackFactory = UserService.UserCallbackFactory.class)
+public interface UserService {
+    @ResponseBody
+    @GetMapping("/getUser/{id}")
+    User getUserById(@PathVariable("id") Long id);
+
+    //定义一个回调工厂类
+    @Component
+    public static class UserCallbackFactory implements FallbackFactory<UserService> {
+        @Override
+        public UserService create(Throwable cause) {
+            return new UserService() {
+                @Override
+                public User getUserById(Long id) {
+                    return null;
+                }
+            };
+        }
+    }
+}

+ 24 - 0
ms-user-web/build.gradle.kts

@@ -0,0 +1,24 @@
+plugins {
+    `java-library`
+}
+
+group = "space.anyi"
+version = "1.0-SNAPSHOT"
+description = "ms-user-web"
+
+repositories {
+    mavenCentral()
+}
+
+dependencies {
+//    implementation(project(":ms-user"))
+    implementation(project(":ms-user-api"))
+    implementation(platform("org.springframework.boot:spring-boot-dependencies:2.7.0"))
+    implementation("org.springframework.boot:spring-boot-starter-web")
+    testImplementation("org.junit.jupiter:junit-jupiter-api:5.6.0")
+    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
+}
+
+tasks.getByName<Test>("test") {
+    useJUnitPlatform()
+}

+ 18 - 0
ms-user-web/src/main/java/space/anyi/user/WebApplication.java

@@ -0,0 +1,18 @@
+package space.anyi.user;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+/**
+ * @ProjectName: springboot-autoconfiguration-learn
+ * @FileName: WebApplication
+ * @Author: 杨逸
+ * @Data:2025/10/13 20:54
+ * @Description:
+ */
+@SpringBootApplication
+public class WebApplication {
+    public static void main(String[] args) {
+        SpringApplication.run(WebApplication.class, args);
+    }
+}

+ 33 - 0
ms-user-web/src/main/java/space/anyi/user/controller/UserController.java

@@ -0,0 +1,33 @@
+package space.anyi.user.controller;
+
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.ResponseBody;
+import space.anyi.user.model.User;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @ProjectName: springboot-autoconfiguration-learn
+ * @FileName: UserController
+ * @Author: 杨逸
+ * @Data:2025/10/13 20:57
+ * @Description:
+ */
+@Controller
+public class UserController{
+    private static final   Map<Long, User> userMap = new HashMap<>();
+    static {
+        //模拟数据
+        userMap.put(1L,new User(1L,"张三"));
+        userMap.put(2L,new User(2L,"李四"));
+    }
+
+    @GetMapping("/getUser/{userId}")
+    @ResponseBody
+    public User getUserById(@PathVariable("userId") Long id){
+        return userMap.get(id);
+    }
+}

+ 4 - 0
settings.gradle.kts

@@ -1,3 +1,7 @@
 rootProject.name = "springboot-autoconfiguration-learn"
 include("ms-user")
 include("ms-marketing")
+include("ms-user-web")
+include("ms-user-api")
+include("ms-user-autoconfig")
+include("ms-user-spring-boot-starter")