Skip to content

Commit 7a6c679

Browse files
author
quding
committed
add dubbo demo
1 parent d0e943a commit 7a6c679

File tree

23 files changed

+561
-56
lines changed

23 files changed

+561
-56
lines changed
Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,31 @@
11
package cn.mrdear.client.dto;
22

3+
import lombok.Data;
4+
5+
import java.io.Serializable;
6+
37
/**
8+
* 用户DTO,用于Service层传输
49
* @author Niu Li
510
* @since 2017/6/12
611
*/
7-
public class UserDTO {
12+
@Data
13+
public class UserDTO implements Serializable{
14+
15+
private static final long serialVersionUID = 4086492518942464226L;
16+
17+
private Long id;
18+
19+
private String username;
20+
21+
private String password;
22+
23+
private Integer age;
24+
25+
private String nickname;
26+
27+
private String mail;
28+
29+
private String memo;
30+
831
}

Dubbo-Demo/Service-Client/src/main/java/cn/mrdear/client/service/IUserService.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import java.util.List;
77

88
/**
9-
* 用户服务
9+
* 用户服务,一般都会在返回层再包裹一层,这里简而化之
1010
* @author Niu Li
1111
* @since 2017/6/12
1212
*/
@@ -28,5 +28,10 @@ public interface IUserService {
2828
*/
2929
UserDTO updateById(UserDTO userDTO);
3030

31+
/**
32+
* 根据id删除用户
33+
*/
34+
Boolean deleteById(Long id);
35+
3136

3237
}

Dubbo-Demo/Service-Consumer/pom.xml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,49 @@
1111
<artifactId>Service-Consumer</artifactId>
1212

1313

14+
<dependencies>
15+
<dependency>
16+
<groupId>cn.mrdear.dubbo</groupId>
17+
<artifactId>Service-Client</artifactId>
18+
<version>1.0.0</version>
19+
</dependency>
20+
21+
<dependency>
22+
<groupId>org.springframework</groupId>
23+
<artifactId>spring-context</artifactId>
24+
<version>4.3.2.RELEASE</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework</groupId>
28+
<artifactId>spring-test</artifactId>
29+
<version>4.3.2.RELEASE</version>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>com.alibaba</groupId>
34+
<artifactId>dubbo</artifactId>
35+
<version>2.5.3</version>
36+
<exclusions>
37+
<exclusion>
38+
<artifactId>spring</artifactId>
39+
<groupId>org.springframework</groupId>
40+
</exclusion>
41+
</exclusions>
42+
</dependency>
43+
<dependency>
44+
<groupId>com.101tec</groupId>
45+
<artifactId>zkclient</artifactId>
46+
<version>0.10</version>
47+
<exclusions>
48+
<exclusion>
49+
<groupId>log4j</groupId>
50+
<artifactId>log4j</artifactId>
51+
</exclusion>
52+
<exclusion>
53+
<groupId>org.slf4j</groupId>
54+
<artifactId>slf4j-log4j12</artifactId>
55+
</exclusion>
56+
</exclusions>
57+
</dependency>
58+
</dependencies>
1459
</project>

Dubbo-Demo/Service-Consumer/src/main/java/cn/mrdear/consumer/manager/UserManager.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
package cn.mrdear.consumer.manager.impl;
1+
package cn.mrdear.consumer.manager;
2+
3+
import cn.mrdear.client.dto.UserDTO;
24

35
/**
46
* manager
@@ -8,4 +10,6 @@
810

911
public interface UserManager {
1012

13+
UserDTO findById(Long id);
14+
1115
}
Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
1-
package cn.mrdear.consumer.manager;
1+
package cn.mrdear.consumer.manager.impl;
2+
3+
import org.springframework.stereotype.Service;
4+
5+
import cn.mrdear.client.dto.UserDTO;
6+
import cn.mrdear.client.service.IUserService;
7+
import cn.mrdear.consumer.manager.UserManager;
8+
9+
import javax.annotation.Resource;
210

311
/**
12+
* manager调用远程RPC组装数据,此处调试只是再加一层封装
413
* @author Niu Li
514
* @since 2017/6/13
615
*/
7-
public class UserManagerImpl {
16+
@Service
17+
public class UserManagerImpl implements UserManager {
18+
19+
@Resource
20+
private IUserService userService;
21+
822

23+
@Override
24+
public UserDTO findById(Long id) {
25+
return userService.findById(id);
26+
}
927
}

Dubbo-Demo/Service-Consumer/src/main/resources/applicationContext.xml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xmlns:context="http://www.springframework.org/schema/context"
55
xmlns:task="http://www.springframework.org/schema/task"
6-
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
6+
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
7+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
78

89
<!-- 启用注解 -->
910
<context:annotation-config/>
@@ -12,4 +13,18 @@
1213

1314
<context:component-scan base-package="cn.mrdear.consumer"/>
1415

16+
<!-- 提供方应用信息,用于计算依赖关系 -->
17+
<dubbo:application name="user_consumer" />
18+
19+
<!-- 使用zookeeper注册中心暴露服务地址 -->
20+
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
21+
22+
<!--配置本地地址-->
23+
<dubbo:protocol host="127.0.0.1"/>
24+
25+
<!-- 声明需要暴露的服务接口 -->
26+
<dubbo:reference interface="cn.mrdear.client.service.IUserService" id="userService"
27+
check="false" retries="2" cluster="failover" loadbalance="roundrobin"
28+
cache="lru" version="1.0.0"/>
29+
1530
</beans>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,31 @@
11
package cn.mrdear.test;
22

3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.test.context.ContextConfiguration;
7+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
8+
9+
import cn.mrdear.client.dto.UserDTO;
10+
import cn.mrdear.consumer.manager.UserManager;
11+
12+
import javax.annotation.Resource;
13+
314
/**
415
* @author Niu Li
516
* @since 2017/6/13
617
*/
18+
@RunWith(SpringJUnit4ClassRunner.class)
19+
@ContextConfiguration(locations = "classpath:applicationContext.xml")
720
public class UserManagerTest {
21+
22+
@Resource
23+
private UserManager userManager;
24+
25+
26+
@Test
27+
public void testFindById() {
28+
UserDTO userDTO = userManager.findById(1L);
29+
Assert.assertNotNull(userDTO);
30+
}
831
}

Dubbo-Demo/Service-Consumer/src/test/resources/applicationContext.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@
2222

2323
<!-- 声明需要暴露的服务接口 -->
2424
<dubbo:reference interface="cn.mrdear.client.service.IUserService" id="userService"
25-
check="false" />
25+
check="false" version="1.0.0"/>
2626

2727
</beans>

Dubbo-Demo/Service-Provider/pom.xml

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,110 @@
1111
<artifactId>Service-Provider</artifactId>
1212

1313

14+
<dependencies>
15+
<dependency>
16+
<groupId>cn.mrdear.dubbo</groupId>
17+
<artifactId>Service-Client</artifactId>
18+
<version>1.0.0</version>
19+
</dependency>
20+
<dependency>
21+
<groupId>com.alibaba</groupId>
22+
<artifactId>dubbo</artifactId>
23+
<version>2.5.3</version>
24+
<exclusions>
25+
<exclusion>
26+
<groupId>org.springframework</groupId>
27+
<artifactId>spring</artifactId>
28+
</exclusion>
29+
<exclusion>
30+
<groupId>org.javassist</groupId>
31+
<artifactId>javassist</artifactId>
32+
</exclusion>
33+
</exclusions>
34+
</dependency>
35+
<dependency>
36+
<groupId>com.101tec</groupId>
37+
<artifactId>zkclient</artifactId>
38+
<version>0.10</version>
39+
<exclusions>
40+
<exclusion>
41+
<groupId>log4j</groupId>
42+
<artifactId>log4j</artifactId>
43+
</exclusion>
44+
<exclusion>
45+
<groupId>org.slf4j</groupId>
46+
<artifactId>slf4j-log4j12</artifactId>
47+
</exclusion>
48+
</exclusions>
49+
</dependency>
50+
<dependency>
51+
<groupId>com.h2database</groupId>
52+
<artifactId>h2</artifactId>
53+
<version>1.4.187</version>
54+
</dependency>
55+
<dependency>
56+
<groupId>org.springframework</groupId>
57+
<artifactId>spring-context</artifactId>
58+
<version>4.3.2.RELEASE</version>
59+
</dependency>
60+
<dependency>
61+
<groupId>org.springframework</groupId>
62+
<artifactId>spring-test</artifactId>
63+
<version>4.3.2.RELEASE</version>
64+
</dependency>
65+
<!--aspectj start-->
66+
<dependency>
67+
<groupId>org.aspectj</groupId>
68+
<artifactId>aspectjweaver</artifactId>
69+
<version>1.8.6</version>
70+
</dependency>
71+
<dependency>
72+
<groupId>org.aspectj</groupId>
73+
<artifactId>aspectjrt</artifactId>
74+
<version>1.8.6</version>
75+
</dependency>
76+
<!--aspectj end-->
77+
<!--cglib start-->
78+
<dependency>
79+
<groupId>cglib</groupId>
80+
<artifactId>cglib</artifactId>
81+
<version>3.2.2</version>
82+
</dependency>
83+
<!--cglib end-->
84+
<!--hibernate start-->
85+
<dependency>
86+
<groupId>org.hibernate</groupId>
87+
<artifactId>hibernate-core</artifactId>
88+
<version>5.1.0.Final</version>
89+
</dependency>
90+
<dependency>
91+
<groupId>org.hibernate</groupId>
92+
<artifactId>hibernate-entitymanager</artifactId>
93+
<version>5.1.0.Final</version>
94+
</dependency>
95+
<!--hibernate end-->
96+
<!--mysql start-->
97+
<dependency>
98+
<groupId>mysql</groupId>
99+
<artifactId>mysql-connector-java</artifactId>
100+
<version>5.1.34</version>
101+
</dependency>
102+
<!--mysql end-->
103+
<!--alibaba start-->
104+
<dependency>
105+
<groupId>com.alibaba</groupId>
106+
<artifactId>druid</artifactId>
107+
<version>1.0.19</version>
108+
</dependency>
109+
<!--alibaba end-->
110+
111+
<!--JPA start-->
112+
<dependency>
113+
<groupId>org.springframework.data</groupId>
114+
<artifactId>spring-data-jpa</artifactId>
115+
<version>1.10.4.RELEASE</version>
116+
</dependency>
117+
<!--JPA end-->
118+
119+
</dependencies>
14120
</project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
package cn.mrdear.provider;
22

3+
import org.springframework.context.support.ClassPathXmlApplicationContext;
4+
5+
import java.io.IOException;
6+
37
/**
48
* @author Niu Li
59
* @since 2017/6/12
610
*/
711
public class Application {
12+
public static void main(String[] args) throws IOException {
13+
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
14+
context.start();
15+
System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟
16+
}
817
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,42 @@
11
package cn.mrdear.provider.convert;
22

3+
import org.springframework.beans.BeanUtils;
4+
import org.springframework.util.CollectionUtils;
5+
6+
import cn.mrdear.client.dto.UserDTO;
7+
import cn.mrdear.provider.domain.User;
8+
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
import java.util.stream.Collectors;
12+
313
/**
414
* @author Niu Li
515
* @since 2017/6/12
616
*/
717
public class UserConvert {
18+
19+
public static UserDTO toDTO(User user) {
20+
UserDTO userDTO = new UserDTO();
21+
BeanUtils.copyProperties(user,userDTO);
22+
return userDTO;
23+
}
24+
25+
26+
public static List<UserDTO> toDTOS(List<User> users) {
27+
if (CollectionUtils.isEmpty(users)) {
28+
return new ArrayList<>(1);
29+
}
30+
List<UserDTO> results = new ArrayList<>();
31+
return users.stream().map(UserConvert::toDTO)
32+
.collect(Collectors.toList());
33+
}
34+
35+
36+
public static User toDO(UserDTO userDTO) {
37+
User user = new User();
38+
BeanUtils.copyProperties(userDTO,user);
39+
return user;
40+
}
41+
842
}

0 commit comments

Comments
 (0)