完成学习nacos请求转发
This commit is contained in:
46
order-service/pom.xml
Normal file
46
order-service/pom.xml
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
<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.mayiming</groupId>
|
||||||
|
<artifactId>javamemories-parent</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>order-service</artifactId>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<name>order-service</name>
|
||||||
|
<url>http://maven.apache.org</url>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 可选:Nacos 配置管理(从 Nacos 读取配置文件) -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- 负载均衡(Feign 内置,但显式引入更清晰) -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
||||||
19
order-service/src/main/java/cn/mayiming/App.java
Normal file
19
order-service/src/main/java/cn/mayiming/App.java
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package cn.mayiming;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hello world!
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@SpringBootApplication
|
||||||
|
@EnableFeignClients
|
||||||
|
public class App
|
||||||
|
{
|
||||||
|
public static void main( String[] args )
|
||||||
|
{
|
||||||
|
SpringApplication.run(App.class, args);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package cn.mayiming.Controller;
|
||||||
|
|
||||||
|
|
||||||
|
import cn.mayiming.Service.OrderService;
|
||||||
|
import cn.mayiming.entity.User;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class OrderController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private OrderService orderService;
|
||||||
|
|
||||||
|
@PostMapping("/order")
|
||||||
|
public User getOrder(@RequestBody User user){
|
||||||
|
return orderService.SearchUserbyname(user);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package cn.mayiming.Service;
|
||||||
|
|
||||||
|
import cn.mayiming.entity.User;
|
||||||
|
import cn.mayiming.feign.UserFeignClient;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class OrderService {
|
||||||
|
@Autowired
|
||||||
|
private UserFeignClient userFeignClient;
|
||||||
|
|
||||||
|
public User SearchUserbyname (User user) {
|
||||||
|
return userFeignClient.selectByUsername(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
22
order-service/src/main/java/cn/mayiming/entity/User.java
Normal file
22
order-service/src/main/java/cn/mayiming/entity/User.java
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package cn.mayiming.entity;
|
||||||
|
|
||||||
|
public class User {
|
||||||
|
public String getUsername() {
|
||||||
|
return username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUsername(String username) {
|
||||||
|
this.username = username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPassword() {
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPassword(String password) {
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
|
||||||
|
String username;
|
||||||
|
String password;
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package cn.mayiming.feign;
|
||||||
|
|
||||||
|
import cn.mayiming.entity.User;
|
||||||
|
import feign.Param;
|
||||||
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
|
||||||
|
@FeignClient(name = "user-service")
|
||||||
|
public interface UserFeignClient {
|
||||||
|
@PostMapping("/user")
|
||||||
|
User selectByUsername(User user);
|
||||||
|
}
|
||||||
21
order-service/src/main/resources/application.yml
Normal file
21
order-service/src/main/resources/application.yml
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
server:
|
||||||
|
port: 9092
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: order-service
|
||||||
|
cloud:
|
||||||
|
nacos:
|
||||||
|
# 服务注册发现配置
|
||||||
|
discovery:
|
||||||
|
server-addr: localhost:8848 # Nacos 服务地址(默认端口 8848)
|
||||||
|
namespace: public # 命名空间(默认 public,自定义需先在 Nacos 控制台创建)
|
||||||
|
group: DEFAULT_GROUP # 分组(默认 DEFAULT_GROUP)
|
||||||
|
service: order-service # 注册到 Nacos 的服务名(建议和子项目 artifactId 一致)
|
||||||
|
# 配置管理配置(如果引入了 config 依赖才需要)
|
||||||
|
config:
|
||||||
|
server-addr: localhost:8848 # 和 discovery 一致
|
||||||
|
file-extension: yaml
|
||||||
|
namespace: public
|
||||||
|
group: DEFAULT_GROUP
|
||||||
|
config:
|
||||||
|
import: nacos:${spring.application.name}.${spring.cloud.nacos.config.file-extension}?server-addr=${spring.cloud.nacos.config.server-addr}
|
||||||
38
order-service/src/test/java/cn/mayiming/AppTest.java
Normal file
38
order-service/src/test/java/cn/mayiming/AppTest.java
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package cn.mayiming;
|
||||||
|
|
||||||
|
import junit.framework.Test;
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
import junit.framework.TestSuite;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unit test for simple App.
|
||||||
|
*/
|
||||||
|
public class AppTest
|
||||||
|
extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Create the test case
|
||||||
|
*
|
||||||
|
* @param testName name of the test case
|
||||||
|
*/
|
||||||
|
public AppTest( String testName )
|
||||||
|
{
|
||||||
|
super( testName );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the suite of tests being tested
|
||||||
|
*/
|
||||||
|
public static Test suite()
|
||||||
|
{
|
||||||
|
return new TestSuite( AppTest.class );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rigourous Test :-)
|
||||||
|
*/
|
||||||
|
public void testApp()
|
||||||
|
{
|
||||||
|
assertTrue( true );
|
||||||
|
}
|
||||||
|
}
|
||||||
1
pom.xml
1
pom.xml
@@ -90,6 +90,7 @@
|
|||||||
<modules>
|
<modules>
|
||||||
<module>javamemories-common</module>
|
<module>javamemories-common</module>
|
||||||
<module>user-service</module>
|
<module>user-service</module>
|
||||||
|
<module>order-service</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<!-- 编译插件(确保 Java 版本兼容) -->
|
<!-- 编译插件(确保 Java 版本兼容) -->
|
||||||
|
|||||||
@@ -36,6 +36,18 @@
|
|||||||
<groupId>com.alibaba.cloud</groupId>
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- MyBatis 版本管理 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mybatis.spring.boot</groupId>
|
||||||
|
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- MySQL 驱动(显式绑定版本) -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-j</artifactId>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<!-- 构建配置:确保编译和打包正常 -->
|
<!-- 构建配置:确保编译和打包正常 -->
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package cn.mayiming;
|
package cn.mayiming;
|
||||||
|
|
||||||
|
|
||||||
|
import org.mybatis.spring.annotation.MapperScan;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
@@ -8,7 +9,9 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|||||||
* Hello world!
|
* Hello world!
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
|
@MapperScan("cn.mayiming.Mapper")
|
||||||
public class App
|
public class App
|
||||||
{
|
{
|
||||||
public static void main( String[] args )
|
public static void main( String[] args )
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package cn.mayiming.Controller;
|
||||||
|
|
||||||
|
import cn.mayiming.Mapper.UserMapper;
|
||||||
|
import cn.mayiming.Service.UserService;
|
||||||
|
import cn.mayiming.entity.User;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class UserController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserMapper userMapper;
|
||||||
|
|
||||||
|
@PostMapping("/user")
|
||||||
|
public User getUser(@RequestBody User user) {
|
||||||
|
return userMapper.selectByUsername(user.getUsername());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
package cn.mayiming.Mapper;
|
||||||
|
|
||||||
|
import cn.mayiming.entity.User;
|
||||||
|
import org.apache.ibatis.annotations.*;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface UserMapper {
|
||||||
|
/**
|
||||||
|
* 根据ID查询用户
|
||||||
|
* @param id 用户ID
|
||||||
|
* @return 用户信息
|
||||||
|
*/
|
||||||
|
@Select("SELECT id, username, password, nickname FROM user WHERE id = #{id}")
|
||||||
|
User selectById(@Param("id") Integer id); // 注意:id类型改为Integer(对应表的int)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据用户名查询用户
|
||||||
|
* @param username 用户名
|
||||||
|
* @return 用户信息
|
||||||
|
*/
|
||||||
|
@Select("SELECT id, username, password, nickname FROM user WHERE username = #{username}")
|
||||||
|
User selectByUsername(@Param("username") String username);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 新增用户(自动回填自增ID)
|
||||||
|
* @param user 用户对象
|
||||||
|
* @return 影响行数
|
||||||
|
*/
|
||||||
|
@Insert("INSERT INTO user (username, password, nickname) " +
|
||||||
|
"VALUES (#{username}, #{password}, #{nickname})")
|
||||||
|
// 适配int类型自增主键,keyProperty对应实体类的id属性
|
||||||
|
@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
|
||||||
|
int insert(User user);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新用户信息(全字段更新)
|
||||||
|
* @param user 用户对象(含要更新的字段)
|
||||||
|
* @return 影响行数
|
||||||
|
*/
|
||||||
|
@Update("UPDATE user SET " +
|
||||||
|
"username = #{username}, " +
|
||||||
|
"password = #{password}, " +
|
||||||
|
"nickname = #{nickname} " +
|
||||||
|
"WHERE id = #{id}")
|
||||||
|
int updateById(User user);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除用户
|
||||||
|
* @param id 用户ID
|
||||||
|
* @return 影响行数
|
||||||
|
*/
|
||||||
|
@Delete("DELETE FROM user WHERE id = #{id}")
|
||||||
|
int deleteById(@Param("id") Integer id);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询所有用户
|
||||||
|
* @return 用户列表
|
||||||
|
*/
|
||||||
|
@Select("SELECT id, username, password, nickname FROM user")
|
||||||
|
List<User> selectAll();
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package cn.mayiming.Service;
|
||||||
|
|
||||||
|
import cn.mayiming.Mapper.UserMapper;
|
||||||
|
import cn.mayiming.entity.User;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class UserService {
|
||||||
|
@Autowired
|
||||||
|
private UserMapper userMapper;
|
||||||
|
|
||||||
|
public int addUser() {
|
||||||
|
User user = new User();
|
||||||
|
user.setUsername("test01");
|
||||||
|
user.setPassword("123456");
|
||||||
|
// 新增后id会自动回填
|
||||||
|
int rows = userMapper.insert(user);
|
||||||
|
return rows;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 根据用户名查询
|
||||||
|
public User getUserByUsername(String username) {
|
||||||
|
return userMapper.selectByUsername(username);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 查询所有用户
|
||||||
|
public List<User> getAllUsers() {
|
||||||
|
return userMapper.selectAll();
|
||||||
|
}
|
||||||
|
}
|
||||||
22
user-service/src/main/java/cn/mayiming/entity/User.java
Normal file
22
user-service/src/main/java/cn/mayiming/entity/User.java
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package cn.mayiming.entity;
|
||||||
|
|
||||||
|
public class User {
|
||||||
|
public String getUsername() {
|
||||||
|
return username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUsername(String username) {
|
||||||
|
this.username = username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPassword() {
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPassword(String password) {
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
|
||||||
|
String username;
|
||||||
|
String password;
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
server:
|
server:
|
||||||
port: 9090
|
port: 9091
|
||||||
spring:
|
spring:
|
||||||
application:
|
application:
|
||||||
name: user-service
|
name: user-service
|
||||||
@@ -14,8 +14,27 @@ spring:
|
|||||||
# 配置管理配置(如果引入了 config 依赖才需要)
|
# 配置管理配置(如果引入了 config 依赖才需要)
|
||||||
config:
|
config:
|
||||||
server-addr: localhost:8848 # 和 discovery 一致
|
server-addr: localhost:8848 # 和 discovery 一致
|
||||||
file-extension: yaml # 配置文件格式(yaml/yml/properties)
|
file-extension: yaml
|
||||||
namespace: public
|
namespace: public
|
||||||
group: DEFAULT_GROUP
|
group: DEFAULT_GROUP
|
||||||
config:
|
config:
|
||||||
import: nacos:${spring.application.name}.${spring.cloud.nacos.config.file-extension}?server-addr=${spring.cloud.nacos.config.server-addr}
|
import: nacos:${spring.application.name}.${spring.cloud.nacos.config.file-extension}?server-addr=${spring.cloud.nacos.config.server-addr}
|
||||||
|
datasource:
|
||||||
|
# 数据库驱动类(MySQL 8.x 用 com.mysql.cj.jdbc.Driver,5.x 用 com.mysql.jdbc.Driver)
|
||||||
|
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||||
|
# 数据库连接 URL(替换为你的数据库地址、端口、库名,如 user_db)
|
||||||
|
url: jdbc:mysql://rm-f8z6oc5a03331500p8o.mysql.rds.aliyuncs.com:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
|
||||||
|
# 数据库用户名(默认 root,根据实际情况修改)
|
||||||
|
username: root
|
||||||
|
# 数据库密码(替换为你的 MySQL 密码)
|
||||||
|
password: Root123456
|
||||||
|
# 可选:连接池配置(推荐使用 HikariCP,Spring Boot 2.x 默认)
|
||||||
|
hikari:
|
||||||
|
# 连接池最大连接数
|
||||||
|
maximum-pool-size: 10
|
||||||
|
# 连接池最小空闲连接数
|
||||||
|
minimum-idle: 2
|
||||||
|
# 连接超时时间(毫秒)
|
||||||
|
connection-timeout: 30000
|
||||||
|
# 连接最大存活时间(毫秒)
|
||||||
|
max-lifetime: 1800000
|
||||||
Reference in New Issue
Block a user