Spring Boot 2.x base case: the integration of Dubbo 2.7.3 + Nacos1.1.3

Mind mapping of this article

1 Overview

This article describes how based on Spring Boot 2.x version, by Nacos as configuration and registration center, implementation of the registration services and consumer Dubbo.

Integrated component release notes:

  • Spring Boot 2.1.9
  • Dubbo 2.7.3
  • Nacos 1.1.3

The article highlights:

  • 1. Using yml way dubbo configuration.
  • 2. the integration of related components using a newer version.
  • 3. Related source placed on Github, can always see.

Source code is placed Github:  https://github.com/raysonfang/spring-boot-demo-all


Before the company when using Dubbo 2.6.1, using Zookeeper as a registration center. At that time, only just take it and use as a registration center, a management background is no specific visualization management operations, and second, single function, use only as a registered center.

After learning and understanding through a period of time, found that the use of open source Nacos Ali as a registration center and external distribution center. Zookeeper it is more than suitable for registration and configuration services, after all, is a maker of open source, after a lot of practice.

If you do not know what Nacos is or what has the primary function of architecture and design. Take the time to check their own data.

nacos:

Note:此次主要实践Nacos作为注册中心,后面会单独整合Nacos作为配置中心的实践分享。


2, base frame structures

Use idea + maven to build a multi-module project

spring-boot-dubbo-nacos- demo: Parent Project

Service-Provider-Shop:  Dubbo service provider

Consumer-Service-Shop:  Dubbo service consumers, is a web project


3, pom.xml description

: the Spring-the Boot-Dubbo-nacos-Demo pom.xml 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> 

-Service-Provider-Shop:  the pom.xml dubbo jar introducing the related nacos

<?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>服务者 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> <!-- 排除自带的logback依赖 --> <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> <!-- 排除自带的logback依赖 --> <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、配置文件说明

网上大部分资料都是基于application.properties配置,或者是基于xml配置dubbo的相关参数。而实际上,在Spring Boot工程中,
shop-service-provider:application.yml配置文件说明


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说明

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相关参数:也可以在DubboConfigurationProperties类中查看


5、编写业务代码

shop-service-provider:代码实现

针对dubbo服务提供者,我没有单独把RpcShopService接口单独放到一个子模块提供,建议在引用到实际项目中,可以单独提供接口包,在消费端直接引用接口包,这样就可以脱离服务提供者。

ShopServiceProviderApplication.java启动主类

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 服务提供方 * @author raysonfang * @公众号 Java技术干货(ID:raysonfang) */ @SpringBootApplication @EnableDubbo public class ShopServiceProviderApplication { //使用jar方式打包的启动方式 private static CountDownLatch countDownLatch = new CountDownLatch(1); public static void main(String[] args) throws InterruptedException{ SpringApplication.run(ShopServiceProviderApplication.class, args).registerShutdownHook(); countDownLatch.await(); } }

需要注意@EnableDubbo这个注解,是开启Dubbo服务必要的。


RpcShopService.java:暴露接口,供消费端使用

package cn.raysonblog.shopserviceprovider.service;

/**
 * 提供暴露的Rpc接口
 * @author raysonfang
 */
public interface RpcShopService { String sayHello(String name); } 

ShopServiceImpl.java: 实现类

package cn.raysonblog.shopserviceprovider.service.impl;

import cn.raysonblog.shopserviceprovider.service.RpcShopService;
import org.apache.dubbo.config.annotation.Service;

/**
 * 接口实现类
 *
 * ## @Service 这个注解是使用dubbo提供的, * 这个注解中有很多属性,需要单独了解去进行配置 * * @author raysonfang */ @Service public class ShopServiceImpl implements RpcShopService { public String sayHello(String name) { return name; } } 

shop-service-consumer: 代码实现
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; /** * * 把主类和controller写在一起,方便简单测试演示。 * * @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!更多原创分享,技术交流,关注:Java技术干货(ID:raysonfang)"); } public static void main(String[] args) { SpringApplication.run(ShopServiceConsumerApplication.class, args); } } 

6、测试

测试的时候,启动顺序
Boot sequence

6.1、启动nacos-server注册中心

下载nacos-server:https://github.com/alibaba/nacos/releases

nacos-server download

解压nacos-server, 找到bin目录

windows点击startup.cmd, 启动nacos
nacos-server

在浏览器中输入:http://localhost:8848/nacos/index.html, 便可以访问到nacos的控制台。用户名和密码默认为nacos

6.2、启动shop-service-provider服务提供者

在nacos控制台可以看到信息:

6.3, start the shop-service-consumer service consumers

In nacos console can see the following information:

In the browser input: HTTP: // localhost: 8081 / sayHello , it will return results.


7, record and solve problems

7.1, the integration time, pom introduced dubbo and nacos dependent packages, it takes time to find many. Mainly the package introduces unsuccessful.

Solution: go to the local maven dependencies, delete the introduction of an unsuccessful dependencies, re reimport.

7.2, when open dubbo, the notes quote is not correct: error injection @EnableDubboConfig.

Solution: Use replaced @EnableDubbo.

7.3, yml configuration Dubbo related attributes, online information is quite thin.

Solution: by looking at the DubboConfigurationProperties.javasource code, to analyze the properties of the configuration.

Guess you like

Origin www.cnblogs.com/tiandanMp/p/11647668.html