注意,我们这边整合的时候分为消费者,生产者和中间服务接口层
<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.0modelVersion>
<groupId>com.flygroupId>
<artifactId>spring-boot-dubboartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>pompackaging>
<modules>
<module>spring-boot-dubbo-interfacemodule>
<module>spring-boot-dubbo-providermodule>
<module>spring-boot-dubbo-consumermodule>
modules>
<properties>
<java.version>1.8java.version>
<dubbo.version>3.0.7dubbo.version>
<spring-boot.version>2.6.7spring-boot.version>
<lombok.version>1.18.24lombok.version>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>${spring-boot.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>${dubbo.version}version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>${lombok.version}version>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-registry-nacosartifactId>
<version>${dubbo.version}version>
dependency>
dependencies>
dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
<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.0modelVersion>
<parent>
<groupId>com.flygroupId>
<artifactId>spring-boot-dubboartifactId>
<version>1.0-SNAPSHOTversion>
parent>
<artifactId>spring-boot-dubbo-providerartifactId>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<dependencies>
<dependency>
<groupId>com.flygroupId>
<artifactId>spring-boot-dubbo-interfaceartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-registry-nacosartifactId>
<exclusions>
<exclusion>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-commonartifactId>
exclusion>
<exclusion>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-remoting-apiartifactId>
exclusion>
<exclusion>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
dependencies>
project>
我这边是用到lombok才依赖的,如果不需要其他依赖就可以不用引入,总之这边按需引入
<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.0modelVersion>
<parent>
<groupId>com.flygroupId>
<artifactId>spring-boot-dubboartifactId>
<version>1.0-SNAPSHOTversion>
parent>
<artifactId>spring-boot-dubbo-interfaceartifactId>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<dependencies>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
dependencies>
project>
<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.0modelVersion>
<parent>
<groupId>com.flygroupId>
<artifactId>spring-boot-dubboartifactId>
<version>1.0-SNAPSHOTversion>
parent>
<artifactId>spring-boot-dubbo-consumerartifactId>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<dependencies>
<dependency>
<groupId>com.flygroupId>
<artifactId>spring-boot-dubbo-interfaceartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-registry-nacosartifactId>
<exclusions>
<exclusion>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-commonartifactId>
exclusion>
<exclusion>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-remoting-apiartifactId>
exclusion>
<exclusion>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
exclusion>
exclusions>
dependency>
dependencies>
project>
这边配置文件含义因为篇幅原因就不多做介绍,后面会出一期介绍的,这边大家自己也可以搜下
server:
port: 8001
spring:
application:
name: spring-boot-dubbo-provider
dubbo:
protocol:
name: dubbo
port: 20881
# 注册中心地址
registry:
address: nacos://127.0.0.1:8848
application:
qos-enable: false
consumer:
check: false
retries: 2
server:
port: 8002
spring:
application:
name: spring-boot-dubbo-consumer
dubbo:
protocol:
name: dubbo
port: 20881
# 注册中心地址
registry:
address: nacos://127.0.0.1:8848
application:
qos-enable: false
consumer:
check: false
这边我们以上步骤配置好后就可以写一下测试代码,检查以下是否可以运行,不过这边要注意的一点,在上面的步骤中我们还没有启动nacos,不过这边先写这,等下后面会有如何下载和启动的步骤
业务代码
package com.fly.service;
import org.apache.dubbo.config.annotation.DubboService;
@DubboService
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
return "hello:" + name ;
}
}
启动类
package com.fly;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
这边只是对生产者提供方法的抽象,不提供具体实现,具体实现在服务提供者(生产者)那边
package com.fly.service;
public interface DemoService {
String sayHello(String name);
}
package com.fly.controller;
import com.fly.service.DemoService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoController {
@DubboReference
private DemoService demoService;
@GetMapping("/test")
public String test(String param) {
return demoService.sayHello(param);
}
}
因为我们用的是nacos,所以现在启动肯定会失败,需要我们先把nacos启动然后将生产者和消费者注册到nacos上
下载地址:https://github.com/alibaba/nacos/releases
这边我们到下载页面后点这个tag可以看到有很多版本的nacos,这里我选的是2.1.2版本选好之后点zip下载就行了,如果是安装到linux上点.gz后缀那个就行了,我这边暂时是用win10系统作介绍
然后下载解压之后的文件是这样
我们这边会发现没有bin目录,也就是没有启动的地方,这边是需要我们先用maven打包,我们在ncaos当前目录下进入cmd然后输入打包命令,注意这边需要提前安装maven并设置其环境变量
打包命令:mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U
打包完成后会生成一个target目录,目录里有好的包,这边我已经打好了
点击后就可以启动了