1. Overview
This article will introduce how to register and consume Dubbo services by using Nacos as configuration and registration center based on Spring Boot 2.x.
Version description of integration components:
- Spring Boot 2.1.9
- Dubbo 2.7.3
- Nacos 1.1.3
The highlights of this paper are as follows:
- 1. dubbo is configured by yml mode.
- 2. Relevant components are integrated with newer versions.
- 3. The relevant source code is placed on Github and can be viewed at any time.
The source code is placed in Github: https://github.com/raysonfang/spring-boot-demo-all
Previously, when using Dubbo 2.6.1, the company used Zookeeper as its registry. At that time, it was only used as a registry, one without a special management background for visual management operations, and the other with a single function, only used as a registry.
After a period of study and understanding, we found that Ali Open Source Nacos was used as the registry and external configuration center. It is more suitable for service registration and configuration than Zookeeper. After all, it is an open source factory with a lot of practice.
If it's not clear what Nacos is, or what major functions Nacos has, and how it's architecturally designed. Take some time to check the information.
Nacos:
Note: The main practice of this time is Nacos as a registry, which will be separately integrated with Nacos as a configuration center for practice sharing.
2. Foundation Framework Construction
Using idea+maven multi-module to build the project
spring-boot-dubbo-nacos-demo: parent project
shop-service-provider: dubbo service provider
shop-service-consumer: dubbo service consumer is a web project
3. Description of pom.xml
spring-boot-dubbo-nacos-demo: pom.xml for the parent project
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.raysonblog</groupId> <artifactId>misco-dubbo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>misco-dubbo</name> <packaging>pom</packaging> <description>Demo project for Spring Boot Dubbo Nacos</description> <modules> <module>shop-service-provider</module> <module>shop-service-consumer</module> </modules> <properties> <java.version>1.8</java.version> <spring-boot.version>2.1.9.RELEASE</spring-boot.version> <dubbo.version>2.7.3</dubbo.version> </properties> <dependencyManagement> <dependencies> <!-- Spring Boot --> <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 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> </dependencies> </dependencyManagement> <repositories> <repository> <id>apache.snapshots.https</id> <name>Apache Development Snapshot Repository</name> <url>https://repository.apache.org/content/repositories/snapshots</url> <releases> <enabled>false</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
shop-service-provider: pom.xml introduces dubbo and nacos-related jar s
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>cn.raysonblog</groupId> <artifactId>misco-dubbo</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> <!-- lookup parent from repository --> </parent> <groupId>cn.raysonblog</groupId> <artifactId>shop-service-provider</artifactId> <version>0.0.1-SNAPSHOT</version> <name>shop-service-provider</name> <description>Server Demo project for Spring Boot dubbo nacos</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <!-- Exclude self-contained logback rely on --> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <!--<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </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> <version>2.7.3</version> </dependency> <dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> </dependency> </dependencies> <repositories> <repository> <id>apache.snapshots.https</id> <name>Apache Development Snapshot Repository</name> <url>https://repository.apache.org/content/repositories/snapshots</url> <releases> <enabled>false</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
shop-service-consumer: pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>cn.raysonblog</groupId> <artifactId>misco-dubbo</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> <!-- lookup parent from repository --> </parent> <groupId>cn.raysonblog</groupId> <artifactId>shop-service-consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <name>shop-service-consumer</name> <description>Demo project for Spring Boot dubbo nacos</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <!-- Exclude self-contained logback rely on --> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <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> <version>2.7.3</version> </dependency> <dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-spring-context</artifactId> </dependency> <dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> </dependency> <dependency> <groupId>cn.raysonblog</groupId> <artifactId>shop-service-provider</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> <repositories> <repository> <id>apache.snapshots.https</id> <name>Apache Development Snapshot Repository</name> <url>https://repository.apache.org/content/repositories/snapshots</url> <releases> <enabled>false</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
4. Configuration file description
Most of the information on the Internet is based on the application.properties configuration, or the parameters of dubbo configuration based on xml. In fact, in the Spring Book project,
shop-service-provider: application.yml configuration file description
spring: application: name: shop-service-provider # log config logging: config: classpath:log4j2.xml level: root: info web: info file: logs/shop-service-provider.log # Dubbo Application nacos ## The default value of dubbo.application.name is ${spring.application.name} ## dubbo.application.name=${spring.application.name} nacos: service-address: 127.0.0.1 port: 8848 dubbo: registry: address: nacos://${nacos.service-address}:${nacos.port} protocol: name: dubbo port: 20881 scan: base-packages: cn.raysonblog.*.service.impl
shop-service-consumer: application.yml description
server: address: port: 8081 servlet: context-path: / tomcat: uri-encoding: UTF-8 spring: application: name: shop-service-consumer # log config logging: config: classpath:log4j2.xml level: root: info web: info file: logs/shop-service-provider.log # Dubbo Application nacos ## The default value of dubbo.application.name is ${spring.application.name} ## dubbo.application.name=${spring.application.name} nacos: service-address: 127.0.0.1 port: 8848 dubbo: registry: address: nacos://${nacos.service-address}:${nacos.port}
dubbo related parameters: You can also view them in the DubboConfiguration Properties class
5. Writing business code
shop-service-provider: code implementation
For dubbo service providers, I did not separate the RpcShopService interface into a sub-module to provide. It is suggested that the interface package can be provided separately when referring to the actual project, and directly refer to the interface package at the consumer side, so that the service provider can be separated.
ShopServiceProviderApplication.java starts the main class
package cn.raysonblog.shopserviceprovider; import org.apache.dubbo.config.spring.context.annotation.DubboConfigConfiguration; import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import java.util.concurrent.CountDownLatch; /** * dubbo service provider * @author raysonfang * @Public Number Java Technology Dry Goods (ID:raysonfang) */ @SpringBootApplication @EnableDubbo public class ShopServiceProviderApplication { //Startup mode of packaging in jar mode private static CountDownLatch countDownLatch = new CountDownLatch(1); public static void main(String[] args) throws InterruptedException{ SpringApplication.run(ShopServiceProviderApplication.class, args).registerShutdownHook(); countDownLatch.await(); } }
Note that the annotation @EnableDubbo is necessary to open the Dubbo service.
RpcShopService.java: Exposure interface for consumer use
package cn.raysonblog.shopserviceprovider.service; /** * Provide exposed Rpc interface * @author raysonfang */ public interface RpcShopService { String sayHello(String name); }
ShopServiceImpl.java: Implementation class
package cn.raysonblog.shopserviceprovider.service.impl; import cn.raysonblog.shopserviceprovider.service.RpcShopService; import org.apache.dubbo.config.annotation.Service; /** * Interface Implementation Class * * ## @Service This annotation is provided using dubbo. * There are many attributes in this annotation that need to be understood separately for configuration. * * @author raysonfang */ @Service public class ShopServiceImpl implements RpcShopService { public String sayHello(String name) { return name; } }
shop-service-consumer: Code implementation
package cn.raysonblog.shopserviceconsumer; import cn.raysonblog.shopserviceprovider.service.RpcShopService; import org.apache.dubbo.config.annotation.Reference; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; /** * * Write the main class and controller together for easy test demonstration. * * @author raysonfang */ @SpringBootApplication @RestController public class ShopServiceConsumerApplication { @Reference RpcShopService shopService; @RequestMapping(name = "/sayHello", method = RequestMethod.GET) public String sayHello(){ return shopService.sayHello("Hello Dubbo Nacos!More original sharing, technology exchange, attention: Java Technical Dry Goods( ID:raysonfang)"); } public static void main(String[] args) { SpringApplication.run(ShopServiceConsumerApplication.class, args); } }
6. Testing
When testing, start sequence
6.1. Start the nacos-server registry
Download nacos-server: https://github.com/alibaba/nacos/releases
Unzip nacos-server and find bin directory
windows clicks startup.cmd to start nacos
Enter http://localhost:8848/nacos/index.html in the browser to access the Nacos console. User name and password default to Nacos
6.2. Start shop-service-provider service provider
You can see the information in the nacos console:
6.3. Start shop-service-consumer service consumers
The following information can be seen in the nacos console:
On the browser side, enter: http://localhost:8081/sayHello The result is returned.
7. Problem Recording and Solution
7.1. When integrating, it takes a lot of time for pom to introduce dubbo and nacos-related dependency packages. Mainly the package introduction was unsuccessful.
Solution: Remove the local dependency library from maven, delete the unsuccessful dependency package and re-reimport it.
7.2. When dubbo was started, the comment reference was incorrect: Error injection @EnableDubboConfig.
Solution: Use @EnableDubbo instead.
7.3. yml configures the relevant attributes of Dubbo, and there is little information on the Internet.
Solution: Analyse property configuration by looking at DubboConfiguration Properties. java source code.
8. Postscript
Due to limited ability, if there are mistakes or inappropriate, please also criticize and correct, learn and communicate together!
The source code is placed in Github: https://github.com/raysonfang/spring-boot-demo-all
Welcome to star, criticism
I usually study and code github, welcome to continue to pay attention to communication.
My github: https://github.com/raysonfang