前面我们说完了服务的发布,今天来看下服务的消费,也就是消费者端底层是怎么运行的。
初识Dubbo中demo里的Consumer消费者类的最后一步:
//5.引用服务UserService userService = referenceConfig.get();
public synchronized T get() {
//检测配置 checkAndUpdateSubConfigs(); //判断当前Reference是否被销毁,如果已经被销毁,就抛出异常 if (destroyed) {
throw new IllegalStateException("The invoker of ReferenceConfig(" + url + ") has already destroyed!"); } //接口代理类的引用 如果为空 if (ref == null) {
//1 初始化 init(); } //返回这个引用 return ref; }
一、可以看出get方法里关键的就是init这个方法:
private void init() {
//当前ReferenceConfig是否已经被初始化 if (initialized) {
return; } //准备开始初始化,标识置为true initialized = true; //校验要引用的接口类 checkStubAndLocal(interfaceClass); /**检查mock配置,mock你就理解为没走真正的业务代码, 而是写死的返回数据,一般用来测试接口用,这个mock后面会 专门出一篇文章说*/ checkMock(interfaceClass); Map<String, String> map = new HashMap<String, String>(); //往map里放入side,这个side就是个标识,消费端是consumer,提供者端是provider map.put(Constants.SIDE_KEY, Constants.CONSUMER_SIDE); /**放入运行时的一些信息,这个和提供者一样,包括dubbo版本, 当前的时间戳,当前的进程号*/ appendRuntimeParameters(map); //是否是泛化调用 if (!isGeneric()) {
//如果不是泛化调用 //获取版本号 String revision = Version.getVersion(interfaceClass, version); if (revision != null && revision.length() > 0) {
//放入修订版本号,其实就是上面那个版本号 map.put("revision", revision); } //获取接口中的方法名 String[] methods = Wrapper.getWrapper(interfaceClass).getMethodNames(); if (methods.length == 0) {
//如果接口中没有定义方法到就放入 methods->* logger.warn("No method found in service interface " + interfaceClass.getName()); map.put("methods", Constants.ANY_VALUE); } else {
//有方法的话就放入methods->方法名,多个用逗号分隔 map.put("methods", StringUtils.join(new HashSet<String>(Arrays.asList(methods)), ",")); } } //放入interface->要引用的接口的名字 map.put(Constants.INTERFACE_KEY, interfaceName); //消费者应用的名字,就是ApplicationConfig中设置的dubbo-consumer appendParameters(map, application); //模块信息,初始化的时候没设置 这个很少设置 appendParameters(map, module); //标识为默认的消费者配置 appendParameters(map, consumer, Constants.DEFAULT_KEY); //将当前ReferenceConfig放入map appendParameters(map, this); Map<String, Object> attributes = null; /**方法相关配置 这个当时测试类里也没配置,没配置的话直接跳过, 不过从下面的代码可以看出,如果配置不为空的话会去取重试次数,默认为0, 这个提供者端是一样的*/ if (CollectionUtils.isNotEmpty(methods)) {
attributes = new HashMap<String, Object>(); for (MethodConfig methodConfig : methods) {
appendParameters(map, methodConfig, methodConfig.getName()); String retryKey = methodConfig.getName() + ".retry"; if (map.containsKey(retryKey)) {
String retryValue = map.remove(retryKey); if ("false".equals(retryValue)) {
map.put(methodConfig.getName() + ".retries", "0"); } } attributes.put(methodConfig.getName(), convertMethodConfig2AyncInfo(methodConfig)); } } //获取注册中心的ip地址 String hostToRegistry = ConfigUtils.getSystemProperty(Constants.DUBBO_IP_TO_REGISTRY); if (StringUtils.isEmpty(hostToRegistry)) {
//如果获取不到,就给个默认值,127.0.0.1 hostToRegistry = NetUtils.getLocalHost(); } //将注册中心的ip地址放入map map.put(Constants.REGISTER_IP_KEY, hostToRegistry); //通过map创建一个ref ref = createProxy(map); //通过接口名以及服务分组和版本号生成一个key String serviceKey = URL.buildKey(interfaceName, group, version); //初始化一个消费者的pojo,可以看到和提供者端很像,提供者端是初始化一个提供者的pojo ApplicationModel.initConsumerModel(serviceKey, buildConsumerModel(serviceKey, attributes)); }
把上面的代码加完注释以后你会发现,其实提供者很像对不对?也就多了一个mock检测(就是提供者端写死一些数据直接返回),所以咱就不细说了,这里面最重要的一步你们应该都能看出来,那就是createProxy这个方法:
@SuppressWarnings({
"unchecked", "rawtypes", "deprecation"}) private T createProxy(Map map) {
//1 是否需要打开本地引用 if (shouldJvmRefer(map)) {
URL url = new URL(Constants.LOCAL_PROTOCOL, Constants.LOCALHOST_VALUE, 0, interfaceClass.getName()).addParameters(map); invoker = refprotocol.refer(interfaceClass, url); if (logger.isInfoEnabled()) {
logger.info("Using injvm service " + interfaceClass.getName()); } } else {
//2 用户是否指定服务提供方地址:可以是服务提供方IP地址(直连方式) if (url != null && url.length() > 0) { // user specified URL, could be p