SpringBoot整合dubbo和zookeeper
程序员文章站
2022-04-10 10:06:14
1、首先创建一个空工程2、创建一个提供者dubbo.application.name=providerdubbo.registry.address=zookeeper://192.168.124.15:2181dubbo.scan.base-packages=com.example.provider.servicecom.alibaba.boot dub...
1、首先创建一个空工程
2、创建一个提供者
dubbo.application.name=provider
dubbo.registry.address=zookeeper://192.168.124.15:2181
dubbo.scan.base-packages=com.example.provider.service
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.1.0</version>
</dependency>
<!--引入zookeeper的客户端工具-->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
public interface ProviderService {
String provider();
}
@Service
@com.alibaba.dubbo.config.annotation.Service //将服务发布出去
public class ProviderServiceImpl implements ProviderService {
@Override
public String provider() {
return "小欢喜";
}
}
3、创建一个消费者
dubbo.application.name=consumer
dubbo.registry.address=zookeeper://192.168.124.15:2181
server.port=8081
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.1.0</version>
</dependency>
<!--引入zookeeper的客户端工具-->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
@Service
public class ConsumerService {
@Reference
ProviderService providerService;
public String consumer(){
return providerService.provider();
}
}
4、将provider中的service接口放一份相同的到consumer中(包名和接口名必须相同,因为用到了反射)
5、测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class ConsumerApplicationTests {
@Autowired
ConsumerService consumerService;
@Test
public void contextLoads() {
String info = consumerService.consumer();
System.err.println(info);
}
}
本文地址:https://blog.csdn.net/ytsdyue/article/details/107280179
推荐阅读
-
SpringBoot+Dubbo+Zookeeper整合搭建简单的分布式应用
-
SpringBoot 整合jdbc和mybatis
-
手把手教你Dubbo与SpringBoot常用两种方式整合
-
Spring Cloud和Dubbo整合开发笔记(1)
-
带着新人学springboot的应用12(springboot+Dubbo+Zookeeper 下)
-
带着新人学springboot的应用11(springboot+Dubbo+Zookeeper 上)
-
SpringBoot整合Redis、ApachSolr和SpringSession的示例
-
SpringBoot整合Swagger和Actuator
-
SpringBoot整合SpringMVC之自定义JSON序列化器和反序列化器
-
springboot整合dubbo+zookeeper最新详细