- 构建多模块应用
- 1.构建父模块
- 2. 清理父模块
- 3.父模块pom管理依赖
- 注意点
- 完整依赖
- 4.构建子模块(commons)
- 5.pom依赖导入
- 引用父模块
- 完整子pom
- 远程调用服务
- 1. 配置声明,注册RestTemplate到spring容器中
- 2.编写controller进行调用
- 声明服务地址
- 客户端调用
- 完整代码
- 项目重构示例
- 目录解释
- 1.编写common-api模块
- 目录
- pom依赖导入
- 将实体类,工具类抽取出来
- user实体类
- 统一结果处理ResultJSONUtil
- 使用maven清理项目
- 2.编写commons服务模块
- 目录
- pom
- yaml配置
- 直接编写业务逻辑
- UserMapper
- UserService
- UserServiceImpl
- UserController
- 3.构建客户端client
- 目录
- pom
- yaml
- ApplicationContextConfig
- ClientController
在上一篇文章中我们进行了quickstart的演示,这篇文章中我们进一步,将所有项目使用spring initializr进行构建
1.构建父模块 2. 清理父模块只要留下.idea和pom.xml即可!
3.父模块pom管理依赖 注意点需要加上
//设置打包为pom完整依赖pom //用于管理下面的子模块//使用properties对版本进行统一管理 client commons 1.8 8.0.17 2.6.7 3.5.1 5.8.2 1.2.9 2021.0.1 2021.0.1.0 1.18.22
4.构建子模块(commons)4.0.0 org.springframework.boot spring-boot-starter-parent 2.6.7 com.example springcloud 0.0.1-SNAPSHOT springcloud Demo project for Spring Boot pom client commons 1.8 8.0.17 2.6.7 3.5.1 5.8.2 1.2.9 2021.0.1 2021.0.1.0 1.18.22 org.springframework.boot spring-boot-starter-validation ${spring.boot.version} org.springframework.boot spring-boot-starter-web ${spring.boot.version} com.baomidou mybatis-plus-boot-starter ${mybatis.plus.version} com.alibaba druid-spring-boot-starter ${druid.version} org.springframework.cloud spring-cloud-dependencies ${cloud.version} pom import com.alibaba.cloud spring-cloud-alibaba-dependencies ${cloud.alibaba.version} pom import mybatis.version}--> com.alibaba druid ${druid.version} org.springframework.boot spring-boot-devtools ${spring.boot.version} runtime true mysql mysql-connector-java ${mysql.version} org.springframework.boot spring-boot-configuration-processor ${spring.boot.version} true org.projectlombok lombok ${lombok.version} true org.springframework.boot spring-boot-starter-test ${spring.boot.version} test org.springframework spring-web 5.3.14 org.junit.jupiter junit-jupiter-api ${junit.version} test org.springframework.boot spring-boot-test ${spring.boot.version} test org.springframework.boot spring-boot-maven-plugin
同父模块一样构建,但是这里只要删除mvn有关的文件即可,如.mvn,mvn.cmd,mvn,mvnw
com.example springcloud 0.0.1-SNAPSHOT ../pom.xml
大家都说父模块写了版本子模块就不用写了,这确实,但是不知道时IDEA的版本问题,我的IDEA2021不会帮我直接下载依赖到库里
当然我发现了解决方法
我们还是以带有
如我们一开始(这里带有version标签)
org.springframework.boot spring-boot-starter-validation ${spring.boot.version}
等到依赖下载到库里之后
我们再把version标签去掉
完整子pomorg.springframework.boot spring-boot-starter-validation
远程调用服务4.0.0 com.example springcloud 0.0.1-SNAPSHOT ../pom.xml com.example commons 0.0.1-SNAPSHOT commons Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-validation org.springframework.boot spring-boot-starter-web com.baomidou mybatis-plus-boot-starter com.alibaba druid-spring-boot-starter org.springframework.cloud spring-cloud-dependencies pom import com.alibaba.cloud spring-cloud-alibaba-dependencies cloud.alibaba.version}-->pom import com.alibaba druid druid.version}-->org.springframework.boot spring-boot-devtools runtime true mysql mysql-connector-java mysql.version}-->org.springframework.boot spring-boot-configuration-processor true org.projectlombok lombok lombok.version}-->true org.springframework.boot spring-boot-starter-test test org.springframework spring-web org.junit.jupiter junit-jupiter-api junit.version}-->test org.springframework.boot spring-boot-test test org.springframework.boot spring-boot-maven-plugin
RestTemplate提供了多种便捷访问远程Http服务的方法,
是一种简单便捷的访问restful服务模板类,是Spring提供的用于访问Rest服务的客户端模板工具集
使用restTemplate访问restful接口非常的简单
(url, requestMap, ResponseBean.class)
这三个参数分别代表REST请求地址、请求参数、HTTP响应转换被转换成的对象类型。
package com.example.client.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class ApplicationContextConfig {
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
2.编写controller进行调用
声明服务地址
客户端调用
完整代码
package com.example.client.contoller;
import com.example.commonapi.util.ResultJSONUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@RestController
public class ClientController {
private static final String COMMONS_URL = "http://localhost:8888";
private static final String GETUSERINFO_URL = "/user/info";
@Resource
private RestTemplate restTemplate;
@GetMapping("/client/userinfo")
public ResultJSONUtil useCommonsGetUserInfo(){
return restTemplate.getForObject(COMMONS_URL+GETUSERINFO_URL,ResultJSONUtil.class);
}
}
项目重构示例
接下来我们对项目进行重构,指明服务端,客户端,将模块的服务区分开来
目录解释我们看到有一个父模块(springcloud)管理下面的子模块,声明pom中依赖的版本
其中client表示客户端(进行调用其他服务)
commons表示常见服务(提供服务接口供client进行调用)
common-api表示项目的常用类,我们把如常用实体类,工具类放置在其中供全局使用,所以我们要把common-api进行打包形成自己的依赖
将实体类,工具类抽取出来 user实体类4.0.0 com.example springcloud 0.0.1-SNAPSHOT ../pom.xml com.example common-api 0.0.1-SNAPSHOT common-api Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test com.baomidou mybatis-plus-boot-starter org.projectlombok lombok org.springframework.boot spring-boot-maven-plugin
package com.example.commonapi.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("user")
public class User {
private Long id;
private String username;
@TableField(value = "nickname")
private String nickName;
}
统一结果处理ResultJSONUtil
package com.example.commonapi.util;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResultJSONUtil {
private Integer code;
private String message;
private Object data;
public ResultJSONUtil(Integer code, String msg) {
this(code, msg, null);
}
public static ResultJSONUtil success(Integer code,String msg,Object data){
ResultJSONUtil resultJSONUtil = new ResultJSONUtil();
resultJSONUtil.setCode(code);
resultJSONUtil.setMessage(msg);
resultJSONUtil.setData(data);
return resultJSONUtil;
}
public static ResultJSONUtil success(String msg,Object data){
return ResultJSONUtil.success(200,msg,data);
}
public static ResultJSONUtil fail(Integer code,String msg,Object data){
ResultJSONUtil resultJSONUtil = new ResultJSONUtil();
resultJSONUtil.setCode(code);
resultJSONUtil.setMessage(msg);
resultJSONUtil.setData(data);
return resultJSONUtil;
}
public static ResultJSONUtil fail(Integer code,String msg){
return ResultJSONUtil.success(code,msg,null);
}
}
使用maven清理项目
先切换为跳过测试模式
然后在生命周期中进行clean
完成后进行install
这样我们就得到了自己的一个依赖
其中要引入自己的common-api的依赖
yaml配置4.0.0 com.example springcloud 0.0.1-SNAPSHOT ../pom.xml com.example commons 0.0.1-SNAPSHOT commons Demo project for Spring Boot 1.8 com.example common-api ${project.version} org.springframework.boot spring-boot-starter-validation org.springframework.boot spring-boot-starter-web com.baomidou mybatis-plus-boot-starter com.alibaba druid-spring-boot-starter org.springframework.cloud spring-cloud-dependencies 2021.0.1 pom import com.alibaba.cloud spring-cloud-alibaba-dependencies ${cloud.alibaba.version} pom import com.alibaba druid druid.version}-->org.springframework.boot spring-boot-devtools runtime true mysql mysql-connector-java mysql.version}-->org.springframework.boot spring-boot-configuration-processor true org.projectlombok lombok lombok.version}-->true org.springframework.boot spring-boot-starter-test test org.springframework spring-web org.junit.jupiter junit-jupiter-api junit.version}-->test org.springframework.boot spring-boot-test test com.example common-api 0.0.1-SNAPSHOT compile org.springframework.boot spring-boot-maven-plugin
配置端口号,名字,数据库连接即可
server:
port: 8888
spring:
application:
name: commons
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/routerboot?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULL&allowPublicKeyRetrieval=true
username: root
password: syf20020816
直接编写业务逻辑
UserMapper
package com.example.commons.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.example.commonapi.entity.User; import org.apache.ibatis.annotations.Mapper; @Mapper public interface UserMapper extends BaseMapperUserService{ }
package com.example.commons.service; import com.baomidou.mybatisplus.extension.service.IService; import com.example.commonapi.entity.User; import com.example.commonapi.util.ResultJSONUtil; public interface UserService extends IServiceUserServiceImpl{ ResultJSONUtil getUserInfo(); }
package com.example.commons.service.impl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.example.commonapi.entity.User; import com.example.commonapi.util.ResultJSONUtil; import com.example.commons.mapper.UserMapper; import com.example.commons.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserServiceImpl extends ServiceImplUserControllerimplements UserService { @Autowired private UserMapper userMapper; @Override public ResultJSONUtil getUserInfo() { User user = userMapper.selectById(1508100589987471362L); if(user!=null){ return ResultJSONUtil.success("success",user); }else { return ResultJSONUtil.fail(501,"fail"); } } }
package com.example.commons.controller;
import com.example.commonapi.util.ResultJSONUtil;
import com.example.commons.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/info")
public ResultJSONUtil getUserInfo(){
return userService.getUserInfo();
}
}
3.构建客户端client
目录
明确client只是充当客户端的角色去调用其他模块的服务,所以无需有service,mapper,entity,util等那些东西
yaml4.0.0 com.example springcloud 0.0.1-SNAPSHOT com.example client 0.0.1-SNAPSHOT client Demo project for Spring Boot 1.8 com.example common-api 0.0.1-SNAPSHOT mysql mysql-connector-java org.springframework.boot spring-boot-starter-validation 2.6.7 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-devtools runtime true org.springframework.boot spring-boot-configuration-processor true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test org.springframework spring-web org.junit.jupiter junit-jupiter-api test org.springframework.boot spring-boot-test test org.springframework.boot spring-boot-maven-plugin
这里解释一下为什么要有数据库的配置,这也是我没有处理好的地方,因为我在common-api中用到了mybatis-plus的注解,所以必须要有数据库配置才能没有异常
server:
port: 9000
spring:
application:
name: client
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/routerboot?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULL&allowPublicKeyRetrieval=true
username: root
password: syf20020816
ApplicationContextConfig
用来注册RestTemplate
package com.example.client.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class ApplicationContextConfig {
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
ClientController
package com.example.client.contoller;
import com.example.commonapi.util.ResultJSONUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@RestController
public class ClientController {
private static final String COMMONS_URL = "http://localhost:8888";
private static final String GETUSERINFO_URL = "/user/info";
@Resource
private RestTemplate restTemplate;
@GetMapping("/client/userinfo")
public ResultJSONUtil useCommonsGetUserInfo(){
return restTemplate.getForObject(COMMONS_URL+GETUSERINFO_URL,ResultJSONUtil.class);
}
}



