rpc(二、springboot2.3.2+dubbo2.7.7)
结构
服务端,消费端,接口工程
zookeeper
dubbo建议版本3.4.X
父项目
创建父项目,删除项目下的src文件
pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.bsoft</groupId> <artifactId>familydoctor</artifactId> <version>0.0.1-SNAPSHOT</version> <name>familydoctor</name> <description>Demo project for Spring Boot</description> <packaging>pom</packaging> <properties> <spring-boot.version>2.3.2.RELEASE</spring-boot.version> <dubbo.version>2.7.7</dubbo.version> </properties> <dependencyManagement> <dependencies> <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- Apache Dubbo --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-dependencies-bom</artifactId> <version>${dubbo.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>${dubbo.version}</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> <exclusion> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> </exclusion> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency> </dependencies> </dependencyManagement> <modules> <module>familydoctor_api</module> <module>familydoctor_server</module> <module>familydoctor_consumer</module> </modules> </project>
子项目-接口工程
创建familydoctor_api子模块项目,包含传递的接口和实体。
pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.bsoft</groupId> <artifactId>familydoctor</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>com.bsoft</groupId> <artifactId>familydoctor_api</artifactId> <version>0.0.1-SNAPSHOT</version> <name>familydoctor_api</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> </dependencies> </project>
UserService
package com.bsoft.api.user; import com.bsoft.entity.User; public interface UserService { public User getUserById(String id); }
User
package com.bsoft.entity; import java.io.Serializable; public class User implements Serializable { private String id; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
子项目-服务端
pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.bsoft</groupId> <artifactId>familydoctor</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>com.bsoft</groupId> <artifactId>familydoctor_server</artifactId> <version>0.0.1-SNAPSHOT</version> <name>familydoctor_server</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <!-- 移除springboot本身的logback依赖 --> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <!-- 引入log4j2依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>${dubbo.version}</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-dependencies-zookeeper</artifactId> <version>${dubbo.version}</version> <type>pom</type> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>com.bsoft</groupId> <artifactId>familydoctor_api</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <!-- json --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.28</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.yml
server: port: 8001 dubbo: scan: base-packages: com.bsoft.api # 接口扫描地址 protocol: name: dubbo port: -1 # dubbo协议 端口随机 registry: address: zookeeper://10.32.16.179:2181 # zookeeper连接地址 timeout: 10000 # 超时时间 metadata-report: address: zookeeper://10.32.16.179:2181 # 数据元中心 demo: service: version: 1.0.0 spring: application: name: test-producer
UserServiceImpl
package com.bsoft.api.impl; import com.bsoft.api.user.UserService; import com.bsoft.entity.User; import org.apache.dubbo.config.annotation.DubboService; import org.springframework.stereotype.Component; @Component @DubboService public class UserServiceImpl implements UserService { @Override public User getUserById(String id) { User user = new User(); user.setId(id); user.setName("name"+id); return user; } }
@DubboService是2.7.7的新注解,原先使用的@Service被废弃。
子项目-消费端
pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.bsoft</groupId> <artifactId>familydoctor</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>com.bsoft</groupId> <artifactId>familydoctor_consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <name>familydoctor_consumer</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <!-- 移除springboot本身的logback依赖 --> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <!-- 引入log4j2依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>${dubbo.version}</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-dependencies-zookeeper</artifactId> <version>${dubbo.version}</version> <type>pom</type> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>com.bsoft</groupId> <artifactId>familydoctor_api</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <!-- json --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.28</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.yml
server:
port: 8002
dubbo:
registry:
address: zookeeper://10.32.16.179:2181
timeout: 10000
# consumer:
# check: false
metadata-report:
address: zookeeper://10.32.16.179:2181
demo:
service:
version: 1.0.0
spring:
application:
name: test-comsumer
UserController
package com.bsoft.controller; import com.bsoft.api.user.UserService; import com.bsoft.entity.User; import org.apache.dubbo.config.annotation.DubboReference; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("user") public class UserController { @DubboReference private UserService userService; @GetMapping("getUserById/{id}") public User getUserById(@PathVariable(value = "id") String id){ return userService.getUserById(id); } }
@DubboReference是2.7.7的新注解,原先使用的@Reference被废弃。