在Spring Boot中集成Dubbo可以通过Spring Boot Starter来简化配置,以下是详细的步骤和相关代码示例。
首先,在Spring Boot项目的 pom.xml
中添加Dubbo相关的依赖:
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>2.7.8version>
dependency>
<dependency>
<groupId>com.alibaba.bootgroupId>
<artifactId>nacos-config-spring-boot-starterartifactId>
<version>0.2.10version>
dependency>
<dependency>
<groupId>com.alibaba.bootgroupId>
<artifactId>nacos-discovery-spring-boot-starterartifactId>
<version>0.2.10version>
dependency>
dependencies>
在 application.yml
中配置Dubbo和Nacos:
spring:
application:
name: dubbo-demo-provider
# 配置Nacos作为注册中心和配置中心
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
config:
server-addr: 127.0.0.1:8848
file-extension: yaml
dubbo:
application:
name: dubbo-demo-provider
registry:
address: nacos://127.0.0.1:8848
protocol:
name: dubbo
port: 20880
scan:
base-packages: com.example.dubbo.provider
定义一个服务接口,例如:
package com.example.dubbo;
public interface MyService {
String sayHello(String name);
}
在Spring Boot项目中实现服务提供者,并使用 @DubboService
注解:
package com.example.dubbo.provider;
import com.example.dubbo.MyService;
import org.apache.dubbo.config.annotation.DubboService;
@DubboService
public class MyServiceImpl implements MyService {
@Override
public String sayHello(String name) {
return "Hello, " + name;
}
}
创建Spring Boot的启动类:
package com.example.dubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DubboProviderApplication.class, args);
}
}
在另一个Spring Boot项目中配置Dubbo和Nacos,并编写服务消费者代码。
服务消费者 pom.xml
:
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>2.7.8version>
dependency>
<dependency>
<groupId>com.alibaba.bootgroupId>
<artifactId>nacos-config-spring-boot-starterartifactId>
<version>0.2.10version>
dependency>
<dependency>
<groupId>com.alibaba.bootgroupId>
<artifactId>nacos-discovery-spring-boot-starterartifactId>
<version>0.2.10version>
dependency>
dependencies>
服务消费者 application.yml
:
spring:
application:
name: dubbo-demo-consumer
# 配置Nacos作为注册中心和配置中心
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
config:
server-addr: 127.0.0.1:8848
file-extension: yaml
dubbo:
application:
name: dubbo-demo-consumer
registry:
address: nacos://127.0.0.1:8848
scan:
base-packages: com.example.dubbo.consumer
服务消费者代码:
package com.example.dubbo.consumer;
import com.example.dubbo.MyService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class MyServiceConsumer implements CommandLineRunner {
@DubboReference
private MyService myService;
@Override
public void run(String... args) throws Exception {
System.out.println(myService.sayHello("Dubbo"));
}
}
服务消费者启动类:
package com.example.dubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(DubboConsumerApplication.class, args);
}
}
确保Nacos配置中心和注册中心在本地或远程服务器上运行。可以从Nacos官网下载并启动Nacos。
DubboProviderApplication
类,确保服务成功注册到Nacos。DubboConsumerApplication
类,调用服务并检查结果。通过以上步骤,我们成功地在Spring Boot中集成了Dubbo,并实现了服务提供者和消费者的示例。关键步骤包括:
pom.xml
中添加Dubbo相关依赖。application.yml
中配置Dubbo和Nacos。@DubboService
注解。通过这些步骤,可以有效地在Spring Boot中集成Dubbo,实现分布式服务的开发和调用。