在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

Dubbo

2016-7-29 15:47| 发布者: zhangjf| 查看: 634| 评论: 0

摘要: Dubbo+Spring+注解+简单Demo(前提是搭建了Dubbo+Zookeeper,搭建可以参考http://blog.csdn.net/u010379996/article/details/51942588) public interface UserService { public ListUser getAll ...
Dubbo+Spring+注解+简单Demo(前提是搭建了Dubbo+Zookeeper,搭建可以参考http://blog.csdn.net/u010379996/article/details/51942588)
  1. public interface UserService {
  2. public List<User> getAllUsers();
  3. public String bind(String name);
  4. }
复制代码
  1. public class User implements Serializable{
  2. private static final long serialVersionUID = 1L;
  3. private String name;
  4. public String getName() {
  5. return name;
  6. }
  7. public void setName(String name) {
  8. this.name = name;
  9. }
  10. public String toString() {
  11. return "User [name=" + name + "]";
  12. }
  13. }
复制代码
  1. public class Provider {
  2. public static void main(String[] args) throws Exception {
  3. @SuppressWarnings({ "unused", "resource" })
  4. ApplicationContext context = new ClassPathXmlApplicationContext("spring-provider.xml");
  5. new Provider().test();
  6. }
  7. public void test() throws IOException{
  8. System.in.read();
  9. }
  10. }
复制代码
  1. @Component("service")
  2. @com.alibaba.dubbo.config.annotation.Service()
  3. public class UserServiceImpl implements UserService{
  4. public List<User> getAllUsers() {
  5. List<User> list = new ArrayList<User>();
  6. User u1 = new User();
  7. u1.setName("jack");
  8. User u2 = new User();
  9. u2.setName("tom");
  10. User u3 = new User();
  11. u3.setName("rose");
  12. list.add(u1);
  13. list.add(u2);
  14. list.add(u3);
  15. return list;
  16. }
  17. public String bind(String name) {
  18. System.out.println(name);
  19. return "provider";
  20. }
  21. }
复制代码
  1. public class Consumer {
  2. public static void main(String[] args) throws Exception {
  3. ApplicationContext context = new ClassPathXmlApplicationContext("spring-consumer.xml");
  4. new Consumer().test(context);
  5. }
  6. public void test(ApplicationContext context) throws IOException {
  7. ServiceFactory factory = (ServiceFactory) context.getBean("factory");
  8. UserService service = factory.getService();
  9. System.out.println(service.bind("consumer"));
  10. List<User> list = service.getAllUsers();
  11. if (list != null && list.size() > 0) {
  12. for (int i = 0; i < list.size(); i++) {
  13. System.out.println(list.get(i));
  14. }
  15. }
  16. System.in.read();
  17. }
  18. }
复制代码
  1. @Service("factory")
  2. public class ServiceFactory {
  3. @Reference
  4. private UserService service;
  5. public UserService getService() {
  6. return service;
  7. }
  8. public void setService(UserService service) {
  9. this.service = service;
  10. }
  11. }
复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-4.0.xsd
  10. http://code.alibabatech.com/schema/dubbo
  11. http://code.alibabatech.com/schema/dubbo/dubbo.xsd
  12. ">
  13. <context:annotation-config />
  14. <context:component-scan base-package="cn.consumer" />
  15. <dubbo:application name="consumer" />
  16. <dubbo:registry address="zookeeper://127.0.0.1:2181" />
  17. <dubbo:annotation package="cn.consumer"/>
  18. </beans>
复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-4.0.xsd
  10. http://code.alibabatech.com/schema/dubbo
  11. http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
  12. <context:annotation-config />
  13. <context:component-scan base-package="cn.provider" />
  14. <dubbo:application name="provider" />
  15. <dubbo:registry address="zookeeper://127.0.0.1:2181" />
  16. <dubbo:protocol name="dubbo" port="20880" />
  17. <dubbo:annotation package="cn.provider"/>
  18. </beans>
复制代码
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>cn</groupId>
  5. <artifactId>DubboDemo</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <packaging>jar</packaging>
  8. <name>DubboDemo</name>
  9. <url>http://maven.apache.org</url>
  10. <properties>
  11. <spring.version>4.0.8.RELEASE</spring.version>
  12. <zookeeper.version>3.3.3</zookeeper.version>
  13. <zkclient.version>0.1</zkclient.version>
  14. <servlet.version>2.5</servlet.version>
  15. <dubbo.version>2.5.3</dubbo.version>
  16. <log4j.version>1.2.16</log4j.version>
  17. </properties>
  18. <dependencies>
  19. <dependency>
  20. <groupId>org.springframework</groupId>
  21. <artifactId>spring-core</artifactId>
  22. <version>${spring.version}</version>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework</groupId>
  26. <artifactId>spring-aop</artifactId>
  27. <version>${spring.version}</version>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework</groupId>
  31. <artifactId>spring-tx</artifactId>
  32. <version>${spring.version}</version>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.springframework</groupId>
  36. <artifactId>spring-context</artifactId>
  37. <version>${spring.version}</version>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.springframework</groupId>
  41. <artifactId>spring-orm</artifactId>
  42. <version>${spring.version}</version>
  43. </dependency>
  44. <dependency>
  45. <groupId>org.springframework</groupId>
  46. <artifactId>spring-context-support</artifactId>
  47. <version>${spring.version}</version>
  48. </dependency>
  49. <dependency>
  50. <groupId>com.alibaba</groupId>
  51. <artifactId>dubbo</artifactId>
  52. <version>${dubbo.version}</version>
  53. </dependency>
  54. <dependency>
  55. <groupId>com.github.sgroschupf</groupId>
  56. <artifactId>zkclient</artifactId>
  57. <version>${zkclient.version}</version>
  58. </dependency>
  59. <dependency>
  60. <groupId>org.apache.zookeeper</groupId>
  61. <artifactId>zookeeper</artifactId>
  62. <version>${zookeeper.version}</version>
  63. </dependency>
  64. <dependency>
  65. <groupId>log4j</groupId>
  66. <artifactId>log4j</artifactId>
  67. <version>${log4j.version}</version>
  68. </dependency>
  69. </dependencies>
  70. </project>
复制代码
  1. log4j.rootLogger=WARN, stdout, R
  2. log4j.appender.stdout=org.apache.log4j.ConsoleAppender
  3. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
  4. # Pattern to output the caller's file name and line number.
  5. #log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
  6. # Print the date in ISO 8601 format
  7. log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
  8. log4j.appender.R=org.apache.log4j.RollingFileAppender
  9. log4j.appender.R.File=example.log
  10. log4j.appender.R.MaxFileSize=100KB
  11. # Keep one backup file
  12. log4j.appender.R.MaxBackupIndex=1
  13. log4j.appender.R.layout=org.apache.log4j.PatternLayout
  14. log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
  15. # Print only messages of level WARN or above in the package com.foo.
  16. log4j.logger.com.foo=WARN
复制代码

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-5-6 13:29

Copyright 2015-2025 djqfx

返回顶部