基础案例搭建
1 依赖
- 父工程POM
<dependencyManagement><dependencies><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>${com.alibaba.cloud.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${com.cloud.version}</version><type>pom</type><scope>import</scope></dependency><!-- https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-dubbo --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-dubbo</artifactId><version>${com.dubbo.version}</version></dependency></dependencies></dependencyManagement>
- 当前工程POM provider + consumer 都是这个POM
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-dubbo</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.liyong.learn</groupId><artifactId>dubbo-Facde</artifactId><version>1.0-SNAPSHOT</version></dependency>
这里的dubbo-Facde是一个单独的模块,提供了公共的接口,打包为jar引入即可:
2 配置(选用nacos作为注册中心)
- provider
server:port: 9070
spring:application:name: dubbo-providercloud:nacos:discovery:server-addr: 111.229.199.181:8848
dubbo:application:name: dubbo-providerprotocol:name: dubboport: 29070 registry:address: nacos://111.229.199.181:8848
- consumer
server:port: 9075
spring:application:name: dubbo-consumercloud:nacos:discovery:server-addr: 111.229.199.181:8848
dubbo:application:name: dubbo-consumerprotocol:name: dubboport: 29075 registry:address: nacos://111.229.199.181:8848
3 使用
- provider
@DubboService
public class DubboDemoServiceImpl implements DubboDemoService {@Overridepublic String dubboDemo() {return "dubboDemo go on !!!!!!";}
}
- consumer
@Component
public class InvokeService {@DubboReferenceprivate 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;
泛化调用
泛化调用官方文档