Spring Cloud 微服务聚合

一、架构说明。

Zuul 网关 -> 服务聚合 -> 基础服务

二、代码开发。

1、增加 Servcie。

@Service
public class DemoService {
    @Autowired
    AdChartClient adChartClient;

    public Observable getAdChartConfig() {
        // 创建一个被观察者
        return Observable.create(observer -> {
            // 请求图表配置
            Result> result = adChartClient.queryAdChartConfig();
            observer.onNext(result);
            observer.onCompleted();
        });
    }

    public Observable getAdUserChartList(Long userId, List roleIds) {
        return Observable.create(observer -> {
            // 请求用户图表配置列表
            Result> result = adChartClient.queryAdUserChartList(userId, roleIds);
            observer.onNext(result);
            observer.onCompleted();
        });
    }
}

说明:

(1)AdChartClient 是基础服务提供的 feign 接口包。

2、增加 接口类。

@Api(value = "demo请求接口", tags = "demo请求接口")
public interface DemoApi {
    @ApiOperation(value = "demo", notes = "demo")
    @GetMapping(value = "/v1/agg/demo")
    DeferredResult> queryDemo(@ApiParam(name = "userId", value = "用户id", required = true) @RequestParam(name = "userId") Long userId,
                                              @ApiParam(name = "roleIds", value = "归属的角色id列表", required = true) @RequestParam(name = "roleIds") List roleIds);
}

3、增加 Controller。

@RestController
@RequestMapping("/api")
@Slf4j
public class DemoController extends BaseController implements DemoApi {
    @Autowired
    DemoService demoService;

    @Override
    public DeferredResult> queryDemo(@ApiParam(name = "userId", value = "用户id", required = true) @RequestParam(name = "userId") Long userId,
                                     @ApiParam(name = "roleIds", value = "归属的角色id列表", required = true) @RequestParam(name = "roleIds") List roleIds) {
        Observable> result = this.aggregateObservable(userId, roleIds);
        return this.toDeferredResult(result);
    }

    public Observable> aggregateObservable(Long userId, List roleIds) {
        // 合并两个或者多个Observables发射出的数据项,根据指定的函数变换它们
        return Observable.zip(
                this.demoService.getAdChartConfig(),
                this.demoService.getAdUserChartList(userId, roleIds),
                (r1, r2) -> {
                    Result result = new Result<>();
                    if(r1.getError().getCode() != 0) {
                        return result.error(r1.getError().getCode(), r1.getError().getMessage());
                    }
                    if(r2.getError().getCode() != 0) {
                        return result.error(r2.getError().getCode(), r2.getError().getMessage());
                    }

                    DemoOVO demoOVO = new DemoOVO();
                    demoOVO.setUserId(userId);
                    demoOVO.setUserName("张三");
                    demoOVO.setAge(10);
                    demoOVO.setSex("男");
                    
                    return Result.ok(demoOVO);
                }
        );
    }

    public DeferredResult> toDeferredResult(Observable> details) {
        DeferredResult> result = new DeferredResult<>();
        // 订阅
        details.subscribe(new Observer>() {
            @Override
            public void onCompleted() {
                log.info("完成...");
            }

            @Override
            public void onError(Throwable throwable) {
                log.error("发生错误...", throwable);
            }

            @Override
            public void onNext(Result r) {
                result.setResult(r);
            }
        });
        return result;
    }
}

4、服务调用。

http://10.2.8.74:8040/api/v1/agg/demo?userId=3&roleIds=9,11

{
    "error": {
        "code": 0,
        "message": "ok"
    },
    "data": {
        "userId": 3,
        "userName": "张三",
        "sex": "男",
        "age": 10
    }
}

【一起学习】

你可能感兴趣的:(Spring,Cloud,Spring,Cloud,服务聚合)