一、环境搭建
从 https://github.com/apache/dubbo-admin/tree/master clone下来
把里面的dubbo-admin拷贝一份并idea打开
1、启动dubbo-admin
进入目录,修改dubbo-admin配置
修改 src\main\resources\application.properties 指定zookeeper地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
1、打包dubbo-admin
cmd进入dubbo-admin根目录
mvn clean package
2、运行dubbo-admin
java -jar dubbo-admin-0.0.1-SNAPSHOT.jar
3、访问
localhost:7001
在这里面可查看注册到zookeeper的服务
二、编码
1、注解方式
1.1 目录
新建一个父工程,在父工程下新建三个module,分别是 provider、consumer、interface
1.2 引入依赖(maven)
父工程
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.0.4.RELEASE</version>
<relativePath></relativePath>
</parent>
版本控制,统一子模块的版本
interface
这个模块主要用来存放javabean、工具类等,因为这里没用到什么工具,就简单的demo,所以没有引入其他的依赖
provider
除了其他的一些依赖,还需要引入自己的 interface
<dependencies>
<dependency>
<groupId>indi.xyouzi</groupId>
<artifactId>interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-registry-zookeeper -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
<version>2.7.8</version>
<!--<scope>test</scope>-->
</dependency>
</dependencies>
consumer
除了其他的一些依赖,还需要引入自己的 interface
<dependencies>
<dependency>
<groupId>indi.xyouzi</groupId>
<artifactId>interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-registry-zookeeper -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
<version>2.7.8</version>
<!--<scope>test</scope>-->
</dependency>
</dependencies>
1.3 代码
interface
在公共模块中编写公共的接口,在消费者或提供者中实现,降低耦合
接口 indi.xyouzi.service
public interface PrintService {
Integer print();
}
provider
实现类 indi.xyouzi.service.impl
@DubboService // dubbo包下的注解,暴露服务
public class PrintServiceImpl implements PrintService {
@Override
public Integer print() {
System.out.println("hello world");
return 1002;
}
}
启动类 indi.xyouzi
@SpringBootApplication
@EnableDubbo // 启用dubbo注解
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
consumer
controller层 indi.xyouzi.controller
@RestController
public class PrintController {
@DubboReference // dubbo包下的注解,引用服务
PrintService printService;
@ResponseBody
@RequestMapping("/index")
public Integer print() {
return printService.print();
}
}
启动类 indi.xyouzi
@SpringBootApplication
@EnableDubbo // 启用dubbo注解
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
1.4 配置
provider
application.properties
#应用名
dubbo.application.name=provider
#注册地址
dubbo.registry.address=127.0.0.1:2181
#注册协议
dubbo.registry.protocol=zookeeper
#上面两句等价于 dubbo.registry.address=zookeeper://127.0.0.1:2181
#dubbo协议
dubbo.protocol.name=dubbo
#dubbo端口
dubbo.protocol.port=20880
#监视器协议
dubbo.monitor.protocol=register
consumer
application.properties
server.port=9999
#应用名
dubbo.application.name=consumer
#注册中心地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
#监视器协议
dubbo.monitor.protocol=registry
1.5 启动
- 先后启动提供者消费者
- 测试 localhost:9999/index
- 可得到返回结果 1002
- 同时控制台会打印 hello world
2、xml配置方式
目录和依赖都与上文一致
2.1 代码
interface无需变动
provider
实现类 indi.xyouzi.service.impl
将@DubboService改成@Component或@Service(Spring包下的)
@Component
public class PrintServiceImpl implements PrintService {
@Override
public Integer print() {
System.out.println("hello world");
return 1002;
}
}
启动类 indi.xyouzi
将@EnableDubbo改为@ImportResource(locations = “classpath:consumer.xml”),路径是存放在resources下的
@SpringBootApplication
@ImportResource(locations = "classpath:provider.xml")
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
consumer
controller层 indi.xyouzi.controller
将@DubboReference改成@Autowired或其他相关注解
@RestController
public class PrintController {
@Autowired
PrintService printService;
@ResponseBody
@RequestMapping("/index")
public Integer print() {
return printService.print();
}
}
启动类 indi.xyouzi
将@EnableDubbo改为@ImportResource(locations = “classpath:consumer.xml”),路径是存放在resources下的
@SpringBootApplication
@ImportResource(locations = "classpath:consumer.xml")
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
2.2 配置
provider
provider.xml
<?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">
<!--应用名-->
<dubbo:application name="provider"></dubbo:application>
<!--注册中心-->
<dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181"></dubbo:registry>
<!--dubbo协议-->
<dubbo:protocol name="dubbo" port="20880"></dubbo:protocol>
<!--暴露服务,ref指向服务真正实现的对象-->
<dubbo:service interface="indi.xyouzi.service.PrintService"
ref="PrintServiceImpl">
<dubbo:method name="print"></dubbo:method>
</dubbo:service>
<!--注入bean-->
<bean id="PrintServiceImpl" class="indi.xyouzi.service.impl.PrintServiceImpl"></bean>
</beans>
consumer
consumer.xml
<?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"
xmlns:context="http://www.springframework.org/schema/context"
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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--应用名-->
<dubbo:application name="consumer"></dubbo:application>
<!--注册中心-->
<dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181"></dubbo:registry>
<!--dubbo协议-->
<dubbo:protocol name="dubbo" port="20880"></dubbo:protocol>
<!--暴露服务、方法-->
<dubbo:reference id="PrintService" interface="indi.xyouzi.service.PrintService">
<dubbo:method name="print"></dubbo:method>
</dubbo:reference>
</beans>