♻️Refactor: 微服务重构

This commit is contained in:
2025-10-28 23:09:46 +08:00
parent 44607b5e1b
commit c0b89fcd95
30 changed files with 279 additions and 40 deletions
+8
View File
@@ -11,6 +11,14 @@
<artifactId>common</artifactId>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
@@ -1,4 +1,4 @@
package cn.hezhaohui.common;
package cn.hezhaohui.weather.common;
public class AIConstant {
@@ -1,4 +1,4 @@
package cn.hezhaohui.common;
package cn.hezhaohui.weather.common;
public class AppConstant {
@@ -1,4 +1,4 @@
package cn.hezhaohui.common;
package cn.hezhaohui.weather.common;
public enum CardEnum {
CARD_WEATHER("AAqxas5etOJDx", "1.0.5");
@@ -1,4 +1,4 @@
package cn.hezhaohui.common;
package cn.hezhaohui.weather.common;
public class WeatherConstant {
@@ -1,4 +1,4 @@
package cn.hezhaohui.entity;
package cn.hezhaohui.weather.entity;
import lombok.Builder;
import lombok.Data;
+27
View File
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.hezhaohui</groupId>
<artifactId>lark-panel</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>eureka</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
@@ -0,0 +1,13 @@
package cn.hezhaohui.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
+16
View File
@@ -0,0 +1,16 @@
server:
port: 8761
spring:
application:
name: eureka-server
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone:
http://${eureka.instance.hostname}:${server.port}/eureka/
+35
View File
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.hezhaohui</groupId>
<artifactId>lark-panel</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>gateway</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
@@ -0,0 +1,13 @@
package cn.hezhaohui.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
@@ -0,0 +1,36 @@
spring:
application:
name: api-gateway
cloud:
gateway:
routes:
# 路由到service-weather服务
- id: service-weather
uri: lb://service-weather
predicates:
- Path=/api/weather/**
filters:
- StripPrefix=2
# 路由到service-consumer服务
- id: service-consumer
uri: lb://service-consumer
predicates:
- Path=/api/consumer/**
filters:
- StripPrefix=2
# 全局过滤器配置
globalcors:
cors-configurations:
"[/**]":
allowedOriginPatterns: "*"
allowedMethods: "*"
allowedHeaders: "*"
server:
port: 8081
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
+2 -3
View File
@@ -18,9 +18,8 @@
</dependency>
<dependency>
<groupId>cn.hezhaohui</groupId>
<artifactId>weather</artifactId>
<version>1.0.0</version>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
@@ -1,16 +1,18 @@
package cn.hezhaohui.lark;
import cn.hezhaohui.lark.client.WeatherClient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.cloud.openfeign.EnableFeignClients;
import static cn.hezhaohui.lark.handler.EventListener.larkClientStart;
@SpringBootApplication
@ComponentScan(basePackages = "cn.hezhaohui")
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
@EnableFeignClients
public class LarkApplication {
public static void main(String[] args) {
SpringApplication.run(LarkApplication.class, args);
larkClientStart();
}
}
@@ -0,0 +1,13 @@
package cn.hezhaohui.lark.client;
import cn.hezhaohui.weather.entity.Weather;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(name = "service-weather")
public interface WeatherClient {
@GetMapping("get")
Weather getWeather();
}
@@ -1,6 +1,6 @@
package cn.hezhaohui.lark.handler;
import cn.hezhaohui.common.AppConstant;
import cn.hezhaohui.weather.common.AppConstant;
import com.lark.oapi.core.request.EventReq;
import com.lark.oapi.core.utils.Jsons;
import com.lark.oapi.event.CustomEventHandler;
@@ -1,7 +1,7 @@
package cn.hezhaohui.lark.util;
import cn.hezhaohui.common.AppConstant;
import cn.hezhaohui.common.CardEnum;
import cn.hezhaohui.weather.common.AppConstant;
import cn.hezhaohui.weather.common.CardEnum;
import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.google.gson.JsonParser;
+8
View File
@@ -0,0 +1,8 @@
spring:
application:
name: lark
server:
port: 8082
service-weather:
url: http://localhost:8081/api/weather
@@ -1,17 +1,17 @@
package cn.hezhaohui.lark.util;
import cn.hezhaohui.common.CardEnum;
import cn.hezhaohui.util.SpringContextUtil;
import cn.hezhaohui.util.WeatherUtil;
import cn.hezhaohui.lark.client.WeatherClient;
import cn.hezhaohui.weather.common.CardEnum;
import cn.hezhaohui.weather.entity.Weather;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class LarkUtilTest {
@Resource
private SpringContextUtil springContextUtil;
private WeatherClient weatherClient;
@Test
void sendText() throws Exception {
@@ -20,6 +20,7 @@ class LarkUtilTest {
@Test
void sendCard() throws Exception {
LarkUtil.sendCard(CardEnum.CARD_WEATHER, WeatherUtil.getWeather());
Weather weather = weatherClient.getWeather();
LarkUtil.sendCard(CardEnum.CARD_WEATHER, weather);
}
}
+10 -1
View File
@@ -16,6 +16,8 @@
<module>lark</module>
<module>weather</module>
<module>common</module>
<module>eureka</module>
<module>gateway</module>
</modules>
<!-- 依赖管理 -->
@@ -24,7 +26,14 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.2.2</version>
<version>3.3.13</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2023.0.6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
+17
View File
@@ -12,15 +12,32 @@
<artifactId>weather</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
@@ -1,11 +1,11 @@
package cn.hezhaohui;
package cn.hezhaohui.weather;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public class WeatherApplication {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
SpringApplication.run(WeatherApplication.class, args);
}
}
@@ -0,0 +1,15 @@
package cn.hezhaohui.weather.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping
public class HealthController {
@GetMapping("health")
public String health() {
return "Hello wonder!";
}
}
@@ -0,0 +1,17 @@
package cn.hezhaohui.weather.controller;
import cn.hezhaohui.weather.entity.Weather;
import cn.hezhaohui.weather.util.WeatherUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping
public class WeatherController {
@GetMapping("get")
public Weather getWeather() {
return WeatherUtil.getWeather();
}
}
@@ -1,6 +1,6 @@
package cn.hezhaohui.service;
package cn.hezhaohui.weather.service;
import cn.hezhaohui.common.AIConstant;
import cn.hezhaohui.weather.common.AIConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.stereotype.Service;
@@ -1,4 +1,4 @@
package cn.hezhaohui.util;
package cn.hezhaohui.weather.util;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
@@ -1,14 +1,14 @@
package cn.hezhaohui.util;
package cn.hezhaohui.weather.util;
import cn.hezhaohui.common.AIConstant;
import cn.hezhaohui.entity.Weather;
import cn.hezhaohui.service.ChatService;
import cn.hezhaohui.weather.common.AIConstant;
import cn.hezhaohui.weather.entity.Weather;
import cn.hezhaohui.weather.service.ChatService;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import static cn.hezhaohui.common.WeatherConstant.*;
import static cn.hezhaohui.weather.common.WeatherConstant.*;
@Slf4j
public class WeatherUtil {
@@ -1,4 +1,7 @@
spring:
application:
name: service-weather
ai:
openai:
api-key: sk-ihsqnxhnzmgsaojdkxomcmgdypdfpzojymfndfklgzfchlgz
@@ -7,3 +10,10 @@ spring:
options:
model: Pro/Qwen/Qwen2.5-7B-Instruct
temperature: 0.7
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
server:
port: 8083
@@ -1,11 +1,11 @@
package cn.hezhaohui.service;
package cn.hezhaohui.weather.service;
import cn.hezhaohui.common.AIConstant;
import cn.hezhaohui.weather.common.AIConstant;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
@SpringBootTest
class ChatServiceTest {
@Resource
@@ -1,10 +1,10 @@
package cn.hezhaohui.util;
package cn.hezhaohui.weather.util;
import cn.hezhaohui.entity.Weather;
import cn.hezhaohui.weather.entity.Weather;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
@SpringBootTest
class WeatherUtilTest {
@Test