dubbo(7):配置(2)

五、多版本(灰度发布)

    在 Dubbo 中为同一个服务配置多个版本。当一个接口实现,出现不兼容升级时,可以用版本号过渡,版本号不同的服务相互间不引用。(详细查看官方文档用法实例)

    可以按照以下的步骤进行版本迁移:

  1. 在低压力时间段,先升级一半提供者为新版本
  2. 再将所有消费者升级为新版本
  3. 然后将剩下的一半提供者升级为新版本

    把UserServiceImpl复制一份,作为新版本。

    分别定义两个版本号。

package com.buba.service.impl;

import com.buba.pojo.UserAddress;
import com.buba.service.UserService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@DubboService(version = "1.0.0") // 暴露服务
@Service
public class UserServiceImpl implements UserService {
    @Override
    public List getUserAddressList(String userId) {
        UserAddress userAddress1 = new UserAddress(1, "北京", "1", "张三", "17611112222", "是");
        UserAddress userAddress2 = new UserAddress(2, "天津", "1", "张三", "17611112222", "是");
        return Arrays.asList(userAddress1, userAddress2);
    }
}
package com.buba.service.impl;

import com.buba.pojo.UserAddress;
import com.buba.service.UserService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@DubboService(version = "2.0.0") // 暴露服务
@Service
public class UserServiceImpl2 implements UserService {
    @Override
    public List getUserAddressList(String userId) {
        UserAddress userAddress1 = new UserAddress(1, "北京", "1", "张三", "17611112222", "是");
        UserAddress userAddress2 = new UserAddress(2, "天津", "1", "张三", "17611112222", "是");
        return Arrays.asList(userAddress1, userAddress2);
    }
}

    这样在消费方就可以指定使用哪个版本的服务。

package com.buba.service.impl;

import com.buba.pojo.UserAddress;
import com.buba.service.OrderService;
import com.buba.service.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @DubboReference(retries = 3,version = "1.0.0") // 去dubbo注册中心中寻找
    private UserService userService;

    @Override
    public List initOrder(String userId) {
        return userService.getUserAddressList("1");
    }

}

六、本地存根

    在 Dubbo 中利用本地存根在客户端执行部分逻辑。远程服务后,客户端通常只剩下接口,而实现全在服务器端,但提供方有些时候想在客户端也执行部分逻辑,比如:做 ThreadLocal 缓存,提前验证参数,调用失败后伪造容错数据等等,此时就需要在 API 中带上 Stub,客户端生成 Proxy 实例,会把 Proxy 通过构造函数传给 Stub 1,然后把 Stub 暴露给用户,Stub 可以决定要不要去调 Proxy。

dubbo(7):配置(2)_第1张图片

    在订单客户端创建一个用户实现(本地存根)用于验证。

    编写本地存根实现。

package com.buba.service.impl;

import com.buba.pojo.UserAddress;
import com.buba.service.UserService;

import java.util.List;

public class UserServiceStub implements UserService {

    private final UserService userService;

    /**
     * 传入UserService远程的代理对象
     *
     * @param userService
     */
    public UserServiceStub(UserService userService) {
        this.userService = userService;
    }

    @Override
    public List getUserAddressList(String userId) {
        if (!"".equals(userId)) {
           return userService.getUserAddressList(userId);
        }
        return null;
    }
}

    配置本地存根。

package com.buba.service.impl;

import com.buba.pojo.UserAddress;
import com.buba.service.OrderService;
import com.buba.service.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @DubboReference(retries = 3,version = "1.0.0",stub = "com.buba.service.impl.UserServiceStub") // 去dubbo注册中心中寻找
    private UserService userService;

    @Override
    public List initOrder(String userId) {
        return userService.getUserAddressList("1");
    }

}

七。dubbo服务方法级配置

    导入dubbo-spring-boot-starter包,在application.yml中配置。使用@DubboService发布服务,@DubboReference引用服务,@EnableDubbo开启dubbo注解扫描。通过@Method可以将dubbo精确到方法级。

package com.buba.service.impl;

import com.buba.pojo.UserAddress;
import com.buba.service.UserService;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.annotation.Method;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@DubboService(version = "1.0.0", methods = {@Method(name = "getUserAddressList", timeout = 250, retries = 0)}) // 暴露服务
@Service
public class UserServiceImpl implements UserService {

    @Override
    public List getUserAddressList(String userId) {
        UserAddress userAddress1 = new UserAddress(1, "北京", "1", "张三", "17611112222", "是");
        UserAddress userAddress2 = new UserAddress(2, "天津", "1", "张三", "17611112222", "是");
        return Arrays.asList(userAddress1, userAddress2);
    }
}

    可以在启动类中通过注解加载dubbo配置(xml)文件方式。

package com.buba;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

// @EnableDubbo // 开启基于注解的Dubbo功能
@ImportResource(locations = "classpath:consumer.xml")
@SpringBootApplication
public class OrderServiceConsumer_8802 {
    public static void main(String[] args) {
        SpringApplication.run(OrderServiceConsumer_8802.class, args);
    }
}

 

你可能感兴趣的:(dubbo)