Dubbo-消费者直连服务者的三种方式
dubbo:reference中配置url
<dubbo:reference interface="org.apache.dubbo.demo.DemoService" url="dubbo://localhost:20880" />
通过 -D 参数指定
java -Dorg.apache.dubbo.demo.DemoService=dubbo://localhost:20880
通过文件映射
如果服务比较多,也可以用文件映射,用 -Ddubbo.resolve.file 指定映射文件路径
java -Ddubbo.resolve.file=resolve.properties resolve.properties org.apache.dubbo.demo.DemoService=dubbo://localhost:20880 org.apache.dubbo.demo.HelloService=dubbo://localhost:20880
ReferenceConfig#resolveFile
public void resolveFile() { // 从系统变量中获取与接口名对应的属性值 java -Dorg.apache.dubbo.demo.DemoService=dubbo://localhost:20880 String resolve = System.getProperty(interfaceName); String resolveFile = null; if (StringUtils.isEmpty(resolve)) { // 从系统属性中获取解析文件路径 java -Ddubbo.resolve.file=resolve.properties resolveFile = System.getProperty("dubbo.resolve.file"); if (StringUtils.isEmpty(resolveFile)) { // 从指定位置加载配置文件 File userResolveFile = new File(new File(System.getProperty("user.home")), "dubbo-resolve.properties"); if (userResolveFile.exists()) { // 获取文件绝对路径 resolveFile = userResolveFile.getAbsolutePath(); } } if (resolveFile != null && resolveFile.length() > 0) { Properties properties = new Properties(); try (FileInputStream fis = new FileInputStream(new File(resolveFile))) { // 从文件中加载配置 properties.load(fis); } catch (IOException e) { throw new IllegalStateException("Failed to load " + resolveFile + ", cause: " + e.getMessage(), e); } // 获取与接口名对应的配置 resolve = properties.getProperty(interfaceName); } } if (resolve != null && resolve.length() > 0) { // 将 resolve 赋值给 url url = resolve; if (logger.isWarnEnabled()) { if (resolveFile != null) { logger.warn("Using default dubbo resolve file " + resolveFile + " replace " + interfaceName + "" + resolve + " to p2p invoke remote service."); } else { logger.warn("Using -D" + interfaceName + "=" + resolve + " to p2p invoke remote service."); } } } }