title: 好客租房dubbo
date: 2019-08-21 12:01:01
categories: 项目实战
tags:
基于dubbo react elk实战整合开发
初始化haoke-manage-web
tyarn install
tyarn start
[外链图片转存失败(img-FkEoYAGo-1566451453140)(https://www.github.com/OneJane/blog/raw/master/小书匠/1566368046491.png)]
src/layouts/BasicLayout.js 全局的布局文件
src/components/SiderMenu/index.js
src/components/SiderMenu/SiderMenu.js
<div className={styles.logo} id="logo">
<Link to="/">
{/* <img src={logo} alt="logo" /> */}
<h1>好客租房 · 管理系统h1>
Link>
div>
src/layouts/Footer.js
config/router.config.js
{ path: '/', redirect: '/house/resource' }, // 进入系统默认打开房源管理
{ //房源管理
path: '/house',
name: 'house',
icon: 'home',
routes: [
{
path: '/house/resource',
name: 'resource',
component: './haoke/House/Resource'
},
{
path: '/house/addResource',
name: 'addResource',
component: './haoke/House/AddResource'
},
{
path: '/house/kanfang',
name: 'kanfang',
component: './haoke/House/KanFang'
},
{
path: '/house/zufang',
name: 'zufang',
component: './haoke/House/ZuFang'
}
]
},
src/pages/haoke/House/AddResource.js
@Form.create() 对页面进行了包装,包装之后,会在this.props中增加form对象,将拥有getFieldDecorator 双向绑定等功能,经过 getFieldDecorator 包装的控件,表单控件会自动添加 value (或 valuePropName 指定的其他属性) onChange (或 trigger 指定的其他属性),数据同步将被 Form 接管
const estateMap = new Map([
['中远两湾城','1001|上海市,上海市,普陀区,远景路97弄'],
['上海康城','1002|上海市,上海市,闵行区,莘松路958弄'],
['保利西子湾','1003|上海市,上海市,松江区,广富林路1188弄'],
['万科城市花园','1004|上海市,上海市,闵行区,七莘路3333弄2区-15区'],
['上海阳城','1005|上海市,上海市,闵行区,罗锦路888弄']
]);
{
let v = estateMap.get(value);
this.setState({
estateAddress: v.substring(v.indexOf('|')+1),
estateId : v.substring(0,v.indexOf('|'))
});
}}
onSearch={this.handleSearch}
filterOption={(inputValue, option) => option.props.children.toUpperCase().indexOf(inputValue.toUpperCase()) !== -1}
/>
通过onSearch进行动态设置数据源,这里使用的数据是静态数据
handleSearch = (value)=>{
let arr = new Array();
if(value.length > 0 ){
estateMap.forEach((v, k) => {
if(k.startsWith(value)){
arr.push(k);
}
});
}
this.setState({
estateDataSource: arr
});
} ;
通过onSelect设置 选中楼盘后,在楼盘地址中填写地址数据
onSelect={(value, option)=>{
let v = estateMap.get(value);
this.setState({
estateAddress: v.substring(v.indexOf('|')+1),
estateId : v.substring(0,v.indexOf('|'))
});
}}
父组件通过属性的方式进行引用子组件,自组件在bind方法中改变this的引用为父组件
<FormItem {...formItemLayout} label="上传室内图">
<PicturesWall handleFileList={this.handleFileList.bind(this)}/>
</FormItem>
父组件中获取数据
handleFileList = (obj)=>{
console.log(obj, "图片列表");
}
src/pages/haoke/Utils/PicturesWall.js
在子组件中通过this.props获取父组件方法传入的函数,进行调用,即可把数据传递到父组件中
handleChange = ({ fileList }) => {
this.setState({ fileList });
this.props.handleFileList(this.state.fileList);
}
<Upload
action="1111111"
listType="picture-card"
fileList={fileList}
onPreview={this.handlePreview}
onChange={this.handleChange}
>
{fileList.length >= 5 ? null : uploadButton}
</Upload>
[外链图片转存失败(img-R10a9LS4-1566451453140)(https://www.github.com/OneJane/blog/raw/master/小书匠/1566360379544.png)]
后台系统服务采用RPC+微服务的架构思想,RPC采用dubbo架构作为服务治理框架,对外接口采用RESTFul+GraphQL接口方式。
apt-get install --reinstall systemd -y
apt-get install -y docker.io
systemctl start docker
docker create --name zk -p 2181:2181 zookeeper:3.5
docker start zk 可以通过ZooInspector连接查看
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache fast
yum list docker-ce --showduplicates | sort -r
yum install -y docker-ce-18.09.5
systemctl restart docker
docker create --name zk -p 2181:2181 zookeeper:3.5
docker start zk
执行ZooInspector\build\start.bat
[外链图片转存失败(img-mtTX4Ojk-1566451453141)(https://www.github.com/OneJane/blog/raw/master/小书匠/1566446399226.png)]
查看zk信息
dubbo/pom.xml
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.0.RELEASEversion>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>com.alibaba.bootgroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>0.2.0version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>dubboartifactId>
<version>2.6.4version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
dubbo/dubbo-service/pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>3.4.13version>
dependency>
<dependency>
<groupId>com.github.sgroschupfgroupId>
<artifactId>zkclientartifactId>
<version>0.1version>
dependency>
dependencies>
com/onejane/dubbo/pojo/User.java
// 使用dubbo要求传输的对象必须实现序列化接口
public class User implements java.io.Serializable {
private static final long serialVersionUID = -7341603933521593227L;
private Long id;
private String username;
private String password;
private Integer age;
}
com/onejane/dubbo/service/UserService.java
public interface UserService {
List<User> queryAll();
}
com/onejane/dubbo/service/impl/UserServiceImpl.java
@Service(version = "${dubbo.service.version}") //声明这是一个dubbo服务
public class UserServiceImpl implements UserService {
public List queryAll() {
List list = new ArrayList();
for (int i = 0; i < 10; i++) {
User user = new User();
user.setAge(10 + i);
user.setId(Long.valueOf(i + 1));
user.setPassword("123456");
user.setUsername("username_" + i);
list.add(user);
}
System.out.println("---------Service 3------------");
return list;
}
}
application.properties
# Spring boot application
spring.application.name = dubbo-service
server.port = 9090
# Service version
dubbo.service.version = 1.0.0
# 服务的扫描包
dubbo.scan.basePackages =com.onejane.dubbo.service
# 应用名称
dubbo.application.name = dubbo-provider-demo
# 协议以及端口
dubbo.protocol.name = dubbo
dubbo.protocol.port = 20882
# zk注册中心
dubbo.registry.address = zookeeper://192.168.3.237:2181
dubbo.registry.client = zkclient
com/onejane/dubbo/DubboProvider.java
@SpringBootApplication
public class DubboProvider {
public static void main(String[] args) {
new SpringApplicationBuilder(DubboProvider.class)
.web(WebApplicationType.NONE) // 非 Web 应用
.run(args);
}
}
dubbo/dubbo-comsumer/pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>3.4.13version>
dependency>
<dependency>
<groupId>com.github.sgroschupfgroupId>
<artifactId>zkclientartifactId>
<version>0.1version>
dependency>
<dependency>
<groupId>com.onejanegroupId>
<artifactId>dubbo-serviceartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
dependencies>
application.properties
# Spring boot application
spring.application.name = dubbo-consumer
server.port = 9091
# 应用名称
dubbo.application.name = dubbo-consumer-demo
# zk注册中心
dubbo.registry.address = zookeeper://192.168.3.237:2181
dubbo.registry.client = zkclient
com/onejane/dubbo/UserServiceTest.java
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserServiceTest {
// 负载均衡策略 默认随机 测试时启动dubbo.protocol.port多个不同端口的userService服务,并修改打印值进行区分
// loadbalance = "roundrobin"设置负载均衡策略
@Reference(version = "1.0.0", loadbalance = "roundrobin")
private UserService userService;
@Test
public void testQueryAll() {
for (int i = 0; i < 100; i++) {
System.out.println("开始调用远程服务 >>>>>" + i);
List users = this.userService.queryAll();
for (User user : users) {
System.out.println(user);
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
启动服务后,即可测试
[外链图片转存失败(img-EEXj9gqd-1566451453141)(https://www.github.com/OneJane/blog/raw/master/小书匠/1566447251377.png)]
tar zxf incubator-dubbo-ops.tar.gz -C /usr/local/
tar zxf apache-maven-3.6.0-bin.tar.gz -C /usr/local/
vim incubator-dubbo-ops/dubbo-admin-backend/src/main/resources/application.properties
dubbo.registry.address=zookeeper://192.168.3.237:2181
vim /etc/profile
如误操作导致基础命令丢失,export PATH=/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
export MAVEN_HOME=/usr/local/apache-maven-3.6.0
export JAVA_HOME=/usr/local/jdk
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$MAVEN_HOME/bin:$PATH
source /etc/profile
cd /usr/local/incubator-dubbo-ops && mvn clean install
vim dubbo-admin-backend/src/main/resources/application.properties server.port=8888
mvn --projects dubbo-admin-backend spring-boot:run
http://192.168.3.237:8888
Dubbo 缺省协议不适合传送大数据量的服务,比如传文件,传视频等,除非请求量很低。
Dubbo 缺省协议dubbo:// 协议采用单一长连接和 NIO 异步通讯,适合于小数据量大并发的服务调用,以及服务消费者机器数远
大于服务提供者机器数的情况。
Transporter (传输): mina, netty, grizzy
Serialization(序列化): dubbo, hessian2, java, json
Dispatcher(分发调度): all, direct, message, execution, connection
ThreadPool(线程池): fixed, cached