springboot学习25:Dubbo+zookeeper实战
-
框架搭建:
-
1,创建一个空的maven项目:Empty Project ; (做为总依赖,或者父项目)
-
导入pom依赖:
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>2.7.9version>
dependency>
<dependency>
<groupId>com.github.sgroschupfgroupId>
<artifactId>zkclientartifactId>
<version>0.1version>
dependency>
?
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-frameworkartifactId>
<version>2.12.0version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-recipesartifactId>
<version>2.12.0version>
dependency>
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>3.4.14version>
<exclusions>
<exclusion>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
exclusion>
exclusions>
dependency>
-
-
2,新建model模块:provider-server:服务提供者
-
新建service包:
-
1,新建TicketService接口:
public interface TicketService {
?
public String getTicket();
} -
2,新建TicketServiceImpl实现类:
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;
? -
3,修改application.properties配置文件:
-
配置要扫描的包
server.port=8001
#当前应用名字
dubbo.application.name=provider_server
#注册中心地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.registry.protocol=26001
#扫描指定包下服务
dubbo.scan.base-packages=com.ljxdemo.service -
-
-
-
3,新建Model模块:consumer-server:服务消费者
-
新建service包:
-
1,新建TicketService接口:
public interface TicketService {
?
public String getTicket();
} -
2,新建UserService服务类:
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;
?
? -
3,修改application.properties配置文件:
server.port=8002
#消费者去哪里拿服务 : 去注册中心拿 ; 消费者需暴漏自己的名字,让注册中心知道是谁拿的
dubbo.application.name=consumer_server
#注册中心的地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
-
-
新建controller包:
-
1,新建HelloController类:
-
-
-
4,启动测试:
-
1,启动zookeeper服务:zkServer.cmd
-
2,启动dubbo-admin服务:java -jar dubbo-admin-0.0.1-SNAPSHOT.jar
-
3,启动IDEA服务:提供者,消费者
-
4,测试URL:http://localhost:8002/t1 : 数据正常返回
-
-