mirror of
https://gitee.com/yudaocode/SpringBoot-Labs.git
synced 2026-09-06 15:27:23 +08:00
增加 spring cloud alibaba dubbo 服务调用的示例,参数校验
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
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">
|
||||
<parent>
|
||||
<artifactId>labx-07-sca-dubbo-demo01</artifactId>
|
||||
<artifactId>labx-07-sca-dubbo-demo02</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
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">
|
||||
<parent>
|
||||
<artifactId>labx-07-sca-dubbo-demo01</artifactId>
|
||||
<artifactId>labx-07-sca-dubbo-demo02</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
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">
|
||||
<parent>
|
||||
<artifactId>labx-07-sca-dubbo-demo01</artifactId>
|
||||
<artifactId>labx-07-sca-dubbo-demo02</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
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">
|
||||
<parent>
|
||||
<artifactId>labx-07-sca-dubbo-demo01</artifactId>
|
||||
<artifactId>labx-07-sca-dubbo-demo02</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
<?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">
|
||||
<parent>
|
||||
<artifactId>labx-07-sca-dubbo-demo03-validation</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>labx-07-sca-dubbo-demo03-api</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<!-- 参数校验相关依赖 -->
|
||||
<dependency>
|
||||
<groupId>javax.validation</groupId>
|
||||
<artifactId>validation-api</artifactId> <!-- JSR 参数校验规范 API -->
|
||||
<version>2.0.1.Final</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate.validator</groupId>
|
||||
<artifactId>hibernate-validator</artifactId> <!-- JSR 参数校验规范实现,我们使用 hibernate-validator -->
|
||||
<version>6.0.18.Final</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish</groupId>
|
||||
<artifactId>javax.el</artifactId> <!-- 可能涉及到 EL 表达,所以引入,否则 hibernate-validator 在初始化会报错 -->
|
||||
<version>3.0.1-b11</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,32 @@
|
||||
package cn.iocoder.springcloudalibaba.labx7.api;
|
||||
|
||||
import cn.iocoder.springcloudalibaba.labx7.dto.UserAddDTO;
|
||||
import cn.iocoder.springcloudalibaba.labx7.dto.UserDTO;
|
||||
|
||||
import javax.validation.ConstraintViolationException;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
/**
|
||||
* 用户服务 RPC Service 接口
|
||||
*/
|
||||
public interface UserService {
|
||||
|
||||
/**
|
||||
* 根据指定用户编号,获得用户信息
|
||||
*
|
||||
* @param id 用户编号
|
||||
* @return 用户信息
|
||||
*/
|
||||
UserDTO get(@NotNull(message = "用户编号不能为空") Integer id)
|
||||
throws ConstraintViolationException;;
|
||||
|
||||
/**
|
||||
* 添加新用户,返回新添加的用户编号
|
||||
*
|
||||
* @param addDTO 添加的用户信息
|
||||
* @return 用户编号
|
||||
*/
|
||||
Integer add(UserAddDTO addDTO)
|
||||
throws ConstraintViolationException;;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package cn.iocoder.springcloudalibaba.labx7.dto;
|
||||
|
||||
import org.hibernate.validator.constraints.Length;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 用户添加 DTO
|
||||
*/
|
||||
public class UserAddDTO implements Serializable {
|
||||
|
||||
/**
|
||||
* 昵称
|
||||
*/
|
||||
@NotEmpty(message = "昵称不能为空")
|
||||
@Length(min = 5, max = 16, message = "账号长度为 5-16 位")
|
||||
private String name;
|
||||
/**
|
||||
* 性别
|
||||
*/
|
||||
@NotNull(message = "性别不能为空")
|
||||
private Integer gender;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public UserAddDTO setName(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getGender() {
|
||||
return gender;
|
||||
}
|
||||
|
||||
public UserAddDTO setGender(Integer gender) {
|
||||
this.gender = gender;
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package cn.iocoder.springcloudalibaba.labx7.dto;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 用户信息 DTO
|
||||
*/
|
||||
public class UserDTO implements Serializable {
|
||||
|
||||
/**
|
||||
* 用户编号
|
||||
*/
|
||||
private Integer id;
|
||||
/**
|
||||
* 昵称
|
||||
*/
|
||||
private String name;
|
||||
/**
|
||||
* 性别
|
||||
*/
|
||||
private Integer gender;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public UserDTO setId(Integer id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public UserDTO setName(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getGender() {
|
||||
return gender;
|
||||
}
|
||||
|
||||
public UserDTO setGender(Integer gender) {
|
||||
this.gender = gender;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
<?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">
|
||||
<parent>
|
||||
<artifactId>labx-07-sca-dubbo-demo03-validation</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>labx-07-sca-dubbo-demo03-consumer</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
|
||||
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
|
||||
<spring.cloud.alibaba.version>2.2.0.RELEASE</spring.cloud.alibaba.version>
|
||||
</properties>
|
||||
|
||||
<!--
|
||||
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
|
||||
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
|
||||
-->
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring.cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
|
||||
<version>${spring.cloud.alibaba.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<!-- 引入定义的 Dubbo API 接口 -->
|
||||
<dependency>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<artifactId>labx-07-sca-dubbo-demo03-api</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 引入 SpringMVC 相关依赖,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 引入 Spring Cloud Alibaba Nacos Discovery 相关依赖,将 Nacos 作为注册中心,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 引入 Spring Cloud Alibaba Dubbo 相关依赖,实现呢 Dubbo 进行远程调用,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-dubbo</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,13 @@
|
||||
package cn.iocoder.springcloudalibaba.labx7.consumerdemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ConsumerApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ConsumerApplication.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package cn.iocoder.springcloudalibaba.labx7.consumerdemo.controller;
|
||||
|
||||
import cn.iocoder.springcloudalibaba.labx7.api.UserService;
|
||||
import cn.iocoder.springcloudalibaba.labx7.dto.UserAddDTO;
|
||||
import cn.iocoder.springcloudalibaba.labx7.dto.UserDTO;
|
||||
import org.apache.dubbo.config.annotation.Reference;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/user")
|
||||
public class UserController {
|
||||
|
||||
@Reference(protocol = "dubbo", version = "1.0.0", validation = "false")
|
||||
private UserService userService;
|
||||
|
||||
@GetMapping("/get")
|
||||
public UserDTO get(@RequestParam("id") Integer id) {
|
||||
return userService.get(id);
|
||||
}
|
||||
|
||||
@PostMapping("/add")
|
||||
public Integer add(UserAddDTO addDTO) {
|
||||
return userService.add(addDTO);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
spring:
|
||||
application:
|
||||
name: demo-consumer
|
||||
cloud:
|
||||
# Nacos 作为注册中心的配置项
|
||||
nacos:
|
||||
discovery:
|
||||
server-addr: 127.0.0.1:8848
|
||||
|
||||
# Dubbo 配置项,对应 DubboConfigurationProperties 类
|
||||
dubbo:
|
||||
# Dubbo 服务注册中心配置,对应 RegistryConfig 类
|
||||
registry:
|
||||
address: spring-cloud://127.0.0.1:8848 # 指定 Dubbo 服务注册中心的地址
|
||||
# Spring Cloud Alibaba Dubbo 专属配置项,对应 DubboCloudProperties 类
|
||||
cloud:
|
||||
subscribed-services: demo-provider # 设置订阅的应用列表,默认为 * 订阅所有应用。
|
||||
@@ -0,0 +1,73 @@
|
||||
<?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">
|
||||
<parent>
|
||||
<artifactId>labx-07-sca-dubbo-demo03-validation</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>labx-07-sca-dubbo-demo03-provider</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
|
||||
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
|
||||
<spring.cloud.alibaba.version>2.2.0.RELEASE</spring.cloud.alibaba.version>
|
||||
</properties>
|
||||
|
||||
<!--
|
||||
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
|
||||
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
|
||||
-->
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring.cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
|
||||
<version>${spring.cloud.alibaba.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<!-- 引入定义的 Dubbo API 接口 -->
|
||||
<dependency>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<artifactId>labx-07-sca-dubbo-demo03-api</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 引入 Spring Cloud Alibaba Nacos Discovery 相关依赖,将 Nacos 作为注册中心,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 引入 Spring Cloud Alibaba Dubbo 相关依赖,实现呢 Dubbo 进行远程调用,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-dubbo</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,13 @@
|
||||
package cn.iocoder.springcloudalibaba.labx7.providerdemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ProviderApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ProviderApplication.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package cn.iocoder.springcloudalibaba.labx7.providerdemo.service;
|
||||
|
||||
import cn.iocoder.springcloudalibaba.labx7.api.UserService;
|
||||
import cn.iocoder.springcloudalibaba.labx7.dto.UserAddDTO;
|
||||
import cn.iocoder.springcloudalibaba.labx7.dto.UserDTO;
|
||||
|
||||
@org.apache.dubbo.config.annotation.Service(protocol = "dubbo", version = "1.0.0", validation = "true")
|
||||
public class UserServiceImpl implements UserService {
|
||||
|
||||
@Override
|
||||
public UserDTO get(Integer id) {
|
||||
return new UserDTO().setId(id)
|
||||
.setName("没有昵称:" + id)
|
||||
.setGender(id % 2 + 1); // 1 - 男;2 - 女
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer add(UserAddDTO addDTO) {
|
||||
return (int) (System.currentTimeMillis() / 1000); // 嘿嘿,随便返回一个 id
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
spring:
|
||||
application:
|
||||
name: demo-provider
|
||||
cloud:
|
||||
# Nacos 作为注册中心的配置项
|
||||
nacos:
|
||||
discovery:
|
||||
server-addr: 127.0.0.1:8848 # Nacos 服务器地址
|
||||
|
||||
# Dubbo 配置项,对应 DubboConfigurationProperties 类
|
||||
dubbo:
|
||||
scan:
|
||||
base-packages: cn.iocoder.springcloudalibaba.labx7.providerdemo.service # 指定 Dubbo 服务实现类的扫描基准包
|
||||
# Dubbo 服务暴露的协议配置,对应 ProtocolConfig Map
|
||||
protocols:
|
||||
dubbo:
|
||||
name: dubbo # 协议名称
|
||||
port: -1 # 协议端口,-1 表示自增端口,从 20880 开始
|
||||
# Dubbo 服务注册中心配置,对应 RegistryConfig 类
|
||||
registry:
|
||||
address: spring-cloud://127.0.0.1:8848 # 指定 Dubbo 服务注册中心的地址
|
||||
# Spring Cloud Alibaba Dubbo 专属配置项,对应 DubboCloudProperties 类
|
||||
cloud:
|
||||
subscribed-services: '' # 设置订阅的应用列表,默认为 * 订阅所有应用。
|
||||
20
labx-07/labx-07-sca-dubbo-demo03-validation/pom.xml
Normal file
20
labx-07/labx-07-sca-dubbo-demo03-validation/pom.xml
Normal file
@@ -0,0 +1,20 @@
|
||||
<?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">
|
||||
<parent>
|
||||
<artifactId>labx-07</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>labx-07-sca-dubbo-demo03-validation</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>labx-07-sca-dubbo-demo03-api</module>
|
||||
<module>labx-07-sca-dubbo-demo03-provider</module>
|
||||
<module>labx-07-sca-dubbo-demo03-consumer</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
@@ -14,6 +14,7 @@
|
||||
<modules>
|
||||
<module>labx-07-sca-dubbo-demo01</module>
|
||||
<module>labx-07-sca-dubbo-demo02</module>
|
||||
<module>labx-07-sca-dubbo-demo03-validation</module>
|
||||
</modules>
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user