增加 SOFARPC 入门

This commit is contained in:
YunaiV
2020-06-09 21:05:28 +08:00
parent ca36e672a0
commit 2a2720e6fe
15 changed files with 369 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
<?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>lab-62-sofarpc-xml-demo</artifactId>
<groupId>cn.iocoder.springboot.labs</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>lab-62-sofarpc-xml-demo-user-rpc-service-api</artifactId>
</project>

View File

@@ -0,0 +1,27 @@
package cn.iocoder.springboot.lab62.rpc.api;
import cn.iocoder.springboot.lab62.rpc.dto.UserAddDTO;
import cn.iocoder.springboot.lab62.rpc.dto.UserDTO;
/**
* 用户服务 RPC Service 接口
*/
public interface UserRpcService {
/**
* 根据指定用户编号,获得用户信息
*
* @param id 用户编号
* @return 用户信息
*/
UserDTO get(Integer id);
/**
* 添加新用户,返回新添加的用户编号
*
* @param addDTO 添加的用户信息
* @return 用户编号
*/
Integer add(UserAddDTO addDTO);
}

View File

@@ -0,0 +1,37 @@
package cn.iocoder.springboot.lab62.rpc.dto;
import java.io.Serializable;
/**
* 用户添加 DTO
*/
public class UserAddDTO implements Serializable {
/**
* 昵称
*/
private String name;
/**
* 性别
*/
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;
}
}

View File

@@ -0,0 +1,49 @@
package cn.iocoder.springboot.lab62.rpc.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;
}
}

View File

@@ -0,0 +1,45 @@
<?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>lab-62-sofarpc-xml-demo</artifactId>-->
<!-- <groupId>cn.iocoder.springboot.labs</groupId>-->
<!-- <version>1.0-SNAPSHOT</version>-->
<groupId>com.alipay.sofa</groupId>
<artifactId>sofaboot-dependencies</artifactId>
<version>3.3.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>lab-62-sofarpc-xml-demo-user-rpc-service-consumer</artifactId>
<dependencies>
<!-- 引入定义的 SOFARPC API 接口 -->
<dependency>
<groupId>cn.iocoder.springboot.labs</groupId>
<artifactId>lab-62-sofarpc-xml-demo-user-rpc-service-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- 引入 SpringMVC 的 Spring Boot 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 实现对 SOFARPC 的自动化配置 -->
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>rpc-sofa-boot-starter</artifactId>
</dependency>
<!-- 使用 Zookeeper 作为注册中心 -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,16 @@
package cn.iocoder.springboot.lab62.rpc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@ImportResource("classpath:sofarpc.xml")
public class ConsumerApplication {
public static void main(String[] args) {
// 启动 Spring Boot 应用
SpringApplication.run(ConsumerApplication.class, args);;
}
}

View File

@@ -0,0 +1,23 @@
package cn.iocoder.springboot.lab62.rpc.controller;
import cn.iocoder.springboot.lab62.rpc.api.UserRpcService;
import cn.iocoder.springboot.lab62.rpc.dto.UserDTO;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserRpcService userRpcService;
@GetMapping("/get")
public UserDTO get(@RequestParam("id") Integer id) {
return userRpcService.get(id);
}
}

View File

@@ -0,0 +1,5 @@
spring:
application:
name: user-service-consumer # 应用名
com.alipay.sofa.rpc.registry.address: zookeeper://127.0.0.1:2181 # SOFA RPC 注解中心

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sofa="http://sofastack.io/schema/sofaboot"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://sofastack.io/schema/sofaboot http://sofastack.io/schema/sofaboot.xsd">
<sofa:reference id="userRpcService" interface="cn.iocoder.springboot.lab62.rpc.api.UserRpcService">
<sofa:binding.bolt/>
</sofa:reference>
</beans>

View File

@@ -0,0 +1,56 @@
<?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>lab-62-sofarpc-xml-demo</artifactId>-->
<!-- <groupId>cn.iocoder.springboot.labs</groupId>-->
<!-- <version>1.0-SNAPSHOT</version>-->
<groupId>com.alipay.sofa</groupId>
<artifactId>sofaboot-dependencies</artifactId>
<version>3.3.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>lab-62-sofarpc-xml-demo-user-rpc-service-provider</artifactId>
<!-- <dependencyManagement>-->
<!-- <dependencies>-->
<!-- <dependency>-->
<!-- <groupId>com.alipay.sofa</groupId>-->
<!-- <artifactId>sofaboot-dependencies</artifactId>-->
<!-- <version>3.3.2</version>-->
<!-- <type>pom</type>-->
<!-- </dependency>-->
<!-- </dependencies>-->
<!-- </dependencyManagement>-->
<dependencies>
<!-- 引入定义的 SOFARPC API 接口 -->
<dependency>
<groupId>cn.iocoder.springboot.labs</groupId>
<artifactId>lab-62-sofarpc-xml-demo-user-rpc-service-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- 引入 Spring Boot 基础 starter 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- 实现对 SOFARPC 的自动化配置 -->
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>rpc-sofa-boot-starter</artifactId>
</dependency>
<!-- 使用 Zookeeper 作为注册中心 -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,16 @@
package cn.iocoder.springboot.lab62.rpc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@ImportResource("classpath:sofarpc.xml")
public class ProviderApplication {
public static void main(String[] args) {
// 启动 Spring Boot 应用
SpringApplication.run(ProviderApplication.class, args);
}
}

View File

@@ -0,0 +1,24 @@
package cn.iocoder.springboot.lab62.rpc.service;
import cn.iocoder.springboot.lab62.rpc.api.UserRpcService;
import cn.iocoder.springboot.lab62.rpc.dto.UserAddDTO;
import cn.iocoder.springboot.lab62.rpc.dto.UserDTO;
import org.springframework.stereotype.Service;
@Service
public class UserRpcServiceImpl implements UserRpcService {
@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
}
}

View File

@@ -0,0 +1,5 @@
spring:
application:
name: user-service-provider # 应用名
com.alipay.sofa.rpc.registry.address: zookeeper://127.0.0.1:2181 # SOFA RPC 注解中心

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sofa="http://sofastack.io/schema/sofaboot"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://sofastack.io/schema/sofaboot http://sofastack.io/schema/sofaboot.xsd">
<sofa:service ref="userRpcServiceImpl" interface="cn.iocoder.springboot.lab62.rpc.api.UserRpcService">
<sofa:binding.bolt/>
</sofa:service>
</beans>

View 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>lab-62</artifactId>
<groupId>cn.iocoder.springboot.labs</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>lab-62-sofarpc-xml-demo</artifactId>
<packaging>pom</packaging>
<modules>
<module>lab-62-sofarpc-xml-demo-user-rpc-service-api</module>
<module>lab-62-sofarpc-xml-demo-user-rpc-service-provider</module>
<module>lab-62-sofarpc-xml-demo-user-rpc-service-consumer</module>
</modules>
</project>