Simple steps to build your microservices using the latest version of DUBBO

NACOS Registry

  • Download the latest version of NacOS from Github
  • Upload the file to the server and decompress it
  • Single machine startsh startup.sh -m standalone
  • Nacos console access addresshttp://192.168.136.129:8848/nacosLogin and access using the account nacos/nacos

The project framework

This case consists of three components

  • Public interface layer dubo-API
  • Producer dubbo – the provider
  • Consumers dubbo – consumer

    The code directory is as follows:

The parent directory

The parent directory mainly defines common component dependencies, version numbers, and POM files as follows

The < 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 > < packaging > pom < / packaging > < modules > < module > dubbo - API < module > <module>dubbo-provider</module> <module>dubbo-consumer</module> </modules> <groupId>com.jianzh5</groupId> < artifactId > dubbo - demo < / artifactId > < version > 1.0 - the SNAPSHOT < / version > < properties > < Java version > 1.8 < / Java version > < netty - all version > 4.0.35. Final < / netty - all version > < spring - the boot. Version > 2.1.9. RELEASE < / spring - the boot. Version > < dubbo version > 2.7.3 < / dubbo version > < nacos - client. Version > 1.1.4 < / nacos - client. Version > < / properties > <dependencyManagement> <dependencies> <! --SpringBoot--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <! -- Apache Dubbo --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-dependencies-bom</artifactId> <version>${dubbo.version}</version> <type>pom</type> <scope>import</scope> </dependency> <! -- Dubbo Registry Nacos --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-registry-nacos</artifactId> <version>${dubbo.version}</version> </dependency> <dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> <version>${nacos-client.version}</version> </dependency> <! -- Dubbo Spring Boot Starter --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>${dubbo.version}</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>${dubbo.version}</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> <exclusion> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> </exclusion> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency> <! Jianzh5 </groupId> <artifactId> dubo-api </artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>${spring-boot.version}</version> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>Copy the code

Directly rely on interfaces in POM files so that both producers and consumers can use them

Interface layer Dubo-API definition

  • pom
The < 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" > < the parent > < artifactId > dubbo - demo < / artifactId > < groupId > com. Jianzh5 < / groupId > < version > 1.0 - the SNAPSHOT < / version > < / parent > The < modelVersion > 4.0.0 < / modelVersion > < groupId > com. Jianzh5 < / groupId > < artifactId > dubbo - API < / artifactId > <packaging>jar</packaging> </project>Copy the code

  • Define the actual interface
/**
 * @author jianzh5
 * @date 2019/11/5 10:45
 */
public interface HelloService {
    String sayHello();
}Copy the code

Producer dubbo – the provider

  • pom
The < 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" > < the parent > < artifactId > dubbo - demo < / artifactId > < groupId > com. Jianzh5 < / groupId > < version > 1.0 - the SNAPSHOT < / version > < / parent > < modelVersion > 4.0.0 < / modelVersion > < groupId > com. Jianzh5 < / groupId > < artifactId > dubbo - provider < / artifactId > < dependencies >  <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <! -- Dubbo --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> </dependency> <! -- Dubbo Registry Nacos --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-registry-nacos</artifactId> </dependency> <dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> </dependency> <dependency> <groupId>com.jianzh5</groupId> <artifactId>dubbo-api</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>Copy the code

  • The configuration fileapplication.xml
Dubbo. Application. Name = dubbo - provider dubbo, registry. Address = nacos: / / 192.168.136.129:8848 dubbo.scan.base-packages=com.jianzh5.provider.service.impl dubbo.protocol.port=20881 dubbo.protocol.name=dubboCopy the code

  • Interface implementation
@service public class HelloServiceImpl implements HelloService {public String sayHello() {return "mailServiceImpl "; }}Copy the code

Note the @ service quoted here is org. Apache. Dubbo. Config. The annotation. Service, don’t quote wrong

  • Start the class
@SpringBootApplication @EnableDubbo public class PrivoderBootstrap { public static void main(String[] args) { SpringApplication.run(PrivoderBootstrap.class, args); }}Copy the code

The @enabledubbo annotation needs to be introduced

Consumers dubbo – consumer

  • pom
The < 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" > < the parent > < artifactId > dubbo - demo < / artifactId > < groupId > com. Jianzh5 < / groupId > < version > 1.0 - the SNAPSHOT < / version > < / parent > The < modelVersion > 4.0.0 < / modelVersion > < groupId > com. Jianzh5 < / groupId > < artifactId > dubbo - consumer < / artifactId > < dependencies >  <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <! -- Dubbo --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> </dependency> <! -- Dubbo Registry Nacos --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-registry-nacos</artifactId> </dependency> <dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> </dependency> <dependency> <groupId>com.jianzh5</groupId> <artifactId>dubbo-api</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>Copy the code

  • Configuration file application.properties
Spring. The application. The name = dubbo - consumer dubbo, registry. Address = nacos: / / 192.168.136.129:8848 server port = 9090Copy the code

  • consumersHelloController
/** * @author jianzh5 * @date 2019/11/5 11:29 */ @RestController public class HelloController { @Reference private HelloService helloService; @GetMapping("/sayHello") public String sayHello(){ return helloService.sayHello(); }}Copy the code

Inject HelloService using the @Reference annotation

  • Interface startup class
@SpringBootApplication public class ConsumerBootstrap { public static void main(String[] args) { SpringApplication.run(ConsumerBootstrap.class, args); }}Copy the code

Project start

  • Start the producer Dubo-provider
  • Start dubbo-consumer
  • Look at the NACOS console to see if you are registered

  • Accessing the browserhttp://localhost:9090/sayHelloInterface Return

For more content, please pay attention to the public number: JAVA Daily Records