forked from yudaocode/SpringBoot-Labs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
YunaiV
committed
Jun 11, 2020
1 parent
3f99bc4
commit 7efaa26
Showing
19 changed files
with
434 additions
and
20 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
.../lab-63-motan-annotations-demo/lab-63-motan-annotations-demo-user-rpc-service-api/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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-63-motan-annotations-demo</artifactId> | ||
<groupId>cn.iocoder.springboot.labs</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>lab-63-motan-annotations-demo-user-rpc-service-api</artifactId> | ||
|
||
</project> |
27 changes: 27 additions & 0 deletions
27
...ser-rpc-service-api/src/main/java/cn/iocoder/springboot/lab63/rpc/api/UserRpcService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package cn.iocoder.springboot.lab63.rpc.api; | ||
|
||
import cn.iocoder.springboot.lab63.rpc.dto.UserAddDTO; | ||
import cn.iocoder.springboot.lab63.rpc.dto.UserDTO; | ||
|
||
/** | ||
* 用户服务 RPC Service 接口 | ||
*/ | ||
public interface UserRpcService { | ||
|
||
/** | ||
* 根据指定用户编号,获得用户信息 | ||
* | ||
* @param id 用户编号 | ||
* @return 用户信息 | ||
*/ | ||
UserDTO get(Integer id); | ||
|
||
/** | ||
* 添加新用户,返回新添加的用户编号 | ||
* | ||
* @param addDTO 添加的用户信息 | ||
* @return 用户编号 | ||
*/ | ||
Integer add(UserAddDTO addDTO); | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
...mo-user-rpc-service-api/src/main/java/cn/iocoder/springboot/lab63/rpc/dto/UserAddDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package cn.iocoder.springboot.lab63.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; | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
...-demo-user-rpc-service-api/src/main/java/cn/iocoder/springboot/lab63/rpc/dto/UserDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package cn.iocoder.springboot.lab63.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; | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
...63-motan-annotations-demo/lab-63-motan-annotations-demo-user-rpc-service-consumer/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?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-63-motan-annotations-demo</artifactId> | ||
<groupId>cn.iocoder.springboot.labs</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>lab-63-motan-annotations-demo-user-rpc-service-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> | ||
<moton.version>1.1.8</moton.version> | ||
</properties> | ||
|
||
<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> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
<dependencies> | ||
<!-- 引入定义的 Motan API 接口 --> | ||
<dependency> | ||
<groupId>cn.iocoder.springboot.labs</groupId> | ||
<artifactId>lab-63-motan-annotations-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> | ||
|
||
<!-- 引入 Motan 相关依赖 --> | ||
<dependency> | ||
<groupId>com.weibo</groupId> | ||
<artifactId>motan-core</artifactId> | ||
<version>${moton.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.weibo</groupId> | ||
<artifactId>motan-transport-netty4</artifactId> | ||
<version>${moton.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.weibo</groupId> | ||
<artifactId>motan-registry-zookeeper</artifactId> | ||
<version>${moton.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.weibo</groupId> | ||
<artifactId>motan-springsupport</artifactId> | ||
<version>${moton.version}</version> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
</project> |
16 changes: 16 additions & 0 deletions
16
...c-service-consumer/src/main/java/cn/iocoder/springboot/lab63/rpc/ConsumerApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package cn.iocoder.springboot.lab63.rpc; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.context.annotation.ImportResource; | ||
|
||
@SpringBootApplication | ||
@ImportResource("classpath:motan.xml") | ||
public class ConsumerApplication { | ||
|
||
public static void main(String[] args) { | ||
// 启动 Spring Boot 应用 | ||
SpringApplication.run(ConsumerApplication.class, args);; | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
...ice-consumer/src/main/java/cn/iocoder/springboot/lab63/rpc/controller/UserController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package cn.iocoder.springboot.lab63.rpc.controller; | ||
|
||
import cn.iocoder.springboot.lab63.rpc.api.UserRpcService; | ||
import cn.iocoder.springboot.lab63.rpc.dto.UserAddDTO; | ||
import cn.iocoder.springboot.lab63.rpc.dto.UserDTO; | ||
import com.weibo.api.motan.config.springsupport.annotation.MotanReferer; | ||
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 | ||
@MotanReferer | ||
private UserRpcService userRpcService; | ||
|
||
@GetMapping("/get") | ||
public UserDTO get(@RequestParam("id") Integer id) { | ||
return userRpcService.get(id); | ||
} | ||
|
||
@GetMapping("/add") // 为了方便测试,实际使用 @PostMapping | ||
public Integer add(@RequestParam("name") String name, | ||
@RequestParam("gender") Integer gender) { | ||
UserAddDTO addDTO = new UserAddDTO().setName(name).setGender(gender); | ||
return userRpcService.add(addDTO); | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
...demo/lab-63-motan-annotations-demo-user-rpc-service-consumer/src/main/resources/motan.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<beans xmlns="http://www.springframework.org/schema/beans" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:motan="http://api.weibo.com/schema/motan" | ||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd | ||
http://api.weibo.com/schema/motan http://api.weibo.com/schema/motan.xsd"> | ||
|
||
<!-- 注册中心配置。 --> | ||
<motan:registry name="registry" regProtocol="zookeeper" address="127.0.0.1:2181" connectTimeout="2000"/> | ||
|
||
<!-- Motan 协议配置。--> | ||
<motan:protocol name="motan2" default="true" | ||
haStrategy="failover" loadbalance="roundrobin" | ||
maxClientConnection="10" minClientConnection="2"/> | ||
|
||
<!-- Motan 服务调用方配置。 --> | ||
<!-- <motan:referer id="userRpcService" interface="cn.iocoder.springboot.lab63.rpc.api.UserRpcService" />--> | ||
|
||
</beans> |
71 changes: 71 additions & 0 deletions
71
...63-motan-annotations-demo/lab-63-motan-annotations-demo-user-rpc-service-provider/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?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-63-motan-annotations-demo</artifactId> | ||
<groupId>cn.iocoder.springboot.labs</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>lab-63-motan-annotations-demo-user-rpc-service-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> | ||
<moton.version>1.1.8</moton.version> | ||
</properties> | ||
|
||
<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> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
<dependencies> | ||
<!-- 引入定义的 Motan API 接口 --> | ||
<dependency> | ||
<groupId>cn.iocoder.springboot.labs</groupId> | ||
<artifactId>lab-63-motan-annotations-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> | ||
|
||
<!-- 引入 Motan 相关依赖 --> | ||
<dependency> | ||
<groupId>com.weibo</groupId> | ||
<artifactId>motan-core</artifactId> | ||
<version>${moton.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.weibo</groupId> | ||
<artifactId>motan-transport-netty4</artifactId> | ||
<version>${moton.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.weibo</groupId> | ||
<artifactId>motan-registry-zookeeper</artifactId> | ||
<version>${moton.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.weibo</groupId> | ||
<artifactId>motan-springsupport</artifactId> | ||
<version>${moton.version}</version> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
</project> |
20 changes: 20 additions & 0 deletions
20
...c-service-provider/src/main/java/cn/iocoder/springboot/lab63/rpc/ProviderApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package cn.iocoder.springboot.lab63.rpc; | ||
|
||
import com.weibo.api.motan.common.MotanConstants; | ||
import com.weibo.api.motan.util.MotanSwitcherUtil; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.context.annotation.ImportResource; | ||
|
||
@SpringBootApplication | ||
@ImportResource("classpath:motan.xml") | ||
public class ProviderApplication { | ||
|
||
public static void main(String[] args) { | ||
// 启动 Spring Boot 应用 | ||
SpringApplication.run(ProviderApplication.class, args); | ||
// 设置 Motan 开启对外服务 | ||
MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true); | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
...ce-provider/src/main/java/cn/iocoder/springboot/lab63/rpc/service/UserRpcServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package cn.iocoder.springboot.lab63.rpc.service; | ||
|
||
|
||
import cn.iocoder.springboot.lab63.rpc.api.UserRpcService; | ||
import cn.iocoder.springboot.lab63.rpc.dto.UserAddDTO; | ||
import cn.iocoder.springboot.lab63.rpc.dto.UserDTO; | ||
import com.weibo.api.motan.config.springsupport.annotation.MotanService; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@MotanService(export = "motan2:8001") | ||
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 | ||
} | ||
|
||
} |
Oops, something went wrong.