本机搭建Springboot+dubbo+zookeeper的项目
还推荐大家看一下官方的文档(中文的):
https://github.com/apache/dubbo-spring-boot-project/blob/master/README_CN.md
网上有很多下载dubbo和zookeeper的博客,我就不赘述了,主要来写一下项目
1.首先创建一个springboot项目(我的项目名称为:dubboprovider1),引入依赖包:
//
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.7</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
2.创建一个sevrice包在其下创建GreetingsService接口和GreetingsServiceImpl类
下面是各个类代码:
public interface GreetingsService {
String sayHi(String name);
}
@Service
public class GreetingsServiceImpl implements GreetingsService {
public String sayHi(String name) {
return "hi,"+name;
}
}
接下来在resources下创建一个provider.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!-- xsi:xml schema instance xml模式实例 -->
<!-- xmlns *.xml文件中用到的命名空间 -->
<!-- xmlns:xsi *.xml遵守xml规范 -->
<!-- xmlns:dubbo 引入dubbo前缀 -->
<!-- xsi:schemaLocation 具体用到的schema资源,必须加上dubbo的资源路径 -->
<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="demo-provider" version="1.0"/>
<!-- 服务将会注册到zookeeper上 ,端口必须与zookeeper配置的端口一致-->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!-- 指明程序名和发布的端口 -->
<dubbo:protocol name="dubbo" port="20880"/>
<!-- 把实现类放入到spring的容器中 -->
<bean id="greetingsService" class="com.feker.dubboprovider1.service.GreetingsServiceImpl"/>
<!-- 注册到服务的注册表上 -->
<dubbo:service interface="com.feker.dubboprovider1.service.GreetingsService" ref="greetingsService"/>
</beans>
还有最后主类:
@ImportResource("classpath:provider.xml")
@SpringBootApplication
public class DubboProvider1Application {
public static void main(String[] args) {
SpringApplication.run(DubboProvider1Application.class, args);
}
}
以上先完成了provider的项目,接下来:
去zookeeper的bin目录下通过zkServer.cmd启动Zookeeper的服务器,然后就是启动我们刚写的项目;接下来去zookeeper的bin目录下通过zkCli.cmd启动Zookeeper的客户端,查看已经注册的服务:
等zkCli.cmd加载完 你输入ls /dubbo就可以查看了,这里显示了我已经在dubbo注册的
第二大步就行写消费者:
同意引入相同的依赖,创建dubbo-consumer1项目
ConsumerController类的代码
@RestController
public class ConsumerController {
@Autowired
private GreetingsService greetingsService;
@GetMapping("/hello")
public String sayHi(){
return greetingsService.sayHi(new String("kk"));
}
}
在resources下创建一个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"
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="demo-customer" version="1.0"/>
<!-- 请求的服务注册的zookeeper的地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!-- 注册到服务的注册表上 -->
<dubbo:reference interface="com.feker.dubboprovider1.service.GreetingsService" id="greetingsService" check="false"/>
</beans>
主类代码:
@ImportResource("classpath:consumer.xml")
@SpringBootApplication
public class DubboConsumer1Application {
public static void main(String[] args) {
SpringApplication.run(DubboConsumer1Application.class, args);
}
}
接下来启动消费者项目就可以了
再访问controller的页面请求:
好了,大功告成
如果,你想在Dubbo Admin查看在dubbo注册的信息可以去Dubbo目录下:
这个dubbo-admin-2.5.10.war文件复制到Tomcat的webapp目录下然后启动tomcat:在浏览器输入127.0.0.1:8080/dubbo-admin-2.5.10去查看这个界面,这里页有显示dubbo上的生产者和消费者哦
但是这里可能可能可能可能可能存在一个问题,就是tomcat端口被zookeeper占用了,那么我们去zookeeper的conf目录下复制zoo_sample.cfg文件新命名为zoo.cfg
打开zoo.cfg(notepad++或者sublime)向其中添加一行
admin.serverPort=8079
意思就是改它改一个端口
这样就大功搞成了。。。