Writing in the front

As a PHP programmer switching to Java development, I have been stuck at the level of Hello World. Recently, DUE to a fever, I wanted to try the new version of Dubbo, but GIthub looked for it again and again, but there was no demo, and the example given by the official government cannot be copied and run separately. It’s not one or the other. I’ve been working on it all afternoon.

So I decided to try and fill the search engine gap.

The development environment

  • Spring the Boot: 2.1.3. RELEASE
  • Dubbo: 2.7.1 – the SNAPSHOT
  • Development tool: Mac IDEA

In actual combat

Show me the code!

Create a project

Now start to create a Spring Boot Maven project with no code in IDEA

Select Spring Initializr to initialize the project

Create a module

Create the API module, the interface definition

Create the Service module, that is, the Provider service provider

Create a Web module, the Consumer service consumer

After creating the module, try MVN Clean Install. The result is an error

[INFO] skip non existing resourceDirectory /Users/jeftom/workspace/java/dubbo-sample/dubbo-demo/src/main/resources [INFO] [INFO] -- maven-compiler-plugin:3.8.0:compile (default-compile) @dubo-demo -- [INFO] No sources to compile [the INFO] [INFO] maven - resources - the plugin: 3.1.0:testResources (default-testResources) @ dubbo-demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/jeftom/workspace/java/dubbo-sample/dubbo-demo/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ dubbo-demo --- [INFO] No sources to compile [INFO] [INFO] --- Maven - surefire plugin: 2.22.1:test(default-test) @dubo-demo -- [INFO] No tests to run. [INFO] [INFO] -- Plugin :3.1.1:jar (default-jar) @ dubbo-demo --- [WARNING] JAR will be empty - no content was markedforinclusion! [INFO] Building jar: / Users/jeftom/workspace/Java/dubbo - sample/dubbo - demo/target/dubbo - demo - 0.0.1 - the SNAPSHOT. Jar [INFO] - [INFO] Spring - the boot - maven - plugin: 2.1.3. RELEASE: repackage (repackage) @ dubbo - demo - [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- [INFO] Total time: 3.142 s [INFO] Finished at: 2019-03-23T22:05:17+08:00 [INFO] Final Memory: 24M/395M [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal Org. Springframework. The boot: spring - the boot - maven - plugin: 2.1.3. RELEASE: repackage (repackage) on project dubbo - demo: Execution repackage of goal org. Springframework. The boot: spring - the boot - maven - plugin: 2.1.3. RELEASE: repackage failed: Unable to find main class -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the-e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException
Copy the code

Go to the Tests unit test case and comment @ignore to skip ahead and forget the rest.

Main Module Configuration

Add to pom.xml in the root module

<packaging>pom</packaging>

<modules>
    <module>dubbo-demo-api</module>
    <module>dubbo-demo-service</module>
    <module>dubbo-demo-web</module>
</modules>
Copy the code

The packaged plug-in also needs to be adjusted, because the root template does not use spring Boot’s main startup class, and will continue with maven’s packaged plug-in.

<build> <plugins> <plugin> <artifactId> Maven-clean-plugin </artifactId> <version>3.1.0</version> </plugin> <plugin> <artifactId> Maven-site-plugin </artifactId> <version>3.7.1</version> </plugin> < artifactId > maven project - info - reports - the plugin < / artifactId > < version > 3.0.0 < / version > < / plugin > < / plugins > </pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-site-plugin</artifactId> <configuration> <locales>en,fr</locales> </configuration> </plugin> </plugins> <! Maven-site-plugin = maven-site-plugin = maven-site-plugin = maven-site-plugin = maven-site-plugin -- <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> --> </build>Copy the code

Pom.xml is added in the other three submodules

<packaging>jar</packaging>
Copy the code

Once the changes are complete, run MVN Clean Install to try them out

[INFO] ------------------------------------------------------------------------ [INFO] Reactor Summary: [INFO] [INFO] dubbo-demo-api ..................................... SUCCESS [s] 5.428 [INFO] dubbo - demo - service... SUCCESS [s] 1.299 [INFO] dubbo - demo - web... SUCCESS [s] 3.195 [INFO] dubbo - demo... SUCCESS [s] 0.039 [INFO] -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 11.258s [INFO] Finished at: 2019-03-23T22:21:46+08:00 [INFO] Final Memory: 50M/525M [INFO] ------------------------------------------------------------------------Copy the code

One step closer to success, there is no code yet, but at least a few modules have been built.

Dubbo – demo – API module

Remove the startup class in dubo-demo-API, because this module mainly holds the definition of the interface.

Add the DemoApi interface class

package com.example.dubbo.demo.api; ** @author jefTom * @date 2019-03-23 22:35 * @since 1.0.0 */ public interface DemoApi {String sayHello(String name); }Copy the code

Pom file packaging plug-in changed to:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
        </plugin>
    </plugins>
</build>
Copy the code

Dubbo – demo – service module

The main function of this module is the realization of the interface, the service provider of business functions.

Let’s start with the module’s POM.xml configuration:

<? xml version="1.0" encoding="UTF-8"? > <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> < modelVersion > 4.0.0 < / modelVersion > < the parent > < groupId > org. Springframework. Boot < / groupId > The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 2.1.3. RELEASE < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <groupId>com.example.dubbo</groupId> < artifactId > dubbo - demo - service < / artifactId > < version > 0.0.1 - the SNAPSHOT < / version > < packaging > jar < / packaging > <name>dubbo-demo-service</name> <description>Demo projectforSpring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <! Dubbo Provider fails to start because spring-boot-starter-web dependency is not added. Has been missing in the startup log shows "O.S.B.W.E mbedded. Tomcat. TomcatWebServer: tomcat initialized with the port (s) : <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <! -- This is the defined interface package, --> <dependency> <groupId>com.example.dubbo</groupId> < < artifactId > dubbo - demo - API/artifactId > < version > 0.0.1 - the SNAPSHOT < / version > < / dependency > <! Dubbo </groupId> <artifactId>dubbo</artifactId> < version > 2.7.1 - the SNAPSHOT < / version > < exclusions > < exclusion > < artifactId > spring < / artifactId > <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency> <dependency> < the groupId > org. Apache. Zookeeper < / groupId > < artifactId > zookeeper < / artifactId > < version > 3.4.10 < / version > < exclusions > <exclusion> <artifactId>slf4j-log4j12</artifactId> <groupId>org.slf4j</groupId> </exclusion> </exclusions> </dependency> < the dependency > < groupId > com. Making. Sgroschupf < / groupId > < artifactId > zkclient < / artifactId > < version > 0.1 < / version > </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>4.0.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>Copy the code

Because the web dependency was not checked when creating the module, the POM only contains spring-boot-starter, but does not contain spring-boot-starter web dependency. When starting the service, it is always finished and then exit. MVN install is normal, too. It took me a few hours. After the spring-boot-starter-web dependency is added, the Tomcat logs are displayed in the startup logs

o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8087 (http)
Copy the code

Now let’s create a DemoApiImpl class that responds to the sayHello method:

package com.example.dubbo.demo.service.impl; import com.example.dubbo.demo.api.DemoApi; import org.apache.dubbo.config.annotation.Service; /** * demo implementation ** @author jefTom * @date 2019-03-23 23:04 * @since 1.0.0 */ @service public class DemoApiImpl Implements DemoApi {/** * implements sayHello ** @param * @return
    */
   @Override
   public String sayHello(String name) {
      return "Hello, " + name + "(from Spring Boot with Dubo-2.7.1)"; }}Copy the code

DubboDemoServiceApplication start class need to increase the dubbo configuration comments:

package com.example.dubbo.demo.service; import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan; import org.apache.dubbo.config.spring.context.annotation.EnableDubboConfig; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * added EnableDubboConfig and DubboComponentScan annotation, automatically scanned at startup ** / @enableDubboConfig @dubboComponentScan ("com.example.dubbo.demo.service.impl") @SpringBootApplication public class DubboDemoServiceApplication { public static void main(String[] args) { SpringApplication.run(DubboDemoServiceApplication.class, args); }}Copy the code

Add a Dubbo profile:

<? xml version="1.0" encoding="UTF-8"? > <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation=" http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <! -- Defines provider application information for calculating dependencies; This name is displayed in dubbo-admin or dubbo-monitor --> <dubbo: Application name="${dubbo.application.name}" owner="jeftom" organization="jeftom"/ > <! <dubbo:registry id="zookeeper-registry" protocol="${dubbo.registry.protocol}" address="${dubbo.registry.address}"/ > <! -- dubbo exposes service on port 20880 --> <dubbo:protocol name="${dubbo.protocol.name}" port="${dubbo.protocol.port}" accesslog="dubbo-access.log"/>
    <dubbo:provider retries="0" timeout="30000"/>
    <dubbo:monitor protocol="registry"/ > <! <dubbo: Service interface="com.example.dubbo.demo.api.DemoApi" ref="DemoApiImpl" retries="0" timeout="60000" />
</beans>
Copy the code

The application. Properties configuration file for the Spring project:

spring.config.name=application

# Spring environment configuration
spring.profiles.active=dev
Service startup port, which is used when the built-in Tomcat is started
server.port=8087

#dubbo config
Application defines the provider application information, which is used to calculate dependencies; This name is displayed in dubbo-admin or dubbo-monitor for easy identification
dubbo.application.name=dubbo-demo-service
# App owner
dubbo.application.owner=jeftom
# Organization to which the application belongs
dubbo.application.organization=jeftom

Use the ZooKeeper registry to expose the service before enabling ZooKeeper
# registry id
dubbo.registry.id=zookeeper-registry
# Registry protocol
dubbo.registry.protocol=zookeeper
Registry address
dubbo.registry.address=zookeeper.tencus.com:2181

# Dubbo protocol exposes service on port 20880
Protocol name
dubbo.protocol.name=dubbo
Protocol port
dubbo.protocol.port=20880
Protocol access log
dubbo.protocol.accesslog=dubbo-access.log
# retries
dubbo.provider.retries=0
# timeout
dubbo.provider.timeout=3000
# Register monitoring center
dubbo.monitor.protocol=registry
Copy the code

At this point, our service provider can start normally, and you can see that the service has been registered with ZooKeeper through dubbo-admin.

Dubbo – demo – web module

This module is dubbo’s Consumer service, which is used to consume the service provided by the provider. After the service is started, it will connect with the provider to complete the service invocation.

Pom.xml configuration file:

<? xml version="1.0" encoding="UTF-8"? > <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> < modelVersion > 4.0.0 < / modelVersion > < the parent > < groupId > org. Springframework. Boot < / groupId > The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 2.1.3. RELEASE < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <groupId>com.example.dubbo</groupId> < artifactId > dubbo - demo - web < / artifactId > < version > 0.0.1 - the SNAPSHOT < / version > < packaging > jar < / packaging > <name>dubbo-demo-web</name> <description>Demo projectforSpring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <! Dubbo Provider fails to start because spring-boot-starter-web dependency is not added. Has been missing in the startup log shows "O.S.B.W.E mbedded. Tomcat. TomcatWebServer: tomcat initialized with the port (s) : <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <! -- This is the defined interface package, --> <dependency> <groupId>com.example.dubbo</groupId> < < artifactId > dubbo - demo - API/artifactId > < version > 0.0.1 - the SNAPSHOT < / version > < / dependency > <! Dubbo </groupId> <artifactId>dubbo</artifactId> < version > 2.7.1 - the SNAPSHOT < / version > < exclusions > < exclusion > < artifactId > spring < / artifactId > <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency> <dependency> < the groupId > org. Apache. Zookeeper < / groupId > < artifactId > zookeeper < / artifactId > < version > 3.4.10 < / version > < exclusions > <exclusion> <artifactId>slf4j-log4j12</artifactId> <groupId>org.slf4j</groupId> </exclusion> </exclusions> </dependency> < the dependency > < groupId > com. Making. Sgroschupf < / groupId > < artifactId > zkclient < / artifactId > < version > 0.1 < / version > </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>4.0.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>Copy the code

Application. Properties configuration for Spring applications:

spring.config.name=application

# Spring environment configuration
spring.profiles.active=dev
Service startup port, which is used when the built-in Tomcat is started
server.port=8088

# Qos operation and maintenance monitoring
dubbo.application.qosEnable=true
dubbo.application.qosPort=33333
dubbo.application.qosAcceptForeignIp=false

# dubbo config
Application defines the provider application information, which is used to calculate dependencies; This name is displayed in dubbo-admin or dubbo-monitor for easy identification
dubbo.application.name=dubbo-demo-service
# App owner
dubbo.application.owner=jeftom
# Organization to which the application belongs
dubbo.application.organization=jeftom

Use the ZooKeeper registry to expose the service before enabling ZooKeeper
# registry id
dubbo.registry.id=zookeeper-registry
# Registry protocol
dubbo.registry.protocol=zookeeper
Registry address
dubbo.registry.address=zookeeper.tencus.com:2181

# Dubbo protocol exposes service on port 20880
Protocol name
dubbo.protocol.name=dubbo
Protocol port
dubbo.protocol.port=20880
Protocol access log
dubbo.protocol.accesslog=dubbo-access.log
# retries
dubbo.provider.retries=0
# timeout
dubbo.provider.timeout=3000
# Register monitoring center
dubbo.monitor.protocol=registry
Copy the code

If two services are enabled on the same machine, Qos port conflicts may occur:

main [server.Server] 102 [ERROR]  [DUBBO] qos-server can not bind localhost:22222
Copy the code

Add the following two lines to the DubboDemoWebApplication consumer’s start class:

@EnableDubboConfig
@DubboComponentScan("com.example.dubbo.demo.web.service")
Copy the code

Create DemoService and DemoController respectively.

DemoService. Java classes

package com.example.dubbo.demo.web.service; import com.example.dubbo.demo.api.DemoApi; import org.apache.dubbo.config.annotation.Reference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; /** * @author jefTom * @date 2019-03-24 00:49 * @since 1.0.0 */ @service public class DemoService {private static final Logger LOGGER = LoggerFactory.getLogger(DemoService.class); @Reference private DemoApi demoApi; public String sayHello(String name) {returndemoApi.sayHello(name); }}Copy the code

Democontroller.java Controller class

package com.example.dubbo.demo.web.controller; import com.example.dubbo.demo.web.service.DemoService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; /** * @author jefTom * @date 2019-03-24 00:51 * @author jefTom * @date 2019-03-24 00:51 */ @restController @requestMapping ("/demo") public class DemoController { private static Logger logger = LoggerFactory.getLogger(DemoController.class); @Autowired private DemoService demoService; /** * test method, browser access /demo/index can see the response result ** @return
    */
   @RequestMapping(value = "/index", method = RequestMethod.GET)
   @ResponseBody
   public String index() {
      return demoService.sayHello("dubbo"); }}Copy the code

Now you can reimport and install MVN to see if there is any error. If there is no error, start the service and Web services, and then open the browser to access them.

conclusion

The above is a dubbo 2.7.1 version of the call service demo I spent two days debugging, if you are also going to use dubbo to try the new version of the service development, I hope this article is a little help to you, very grateful to ** Ali team ** open source out of such an excellent RPC solution.

  • Attached demo source download: github.com/jeftom/dubb…