Skip to content

Commit 7efaa26

Browse files
author
YunaiV
committed
增加 Motan 入门
1 parent 3f99bc4 commit 7efaa26

File tree

19 files changed

+434
-20
lines changed

19 files changed

+434
-20
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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-63-motan-annotations-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-63-motan-annotations-demo-user-rpc-service-api</artifactId>
13+
14+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cn.iocoder.springboot.lab63.rpc.api;
2+
3+
import cn.iocoder.springboot.lab63.rpc.dto.UserAddDTO;
4+
import cn.iocoder.springboot.lab63.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.lab63.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.lab63.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,71 @@
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-63-motan-annotations-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-63-motan-annotations-demo-user-rpc-service-consumer</artifactId>
13+
14+
<properties>
15+
<maven.compiler.target>1.8</maven.compiler.target>
16+
<maven.compiler.source>1.8</maven.compiler.source>
17+
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
18+
<moton.version>1.1.8</moton.version>
19+
</properties>
20+
21+
<dependencyManagement>
22+
<dependencies>
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-parent</artifactId>
26+
<version>${spring.boot.version}</version>
27+
<type>pom</type>
28+
<scope>import</scope>
29+
</dependency>
30+
</dependencies>
31+
</dependencyManagement>
32+
33+
<dependencies>
34+
<!-- 引入定义的 Motan API 接口 -->
35+
<dependency>
36+
<groupId>cn.iocoder.springboot.labs</groupId>
37+
<artifactId>lab-63-motan-annotations-demo-user-rpc-service-api</artifactId>
38+
<version>1.0-SNAPSHOT</version>
39+
</dependency>
40+
41+
<!-- 引入 SpringMVC 的 Spring Boot 依赖 -->
42+
<dependency>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-starter-web</artifactId>
45+
</dependency>
46+
47+
<!-- 引入 Motan 相关依赖 -->
48+
<dependency>
49+
<groupId>com.weibo</groupId>
50+
<artifactId>motan-core</artifactId>
51+
<version>${moton.version}</version>
52+
</dependency>
53+
<dependency>
54+
<groupId>com.weibo</groupId>
55+
<artifactId>motan-transport-netty4</artifactId>
56+
<version>${moton.version}</version>
57+
</dependency>
58+
<dependency>
59+
<groupId>com.weibo</groupId>
60+
<artifactId>motan-registry-zookeeper</artifactId>
61+
<version>${moton.version}</version>
62+
</dependency>
63+
<dependency>
64+
<groupId>com.weibo</groupId>
65+
<artifactId>motan-springsupport</artifactId>
66+
<version>${moton.version}</version>
67+
</dependency>
68+
69+
</dependencies>
70+
71+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package cn.iocoder.springboot.lab63.rpc;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.context.annotation.ImportResource;
6+
7+
@SpringBootApplication
8+
@ImportResource("classpath:motan.xml")
9+
public class ConsumerApplication {
10+
11+
public static void main(String[] args) {
12+
// 启动 Spring Boot 应用
13+
SpringApplication.run(ConsumerApplication.class, args);;
14+
}
15+
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package cn.iocoder.springboot.lab63.rpc.controller;
2+
3+
import cn.iocoder.springboot.lab63.rpc.api.UserRpcService;
4+
import cn.iocoder.springboot.lab63.rpc.dto.UserAddDTO;
5+
import cn.iocoder.springboot.lab63.rpc.dto.UserDTO;
6+
import com.weibo.api.motan.config.springsupport.annotation.MotanReferer;
7+
import org.springframework.web.bind.annotation.GetMapping;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RequestParam;
10+
import org.springframework.web.bind.annotation.RestController;
11+
12+
@RestController
13+
@RequestMapping("/user")
14+
public class UserController {
15+
16+
// @Autowired
17+
@MotanReferer
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,18 @@
1+
<beans xmlns="http://www.springframework.org/schema/beans"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns:motan="http://api.weibo.com/schema/motan"
4+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
5+
http://api.weibo.com/schema/motan http://api.weibo.com/schema/motan.xsd">
6+
7+
<!-- 注册中心配置。 -->
8+
<motan:registry name="registry" regProtocol="zookeeper" address="127.0.0.1:2181" connectTimeout="2000"/>
9+
10+
<!-- Motan 协议配置。-->
11+
<motan:protocol name="motan2" default="true"
12+
haStrategy="failover" loadbalance="roundrobin"
13+
maxClientConnection="10" minClientConnection="2"/>
14+
15+
<!-- Motan 服务调用方配置。 -->
16+
<!-- <motan:referer id="userRpcService" interface="cn.iocoder.springboot.lab63.rpc.api.UserRpcService" />-->
17+
18+
</beans>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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-63-motan-annotations-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-63-motan-annotations-demo-user-rpc-service-provider</artifactId>
13+
14+
<properties>
15+
<maven.compiler.target>1.8</maven.compiler.target>
16+
<maven.compiler.source>1.8</maven.compiler.source>
17+
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
18+
<moton.version>1.1.8</moton.version>
19+
</properties>
20+
21+
<dependencyManagement>
22+
<dependencies>
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-parent</artifactId>
26+
<version>${spring.boot.version}</version>
27+
<type>pom</type>
28+
<scope>import</scope>
29+
</dependency>
30+
</dependencies>
31+
</dependencyManagement>
32+
33+
<dependencies>
34+
<!-- 引入定义的 Motan API 接口 -->
35+
<dependency>
36+
<groupId>cn.iocoder.springboot.labs</groupId>
37+
<artifactId>lab-63-motan-annotations-demo-user-rpc-service-api</artifactId>
38+
<version>1.0-SNAPSHOT</version>
39+
</dependency>
40+
41+
<!-- 引入 Spring Boot 基础 Starter 依赖 -->
42+
<dependency>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-starter</artifactId>
45+
</dependency>
46+
47+
<!-- 引入 Motan 相关依赖 -->
48+
<dependency>
49+
<groupId>com.weibo</groupId>
50+
<artifactId>motan-core</artifactId>
51+
<version>${moton.version}</version>
52+
</dependency>
53+
<dependency>
54+
<groupId>com.weibo</groupId>
55+
<artifactId>motan-transport-netty4</artifactId>
56+
<version>${moton.version}</version>
57+
</dependency>
58+
<dependency>
59+
<groupId>com.weibo</groupId>
60+
<artifactId>motan-registry-zookeeper</artifactId>
61+
<version>${moton.version}</version>
62+
</dependency>
63+
<dependency>
64+
<groupId>com.weibo</groupId>
65+
<artifactId>motan-springsupport</artifactId>
66+
<version>${moton.version}</version>
67+
</dependency>
68+
69+
</dependencies>
70+
71+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package cn.iocoder.springboot.lab63.rpc;
2+
3+
import com.weibo.api.motan.common.MotanConstants;
4+
import com.weibo.api.motan.util.MotanSwitcherUtil;
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
import org.springframework.context.annotation.ImportResource;
8+
9+
@SpringBootApplication
10+
@ImportResource("classpath:motan.xml")
11+
public class ProviderApplication {
12+
13+
public static void main(String[] args) {
14+
// 启动 Spring Boot 应用
15+
SpringApplication.run(ProviderApplication.class, args);
16+
// 设置 Motan 开启对外服务
17+
MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true);
18+
}
19+
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cn.iocoder.springboot.lab63.rpc.service;
2+
3+
4+
import cn.iocoder.springboot.lab63.rpc.api.UserRpcService;
5+
import cn.iocoder.springboot.lab63.rpc.dto.UserAddDTO;
6+
import cn.iocoder.springboot.lab63.rpc.dto.UserDTO;
7+
import com.weibo.api.motan.config.springsupport.annotation.MotanService;
8+
import org.springframework.stereotype.Service;
9+
10+
@Service
11+
@MotanService(export = "motan2:8001")
12+
public class UserRpcServiceImpl implements UserRpcService {
13+
14+
@Override
15+
public UserDTO get(Integer id) {
16+
return new UserDTO().setId(id)
17+
.setName("没有昵称:" + id)
18+
.setGender(id % 2 + 1); // 1 - 男;2 - 女
19+
}
20+
21+
@Override
22+
public Integer add(UserAddDTO addDTO) {
23+
return (int) (System.currentTimeMillis() / 1000); // 嘿嘿,随便返回一个 id
24+
}
25+
26+
}

0 commit comments

Comments
 (0)