| <!DOCTYPE html> |
| <html class="writer-html5" lang="en" > |
| <head> |
| <meta charset="utf-8" /> |
| <meta http-equiv="X-UA-Compatible" content="IE=edge" /> |
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| <link rel="shortcut icon" href="../../img/favicon.ico" /> |
| <title>体质指数微服务应用开发 - ServiceComb Java Chassis 开发指南</title> |
| <link rel="stylesheet" href="../../css/theme.css" /> |
| <link rel="stylesheet" href="../../css/theme_extra.css" /> |
| <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/github.min.css" /> |
| |
| <script> |
| // Current page data |
| var mkdocs_page_name = "\u4f53\u8d28\u6307\u6570\u5fae\u670d\u52a1\u5e94\u7528\u5f00\u53d1"; |
| var mkdocs_page_input_path = "featured-topics/application-bmi/quick-start-bmi.md"; |
| var mkdocs_page_url = null; |
| </script> |
| |
| <script src="../../js/jquery-3.6.0.min.js" defer></script> |
| <!--[if lt IE 9]> |
| <script src="../../js/html5shiv.min.js"></script> |
| <![endif]--> |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/highlight.min.js"></script> |
| <script>hljs.initHighlightingOnLoad();</script> |
| </head> |
| |
| <body class="wy-body-for-nav" role="document"> |
| |
| <div class="wy-grid-for-nav"> |
| <nav data-toggle="wy-nav-shift" class="wy-nav-side stickynav"> |
| <div class="wy-side-scroll"> |
| <div class="wy-side-nav-search"> |
| <a href="../../index.html" class="icon icon-home"> ServiceComb Java Chassis 开发指南 |
| </a> |
| </div> |
| |
| <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu"> |
| <ul> |
| <li class="toctree-l1"><a class="reference internal" href="../../toc.html">目录</a> |
| </li> |
| </ul> |
| <ul> |
| <li class="toctree-l1"><a class="reference internal" href="../../index.html">概述</a> |
| </li> |
| </ul> |
| <ul> |
| <li class="toctree-l1"><a class="reference internal" href="../../start/catalog.html">快速入门</a> |
| </li> |
| </ul> |
| <ul> |
| <li class="toctree-l1"><a class="reference internal" href="../../start/design.html">设计选型参考</a> |
| </li> |
| </ul> |
| <ul> |
| <li class="toctree-l1"><a class="reference internal" href="../../build-provider/definition/service-definition.html">微服务定义</a> |
| </li> |
| </ul> |
| <ul> |
| <li class="toctree-l1"><a class="reference internal" href="../../build-provider/catalog.html">开发服务提供者</a> |
| </li> |
| </ul> |
| <ul> |
| <li class="toctree-l1"><a class="reference internal" href="../../build-consumer/catalog.html">开发服务消费者</a> |
| </li> |
| </ul> |
| <ul> |
| <li class="toctree-l1"><a class="reference internal" href="../../general-development/catalog.html">通用功能开发</a> |
| </li> |
| </ul> |
| <p class="caption"><span class="caption-text">多样化的通信协议功能参考</span></p> |
| <ul> |
| <li class="toctree-l1"><a class="reference internal" href="../../transports/introduction.html">多协议介绍</a> |
| </li> |
| <li class="toctree-l1"><a class="reference internal" href="../../transports/rest-over-servlet.html">REST over Servlet</a> |
| </li> |
| <li class="toctree-l1"><a class="reference internal" href="../../transports/rest-over-vertx.html">REST over Vertx</a> |
| </li> |
| <li class="toctree-l1"><a class="reference internal" href="../../transports/http2.html">REST over HTTP2</a> |
| </li> |
| <li class="toctree-l1"><a class="reference internal" href="../../transports/highway-rpc.html">Highway</a> |
| </li> |
| </ul> |
| <p class="caption"><span class="caption-text">多样化的服务注册与发现功能参考</span></p> |
| <ul> |
| <li class="toctree-l1"><a class="reference internal" href="../../registry/introduction.html">注册发现说明</a> |
| </li> |
| <li class="toctree-l1"><a class="reference internal" href="../../registry/service-center.html">使用服务中心</a> |
| </li> |
| <li class="toctree-l1"><a class="reference internal" href="../../registry/local-registry.html">本地注册发现</a> |
| </li> |
| <li class="toctree-l1"><a class="reference internal" href="../../registry/distributed.html">去中心化注册发现</a> |
| </li> |
| </ul> |
| <p class="caption"><span class="caption-text">管理服务配置</span></p> |
| <ul> |
| <li class="toctree-l1"><a class="reference internal" href="../../config/general-config.html">通用配置说明</a> |
| </li> |
| <li class="toctree-l1"><a class="reference internal" href="../../config/read-config.html">在程序中读取配置信息</a> |
| </li> |
| </ul> |
| <p class="caption"><span class="caption-text">服务治理功能参考</span></p> |
| <ul> |
| <li class="toctree-l1"><a class="reference internal" href="../../references-handlers/intruduction.html">处理链介绍</a> |
| </li> |
| <li class="toctree-l1"><a class="reference internal" href="../../references-handlers/loadbalance.html">负载均衡</a> |
| </li> |
| <li class="toctree-l1"><a class="reference internal" href="../../references-handlers/ratelimit.html">限流</a> |
| </li> |
| <li class="toctree-l1"><a class="reference internal" href="../../references-handlers/router.html">灰度发布</a> |
| </li> |
| <li class="toctree-l1"><a class="reference internal" href="../../references-handlers/fault-injection.html">故障注入</a> |
| </li> |
| <li class="toctree-l1"><a class="reference internal" href="../../references-handlers/governance.html">流量特征治理</a> |
| </li> |
| <li class="toctree-l1"><a class="reference internal" href="../../references-handlers/fail-retry.html">快速失败和重试</a> |
| </li> |
| </ul> |
| <p class="caption"><span class="caption-text">网关功能参考</span></p> |
| <ul> |
| <li class="toctree-l1"><a class="reference internal" href="../../edge/open-service.html">介绍</a> |
| </li> |
| <li class="toctree-l1"><a class="reference internal" href="../../edge/by-servicecomb-sdk.html">使用 Edge Service 做网关</a> |
| </li> |
| <li class="toctree-l1"><a class="reference internal" href="../../edge/zuul.html">使用 `zuul` 和 `spring cloud gateway` 做网关</a> |
| </li> |
| <li class="toctree-l1"><a class="reference internal" href="../../edge/nginx.html">nginx 网关简单介绍</a> |
| </li> |
| </ul> |
| <p class="caption"><span class="caption-text">安全特性参考</span></p> |
| <ul> |
| <li class="toctree-l1"><a class="reference internal" href="../../references-handlers/publickey.html">公钥认证</a> |
| </li> |
| <li class="toctree-l1"><a class="reference internal" href="../../security/tls.html">使用TLS通信</a> |
| </li> |
| <li class="toctree-l1"><a class="reference internal" href="../../security/shi-yong-rsa-ren-zheng.html">使用RSA认证</a> |
| </li> |
| </ul> |
| <p class="caption"><span class="caption-text">服务打包和运行</span></p> |
| <ul> |
| <li class="toctree-l1"><a class="reference internal" href="../../packaging/standalone.html">以standalone模式打包</a> |
| </li> |
| <li class="toctree-l1"><a class="reference internal" href="../../packaging/web-container.html">以WEB容器模式打包</a> |
| </li> |
| </ul> |
| <p class="caption"><span class="caption-text">专题文章</span></p> |
| <ul> |
| <li class="toctree-l1"><a class="reference internal" href="../../using-java-chassis-in-spring-boot/using-java-chassis-in-spring-boot.html">在Spring Boot中使用java chassis</a> |
| </li> |
| <li class="toctree-l1"><a class="reference internal" href="../features.html">新功能介绍系列文章</a> |
| </li> |
| <li class="toctree-l1"><a class="reference internal" href="../compatibility.html">兼容问题和兼容性策略</a> |
| </li> |
| <li class="toctree-l1"><a class="reference internal" href="../upgrading.html">升级指导系列文章</a> |
| </li> |
| <li class="toctree-l1"><a class="reference internal" href="../performance.html">性能问题分析和调优</a> |
| </li> |
| </ul> |
| <p class="caption"><span class="caption-text">常用配置项参考</span></p> |
| <ul> |
| <li class="toctree-l1"><a class="reference internal" href="../../config-reference/rest-transport-client.html">REST Transport Client 配置项</a> |
| </li> |
| <li class="toctree-l1"><a class="reference internal" href="../../config-reference/config-center-client.html">Config Center Client 配置项</a> |
| </li> |
| <li class="toctree-l1"><a class="reference internal" href="../../config-reference/service-center-client.html">Service Center Client 配置项</a> |
| </li> |
| <li class="toctree-l1"><a class="reference internal" href="../../config-reference/kie-client.html">ServiceComb Kie Client 配置项</a> |
| </li> |
| </ul> |
| <p class="caption"><span class="caption-text">常见问题</span></p> |
| <ul> |
| <li class="toctree-l1"><a class="reference internal" href="../../question-and-answer/faq.html">FAQ</a> |
| </li> |
| <li class="toctree-l1"><a class="reference internal" href="../../question-and-answer/question_answer.html">Q & A</a> |
| </li> |
| <li class="toctree-l1"><a class="reference internal" href="../../question-and-answer/interface-compatibility.html">微服务接口兼容常见问题</a> |
| </li> |
| </ul> |
| </div> |
| </div> |
| </nav> |
| |
| <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"> |
| <nav class="wy-nav-top" role="navigation" aria-label="Mobile navigation menu"> |
| <i data-toggle="wy-nav-top" class="fa fa-bars"></i> |
| <a href="../../index.html">ServiceComb Java Chassis 开发指南</a> |
| |
| </nav> |
| <div class="wy-nav-content"> |
| <div class="rst-content"><div role="navigation" aria-label="breadcrumbs navigation"> |
| <ul class="wy-breadcrumbs"> |
| <li><a href="../../index.html" class="icon icon-home" alt="Docs"></a> »</li> |
| <li>体质指数微服务应用开发</li> |
| <li class="wy-breadcrumbs-aside"> |
| </li> |
| </ul> |
| <hr/> |
| </div> |
| <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article"> |
| <div class="section" itemprop="articleBody"> |
| |
| <h1 id="_1">体质指数微服务应用开发</h1> |
| <p>在您进一步阅读之前,请确保您已阅读了<a href="quick-start.html">快速入门</a>,并已成功运行<strong>体质指数</strong>微服务。接下来将进入<strong>体质指数</strong>微服务应用的开发之旅。</p> |
| <h2 id="_2">快速开发微服务应用</h2> |
| <p>BMI主要由两个微服务组成:</p> |
| <ul> |
| <li> |
| <p><strong>体质指数计算器</strong>:负责处理运算事务。</p> |
| </li> |
| <li> |
| <p><strong>体质指数界面</strong>:提供用户界面及网关服务。</p> |
| </li> |
| </ul> |
| <p>在开始前,需要先在服务的父工程中添加以下依赖项:</p> |
| <pre><code class="language-java"> <dependencyManagement> |
| <dependencies> |
| <dependency> |
| <groupId>org.apache.servicecomb</groupId> |
| <artifactId>java-chassis-dependencies</artifactId> |
| <version>${project.version}</version> |
| <type>pom</type> |
| <scope>import</scope> |
| </dependency> |
| </dependencies> |
| </dependencyManagement> |
| </code></pre> |
| <p><strong>注意</strong>: <code>java-chassis-dependencies</code> 这个依赖是以pom的形式导入来统一项目中的依赖项的版本管理。</p> |
| <p>下面将对这两个微服务的实现进行介绍,其代码已托管于<a href="https://github.com/apache/servicecomb-samples/tree/master/java-chassis-samples/bmi">github</a>上。</p> |
| <h3 id="_3">体质指数计算器实现</h3> |
| <p>体质指数计算器提供运算服务,其实现分为三部分:</p> |
| <ul> |
| <li> |
| <p>具体运算实现</p> |
| </li> |
| <li> |
| <p>服务端点定义</p> |
| </li> |
| <li> |
| <p>服务启动入口</p> |
| </li> |
| </ul> |
| <h4 id="_4">具体运算实现</h4> |
| <p>本模块负责计算体质指数,根据公式 <code>体质指数=体重 / 身高^2</code> 进行实现,代码如下:</p> |
| <pre><code class="language-java">public interface CalculatorService { |
| double calculate(double height, double weight); |
| } |
| |
| @Service |
| public class CalculatorServiceImpl implements CalculatorService { |
| @Override |
| public double calculate(double height, double weight) { |
| if (height <= 0 || weight <= 0) { |
| throw new IllegalArgumentException("Arguments must be above 0"); |
| } |
| double heightInMeter = height / 100; |
| return weight / (heightInMeter * heightInMeter); |
| } |
| } |
| </code></pre> |
| <h4 id="_5">服务端点定义</h4> |
| <p>服务端点用于生成服务契约,使得服务间能无缝进行通信。首先定义端点接口:</p> |
| <pre><code class="language-java">public interface CalculatorEndpoint { |
| double calculate(double height, double weight); |
| } |
| </code></pre> |
| <p>引入 <strong>ServiceComb</strong> 依赖:</p> |
| <pre><code class="language-xml"> <dependency> |
| <groupId>org.apache.servicecomb</groupId> |
| <artifactId>java-chassis-spring-boot-starter-servlet</artifactId> |
| </dependency> |
| </code></pre> |
| <p>暴露运算服务的Restful端点:</p> |
| <pre><code class="language-java">@RestSchema(schemaId = "calculatorRestEndpoint") |
| @RequestMapping("/") |
| public class CalculatorRestEndpoint implements CalculatorEndpoint { |
| |
| private final CalculatorService calculatorService; |
| |
| @Autowired |
| public CalculatorRestEndpoint(CalculatorService calculatorService) { |
| this.calculatorService = calculatorService; |
| } |
| |
| @Override |
| @GetMapping("/bmi") |
| public double calculate(double height, double weight) { |
| return calculatorService.calculate(height, weight); |
| } |
| } |
| </code></pre> |
| <p>这里用<code>@RestSchema</code>注释端点后, <strong>ServiceComb</strong> 微服务框架会自动生成对应的服务端点契约,并根据 |
| 如下的 <code>application.yml</code> 文件中的定义来配置端点端口,将契约和服务一起注册到服务注册中心。</p> |
| <pre><code class="language-yaml">APPLICATION_ID: bmi |
| service_description: |
| name: calculator |
| version: 0.0.1 |
| servicecomb: |
| service: |
| registry: |
| address: http://127.0.0.1:30100 |
| rest: |
| address: 0.0.0.0:7777 |
| </code></pre> |
| <p><strong><em>注意</em></strong>: <strong>ServiceComb</strong>默认的配置文件名称是<code>microservice.yaml</code>。 本应用采用 spring boot作为运行环境,因此遵循 |
| spring boot的规范,配置文件名称使用 <code>application.yml</code>。</p> |
| <h4 id="_6">服务启动入口</h4> |
| <p>服务启动入口中只需添加 <code>@EnableServiceComb</code> 的注解即可启用 <em>ServiceComb</em> 微服务框架,代码如下:</p> |
| <pre><code class="language-java">@SpringBootApplication |
| @EnableServiceComb |
| public class CalculatorApplication { |
| public static void main(String[] args) { |
| SpringApplication.run(CalculatorApplication.class, args); |
| } |
| } |
| </code></pre> |
| <h3 id="_7">体质指数界面实现</h3> |
| <p>本模块负责提供用户界面及网关服务。其实现主要分为三部分:</p> |
| <ul> |
| <li> |
| <p>前端界面</p> |
| </li> |
| <li> |
| <p>网关及路由规则</p> |
| </li> |
| <li> |
| <p>服务启动入口</p> |
| </li> |
| </ul> |
| <p>其中,前端界面的组件使用了<a href="http://getbootstrap.com/">Bootstrap</a>来开发。</p> |
| <h4 id="_8">网关及路由规则</h4> |
| <p>网关服务使用 <strong>ServiceComb</strong> 提供的 <a href="http://localhost:8000/edge/by-servicecomb-sdk/">Edge Service</a> 来实现。</p> |
| <p>引入依赖:</p> |
| <pre><code class="language-xml"><dependency> |
| <groupId>org.apache.servicecomb</groupId> |
| <artifactId>java-chassis-spring-boot-starter-standalone</artifactId> |
| </dependency> |
| <dependency> |
| <groupId>org.apache.servicecomb</groupId> |
| <artifactId>edge-core</artifactId> |
| </dependency> |
| </code></pre> |
| <p>在 <code>application.yml</code> 文件中配置路由规则及服务端口信息:</p> |
| <pre><code class="language-yaml">ervicecomb: |
| service: |
| registry: |
| address: http://127.0.0.1:30100 |
| rest: |
| address: 0.0.0.0:8889 |
| |
| tracing: |
| enabled: false |
| |
| http: |
| dispatcher: |
| edge: |
| default: |
| enabled: false |
| prefix: api |
| withVersion: false |
| prefixSegmentCount: 1 |
| url: |
| enabled: true |
| mappings: |
| calculator: |
| prefixSegmentCount: 1 |
| path: "/calculator/.*" |
| microserviceName: calculator |
| versionRule: 0.0.0+ |
| |
| # This is web root for windows server, change this path according to where you put your source code |
| gateway: |
| webroot: /code/servicecomb-samples/java-chassis-samples/bmi/webapp/src/main/resources/static |
| </code></pre> |
| <p>其中 <code>servicecomb.http.dispatcher.edge.default.enabled</code> 禁用了默认的路由规则, <code>servicecomb.http.dispatcher.edge.url.enabled</code> |
| 启用了基于URL映射的路由规则。 规则的含义表示将URL为 <code>/calculator/.*</code> 的请求转发到 <code>calculator</code>服务。</p> |
| <p><code>gateway.webroot</code> 配置了静态页面所在的目录位置。 示例里面采用的是 Windows 目录环境,如果采用 Linux 环境,需要配置相对路径。</p> |
| <p><code>StaticWebpageDispatcher</code> 扩展了 Edge Service 的转发规则, 使用了 vert.x 提供的 <code>StaticHandler</code> 发布静态页面。 Edge |
| Service 采用 SPI 的方式扩展 Dispatcher, 需要创建文件 <code>org.apache.servicecomb.transport.rest.vertx.VertxHttpDispatcher</code>。 </p> |
| <h4 id="_9">服务启动入口</h4> |
| <p>服务启动入口也只需要声明启用 <code>ServiceComb</code> 即可。</p> |
| <pre><code class="language-java">@SpringBootApplication |
| @EnableServiceComb |
| public class GatewayApplication { |
| public static void main(String[] args) { |
| new SpringApplicationBuilder().web(WebApplicationType.NONE).sources(GatewayApplication.class).run(args); |
| } |
| } |
| </code></pre> |
| <p>至此,<strong>体质指数</strong>应用已开发完毕,您可以通过<a href="quick-start.html">快速入门</a>中的步骤对其进行验证。</p> |
| <h2 id="_10">下一步</h2> |
| <ul> |
| <li>阅读 <a href="quick-start-advance.html">微服务开发进阶</a></li> |
| </ul> |
| |
| </div> |
| </div><footer> |
| |
| <hr/> |
| |
| <div role="contentinfo"> |
| <!-- Copyright etc --> |
| </div> |
| |
| Built with <a href="https://www.mkdocs.org/">MkDocs</a> using a <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. |
| </footer> |
| |
| </div> |
| </div> |
| |
| </section> |
| |
| </div> |
| |
| <div class="rst-versions" role="note" aria-label="Versions"> |
| <span class="rst-current-version" data-toggle="rst-current-version"> |
| |
| |
| |
| </span> |
| </div> |
| <script>var base_url = '../..';</script> |
| <script src="../../js/theme_extra.js" defer></script> |
| <script src="../../js/theme.js" defer></script> |
| <script src="../../search/main.js" defer></script> |
| <script defer> |
| window.onload = function () { |
| SphinxRtdTheme.Navigation.enable(true); |
| }; |
| </script> |
| |
| </body> |
| </html> |