在dubbo-admin-0.0.1-SNAPSHOT.jar的路径下启动dubbo,java -jar dubbo-admin-0.0.1-SNAPSHOT.jar dubbo-admin-0.0.1-SNAPSHOT.jar的包需要提前打好
启动zookeeper的zkServer.cmd和zkCli.cmd
需要的依赖
<!-- 导入依赖:zookeeper + dubbo -->
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
<!-- zkclient -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!-- 引入zookeeper依赖 -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.12.0</version>
</dependency>
<!-- 上面两个都是zookeeper服务端的一个依赖-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.14</version>
<!--排除这个slf4j-log4j12-->
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
服务端和消费端都需要这些依赖
服务端
在服务端设置一个服务
@Component
@Service
public class TickServiceImp implements TicketService {
@Override
public String getTick() {
return "《今天是个好日子》";
}
}
这里的@Service是dubbo下的用来吧服务注册到注册中心 @Component用来把类注入到容器中
application.yml配置如下
server:
port: 8001
#服务应用名字
dubbo:
application:
name: provider-server
#注册中心地址
registry:
address: zookeeper://127.0.0.1:2181
#那些服务要被注册
scan:
base-packages: com.cfz.service
启动服务端,在zookeeper设置的端口http://localhost:7001下查看
消费端
首先导入所需依赖(同上文)
设置服务
这里的接口要和服务端的相同
Userservice类中如下
@Service
public class Userservice {
@Reference
TicketService ticketService;
public void buyTicket() {
System.out.println("在注册中心拿到"+ticketService.getTick());
}
}
application.yml配置如下 启动消费端
|