content/references/java-chassis/en_US/build-provider/transparent-rpc/index.html (429 lines of code) (raw):

<!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>Develop with Transparent RPC - ServiceComb Java Chassis Developers Guide</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 = "Develop with Transparent RPC"; var mkdocs_page_input_path = "build-provider/transparent-rpc.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="../.." class="icon icon-home"> ServiceComb Java Chassis Developers Guide </a><div role="search"> <form id ="rtd-search-form" class="wy-form" action="../../search.html" method="get"> <input type="text" name="q" placeholder="Search docs" title="Type search term here" /> </form> </div> </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="../..">Introduction</a> </li> </ul> <p class="caption"><span class="caption-text">Getting Started</span></p> <ul> <li class="toctree-l1"><a class="reference internal" href="../../start/terminology/">Glossary</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../start/architecture/">Architecture</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../start/development-environment/">Development environment</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../start/first-sample/">Develop the first microservice</a> </li> </ul> <p class="caption"><span class="caption-text">Development Service Provider</span></p> <ul class="current"> <li class="toctree-l1"><a class="reference internal" href="../definition/service-definition/">Service definition</a> </li> <li class="toctree-l1"><a class="reference internal" href="../define-contract/">Service contract definition</a> </li> <li class="toctree-l1"><a class="reference internal" href="../code-first/">Implicit API definition</a> </li> <li class="toctree-l1"><a class="reference internal" href="../swagger-annotation/">Use Swagger annotations</a> </li> <li class="toctree-l1"><a class="reference internal" href="../springmvc/">Develop with SpringMVC</a> </li> <li class="toctree-l1"><a class="reference internal" href="../jaxrs/">Develop with JAX-RS</a> </li> <li class="toctree-l1 current"><a class="reference internal current" href="./">Develop with Transparent RPC</a> <ul class="current"> <li class="toctree-l2"><a class="reference internal" href="#concept-description">Concept Description</a> </li> <li class="toctree-l2"><a class="reference internal" href="#development-example">Development Example</a> </li> </ul> </li> <li class="toctree-l1"><a class="reference internal" href="../interface-constraints/">Interface definition and data type</a> </li> <li class="toctree-l1"><a class="reference internal" href="../listen-address-and-publish-address/">Service listening address and publishing address</a> </li> <li class="toctree-l1"><a class="reference internal" href="../thread-pool/">Thread pool</a> </li> <li class="toctree-l1"><a class="reference internal" href="#">Service Configuration</a> <ul> <li class="toctree-l2"><a class="reference internal" href="../configuration/ratelimite-strategy/">Rate Limiting Policy</a> </li> <li class="toctree-l2"><a class="reference internal" href="../configuration/downgrade-strategy/">Fallback Policy</a> </li> <li class="toctree-l2"><a class="reference internal" href="../configuration/parameter-validator/">Parameter Validator</a> </li> </ul> </li> <li class="toctree-l1"><a class="reference internal" href="../bootup/">Boot-up Process</a> </li> <li class="toctree-l1"><a class="reference internal" href="../access-log-configuration/">Access Log Configuration</a> </li> </ul> <p class="caption"><span class="caption-text">Writing Service Consumer</span></p> <ul> <li class="toctree-l1"><a class="reference internal" href="../../build-consumer/common-configuration/">Consumer common configuration</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../build-consumer/using-resttemplate/">Using Rest Template</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../build-consumer/using-AsyncRestTemplate/">Using AsyncRestTemplate</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../build-consumer/develop-consumer-using-rpc/">Using with RPC</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../build-consumer/with-contract/">Contract</a> </li> <li class="toctree-l1"><a class="reference internal" href="#">Invoke control</a> <ul> <li class="toctree-l2"><a class="reference internal" href="../../build-consumer/circuit-breaker/">Circuit Breaker</a> </li> <li class="toctree-l2"><a class="reference internal" href="../../build-consumer/flow-control/">Flow Control</a> </li> <li class="toctree-l2"><a class="reference internal" href="../../build-consumer/fault-injection/">Fault Injection</a> </li> </ul> </li> <li class="toctree-l1"><a class="reference internal" href="../../build-consumer/3rd-party-service-invoke/">Invoke 3rd-party REST services</a> </li> </ul> <p class="caption"><span class="caption-text">Transports</span></p> <ul> <li class="toctree-l1"><a class="reference internal" href="../../transports/rest-over-servlet/">REST over Servlet</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../transports/rest-over-vertx/">REST over Vertx</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../transports/highway-rpc/">Highway</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../transports/http2/">HTTP2</a> </li> </ul> <p class="caption"><span class="caption-text">General Development</span></p> <ul> <li class="toctree-l1"><a class="reference internal" href="../../general-development/visit-sc/">Access Service Center</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../general-development/metrics/">Metrics</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../general-development/microservice-invocation-chain/">Microservice invocation chain</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../general-development/customized-tracing/">Customized-Tracing</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../general-development/local-develop-test/">Local development and testing</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../general-development/http-filter/">Http Filter</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../general-development/file-upload/">File Uploading</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../general-development/file-download/">File Downloading</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../general-development/reactive/">Reactive Programing</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../general-development/dnsconfig/">DNS Custom Configuration</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../general-development/dai-li-she-zhi/">Proxy Settings</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../general-development/report-framework-version/">Report framework version</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../general-development/cross-app-invocation/">Cross-application invocation</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../general-development/secret-field/">Customized serialization and deserialization</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../general-development/context/">Using Context to pass control messages</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../general-development/produceprocess/">Return value serialization extension</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../general-development/CORS/">CORS mechanism</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../general-development/AlarmEvent/">Get fuse and instance isolation alarm event information</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../general-development/shutdown/">Shutdown gracefully</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../general-development/error-handling/">Handling exceptions</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../general-development/multienvironment/">Multi-environment isolation between microservice instances</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../general-development/thread-model/">Thread Model</a> </li> </ul> <p class="caption"><span class="caption-text">Configuration</span></p> <ul> <li class="toctree-l1"><a class="reference internal" href="../../config/general-config/">General config</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../config/inject-config/">Configuration injection</a> </li> </ul> <p class="caption"><span class="caption-text">Service Capability Open</span></p> <ul> <li class="toctree-l1"><a class="reference internal" href="../../edge/open-service/">Intruductions</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../edge/by-servicecomb-sdk/">Using Edge Service</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../edge/nginx/">Using confd and Nginx as edge services</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../edge/zuul/">Use zuul as edge services</a> </li> </ul> <p class="caption"><span class="caption-text">Service Packing and Running</span></p> <ul> <li class="toctree-l1"><a class="reference internal" href="../../packaging/standalone/">Standalone mode</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../packaging/web-container/">WEB container mode</a> </li> </ul> <p class="caption"><span class="caption-text">Micro Service Security</span></p> <ul> <li class="toctree-l1"><a class="reference internal" href="../../security/tls/">Using TLS</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../security/rsa/">Using RSA certification</a> </li> </ul> <p class="caption"><span class="caption-text">Using java chassis in Spring Boot</span></p> <ul> <li class="toctree-l1"><a class="reference internal" href="../../using-java-chassis-in-spring-boot/using-java-chassis-in-spring-boot/">Intruductions</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../using-java-chassis-in-spring-boot/components-for-spring-boot/">spring boot starter for java-chassis</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../using-java-chassis-in-spring-boot/java-application/">JAVA application development</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../using-java-chassis-in-spring-boot/web-application/">Web development method development</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../using-java-chassis-in-spring-boot/diff-between-java-web/">The difference between JAVA application method and Web development method</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../using-java-chassis-in-spring-boot/diff-spring-mvc/">The difference in Spring MVC mode</a> </li> </ul> <p class="caption"><span class="caption-text">Handlers reference</span></p> <ul> <li class="toctree-l1"><a class="reference internal" href="../../references-handlers/intruduction/">Intruductions</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../references-handlers/loadbalance/">Load Balancing</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../references-handlers/publickey/">Public key authentication</a> </li> </ul> <p class="caption"><span class="caption-text">FAQ</span></p> <ul> <li class="toctree-l1"><a class="reference internal" href="../../question-and-answer/question_answer/">Q & A</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../question-and-answer/faq/">FAQ</a> </li> <li class="toctree-l1"><a class="reference internal" href="../../question-and-answer/interface-compatibility/">Micro Service Interface Compatibility FAQ</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="../..">ServiceComb Java Chassis Developers Guide</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="../.." class="icon icon-home" alt="Docs"></a> &raquo;</li> <li>Development Service Provider &raquo;</li> <li>Develop with Transparent RPC</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="develop-microservice-with-transparent-rpc">Develop Microservice with Transparent RPC</h1> <h2 id="concept-description">Concept Description</h2> <p>The transparent remote procedure call(RPC) development mode is a development mode based on API and API implementation. The service developer does not need to use the description of Spring MVC and JAX-RS.</p> <h2 id="development-example">Development Example</h2> <ul> <li><strong>Step 1</strong> Import dependencies into your maven project:</li> </ul> <p><code>xml &lt;dependencyManagement&gt; &lt;dependencies&gt; &lt;dependency&gt; &lt;groupId&gt;org.apache.servicecomb&lt;/groupId&gt; &lt;artifactId&gt;java-chassis-dependencies&lt;/artifactId&gt; &lt;version&gt;1.0.0-m1&lt;/version&gt; &lt;type&gt;pom&lt;/type&gt; &lt;scope&gt;import&lt;/scope&gt; &lt;/dependency&gt; &lt;/dependencies&gt; &lt;/dependencyManagement&gt; &lt;dependencies&gt; &lt;!--transport can optional import through endpoint setting in microservice.yaml, we import both rest and highway as example--&gt; &lt;dependency&gt; &lt;groupId&gt;org.apache.servicecomb&lt;/groupId&gt; &lt;artifactId&gt;transport-rest-vertx&lt;/artifactId&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;org.apache.servicecomb&lt;/groupId&gt; &lt;artifactId&gt;transport-highway&lt;/artifactId&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;org.apache.servicecomb&lt;/groupId&gt; &lt;artifactId&gt;provider-pojo&lt;/artifactId&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;org.slf4j&lt;/groupId&gt; &lt;artifactId&gt;slf4j-log4j12&lt;/artifactId&gt; &lt;/dependency&gt; &lt;/dependencies&gt;</code></p> <ul> <li><strong>Step 2</strong> Define a service API. Compile the Java API definition based on the API definition defined before development. The code is as follows:</li> </ul> <p><code>java public interface Hello { String sayHi(String name); String sayHello(Person person); }</code></p> <ul> <li><strong>Step 3</strong> implement the service. The implementation of the Hello service is as follows:</li> </ul> <p>```java import org.apache.servicecomb.samples.common.schema.Hello; import org.apache.servicecomb.samples.common.schema.models.Person;</p> <p>public class HelloImpl implements Hello { @Override public String sayHi(String name) { return "Hello " + name; }</p> <pre><code> @Override public String sayHello(Person person) { return "Hello person " + person.getName(); } </code></pre> <p>} ```</p> <ul> <li><strong>Step 4</strong> Release the service. The transparent RPC development mode supports two service release mode: Spring XML configuration and Annotation configuration:</li> <li>Spring XML configuration Mode: Create the pojoHello.bean.xml file in the resources/META-INF/spring directory and declare the schema in the file. The content of the file is as follows:</li> </ul> <p>```xml <?xml version="1.0" encoding="UTF-8"?></p> <p><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:cse="http://www.huawei.com/schema/paas/cse/rpc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans classpath:org/springframework/beans/factory/xml/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.huawei.com/schema/paas/cse/rpc classpath:META-INF/spring/spring-paas-cse-rpc.xsd"></p> <pre><code> &lt;cse:rpc-schema schema-id="pojoHello" implementation="org.apache.servicecomb.samples.pojo.provider.PojoHelloImpl"/&gt; </code></pre> <p></beans> ```</p> <ol> <li>Annotation configuration Mode: @RpcSchema is used to define schema during the API Hello implementation. The code is as follows:</li> </ol> <p>```java import org.apache.servicecomb.provider.pojo.RpcSchema; @RpcSchema(schemaId = "pojoHello") public class HelloImpl implements Hello { @Override public String sayHi(String name) { return "Hello " + name; }</p> <pre><code> @Override public String sayHello(Person person) { return "Hello person " + person.getName(); } </code></pre> <p>} ```</p> <p>In the pojoHello.bean.xml file of resources/META-INF/spring directory, configure base-package that performs scanning. The content of the file is as follows:</p> <p>```xml <?xml version="1.0" encoding="UTF-8"?></p> <p><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:cse="http://www.huawei.com/schema/paas/cse/rpc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans classpath:org/springframework/beans/factory/xml/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.huawei.com/schema/paas/cse/rpc classpath:META-INF/spring/spring-paas-cse-rpc.xsd"></p> <pre><code> &lt;context:component-scan base-package="org.apache.servicecomb.samples.pojo.provider"/&gt; </code></pre> <p></beans> ```</p> <p><strong>Note: THE PATH FOR RPC IS <code>ClassName/MethodName</code>, AND THE METHOD IS <code>POST</code>.</strong></p> <p>In this sample the Path of sayHi is <code>/HelloImpl/sayHi</code>, and the Path of sayHello is <code>/HelloImpl/sayHello</code>.</p> <blockquote> <p><strong>NOTE</strong>: Different from the Spring MVC and JAX-RS development modes, the transparent RPC development mode used <code>@RpcSchema</code> instead of <code>@RestSchema</code>.</p> </blockquote> <ul> <li><strong>Step 5</strong> Add service definition file:</li> </ul> <p>Add <a href="http://servicecomb.incubator.apache.org/cn/users/service-definition/">microservice.yaml</a> file into resources folder of your project.</p> <ul> <li><strong>Step 6</strong> Add Main class:</li> </ul> <p>```java import org.apache.servicecomb.foundation.common.utils.BeanUtils; import org.apache.servicecomb.foundation.common.utils.Log4jUtils;</p> <p>public class Application { public static void main(String[] args) throws Exception { //initializing log, loading bean(including its parameters), and registering service, more detail can be found here : http://servicecomb.incubator.apache.org/users/application-boot-process/ Log4jUtils.init(); BeanUtils.init(); } } ```</p> </div> </div><footer> <div class="rst-footer-buttons" role="navigation" aria-label="Footer Navigation"> <a href="../jaxrs/" class="btn btn-neutral float-left" title="Develop with JAX-RS"><span class="icon icon-circle-arrow-left"></span> Previous</a> <a href="../interface-constraints/" class="btn btn-neutral float-right" title="Interface definition and data type">Next <span class="icon icon-circle-arrow-right"></span></a> </div> <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><a href="../jaxrs/" style="color: #fcfcfc">&laquo; Previous</a></span> <span><a href="../interface-constraints/" style="color: #fcfcfc">Next &raquo;</a></span> </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>