创建一个maven项目,删除src目录将其作物父项目
分别创建demo-provider(服务方),demo-consumer(消费方)两个springBoot项目,创建的时候需要配置热部署和web依赖
集成步骤:前提zookeeper的服务一定要打开
服务方:
org.apache.dubbo
dubbo-spring-boot-starter
3.0.10
com.github.sgroschupf
zkclient
0.1
org.apache.curator
curator-framework
5.3.0
org.apache.curator
curator-recipes
5.3.0
org.apache.curator
curator-x-discovery
5.3.0
org.apache.zookeeper
zookeeper
3.8.0
org.slf4j
slf4j-log4j12
public interface RegisterService {
String sayHello(String name);
}
package com.example.service;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.rpc.RpcContext;
/**
* @author CNCLUKZK
* @create 2022/8/21-23:00
*/
//服务的注册于发现
@DubboService
public class RegisterServiceImpl implements RegisterService{
@Override
public String sayHello(String name) {
System.out.println(name+"hello!"+", request from consumer: "+ RpcContext.getServerContext().getRemoteAddress());
return name+"hello!";
}
}
@DubboService 服务的注册于发现, 早先版本是@Service注意是Dubbo的
server:
port: 8081
#应用名称
dubbo:
application:
name: demo-provider
protocol:
name: dubbo
port: -1
#服务注册中心地址
registry:
id: zk-registry
address: zookeeper://127.0.0.1:2181
config-center:
address: zookeeper://127.0.0.1:2181
metadata-report:
address: zookeeper://127.0.0.1:2181
#那些服务被注册
scan:
base-packages: com.example.service
注意:生产者的启动类需要用@EnableDubbo注解标注,说明要启动Dubbo通信功能
@SpringBootApplication
@EnableDubbo
public class DemoProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DemoProviderApplication.class, args);
System.out.println("dubbo service started");
}
}
消费者消费
dubbo:
application:
name: demo-consumer
protocol:
name: dubbo
port: -1
#到服务注册中心地址
registry:
id: zk-registry
address: zookeeper://127.0.0.1:2181
config-center:
address: zookeeper://127.0.0.1:2181
metadata-report:
address: zookeeper://127.0.0.1:2181
package com.example.service;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
/**
* @author CNCLUKZK
* @create 2022/8/21-23:02
*/
@Service //需要将组件放到spring容器中
public class SubscribeService {
//拿到注册中心的服务
//方式1:引用pom坐标
//方式2:可以定义和服务方相同路径的接口名
@DubboReference
RegisterService registerService;
public void sayHelloConsumer() {
String name = registerService.sayHello("张三");
System.out.println("远程执行的方法:"+name);
}
}
@Service //需要将组件放到spring容器中,是import org.springframework.stereotype.Service;
@DubboReference:需要可以定义和服务方相同路径的接口名,这样才能引用到,实际开发不会用这种方式来调用
消费者的启动类需要用@EnableDubbo注解标注,说明要启动Dubbo通信功能
@SpringBootApplication
@EnableDubbo
public class DemoConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(DemoConsumerApplication.class, args);
}
}
然后访问http://localhost:7001/输入登录账户和密码root/root进入dubbo-admin,进行查看服务注册信息
测试时,zookeeper的服务打开后,接着将dubbo-admin监控平台打开,之后将服务方demo-provider程序启动,最后在消费方进行测试使用
package com.example.democonsumer;
import com.example.service.SubscribeService;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
@EnableDubbo
class DemoConsumerApplicationTests {
@Autowired
SubscribeService subscribeService;
@Test
void contextLoads() {
subscribeService.sayHelloConsumer();
}
}