Dubbo+Zookeeper依赖
<!--dubbo-->
<!-- Dubbo Spring Boot Starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
<!--zookeeper-->
<!-- https://mvnrepository.com/artifact/com.github.sgroschupf/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>4.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.0.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>
服务者代码
yml
server:
port: 8086
dubbo:
application:
name: firstone #暴露服务的名字
registry: #连接的zookeeper地址
address: zookeeper://192.168.93.131:2181
##协议端口
protocol:
name: dubbo
port: 20880
scan: ##扫描路径,改成自己@Service那个包
base-packages: com.xiao.dubboprovider
实现类代码
@Service // 注意使用dubbo
public class TestInterfaceImpl implements TestInterface {
@Override
public void ins() {
System.out.println("insert");
}
@Override
public void sel() {
System.out.println("select");
}
@Override
public void del() {
System.out.println("delete");
}
@Override
public void upd() {
System.out.println("update");
}
}
消费者代码
yml
server:
port: 8088
dubbo:
application:
name: firstwo
#注册中心
registry:
address: zookeeper://192.168.93.131:2181
scan: ##扫描路径,改成自己@Service那个包
base-packages: com.xiao.consumer
java
@RestController
@RequestMapping("/test")
public class TestController {
@Reference
private TestInterface testInterface;
@RequestMapping("/dubbo")
public void testController(){
testInterface.upd();
}
}