欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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.bootdub...

1、首先创建一个空工程
SpringBoot整合dubbo和zookeeper
2、创建一个提供者
SpringBoot整合dubbo和zookeeper

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、创建一个消费者
SpringBoot整合dubbo和zookeeper

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);
    }
}

SpringBoot整合dubbo和zookeeper

本文地址:https://blog.csdn.net/ytsdyue/article/details/107280179