Skip to content

Commit f7fe0a2

Browse files
author
YunaiV
committed
增加 SOFARPC 入门
1 parent 2a2720e commit f7fe0a2

File tree

38 files changed

+776
-13
lines changed

38 files changed

+776
-13
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>lab-62-sofarpc-xml-demo</artifactId>
7+
<groupId>cn.iocoder.springboot.labs</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>lab-62-sofarpc-annotations-demo-user-rpc-service-api</artifactId>
13+
14+
15+
16+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cn.iocoder.springboot.lab62.rpc.api;
2+
3+
import cn.iocoder.springboot.lab62.rpc.dto.UserAddDTO;
4+
import cn.iocoder.springboot.lab62.rpc.dto.UserDTO;
5+
6+
/**
7+
* 用户服务 RPC Service 接口
8+
*/
9+
public interface UserRpcService {
10+
11+
/**
12+
* 根据指定用户编号,获得用户信息
13+
*
14+
* @param id 用户编号
15+
* @return 用户信息
16+
*/
17+
UserDTO get(Integer id);
18+
19+
/**
20+
* 添加新用户,返回新添加的用户编号
21+
*
22+
* @param addDTO 添加的用户信息
23+
* @return 用户编号
24+
*/
25+
Integer add(UserAddDTO addDTO);
26+
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package cn.iocoder.springboot.lab62.rpc.dto;
2+
3+
import java.io.Serializable;
4+
5+
/**
6+
* 用户添加 DTO
7+
*/
8+
public class UserAddDTO implements Serializable {
9+
10+
/**
11+
* 昵称
12+
*/
13+
private String name;
14+
/**
15+
* 性别
16+
*/
17+
private Integer gender;
18+
19+
public String getName() {
20+
return name;
21+
}
22+
23+
public UserAddDTO setName(String name) {
24+
this.name = name;
25+
return this;
26+
}
27+
28+
public Integer getGender() {
29+
return gender;
30+
}
31+
32+
public UserAddDTO setGender(Integer gender) {
33+
this.gender = gender;
34+
return this;
35+
}
36+
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package cn.iocoder.springboot.lab62.rpc.dto;
2+
3+
import java.io.Serializable;
4+
5+
/**
6+
* 用户信息 DTO
7+
*/
8+
public class UserDTO implements Serializable {
9+
10+
/**
11+
* 用户编号
12+
*/
13+
private Integer id;
14+
/**
15+
* 昵称
16+
*/
17+
private String name;
18+
/**
19+
* 性别
20+
*/
21+
private Integer gender;
22+
23+
public Integer getId() {
24+
return id;
25+
}
26+
27+
public UserDTO setId(Integer id) {
28+
this.id = id;
29+
return this;
30+
}
31+
32+
public String getName() {
33+
return name;
34+
}
35+
36+
public UserDTO setName(String name) {
37+
this.name = name;
38+
return this;
39+
}
40+
41+
public Integer getGender() {
42+
return gender;
43+
}
44+
45+
public UserDTO setGender(Integer gender) {
46+
this.gender = gender;
47+
return this;
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<!-- <artifactId>lab-62-sofarpc-xml-demo</artifactId>-->
7+
<!-- <groupId>cn.iocoder.springboot.labs</groupId>-->
8+
<!-- <version>1.0-SNAPSHOT</version>-->
9+
<groupId>com.alipay.sofa</groupId>
10+
<artifactId>sofaboot-dependencies</artifactId>
11+
<version>3.3.2</version>
12+
</parent>
13+
<modelVersion>4.0.0</modelVersion>
14+
15+
<artifactId>lab-62-sofarpc-annotations-demo-user-rpc-service-consumer</artifactId>
16+
17+
<dependencies>
18+
<!-- 引入定义的 SOFARPC API 接口 -->
19+
<dependency>
20+
<groupId>cn.iocoder.springboot.labs</groupId>
21+
<artifactId>lab-62-sofarpc-annotations-demo-user-rpc-service-api</artifactId>
22+
<version>1.0-SNAPSHOT</version>
23+
</dependency>
24+
25+
<!-- 引入 SpringMVC 的 Spring Boot 依赖 -->
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-web</artifactId>
29+
</dependency>
30+
31+
<!-- 实现对 SOFARPC 的自动化配置 -->
32+
<dependency>
33+
<groupId>com.alipay.sofa</groupId>
34+
<artifactId>rpc-sofa-boot-starter</artifactId>
35+
</dependency>
36+
37+
<!-- 使用 Zookeeper 作为注册中心 -->
38+
<dependency>
39+
<groupId>org.apache.curator</groupId>
40+
<artifactId>curator-framework</artifactId>
41+
</dependency>
42+
43+
</dependencies>
44+
45+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package cn.iocoder.springboot.lab62.rpc;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
//@ImportResource("classpath:sofarpc.xml")
8+
public class ConsumerApplication {
9+
10+
public static void main(String[] args) {
11+
// 启动 Spring Boot 应用
12+
SpringApplication.run(ConsumerApplication.class, args);;
13+
}
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package cn.iocoder.springboot.lab62.rpc.controller;
2+
3+
import cn.iocoder.springboot.lab62.rpc.api.UserRpcService;
4+
import cn.iocoder.springboot.lab62.rpc.dto.UserAddDTO;
5+
import cn.iocoder.springboot.lab62.rpc.dto.UserDTO;
6+
import com.alipay.sofa.runtime.api.annotation.SofaReference;
7+
import com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding;
8+
import org.springframework.web.bind.annotation.GetMapping;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
import org.springframework.web.bind.annotation.RequestParam;
11+
import org.springframework.web.bind.annotation.RestController;
12+
13+
@RestController
14+
@RequestMapping("/user")
15+
public class UserController {
16+
17+
@SofaReference(binding = @SofaReferenceBinding(bindingType = "bolt"))
18+
private UserRpcService userRpcService;
19+
20+
@GetMapping("/get")
21+
public UserDTO get(@RequestParam("id") Integer id) {
22+
return userRpcService.get(id);
23+
}
24+
25+
@GetMapping("/add") // 为了方便测试,实际使用 @PostMapping
26+
public Integer add(@RequestParam("name") String name,
27+
@RequestParam("gender") Integer gender) {
28+
UserAddDTO addDTO = new UserAddDTO().setName(name).setGender(gender);
29+
return userRpcService.add(addDTO);
30+
}
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
spring:
2+
application:
3+
name: user-service-consumer # 应用名
4+
5+
com.alipay.sofa.rpc.registry.address: zookeeper://127.0.0.1:2181 # SOFARPC 注解中心
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<!-- <artifactId>lab-62-sofarpc-xml-demo</artifactId>-->
7+
<!-- <groupId>cn.iocoder.springboot.labs</groupId>-->
8+
<!-- <version>1.0-SNAPSHOT</version>-->
9+
<groupId>com.alipay.sofa</groupId>
10+
<artifactId>sofaboot-dependencies</artifactId>
11+
<version>3.4.0</version>
12+
</parent>
13+
<modelVersion>4.0.0</modelVersion>
14+
15+
<artifactId>lab-62-sofarpc-annotations-demo-user-rpc-service-provider</artifactId>
16+
17+
<!-- <dependencyManagement>-->
18+
<!-- <dependencies>-->
19+
<!-- <dependency>-->
20+
<!-- <groupId>com.alipay.sofa</groupId>-->
21+
<!-- <artifactId>sofaboot-dependencies</artifactId>-->
22+
<!-- <version>3.3.2</version>-->
23+
<!-- <type>pom</type>-->
24+
<!-- </dependency>-->
25+
<!-- </dependencies>-->
26+
<!-- </dependencyManagement>-->
27+
28+
<dependencies>
29+
<!-- 引入定义的 SOFARPC API 接口 -->
30+
<dependency>
31+
<groupId>cn.iocoder.springboot.labs</groupId>
32+
<artifactId>lab-62-sofarpc-annotations-demo-user-rpc-service-api</artifactId>
33+
<version>1.0-SNAPSHOT</version>
34+
</dependency>
35+
36+
<!-- 引入 Spring Boot 基础 Starter 依赖 -->
37+
<dependency>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-starter</artifactId>
40+
</dependency>
41+
42+
<!-- 实现对 SOFARPC 的自动化配置 -->
43+
<dependency>
44+
<groupId>com.alipay.sofa</groupId>
45+
<artifactId>rpc-sofa-boot-starter</artifactId>
46+
</dependency>
47+
48+
<!-- 使用 Zookeeper 作为注册中心 -->
49+
<dependency>
50+
<groupId>org.apache.curator</groupId>
51+
<artifactId>curator-framework</artifactId>
52+
</dependency>
53+
54+
</dependencies>
55+
56+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package cn.iocoder.springboot.lab62.rpc;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
//@ImportResource("classpath:sofarpc.xml")
8+
public class ProviderApplication {
9+
10+
public static void main(String[] args) {
11+
// 启动 Spring Boot 应用
12+
SpringApplication.run(ProviderApplication.class, args);
13+
}
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cn.iocoder.springboot.lab62.rpc.service;
2+
3+
4+
import cn.iocoder.springboot.lab62.rpc.api.UserRpcService;
5+
import cn.iocoder.springboot.lab62.rpc.dto.UserAddDTO;
6+
import cn.iocoder.springboot.lab62.rpc.dto.UserDTO;
7+
import com.alipay.sofa.runtime.api.annotation.SofaService;
8+
import com.alipay.sofa.runtime.api.annotation.SofaServiceBinding;
9+
import org.springframework.stereotype.Service;
10+
11+
@Service
12+
@SofaService(bindings = @SofaServiceBinding(bindingType = "bolt"))
13+
public class UserRpcServiceImpl implements UserRpcService {
14+
15+
@Override
16+
public UserDTO get(Integer id) {
17+
return new UserDTO().setId(id)
18+
.setName("没有昵称:" + id)
19+
.setGender(id % 2 + 1); // 1 - 男;2 - 女
20+
}
21+
22+
@Override
23+
public Integer add(UserAddDTO addDTO) {
24+
return (int) (System.currentTimeMillis() / 1000); // 嘿嘿,随便返回一个 id
25+
}
26+
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
spring:
2+
application:
3+
name: user-service-provider # 应用名
4+
5+
com.alipay.sofa.rpc.registry.address: zookeeper://127.0.0.1:2181 # SOFA RPC 注册中心
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>lab-62</artifactId>
7+
<groupId>cn.iocoder.springboot.labs</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>lab-62-sofarpc-annotations-demo</artifactId>
13+
<packaging>pom</packaging>
14+
15+
<modules>
16+
<module>lab-62-sofarpc-annotations-demo-user-rpc-service-api</module>
17+
<module>lab-62-sofarpc-annotations-demo-user-rpc-service-provider</module>
18+
<module>lab-62-sofarpc-annotations-demo-user-rpc-service-consumer</module>
19+
</modules>
20+
21+
</project>

lab-62/lab-62-sofarpc-xml-demo/lab-62-sofarpc-xml-demo-user-rpc-service-consumer/src/main/java/cn/iocoder/springboot/lab62/rpc/controller/UserController.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package cn.iocoder.springboot.lab62.rpc.controller;
22

33
import cn.iocoder.springboot.lab62.rpc.api.UserRpcService;
4+
import cn.iocoder.springboot.lab62.rpc.dto.UserAddDTO;
45
import cn.iocoder.springboot.lab62.rpc.dto.UserDTO;
56
import org.springframework.beans.factory.annotation.Autowired;
6-
import org.springframework.web.bind.annotation.GetMapping;
7-
import org.springframework.web.bind.annotation.RequestMapping;
8-
import org.springframework.web.bind.annotation.RequestParam;
9-
import org.springframework.web.bind.annotation.RestController;
7+
import org.springframework.web.bind.annotation.*;
108

119
@RestController
1210
@RequestMapping("/user")
@@ -20,4 +18,11 @@ public UserDTO get(@RequestParam("id") Integer id) {
2018
return userRpcService.get(id);
2119
}
2220

21+
@GetMapping("/add") // 为了方便测试,实际使用 @PostMapping
22+
public Integer add(@RequestParam("name") String name,
23+
@RequestParam("gender") Integer gender) {
24+
UserAddDTO addDTO = new UserAddDTO().setName(name).setGender(gender);
25+
return userRpcService.add(addDTO);
26+
}
27+
2328
}

lab-62/lab-62-sofarpc-xml-demo/lab-62-sofarpc-xml-demo-user-rpc-service-consumer/src/main/resources/application.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ spring:
22
application:
33
name: user-service-consumer # 应用名
44

5-
com.alipay.sofa.rpc.registry.address: zookeeper://127.0.0.1:2181 # SOFA RPC 注解中心
5+
com.alipay.sofa.rpc.registry.address: zookeeper://127.0.0.1:2181 # SOFARPC 注解中心

0 commit comments

Comments
 (0)