标签:查看 code recipe prope 引用 ica VID rap 项目
<!-- 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> <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>
public interface TickerService { String getTicket(); }
import org.apache.dubbo.config.annotation.Service; import org.springframework.stereotype.Component; @Service //这个是dubbo的service注解不是spring的,加了这个注解在项目启动后就自动注册到注册中心zookeeper中 @Component //这个在spring中注册,由spring管理的组件,不用Service是因为为了避免与dubbo的service冲突(而忘了import) public class TickerServiceImpl implements TickerService { @Override public String getTicket() { return "getTicket success!"; } }
server.port=8001 #当前应用名字 dubbo.application.name=provider-server #注册中心地址 dubbo.registry.address=zookeeper://127.0.0.1:2181 #扫描指定包下服务 dubbo.scan.base-packages=ustc.wzh.service
import org.apache.dubbo.config.annotation.Reference; import org.springframework.stereotype.Service; @Service //spring容器管理 public class UserService { //拿到远程的provider-server提供的服务 @Reference //dubbo的注解用于引用服务,引用服务方式有两种:1.Pom坐标,2.需要定义路径相同的接口名 TickerService tickerService; public void buyTicket(){ String ticket = tickerService.getTicket(); System.out.println("在注册中心拿到:"+ticket); } }
import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import ustc.wzh.service.UserService; @SpringBootTest class ConsumerServerApplicationTests { @Autowired UserService userService; @Test void contextLoads() throws InterruptedException { while (true){ Thread.sleep(1000); userService.buyTicket(); } } }
server.port=8002
#当前应用名字
dubbo.application.name=consumer-server
#注册中心地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
9.2 SpringBoot使用Zookeeper和Dubbo
标签:查看 code recipe prope 引用 ica VID rap 项目
原文地址:https://www.cnblogs.com/zhihaospace/p/12449748.html