一、使用Spring Initializr 构建Dubbo服务提供者 dubbo-provider 项目
- 登录 http://start.spring.io/ 填写如下信息后点击 “Generate Project” 按钮,得到 dubbo-provider 项目骨架
- 为 dubbo-provider 项目添加依赖:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.4.10</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<exclusions>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
- 定义接口:
/**
* 测试远程调用的接口;
*/
public interface TestService {
String sayHello(String name);
}
- 实现接口方法:
import com.shawearn.dubbo.remote.TestService;
public class TestServiceImpl implements TestService {
@Override
public String sayHello(String name) {
return "Hello " + name + "!";
}
}
5、在 resource/ 下添加 providers.xml 配置文件,用于向 zookeeper 注册中心注册服务。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 配置可参考 http://dubbo.io/User+Guide-zh.htm -->
<!-- 服务提供方应用名,用于计算依赖关系 -->
<dubbo:application name="dubbo-provider" owner="dubbo-provider"/>
<!-- 定义 zookeeper 注册中心地址及协议 -->
<dubbo:registry protocol="zookeeper" address="192.168.10.41:4183" client="zkclient"/>
<!-- 定义 Dubbo 协议名称及使用的端口,dubbo 协议缺省端口为 20880,如果配置为 -1 或者没有配置 port,则会分配一个没有被占用的端口 -->
<dubbo:protocol name="dubbo" port="-1"/>
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.shawearn.dubbo.remote.TestService" ref="testService" timeout="10000"/>
<!-- 和本地 bean 一样实现服务 -->
<bean id="testService" class="com.shawearn.dubbo.provider.impl.TestServiceImpl" />
</beans>
6、DubboProviderApplication 中使用 providers.xml 配置文件。
package com.shawearn.dubbo.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@ImportResource(value = {"classpath:providers.xml"}) // 使用 providers.xml 配置;
public class DubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DubboProviderApplication.class, args);
}
}
7、application.properties 中指定项目启动时占用的端口号:
server.port=8011
- 使用 Spring Initializr 构建 Dubbo 服务消费者 dubbo-consumer 项目
2、为 dubbo-provider 项目添加依赖:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.41</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.4.10</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<exclusions>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
3、引入接口(此处偷懒了,没有将接口打成 jar 包后导入,而是直接把接口文件添加到项目下,强烈不建议此种做法):
package com.shawearn.dubbo.remote;
/**
* 测试远程调用的接口;
*/
public interface TestService {
String sayHello(String name);
}
4、定义测试用的 Controller 类:
package com.shawearn.dubbo.consumer.controller;
import com.alibaba.fastjson.JSONObject;
import com.shawearn.dubbo.remote.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* 测试用的 Controller 类;
*/
@Controller
public class TestController {
@Autowired
TestService testService;
/**
* 测试 JSON 接口;
*
* @param name 名字;
* @return
*/
@ResponseBody
@RequestMapping("/test/{name}")
public JSONObject testJson(@PathVariable("name") String name) {
JSONObject jsonObject = new JSONObject();
String testStr = testService.sayHello(name);
jsonObject.put("str", testStr);
return jsonObject;
}
}
5、 在 resource/ 下添加 consumers.xml 配置文件,用于向 zookeeper 注册中心注册服务。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 配置可参考 http://dubbo.io/User+Guide-zh.htm -->
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="dubbo-consumer" owner="dubbo-consumer"/>
<!-- 定义 zookeeper 注册中心地址及协议 -->
<dubbo:registry protocol="zookeeper" address="192.168.10.41:4183" client="zkclient" />
<!-- 生成远程服务代理,可以和本地 bean 一样使用 testService -->
<dubbo:reference id="testService" interface="com.shawearn.dubbo.remote.TestService"/>
</beans>
6、DubboConsumerApplication 中使用 consumers.xml 配置文件。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@ImportResource(value = {"classpath:consumers.xml"}) // 使用 consumers.xml 配置;
public class DubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(DubboConsumerApplication.class, args);
}
}
7、application.properties 中指定项目启动时占用的端口号:
server.port=8012