-
Notifications
You must be signed in to change notification settings - Fork 615
dubbo spring boot starter使用指南
javahongxi edited this page Jan 13, 2019
·
5 revisions
https://github.com/apache/incubator-dubbo-spring-boot-project.git
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.4</version>
</dependency>
千万不要使用以下依赖(已废弃)
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
另外,如果你的公共包如xx-common
也引用了dubbo,请在xx-common
里加上<optional>true</optional>
application.yml
dubbo:
registry:
protocol: zookeeper
address: 127.0.0.1:2181
id: my-registry
protocol:
port: 20882
name: dubbo
status: server
id: dubbo
application:
name: demo-provider
id: demo-provider
qosEnable: true
qosPort: 22223
scan:
basePackages: org.hongxi.whatsmars.dubbo.demo.provider.service
provider
@Service(version = "1.0.0")
public class DemoServiceImpl implements DemoService {
@Autowired
private UserService userService;
public String sayHello(String name) {
boolean registerSuccess = userService.register(name);
System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
return "Hello " + name + ", registerSuccess:" + registerSuccess + ", response form provider: " + RpcContext.getContext().getLocalAddress();
}
}
application.yml
dubbo:
registry:
protocol: zookeeper
address: 127.0.0.1:2181
id: my-registry
protocol:
port: 20882
name: dubbo
id: dubbo
application:
name: demo-consumer
id: demo-consumer
qosEnable: true
qosPort: 22224
consumer
/**
* Created by javahongxi on 2017/12/4.
* 将服务调用包装,可以方便进行调用监控
*/
@Component
public class DemoRpc {
@Reference(version = "1.0.0")
private DemoService demoService;
public String sayHello(String name) {
String result = null;
try {
result = demoService.sayHello(name);
} catch (Exception e) {
// log
e.printStackTrace();
}
return result;
}
}
application.yml
dubbo:
registry:
protocol: zookeeper
address: 127.0.0.1:2181
id: my-registry
protocol:
port: 20882
name: dubbo
id: dubbo
application:
name: demo-consumer
id: demo-consumer
qosEnable: true
qosPort: 22224
registry:
other:
protocol: zookeeper
address: 127.0.0.1:2181
id: other
@Configuration
public class DubboConfig {
/**
* 配置文件里配置默认的,这里配置其他需要的
*/
@Bean("otherRegistry")
public RegistryConfig otherRegistry(@Value("${registry.other.protocol}") String protocol,
@Value("${registry.other.address}") String address,
@Value("${registry.other.id}") String id) {
RegistryConfig registry = new RegistryConfig();
registry.setProtocol(protocol);
registry.setAddress(address);
registry.setId(id);
return registry;
}
}
/**
* Created by javahongxi on 2017/12/4.
* 将服务调用包装,可以方便进行调用监控
*/
@Component
public class DemoRpc {
@Reference(version = "1.0.0")
private DemoService demoService;
@Reference(version = "1.0.0", registry = "otherRegistry")
private OtherService otherService;
public String sayHello(String name) {
String result = null;
try {
result = demoService.sayHello(name);
} catch (Exception e) {
// log
e.printStackTrace();
}
return result;
}
public String sayHello2(String name) {
String result = null;
try {
result = otherService.sayHello(name);
} catch (Exception e) {
// log
e.printStackTrace();
}
return result;
}
}
@Service(
version = "1.0.0",
registry = {"my-registry", "otherRegistry"}
)
public class OtherServiceImpl implements OtherService {
@Autowired
private UserService userService;
public String sayHello(String name) {
boolean registerSuccess = userService.register(name);
System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
return "Hello " + name + ", registerSuccess:" + registerSuccess + ", response form provider: " + RpcContext.getContext().getLocalAddress();
}
}
PS: Dubbo优雅停机应该 kill -> sleep -> kill -9,切勿直接kill -9
wiki.hongxi.org
首页
Java核心技术
- JUC JMM与线程安全
- JUC 指令重排与内存屏障
- JUC Java内存模型FAQ
- JUC 同步和Java内存模型
- JUC volatile实现原理
- JUC AQS详解
- JUC AQS理解
- JUC synchronized优化
- JUC 线程和同步
- JUC 线程状态
- JUC 线程通信
- JUC ThreadLocal介绍及原理
- JUC 死锁及避免方案
- JUC 读写锁简单实现
- JUC 信号量
- JUC 阻塞队列
- NIO Overview
- NIO Channel
- NIO Buffer
- NIO Scatter与Gather
- NIO Channel to Channel Transfers
- NIO Selector
- NIO FileChannel
- NIO SocketChannel
- NIO ServerSocketChannel
- NIO Non-blocking Server
- NIO DatagramChannel
- NIO Pipe
- NIO NIO vs. IO
- NIO DirectBuffer
- NIO zero-copy
- NIO Source Code
- NIO HTTP Protocol
- NIO epoll bug
- Reflection 基础
- Reflection 动态代理
- JVM相关
- 设计模式典型案例
Netty
RocketMQ深入研究
kafka深入研究
Pulsar深入研究
Dubbo源码导读
- Dubbo SPI
- Dubbo 自适应拓展机制
- Dubbo 服务导出
- Dubbo 服务引用
- Dubbo 服务字典
- Dubbo 服务路由
- Dubbo 集群
- Dubbo 负载均衡
- Dubbo 服务调用过程
微服务架构
Redis
Elasticsearch
其他
- Dubbo 框架设计
- Dubbo 优雅停机
- dubbo-spring-boot-starter使用指南
- rocketmq-spring-boot-starter使用指南
- Mybatis multi-database in spring-boot 2
- RocketMQ 客户端简单封装
- Otter 入门
杂谈
关于我