論dubbo版本升級的重要性【spring boot +dubbo】
阿新 • • 發佈:2019-02-17
dubbo整合到spring boot中,充當消費者
使用的是註解的配置方式。
1、pom.xml中新增
<dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.4</version> <scope>provided</scope> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency>
2、新增dubbo基礎配置DubboBaseConfig.java
import org.springframework.context.annotation.Bean; import com.alibaba.dubbo.config.ApplicationConfig; import com.alibaba.dubbo.config.RegistryConfig; public class DubboBaseConfig { @Bean public RegistryConfig registry() { RegistryConfig registryConfig = new RegistryConfig(); registryConfig.setAddress("127.0.0.1:2181"); registryConfig.setProtocol("zookeeper"); return registryConfig; } @Bean public ApplicationConfig application() { ApplicationConfig applicationConfig = new ApplicationConfig(); applicationConfig.setName("o2o-platform"); return applicationConfig; } }
3、新增bean引用DubboReferenceConfig.java繼承DubboBaseConfig
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.alibaba.dubbo.config.spring.ReferenceBean; import com.aomygod.pay.PayService; @Configuration public class DubboReferenceConfig extends DubboBaseConfig { @Bean public ReferenceBean<PayService> payService() { ReferenceBean<PayService> ref = new ReferenceBean<>(); ref.setGroup("o2o"); ref.setVersion("1.0"); ref.setInterface(PayService.class); ref.setCheck(false); return ref; } }
4、Spring註解方式直接使用bean
@Autowired
private PayService payService;
然後就報錯了:interface is not visible from class loader
一天的時間全坑在裡面,這種查,斷點進去看:
if (tmp != ics[i])
throw new IllegalArgumentException(ics[i] + " is not visible from class loader");
發現tmp 和 ics[i] 值相同但物件不一樣,難道dubbo和spring類載入器不一致或衝突的問題。。。
各種找解決方案,還是沒解決了,最後看maven庫dubbo最新版本是 2.5.7
就試了一下升級版本
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.7</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
沒想到,真的就可以了,好坑爹