####主要是使用两个注解进行RPC通信:
RpcService
用来标注服务提供者
RpcReference
用来标注服务消费者
Bubbo在Zookeeper上注册的节点目录与Dubbo基本一致,如图:
1.引入项目,本地执行
mvn clean install
2. 在自己的项目的pom.xml 中引入项目
<dependency>
<groupId>com.grady.bubbo</groupId>
<artifactId>bubbo</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
3. 配置项目(可使用xml配置,这里使用代码配置)
服务提供者配置:
@Configuration
public class AppConfig {
@Value("${registry.address}")
private String registryAddress;
@Value("${server.port}")
private String rpcListenPort;
@Bean
public ServiceRegistry serviceRegistry() {
ServiceRegistry registry = new ServiceRegistry(registryAddress);
return registry;
}
@Bean
public RpcServer rpcServer() {
RpcServer rpcServer = new RpcServer(rpcListenPort, serviceRegistry());
return rpcServer;
}
}
服务消费者配置:
@Configuration
public class AppConfig {
@Value("${registry.address}")
private String registryAddress;
@Bean
public ServiceDiscovery serviceDiscovery() {
return new ServiceDiscovery(registryAddress);
}
}
@RpcService
public class TestServiceImpl implements TestService {
@Override
public String testHello() {
return "Test Hello!";
}
}
@RestController
@RequestMapping("/hello")
public class HelloController {
@RpcReference
TestService testService;
@GetMapping("test")
@ResponseBody
public String test() {
return testService.testHello();
}
}