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