我一共搭建了3个环境。
整体入口:
https://blog.csdn.net/lwb314/article/details/108222433
这里先说几点我觉得需要注意得地方,然后再贴代码
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.8</version>
</dependency>
这里我用的dubbo-spring-boot-starter是是2.7.6,这个时候最新的是2.7.8,但是因为2.7.8依赖的spring boot版本是2.3,而我的nacos最高只支持2.2所以,只能把dubbo的版本降低一点
<?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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.lwb</groupId>
<artifactId>dubbo-starter-provider-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo-starter-provider-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
启动类
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableDubbo
@SpringBootApplication
public class DubboStarterProviderDemoApplication {
public static void main(String[] args) {
SpringApplication.run(DubboStarterProviderDemoApplication.class, args);
}
}
服务类
import com.lwb.DubboDemoService;
import org.apache.dubbo.config.annotation.Service;
@Service(version = "1.0.1",group = "lwb")
public class DubboServiceImpl implements DubboDemoService {
@Override
public String helloDubbo() {
return "helloDubboStarter";
}
}
服务接口
public interface DubboDemoService {
public String helloDubbo();
}
application.properties配置文件
server.port=8003
dubbo.application.name=dubbostarterprovider
# 禁用QOS同一台机器可能会有端口冲突现象
dubbo.application.qos-enable=false
dubbo.application.qos-accept-foreign-ip=false
# 配置注册中心
dubbo.registry.address=nacos://10.10.171.249:8848
dubbo.protocol.name=dubbo
dubbo.protocol.port=20883
@EnableDubbo
@SpringBootApplication
public class DubboStarterConsumerDemoApplication {
@Reference(version = "1.0.1", check = false, group = "lwb")
private DubboDemoService demoService;
@PostConstruct
public void init() {
String a = demoService.helloDubbo();
System.out.println(a);
}
public static void main(String[] args) {
SpringApplication.run(DubboStarterConsumerDemoApplication.class, args);
}
}
application.properties配置文件
server.port=8004
# 禁用QOS同一台机器可能会有端口冲突现象
dubbo.application.name=dubbo-starter-consumer
dubbo.application.qos-enable=false
dubbo.application.qos-accept-foreign-ip=false
# 配置注册中心
dubbo.registry.address = nacos://10.10.171.249:8848
dubbo.registry.register=false
这个例子又遇到了一些坑,也有一些方便的地方