dubbo是国内常用的rpc框架,使用起来非常方便。
定义common项目,定义producer项目,consumer项目
引入包
代码语言:html复制 <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
</dependencies>
common
定义公共接口
代码语言:txt复制public interface HelloService {
String sayHi(String name) ;
}
producer
定义生产者
代码语言:txt复制@Service(timeout = 2000, version="1.0")
@Component
public class DubboHelloServiceImpl implements HelloService {
@Override
public String sayHi(String name) {
return "version1 hi," name;
}
}
@Service(timeout = 2000, version="2.0")
@Component
public class DubboHelloServiceV2Impl implements HelloService {
@Override
public String sayHi(String name) {
return "version hi," name;
}
}
定义服务端配置
代码语言:txt复制dubbo:
application:
name: hello-dubbo-provider
registry:
address: tt.com:12181
protocol: zookeeper
protocol:
name: dubbo
port: 18888
scan:
base-packages: xxxx.com
consumer
消费者
代码语言:txt复制@Service
public class HelloServiceImpl implements HelloService {
@Reference(version = "1.0")
private HelloService helloService1 ;
@Reference(version = "2.0")
private HelloService helloService2;
@Override
public String sayHi(String name) {
return helloService1.sayHi(name) ;
}
public String sayHi2 (String name){
return helloService2.sayHi(name) ;
}
}
消费者配置
代码语言:txt复制dubbo:
application:
name: hello-dubbo-consume
registry:
address: tt.com:12181
protocol: zookeeper
然后服务就可以正常运转了,很方便简洁。