1.新建项目
项目命名为dubbotest,然后分别通过Moudle建立produce,dubbo_api和consumer
此处选maven-archetype-quickstart是可以的,什么都不选也没问题
4.0.0
com.wj
dubbo_demo
1.0-SNAPSHOT
dubbo_demo
http://www.example.com
UTF-8
1.7
1.7
0.3.0
2.5.3
2.8.4
4.3.6.RELEASE
1.8
UTF-8
com.alibaba
dubbo
2.5.3
org.springframework
spring
com.github.sgroschupf
zkclient
0.1
org.springframework
spring-core
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-orm
4.3.12.RELEASE
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-test
${spring.version}
org.springframework
spring-jms
4.2.1.RELEASE
org.aspectj
aspectjrt
1.7.4
org.aspectj
aspectjweaver
1.6.11
dubbo_api
dubbo_consumer
dubbo_provider
②修改product里的pom文件
dubbo_demo
com.wj
1.0-SNAPSHOT
4.0.0
com.wj
dubbo_provider
dubbo_provider
http://www.example.com
UTF-8
1.7
1.7
com.wj
dubbo_api
1.0-SNAPSHOT
compile
③dubbo_api里的pom文件
dubbo_demo
com.wj
1.0-SNAPSHOT
4.0.0
com.wj
dubbo_api
dubbo_api
http://www.example.com
UTF-8
1.7
1.7
junit
junit
4.11
test
④consumer里的pom文件
dubbo_demo
com.wj
1.0-SNAPSHOT
4.0.0
com.wj
dubbo_consumer
dubbo_consumer
http://www.example.com
UTF-8
1.7
1.7
com.wj
dubbo_api
1.0-SNAPSHOT
compile
3、配置文件和编写代码:
①在dubbo_api中定义服务接口:
package com.wj.service;
public interface HelloService {
String sayHello(String name);
}
②在produce中实现接口:
package com.wj.impl;
import com.wj.service.HelloService;
public class HelloServiceImpl implements HelloService{
@Override
public String sayHello(String name) {
return "Hello World!";
}
}
③produce项目中的resource文件包含dubbo-produce.xml和springmvc.xml配置文件,没有resource,自己新建,并标记为Resources Root
其中dubbo-produce.xml如下:
springmvc.xml:
④编写测试类:
package com.wj.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class ProviderTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:springmvc.xml");
context.start();
System.out.println("Dubbo provider");
try {
System.in.read(); // 按任意键退出
} catch (IOException e) {
e.printStackTrace();
}
}
}
⑤consumer在resource下新建dubbo-consumer.xml:
springmvc.xml:
⑥消费者测试类:
package com.wj.test;
import com.wj.service.HelloService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class ConsumerTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "classpath:springmvc.xml" });
context.start();
HelloService demoService = (HelloService) context.getBean("helloService");
System.out.println(demoService.sayHello("Hello World!wj"));
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
}
}
4、按照先后顺序,首先启动Zookeeper:zkServer.cmd 然后启动服务生产者测试类,最后启动服务消费者的测试类,当控制台输出以下结果,则代表成功。
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
Dubbo provider start...
Process finished with exit code 1
log4j:WARN Please initialize the log4j system properly.
Hello World!