使用ServiceComb/CSE如何进行本地开发和测试?

本文档详细介绍了如何在本地使用ServiceComb/CSE进行服务的开发和调试,包括启动本地服务中心、Mock机制启动服务中心的方法,以及配置和注意事项,旨在帮助开发者高效地进行微服务测试。

轻量服务中心给开发者提供开发、调试、测试过程中的服务发现、注册和查询功能。

概念阐述

本小节介绍如何在开发者本地进行消费者/提供者应用的开发调试。服务提供者和消费者均需要连接到在远程的服务中心,为了本地微服务的开发和调试,本小节介绍了两种搭建本地服务中心的方法进行本地微服务调试:

  1. 启动本地服务中心
  2. 通过local file模拟启动服务中心(Mock机制)。

服务中心是微服务框架中的重要组件,用于服务元数据以及服务实例元数据的管理和处理注册、发现。服务中心与微服务提供/消费者的逻辑关系下图所示:

前提条件

  • 已经安装1.8版本的JDK,并且正确配置JAVA_HOME环境变量。
  • 由于启动本地服务中心将会占用此台机器的30100和30103端口,其分别表示服务中心的后台和前台服务端口。请确认以上端口未被使用。

启动本地服务中心

  1. 进入“微服务开发 > 工具下载”页面下载本地轻量化服务中心安装包。
  2. 安装服务中心。

     

    • 软件包安装:解压缩安装包到安装目录。
    • 镜像包安装:执行以下命令安装服务中心。docker load -i local-service-center-{version}-docker.tar 说明:
      1. 只支持64位的操作系统。
      2. 通过下载软件包方式安装服务中心时,如果需要修改IP地址和端口,请修改/../conf/app.conf文件,其中frontend_host_ip和frontend_host_port为服务中心前台服务监听的IP地址和端口配置,httpaddr和httpport为服务中心后台服务监听的IP地址和端口配置。以修改服务中心后台服务为例,修改方法如下所示。当httpaddr配置为127.0.0.1时,服务中心只能被本机进程访问。若希望服务中心可以被远程访问,请配置httpaddr为外部IP地址或者0.0.0.0。
        # listen address
        httpaddr = 127.0.0.1
        httpport = 30100
  3. 启动服务中心。
    • windows系统:单击服务中心安装包里的start.bat脚本启动服务中心。
    • linux系统:执行start.sh脚本启动服务中心。 说明:

      执行start.sh脚本前请执行以下命令修改文件权限。

      chmod -R 550 /../bin

    • docker容器:执行以下命令启动服务中心。

      docker rm -f sc

      docker run -d --name sc -p 30100:30100 -p 30103:30103 local-service-center:latest

  4. 在浏览器中输入http://127.0.0.1:30103,访问服务中心前台界面。
  5. 启动服务中心后,在存放sdk配置信息的microservice.yaml文件中配置服务中心地址,如下。
    cse:
      service:
        registry:
          address: http://127.0.0.1:30100      # 服务中心后台地址及端口
     说明:
    • 服务中心为提高易用性,默认监听本机0.0.0.0地址,但从安全性考虑,建议用户自行修改配置监听127.0.0.1。
    • 如果使用服务中心时自定义设置了IP地址和端口,则需根据实际情况修改address的值。
    • 若不使用配置中心和仪表盘,请将相应配置删除。

    微服务应用开发完毕后,运行服务端MainServer,启动服务进程,可以向服务中心注册服务。

  6. 开发服务提供/消费者,启动微服务进行本地测试。

