Skip to content

Commit

Permalink
增加 Motan 入门
Browse files Browse the repository at this point in the history
  • Loading branch information
YunaiV committed Jun 11, 2020
1 parent 3f99bc4 commit 7efaa26
Show file tree
Hide file tree
Showing 19 changed files with 434 additions and 20 deletions.
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>
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);

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

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

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

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

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

}
Loading

0 comments on commit 7efaa26

Please sign in to comment.