整体思路:
+ 搭建本地nacos服务,详见docker安装nacos_xgjj68163的博客-CSDN博客
+ 共三个工程,生产者服务、消费者服务、生产者和消费者共同依赖的接口工程(打成jar,供生产者和消费者依赖);
+ 生产者注册服务到nacos,消费者调用nacos上的生产者服务;
目录
1. 共同依赖的接口服务搭建
1.1 pom
1.2 公共接口
1.3 maven install , 将可依赖jar包安装到本地仓库
2. 生产者服务搭建
2.1 生产者服务pom
2. 配置文件及注册服务
3. 消费者服务搭建
3.1 消费者服务pom
3.2 nacos及dubbo配置
3.3 调用dubbo服务
4. 测试
4.1 生产者服务注册成功
4.2 消费者服务注册成功
4.3 测试controller,消费者调用生成者服务
注意:
其中build plugins spring-boot-maven-plugin插件,classifier为exec,表示构建可依赖的jar包及可启动的jar包
4.0.0
hj.example
springboot-provider
0.0.1-SNAPSHOT
sample-api
0.0.1-SNAPSHOT
sample-api
sample-api
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-maven-plugin
exec
package hj.example.sample;
public interface IHelloService {
String sayHello(String name);
}
包括4个依赖:接口依赖sample-api、nacos配置中心依赖spring-cloud-starter-alibaba-nacos-config、nacos注册中心依赖spring-cloud-starter-alibaba-nacos-discovery,spring-cloud dubbo依赖spring-cloud-starter-dubbo
4.0.0
hj.example
springboot-provider
0.0.1-SNAPSHOT
sample-provider
0.0.1-SNAPSHOT
jar
sample-provider
sample-provider
1.8
org.springframework.boot
spring-boot-starter-web
hj.example
sample-api
0.0.1-SNAPSHOT
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
2.2.5.RELEASE
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-config
2.2.5.RELEASE
com.alibaba.cloud
spring-cloud-starter-dubbo
2.2.5.RELEASE
org.springframework.boot
spring-boot-maven-plugin
exec
通过@DubboService注解,将dubbo服务注册到nacos上;
dubbo配置,如果不在bootstrap.properties上配置spring.cloud.nacos.config.prefix,默认连接nacos配置中心的dubbo.properties配置文件;
程序优先读取bootstrap.properties配置文件,内容为:
spring.cloud.nacos.config.server-addr=127.0.0.1:8948
spring.cloud.nacos.config.username=nacos
spring.cloud.nacos.config.password=
spring.cloud.nacos.config.enabled=false
application.propertes文件内容为:
spring.application.name=sample-provider
server.port=8089
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8948
spring.cloud.nacos.discovery.username=nacos
spring.cloud.nacos.discovery.password=
spring.cloud.nacos.discovery.service=sample-provider
nacos上dubbo.properties文件内容:
启动类:
package hj.example.sampleprovider;
import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.ConfigurableApplicationContext;
@DubboComponentScan
@EnableDiscoveryClient
@SpringBootApplication
@EnableDubbo(scanBasePackages="hj.example.sampleprovider.sample")
public class SampleProviderApplication {
public static void main(String[] args) {
// Main.main(args);
ConfigurableApplicationContext context = SpringApplication.run(SampleProviderApplication.class, args);
String info = context.getEnvironment().getProperty("info");
System.out.println("==========" + info);
}
}
注册服务类:
package hj.example.sampleprovider.sample;
import hj.example.sample.IHelloService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Value;
@DubboService
public class HelloServiceImpl implements IHelloService {
@Value("${dubbo.application.name}")
private String serviceName;
public String sayHello(String name) {
return String.format("[%s]: Hello, %s", serviceName, name);
}
}
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.2.10.RELEASE
hj.example
sample-consumer
0.0.1-SNAPSHOT
sample-consumer
sample-consumer
1.8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
hj.example
sample-api
0.0.1-SNAPSHOT
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
2.2.5.RELEASE
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-config
2.2.5.RELEASE
com.alibaba.cloud
spring-cloud-starter-dubbo
2.2.5.RELEASE
org.springframework.boot
spring-boot-maven-plugin
exec
配置中心配置bootstrap.properties及nacos配置中心文件dubboConsumer.properties
bootstrap.properties
spring.cloud.nacos.config.server-addr=127.0.0.1:8948
spring.cloud.nacos.config.username=nacos
spring.cloud.nacos.config.password=
spring.cloud.nacos.config.prefix=dubboConsumer.properties
dubboConsumer.properties
使用注解@DubboReference调用dubbo服务,测试controller
package hj.example.sampleconsumer.controller;
import hj.example.sample.IHelloService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@DubboReference
private IHelloService iHelloService;
@RequestMapping("/test")
public ResponseEntity
启动类:
package hj.example.sampleconsumer;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDubbo
@EnableDiscoveryClient
public class SampleConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(SampleConsumerApplication.class, args);
}
}