dubbo学习之apacheDubbo
alibabaDubbo件本人另一篇文章
详见本人alibabDubboDemo
apache版本的dubbo孵化出来有一段时间了,新增了不少特性。
现在开始apacheDubbo之旅
1.接口
/**
* 测试接口
*/
public interface CartService {
/**
* 根据用户名获取购物车详情
* @param userId
* @return
*/
String findCartByUserId(Long userId);
}
2.项目依赖,生产者和消费者一样
<properties>
<!-- 坑点 -->
<java.version>11</java.version>
<dubbo.version>2.7.4.1</dubbo.version>
</properties>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.git</groupId>
<artifactId>dubboInterface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-recipes -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
2.生产者
1.实现类
//@Service(version = "1.0.0")
@Service(group = "1")//这个Service不是spring的而是Dubbo的,别倒错包
public class CartServiceImpl implements CartService {
private static final Logger logger = LoggerFactory.getLogger(CartServiceImpl.class);
public String findCartByUserId(Long userId) {
Service annotation = AnnotationUtil.getAnnotation(this, Service.class);
logger.info("当前调用服务版本号为:{},分组为:{}",annotation.version(),annotation.group());
return "购物车没有东西了"+userId;
}
}
2.配置
yml
dubbo:
protocol:
name: dubbo
port: 20888
registry:
address: zookeeper://127.0.0.1:2181
application:
name: provider-of-cart
#scan:可以用注解实现
#base-packages: com.git.dubboprovider.service
启动类
@SpringBootApplication
@DubboComponentScan("com.git.dubboprovider.service")
public class DubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DubboProviderApplication.class, args);
}
}
3.消费者
yml
dubbo:
protocol:
name: dubbo
registry:
address: zookeeper://127.0.0.1:2181
application:
name: consumer-of-cart
测试类
//@Reference(version = "1.0.0")
@Reference(group = "1")
private CartService cartService;
//Reference(version = "2.0.0")
@Reference(group = "2")
private CartService cartService2;
@Reference
private UserService userService;
@Test
void contextLoads() {
System.out.println(cartService.findCartByUserId(8L));
System.out.println(cartService2.findCartByUserId(8L));
}
常用配置说明
1.version,不配置是可以的,引用的时候不配置就行,这个主要针对版本升级等
2.group 一个接口多个实现类可以使用group还分组
4.异步和回调
1.异步很简单
@Reference(async=true)
2.回调
//接口
public interface RoleService {
Role checkRole2(Role role, Function<Role,Role> function);
}
既然是回调使用jdk自带的函数式接口就很爽了
//生产者
@Service(methods = @Method(name = "checkRole2",//回调函数名称,可一赔多个
arguments = @Argument(index = 1//参数下标,回调函数下标,错了报错
,callback = true//开启回调)//参数配置,可配置多个
),
callbacks = 2//回调实例的个数,如果为1那么每次调用必须是同一个回调函数)
public class RoleServiceImpl implements RoleService {
@Override
public Role checkRole2(Role role, Function<Role, Role> function) {
System.out.println("function函数处理");
if(role==null) throw new RuntimeException("角色不可为空");
if(role.getName().equals("财务")){
System.out.println("角色名字应该为为管理员");
return function.apply(role);
}
return role;
}
}
测试
@SpringBootTest
public class BootCallBackTest {
@Reference
private RoleService roleService;
@Test
public void test1(){
Role role = new Role();
role.setName("财务");
Role role1 = roleService.checkRole2(role, r->{
System.out.println("===================开始修正===================");
System.out.println("===================修正之前===================");
System.out.println(r);
r.setName("管理");
r.setDesc("搞管理的");
return r;
});
//注意如果callbacks=1,第二次这么调用会报错,除非用同一个Function实例
//
Role role2 = roleService.checkRole2(role, r->{
System.out.println("===================开始修正===================");
System.out.println("===================修正之前===================");
System.out.println(r);
r.setName("管理");
r.setDesc("搞管理的");
return r;
});
System.out.println("===================角色调用完毕===================");
System.out.println("===================修正之后===================");
System.out.println(role1);
}
}
5.坑点
1.这两个包不导入回报类找不到 ,并且版本和zookeeper要对应,3.5以上的用下面这两个就行
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-recipes -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.2.0</version>
</dependency>
2.netty报错
java.lang.IllegalAccessException: class io.netty.util.internal.PlatformDependent0$6 cannot access class jdk.internal.misc.Unsafe (in module java.base) because module java.base does not export jdk.internal.misc to unnamed module @486be205
本人用的jdk11
其中的Unsafe 类和1.8的位置不同了,不了解Unsafe 类的可以去百度一下,功能很强大。
6.结束
简单应用就结束了,其他的配置也没有一一测试,跟着部门的配置走就是了。
本文为作者原创,转载请著名出处!
github地址