增加 Motan 入门

This commit is contained in:
YunaiV
2020-06-11 09:29:33 +08:00
parent 3f99bc4e97
commit 7efaa26c65
19 changed files with 434 additions and 20 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-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>

View File

@@ -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);
}

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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>

View File

@@ -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);;
}
}

View File

@@ -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);
}
}

View File

@@ -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>

View File

@@ -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>

View File

@@ -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);
}
}

View File

@@ -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
}
}

View File

@@ -0,0 +1,20 @@
<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"
maxServerConnection="80000" maxContentLength="1048576"
maxWorkerThread="800" minWorkerThread="20" />
<!-- Motan 服务提供方配置。 -->
<!-- <motan:service ref="userRpcServiceImpl" interface="cn.iocoder.springboot.lab63.rpc.api.UserRpcService" export="motan2:8001" />-->
<motan:annotation package="cn.iocoder.springboot.lab63.rpc.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-63</artifactId>
<groupId>cn.iocoder.springboot.labs</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>lab-63-motan-annotations-demo</artifactId>
<packaging>pom</packaging>
<modules>
<module>lab-63-motan-annotations-demo-user-rpc-service-api</module>
<module>lab-63-motan-annotations-demo-user-rpc-service-provider</module>
<module>lab-63-motan-annotations-demo-user-rpc-service-consumer</module>
</modules>
</project>

View File

@@ -1,3 +0,0 @@
spring:
application:
name: user-service-consumer # 应用名

View File

@@ -4,13 +4,15 @@
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">
<!-- 使用 Zookeeper 作为注册中心 -->
<!-- 注册中心配置。 -->
<motan:registry name="registry" regProtocol="zookeeper" address="127.0.0.1:2181" connectTimeout="2000"/>
<!-- motan2 with hessian serialization -->
<motan:protocol id="motan2" name="motan2" haStrategy="failover"
loadbalance="roundrobin" maxClientConnection="10" minClientConnection="2"/>
<!-- 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>

View File

@@ -11,9 +11,9 @@ import org.springframework.context.annotation.ImportResource;
public class ProviderApplication {
public static void main(String[] args) {
// 启动 Spring Boot 应用
SpringApplication.run(ProviderApplication.class, args);
// 设置 Motan 开启对外服务
MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true);
}

View File

@@ -1,3 +0,0 @@
spring:
application:
name: user-service-provider # 应用名

View File

@@ -4,19 +4,15 @@
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">
<!-- 使用 Zookeeper 作为注册中心 -->
<motan:registry name="registry" regProtocol="zookeeper" address="127.0.0.1:2181" connectTimeout="2000"/>
<!-- 注册中心配置。 -->
<motan:registry name="registry" regProtocol="zookeeper" address="127.0.0.1:2181" connectTimeout="2000" />
<!-- <motan:service ref="userRpcServiceImpl" interface="cn.iocoder.springboot.lab63.rpc.service.UserRpcServiceImpl"-->
<!-- export="motan2:8001">-->
<!-- </motan:service>-->
<!-- 协议配置。为防止多个业务配置冲突推荐使用id表示具体协议。-->
<!-- motan2 with hessian2 serialization -->
<motan:protocol id="motan2" default="true" name="motan2"
<!-- Motan 协议配置。-->
<motan:protocol name="motan2" default="true"
maxServerConnection="80000" maxContentLength="1048576"
maxWorkerThread="800" minWorkerThread="20" />
<!-- Motan 服务提供方配置。 -->
<motan:service ref="userRpcServiceImpl" interface="cn.iocoder.springboot.lab63.rpc.api.UserRpcService" export="motan2:8001" />
</beans>

View File

@@ -13,6 +13,7 @@
<modules>
<module>lab-63-motan-xml-demo</module>
<module>lab-63-motan-annotations-demo</module>
</modules>
</project>