目录
1 API模块
1.1 pom.xml
1.2 UserService
2 服务提供者模块
2.1 pom.xml
2.2 application.properties
2.3 启动类
2.4 UserServiceImpl
3 服务消费者模块
3.1 pom.xml
3.2 application.properties
3.3 启动类
3.4 UserController
4 运行及结果
5 Dubbo Admin
我搭建所使用的Spring Boot版本是2.1.8.RELEASE,dubbo-spring-boot-starter的版本是2.7.3,ZooKeeper的版本是3.4.14。
API模块由服务提供方为服务消费方暴露接口,一般将接口和pojo放入其中,提供者和消费者引入该模块即可。创建一个Spring Boot模块,命名为dubbo-user-interface。
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.8.RELEASE
com.hys
dubbo-user-interface
0.0.1-SNAPSHOT
dubbo-user-interface
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
org.apache.dubbo
dubbo-spring-boot-starter
2.7.3
org.springframework.boot
spring-boot-maven-plugin
package com.hys.dubbo.user.service;
public interface UserService {
String sayHello(String name);
}
创建一个Spring Boot模块,命名为dubbo-user-provider。
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.8.RELEASE
com.hys
dubbo-user-provider
0.0.1-SNAPSHOT
dubbo-user-provider
Demo project for Spring Boot
1.8
com.hys
dubbo-user-interface
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
org.apache.dubbo
dubbo-spring-boot-starter
2.7.3
org.apache.zookeeper
zookeeper
3.4.14
pom
org.apache.curator
curator-recipes
2.13.0
org.springframework.boot
spring-boot-maven-plugin
dubbo.application.name=dubbo-user-provider
dubbo.registry.protocol=zookeeper
dubbo.registry.address=192.168.253.129:2181,192.168.253.129:2182,192.168.253.129:2183
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
dubbo.monitor.protocol=registry
package com.hys.dubbo;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableDubbo
@SpringBootApplication
public class DubboUserProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DubboUserProviderApplication.class, args);
}
}
package com.hys.dubbo.user.service.impl;
import com.hys.dubbo.user.service.UserService;
import org.apache.dubbo.config.annotation.Service;
@Service(version = "1.0.0", timeout = 10000, interfaceClass = UserService.class)
public class UserServiceImpl implements UserService {
@Override
public String sayHello(String name) {
return "Hello " + name + " !";
}
}
这里需要注意的是@Service注解使用的是Dubbo而不是Spring的注解。
创建一个Spring Boot模块,命名为dubbo-user-consumer。
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.8.RELEASE
com.hys
dubbo-user-consumer
0.0.1-SNAPSHOT
dubbo-user-consumer
Demo project for Spring Boot
1.8
com.hys
dubbo-user-interface
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-web
org.apache.dubbo
dubbo-spring-boot-starter
2.7.3
org.apache.zookeeper
zookeeper
3.4.14
pom
org.apache.curator
curator-recipes
2.13.0
org.springframework.boot
spring-boot-maven-plugin
server.port=8081
dubbo.application.name=dubbo-user-consumer
dubbo.registry.address=zookeeper://192.168.253.129:2181,192.168.253.129:2182,192.168.253.129:2183
dubbo.monitor.protocol=registry
package com.hys.dubbo;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableDubbo
@SpringBootApplication
public class DubboUserConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(DubboUserConsumerApplication.class, args);
}
}
package com.hys.dubbo.user.controller;
import com.hys.dubbo.user.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/dubbo")
public class UserController {
@Reference(version = "1.0.0")
private UserService userService;
@RequestMapping("/sayHello/{name}")
public String sayHello(@PathVariable("name") String name) {
String result = userService.sayHello(name);
return result;
}
}
分别启动上述三个Spring Boot模块,页面访问http://localhost:8081/dubbo/sayHello/Robert Hou,结果如下所示:
由上可以看到,访问成功,并且在ZooKeeper中可以看到已经含有Dubbo的相关注册信息了:
新版本Dubbo Admin的运行需要依赖于Node.js,在官网上下载即可:
下载完成后打开cmd,输入npm查看该命令是否可用:
然后输入以下的命令来配置国内的镜像站(不配置的话,在后续打包的过程中会等待非常久的时间):
npm config set registry=https://registry.npm.taobao.org
配置好了后会在C盘用户下生成一个.npmrc的文件:
打开后里面的内容即是我们刚才配置的镜像地址:
之后,我们克隆dubbo-admin的项目代码到当前的工作空间:
git clone https://github.com/apache/dubbo-admin.git
克隆完毕后,我的idea工作空间如下所示:
修改其中dubbo-admin-server子模块中的application.properties文件中的内容,将其中ZooKeeper的注册信息改成相应的内容(如果是本地开启的ZooKeeper,则不需要更改。我是虚拟机搭建的ZooKeeper伪集群,需要更改):
# centers in dubbo2.7
admin.registry.address=zookeeper://192.168.101.128:2181,192.168.101.128:2182,192.168.101.128:2183
admin.config-center=zookeeper://192.168.101.128:2181,192.168.101.128:2182,192.168.101.128:2183
admin.metadata-report.address=zookeeper://192.168.101.128:2181,192.168.101.128:2182,192.168.101.128:2183
因为该代码是develop分支的最新代码,我在打包过程中出现了测试代码没通过的情况,所以这里我把dubbo-admin-server子模块中的所有测试代码都删掉了,确保打包顺利进行。如果各位打包过程顺利,则不需要删除。
最后就是利用idea自身的maven插件进行打包,如果打包成功后,会在dubbo-admin\dubbo-admin-distribution\target路径下生成一个dubbo-admin-0.1.jar文件,如下所示:
java -jar运行它,页面访问http://localhost:8080,即可看到管理页面: