1 依赖
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>${com.alibaba.cloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${com.cloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-dubboartifactId>
<version>${com.dubbo.version}version>
dependency>
dependencies>
dependencyManagement>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-dubboartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.liyong.learngroupId>
<artifactId>dubbo-FacdeartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
这里的dubbo-Facde是一个单独的模块,提供了公共的接口,打包为jar引入即可:
2 配置(选用nacos作为注册中心)
server:
port: 9070
spring:
application:
name: dubbo-provider
cloud:
nacos:
discovery:
server-addr: 111.229.199.181:8848
dubbo:
application:
name: dubbo-provider
protocol:
name: dubbo
port: 29070
registry:
address: nacos://111.229.199.181:8848
server:
port: 9075
spring:
application:
name: dubbo-consumer
cloud:
nacos:
discovery:
server-addr: 111.229.199.181:8848
dubbo:
application:
name: dubbo-consumer
protocol:
name: dubbo
port: 29075
registry:
address: nacos://111.229.199.181:8848
3 使用
@DubboService
public class DubboDemoServiceImpl implements DubboDemoService {
@Override
public String dubboDemo() {
return "dubboDemo go on !!!!!!";
}
}
@Component
public class InvokeService {
@DubboReference
private DubboDemoService dubboDemoService;
public String echo() {
return dubboDemoService.dubboDemo();
}
}
@SpringBootApplication
@EnableDiscoveryClient
@EnableDubbo
public class DubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DubboProviderApplication.class, args);
}
}
1 依赖检查
所谓的依赖检查也就是,检查提供方是否提供了对应的服务,如果没有则消费方是会启动报错的。
可以通过注解里面的参数控制:
// 关闭检查,没有提供方也能正常启动
@DubboReference(check = false);
check设置为false后,如果没有提供方调用会抛异常,如果将来提供方正常了以后,还是可以调用成功。
2 负载均衡
默认就有负载均衡,也可以通过@DubboReference进行设置
@DubboReference(check = false , loadbalance = LoadbalanceRules.RANDOM)
// 有下面这些规则可以设置
public interface LoadbalanceRules {
/**
* This class select one provider from multiple providers randomly.
**/
String RANDOM = "random";
/**
* Round robin load balance.
**/
String ROUND_ROBIN = "roundrobin";
/**
* Filter the number of invokers with the least number of active calls and count the weights and quantities of these invokers.
**/
String LEAST_ACTIVE = "leastactive";
/**
* Consistent Hash, requests with the same parameters are always sent to the same provider.
**/
String CONSISTENT_HASH = "consistenthash";
/**
* Filter the number of invokers with the shortest response time of success calls and count the weights and quantities of these invokers.
**/
String SHORTEST_RESPONSE = "shortestresponse";
String EMPTY = "";
}
广播调用就是我们调用一个引用,但是其实所有节点都会执行这个方法,这个应用场景就比如我们要清空每个节点的业务缓存,或者每个节点都要记录一些日志。
官方文档广播调用
//broadcast.fail.percent=20 代表了当 20% 的节点调用失败就抛出异常,不再调用其他节点。
@DubboReference(cluster = "broadcast", parameters = {"broadcast.fail.percent", "20"})
也是就是提供方返回的数据进行缓存,前提是数据不需要频繁的变动。这里需要注意缓存数据的大小适不适合做本地缓存,如果是大数据缓存那么需要整合第三方缓存,例如Redis。
官方文档:
dubbo调用结果缓存
// 接口粒度
@DubboReference(cache = "lru")
private DemoService demoService;
// 方法粒度
@DubboReference(methods = {@Method(name="sayHello",cache = "lru")})
private DemoService demoService;
点对点调用就是,我们要指定一台特定的机器来调用。例如本地和其他人联调,例如需要选择一台服务器做一些事情,不关心有几个节点。官方不推荐在生产环境中使用,所以了解即可,可能会在调试中使用到。
官网网站
@DubboReference(url = "dubbo://localhost:29072")
private DemoService demoService;
泛化调用官方文档