获取代码请关注公众号 算法小生,回复cloud即可,不懂的欢迎加群沟通
我们删除src目录,如下图所示
我们新建模块api、common、client,并删除无用内容,结果如下
我们修改父模块build.gradle中内容如下
plugins {
id 'java'
id 'org.springframework.boot' version '3.1.0'
id 'io.spring.dependency-management' version '1.1.0'
}
archivesBaseName = "cloud-api"
bootJar.enabled = true
// 版本信息
ext {
set('springCloudVersion', "2022.0.3")
lombokVersion = '1.18.26'
}
// 通用配置
subprojects {
// 使用插件
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
// 项目默认名称与版本
group = 'online.shenjian'
version = '1.0-SNAPSHOT'
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
// 指定JDK版本
sourceCompatibility = '17'
jar.enabled = true
// 指定仓库地址
repositories {
mavenLocal()
mavenCentral()
}
dependencyManagement {
// 导入SpringBoot和SpringCloud依赖Bom
imports {
mavenBom org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
}
我们修改common中build.gradle内容如下
archivesBaseName = "common"
// 取消打包为 bootJar 可独立运行包,因为 common 只是一个普通的jar包
// 如果是需要独立运行的jar包,这个就改为true
bootJar.enabled = false
dependencies {
compileOnly 'org.projectlombok:lombok:1.18.28'
annotationProcessor 'org.projectlombok:lombok:1.18.28'
implementation 'com.alibaba:fastjson:2.0.35'
implementation 'cn.hutool:hutool-core:5.8.20'
implementation 'org.apache.commons:commons-lang3:3.12.0'
}
我们修改client中build.gradle内容如下
archivesBaseName = "client"
bootJar.enabled = false
dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.1.0'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-api:2.1.0'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok:1.18.28'
annotationProcessor 'org.projectlombok:lombok:1.18.28'
}
我们修改api中build.gradle内容如下
archivesBaseName = "api"
bootJar.enabled = true
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.apache.commons:commons-lang3:3.12.0'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.1.0'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-api:2.1.0'
implementation 'com.alibaba:fastjson:2.0.35'
implementation 'com.nimbusds:nimbus-jose-jwt:9.31'
implementation 'com.mysql:mysql-connector-j'
implementation 'com.alibaba:druid-spring-boot-3-starter:1.2.18'
implementation 'org.springframework.boot:spring-boot-starter-test'
compileOnly 'org.projectlombok:lombok:1.18.28'
annotationProcessor 'org.projectlombok:lombok:1.18.28'
implementation 'cn.hutool:hutool-core:5.8.20'
// 本地方法调用依赖
implementation 'net.java.dev.jna:jna:5.13.0'
// 引入本地的 common 模块
implementation project(':common')
implementation project(':client')
testImplementation 'junit:junit:4.13.1'
}
我们点击gradle刷新按钮,耐心等待相关文件下载
我们新建文件,结构如下
ResponseCode.java代码如下
public enum ResponseCode {
SUCCESS(200, "成功"),
FAIL(-1, "失败"),
UN_AUTHORIZED(401, "用户未授权,请联系管理员"),
TOKEN_EXPIRATION(402, "登录已过期,请退出后重新登录"),
LICENSE_EXPIRED(403, "授权已失效");
private Integer val;
private String des;
ResponseCode(Integer val, String des) {
this.val = val;
this.des = des;
}
public Integer val() {
return val;
}
public String des() {
return des;
}
}
ResponseVO.java代码如下
@Data
public class ResponseVo<T> implements Serializable {
private Integer code;
private String message;
private T data;
public static ResponseVo message(int code, String message) {
ResponseVo responseVo = new ResponseVo();
responseVo.setCode(code);
responseVo.setMessage(message);
return responseVo;
}
public static ResponseVo message(ResponseCode responseCode) {
ResponseVo responseVo = message(responseCode.val(), responseCode.des());
return responseVo;
}
public static ResponseVo message(int code, String message, Object data) {
ResponseVo responseVo = new ResponseVo();
responseVo.setCode(code);
responseVo.setMessage(message);
responseVo.setData(data);
return responseVo;
}
public static ResponseVo success() {
ResponseVo responseVo = new ResponseVo();
responseVo.setCode(ResponseCode.SUCCESS.val());
return responseVo;
}
public static ResponseVo success(Object data) {
ResponseVo responseVo = new ResponseVo();
responseVo.setCode(ResponseCode.SUCCESS.val());
responseVo.setData(data);
return responseVo;
}
public static ResponseVo error(String message) {
ResponseVo responseVo = new ResponseVo();
responseVo.setCode(ResponseCode.FAIL.val());
responseVo.setMessage(message);
return responseVo;
}
}
UserInfoDto.java代码如下
@Schema(description = "用户信息DTO")
@Data
public class UserInfoDto {
@Schema(description = "用户ID")
private String userId;
@Schema(description = "用户名")
private String username;
@Schema(description = "登录账号")
private String account;
@Schema(description = "密码")
private String password;
@Schema(description = "机构编码")
private String orgCode;
@Schema(description = "机构名称")
private String orgName;
@Schema(description = "角色ID")
private String roleId;
@Schema(description = "用户角色")
private String roleName;
}
CloudClient.java代码如下
@FeignClient(value = "cloud", contextId = "cloud")
@Component
public interface CloudClient {
@PostMapping(value = "/login", produces = MediaType.APPLICATION_JSON_VALUE)
@Operation(summary = "登录", tags = "用户管理")
ResponseVo login(@RequestBody UserInfoDto userInfoDto);
}
新建CloudController.java类,代码如下
@RestController
public class CloudController implements CloudClient {
@Override
public ResponseVo login(UserInfoDto userInfoDto) {
return ResponseVo.success("登录成功");
}
}
新建CloudApplication.java类,代码如下
@SpringBootApplication
public class CloudApplication {
public static void main(String[] args) {
SpringApplication.run(CloudApplication.class, args);
}
}
新建bootstrap.yml,代码如下
spring:
application:
name: cloud
profiles:
active: dev
我们点击CloudApplication.java中run按钮,然后用apifox工具进行请求,success