Mock机制启动服务中心

  1. 新建本地服务中心定义文件registry.yaml,内容如下:
    springmvctest:
      - id: "001"
        version: "1.0"
        appid: myapp                        #调试的服务id
        instances:
          - endpoints:
            - rest://127.0.0.1:8080
     说明:

    mock机制需要自己准备契约,并且当前只支持在本地进行服务消费端(consumer)侧的调试,不支持服务提供者(provider)

  2. 在服务消费者Main函数首末添加如下代码:
    public class xxxClient {
    public static void main(String[] args) {
        System.setProperty("local.registry.file", "/path/registry.yaml");
        //your code
        System.clearProperty("local.registry.file");
    }
     说明:

    setProperty第二个参数填写registry.yaml在磁盘中的系统绝对路径,注意区分在不同系统下使用对应的路径分隔符。

  3. 开发服务消费者,启动微服务进行本地测试。
2025-12-11 14:28:02.633|ERROR|vmall_car_server|WatcherScheduledJob-thread:136|2.0.1.9|7582482721440371074||||||VmallException,regCenter is empty,,DSS# regCenter is empty.|com.huawei.vmall.dss.job.scheduled.WatcherScheduledJob 2025-12-11 14:28:09.071|ERROR|vmall_car_server|transport-vert.x-eventloop-thread-20:108|2.0.1.9|7582482707673960019||||||locate path failed, status:Not Found, http method:POST, path:/api/o2o-activity/query/, microserviceName:vmall-car-server-mock-alpha|org.apache.servicecomb.common.rest.locator.OperationLocator 2025-12-11 14:28:09.072|ERROR|vmall_car_server|transport-vert.x-eventloop-thread-20:108|2.0.1.9|7582482707673960019||||||Exception happened null:Not Found,Real Exception of InvocationException is null.|com.huawei.vmall.car.server.filter.util.ExceptionUtils 2025-12-11 14:28:09.072|ERROR|vmall_car_server|transport-vert.x-eventloop-thread-20:108|2.0.1.9|7582482707673960019||||||Failed to execute HttpServerFilters, operation:UNKNOWN_OPERATION, request uri:/api/o2o-activity/query|org.apache.servicecomb.common.rest.AbstractRestInvocation java.lang.NullPointerException: Cannot invoke "org.apache.servicecomb.core.Invocation.getLocalContext(String)" because "invocation" is null at com.huawei.vmall.car.server.filter.cse.CseServerFilter.beforeSendResponseAsync(CseServerFilter.java:78) at org.apache.servicecomb.common.rest.filter.HttpServerFilterBeforeSendResponseExecutor.safeInvoke(HttpServerFilterBeforeSendResponseExecutor.java:54) at org.apache.servicecomb.common.rest.filter.HttpServerFilterBeforeSendResponseExecutor.doRun(HttpServerFilterBeforeSendResponseExecutor.java:84) at org.apache.servicecomb.common.rest.filter.HttpServerFilterBeforeSendResponseExecutor.lambda$doRun$0(HttpServerFilterBeforeSendResponseExecutor.java:87) at java.base/java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:863) at java.base/java.util.concurrent.CompletableFuture.uniWhenCompleteStage(CompletableFuture.java:887) at java.base/java.util.concurrent.CompletableFuture.whenComplete(CompletableFuture.java:2357) at org.apache.servicecomb.common.rest.filter.HttpServerFilterBeforeSendResponseExecutor.doRun(HttpServerFilterBeforeSendResponseExecutor.java:85) at org.apache.servicecomb.common.rest.filter.HttpServerFilterBeforeSendResponseExecutor.lambda$doRun$0(HttpServerFilterBeforeSendResponseExecutor.java:87) at java.base/java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:863) at java.base/java.util.concurrent.CompletableFuture.uniWhenCompleteStage(CompletableFuture.java:887) at java.base/java.util.concurrent.CompletableFuture.whenComplete(CompletableFuture.java:2357) at org.apache.servicecomb.common.rest.filter.HttpServerFilterBeforeSendResponseExecutor.doRun(HttpServerFilterBeforeSendResponseExecutor.java:85) at org.apache.servicecomb.common.rest.filter.HttpServerFilterBeforeSendResponseExecutor.lambda$doRun$0(HttpServerFilterBeforeSendResponseExecutor.java:87) at java.base/java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:863) at java.base/java.util.concurrent.CompletableFuture.uniWhenCompleteStage(CompletableFuture.java:887) at java.base/java.util.concurrent.CompletableFuture.whenComplete(CompletableFuture.java:2357) at org.apache.servicecomb.common.rest.filter.HttpServerFilterBeforeSendResponseExecutor.doRun(HttpServerFilterBeforeSendResponseExecutor.java:85) at org.apache.servicecomb.common.rest.filter.HttpServerFilterBeforeSendResponseExecutor.lambda$doRun$0(HttpServerFilterBeforeSendResponseExecutor.java:87) at java.base/java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:863) at java.base/java.util.concurrent.CompletableFuture.uniWhenCompleteStage(CompletableFuture.java:887) at java.base/java.util.concurrent.CompletableFuture.whenComplete(CompletableFuture.java:2357) at org.apache.servicecomb.common.rest.filter.HttpServerFilterBeforeSendResponseExecutor.doRun(HttpServerFilterBeforeSendResponseExecutor.java:85) at org.apache.servicecomb.common.rest.filter.HttpServerFilterBeforeSendResponseExecutor.lambda$doRun$0(HttpServerFilterBeforeSendResponseExecutor.java:87) at java.base/java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:863) at java.base/java.util.concurrent.CompletableFuture.uniWhenCompleteStage(CompletableFuture.java:887) at java.base/java.util.concurrent.CompletableFuture.whenComplete(CompletableFuture.java:2357) at org.apache.servicecomb.common.rest.filter.HttpServerFilterBeforeSendResponseExecutor.doRun(HttpServerFilterBeforeSendResponseExecutor.java:85) at org.apache.servicecomb.common.rest.filter.HttpServerFilterBeforeSendResponseExecutor.lambda$doRun$0(HttpServerFilterBeforeSendResponseExecutor.java:87) at java.base/java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:863) at java.base/java.util.concurrent.CompletableFuture.uniWhenCompleteStage(CompletableFuture.java:887) at java.base/java.util.concurrent.CompletableFuture.whenComplete(CompletableFuture.java:2357) at org.apache.servicecomb.common.rest.filter.HttpServerFilterBeforeSendResponseExecutor.doRun(HttpServerFilterBeforeSendResponseExecutor.java:85) at org.apache.servicecomb.common.rest.filter.HttpServerFilterBeforeSendResponseExecutor.lambda$doRun$0(HttpServerFilterBeforeSendResponseExecutor.java:87) at java.base/java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:863) at java.base/java.util.concurrent.CompletableFuture.uniWhenCompleteStage(CompletableFuture.java:887) at java.base/java.util.concurrent.CompletableFuture.whenComplete(CompletableFuture.java:2357) at org.apache.servicecomb.common.rest.filter.HttpServerFilterBeforeSendResponseExecutor.doRun(HttpServerFilterBeforeSendResponseExecutor.java:85) at org.apache.servicecomb.common.rest.filter.HttpServerFilterBeforeSendResponseExecutor.lambda$doRun$0(HttpServerFilterBeforeSendResponseExecutor.java:87) at java.base/java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:863) at java.base/java.util.concurrent.CompletableFuture.uniWhenCompleteStage(CompletableFuture.java:887) at java.base/java.util.concurrent.CompletableFuture.whenComplete(CompletableFuture.java:2357) at org.apache.servicecomb.common.rest.filter.HttpServerFilterBeforeSendResponseExecutor.doRun(HttpServerFilterBeforeSendResponseExecutor.java:85) at org.apache.servicecomb.common.rest.filter.HttpServerFilterBeforeSendResponseExecutor.lambda$doRun$0(HttpServerFilterBeforeSendResponseExecutor.java:87) at java.base/java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:863) at java.base/java.util.concurrent.CompletableFuture.uniWhenCompleteStage(CompletableFuture.java:887) at java.base/java.util.concurrent.CompletableFuture.whenComplete(CompletableFuture.java:2357) at org.apache.servicecomb.common.rest.filter.HttpServerFilterBeforeSendResponseExecutor.doRun(HttpServerFilterBeforeSendResponseExecutor.java:85) at org.apache.servicecomb.common.rest.filter.HttpServerFilterBeforeSendResponseExecutor.run(HttpServerFilterBeforeSendResponseExecutor.java:44) at org.apache.servicecomb.common.rest.AbstractRestInvocation.executeHttpServerFilters(AbstractRestInvocation.java:336) at org.apache.servicecomb.common.rest.AbstractRestInvocation.sendResponse(AbstractRestInvocation.java:330) at org.apache.servicecomb.common.rest.AbstractRestInvocation.sendResponseQuietly(AbstractRestInvocation.java:313) at org.apache.servicecomb.common.rest.AbstractRestInvocation.sendFailResponse(AbstractRestInvocation.java:305) at org.apache.servicecomb.common.rest.RestProducerInvocation.invoke(RestProducerInvocation.java:48) at org.apache.servicecomb.transport.rest.vertx.VertxRestDispatcher.onRequest(VertxRestDispatcher.java:220) at io.vertx.ext.web.impl.RouteState.handleContext(RouteState.java:1281) at io.vertx.ext.web.impl.RoutingContextImplBase.iterateNext(RoutingContextImplBase.java:177) at io.vertx.ext.web.impl.RoutingContextImpl.next(RoutingContextImpl.java:151) at org.apache.servicecomb.transport.rest.vertx.RestBodyHandler$BHandler.doEnd(RestBodyHandler.java:416) at org.apache.servicecomb.transport.rest.vertx.RestBodyHandler$BHandler.end(RestBodyHandler.java:393) at io.vertx.core.impl.ContextInternal.dispatch(ContextInternal.java:270) at io.vertx.core.impl.ContextInternal.dispatch(ContextInternal.java:252) at io.vertx.core.http.impl.HttpEventHandler.handleEnd(HttpEventHandler.java:76) at io.vertx.core.http.impl.Http1xServerRequest.onEnd(Http1xServerRequest.java:640) at io.vertx.core.http.impl.Http1xServerRequest.lambda$pendingHandler$0(Http1xServerRequest.java:375) at io.vertx.core.streams.impl.InboundBuffer.handleEvent(InboundBuffer.java:279) at io.vertx.core.streams.impl.InboundBuffer.write(InboundBuffer.java:157) at io.vertx.core.http.impl.Http1xServerRequest.handleEnd(Http1xServerRequest.java:617) at io.vertx.core.impl.ContextImpl.execute(ContextImpl.java:327) at io.vertx.core.impl.DuplicatedContext.execute(DuplicatedContext.java:158) at io.vertx.core.http.impl.Http1xServerConnection.onEnd(Http1xServerConnection.java:215) at io.vertx.core.http.impl.Http1xServerConnection.onContent(Http1xServerConnection.java:206) at io.vertx.core.http.impl.Http1xServerConnection.handleOther(Http1xServerConnection.java:189) at io.vertx.core.http.impl.Http1xServerConnection.handleMessage(Http1xServerConnection.java:176) at io.vertx.core.net.impl.ConnectionBase.read(ConnectionBase.java:159) at io.vertx.core.net.impl.VertxHandler.channelRead(VertxHandler.java:153) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:442) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:412) at io.netty.handler.timeout.IdleStateHandler.channelRead(IdleStateHandler.java:289) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:442) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:412) at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:346) at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:318) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:444) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:412) at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1357) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:440) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420) at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:868) at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:166) at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:796) at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:732) at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:658) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:562) at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:998) at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) at java.base/java.lang.Thread.run(Thread.java:1583) 2025-12-11 14:28:32.632|ERROR|vmall_car_server|WatcherScheduledJob-thread:136|2.0.1.9|7582482721440371074||||||VmallException,regCenter is empty,,DSS# regCenter is empty.|com.huawei.vmall.dss.job.scheduled.WatcherScheduledJob 2025-12-11 14:29:02.638|ERROR|vmall_car_server|WatcherScheduledJob-thread:136|2.0.1.9|7582482721440371074||||||VmallException,regCenter is empty,,DSS# regCenter is empty.|com.huawei.vmall.dss.job.scheduled.WatcherScheduledJob 2025-12-11 14:29:32.644|ERROR|vmall_car_server|WatcherScheduledJob-thread:136|2.0.1.9|7582482721440371074||||||VmallException,regCenter is empty,,DSS# regCenter is empty.|com.huawei.vmall.dss.job.scheduled.WatcherScheduledJob 2025-12-11 14:30:02.632|ERROR|vmall_car_server|WatcherScheduledJob-thread:136|2.0.1.9|7582482721440371074||||||VmallException,regCenter is empty,,DSS# regCenter is empty.|com.huawei.vmall.dss.job.scheduled.WatcherScheduledJob 2025-12-11 14:30:24.367|WARN |vmall_car_server|Apollo-RemoteConfigRepository-1:54|2.0.1.9|7582482668232902934||||||Sync config failed, will retry. Repository class com.ctrip.framework.apollo.internals.RemoteConfigRepository, reason: Load Apollo Config failed - appId: ms_car_service, cluster: mock-alpha, namespace: dcs_instance_cfg.json, url: https://10.51.15.105:28443/configs/ms_car_service/mock-alpha/dcs_instance_cfg.json?dataCenter=mock-alpha&ip=10.45.17.220 [Cause: [status code: 404] Could not find config for namespace - appId: ms_car_service, cluster: mock-alpha, namespace: dcs_instance_cfg.json, please check whether the configs are released in Apollo!]|com.ctrip.framework.apollo.internals.AbstractConfigRepository 2025-12-11 14:30:32.632|ERROR|vmall_car_server|WatcherScheduledJob-thread:136|2.0.1.9|7582482721440371074||||||VmallException,regCenter is empty,,DSS# regCenter is empty.|com.huawei.vmall.dss.job.scheduled.WatcherScheduledJob 2025-12-11 14:31:02.631|ERROR|vmall_car_server|WatcherScheduledJob-thread:136|2.0.1.9|7582482721440371074||||||VmallException,regCenter is empty,,DSS# regCenter is empty.|com.huawei.vmall.dss.job.scheduled.WatcherScheduledJob
最新发布
12-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值