其中需要注意的点:
- 服务提供者和服务消费者在Dubbo框架中进行通信时,它们必须使用相同的接口路径。
例如两个接口的包路径都是package com.example.demo.service;
- 注意如果提供者的接口用的是@Service,要查看引入的包路径是否为
import org.apache.dubbo.config.annotation.Service;
- @Service(@DubboServie)添加在Impl上
- 一般来说,在配置文件的dubbo中配置了nacos的地址,则不需要再在discovery.server-addr的地址。但项目一般采用则是只需要在配置文件中配置nacos的地址,然后服务去nacos去获取配置文件的信息,例如dubbo配置。
- 注意 dubbo里的port不能被占用。
- 接口暴露成功后可以在nacos中看到。
Demo
提供者
public interface HelloService {
public String say();
}
@Service
public class HelloServiceImpl implements HelloService{
@Override
public String say() {
return "Hello";
}
}
@EnableDubbo
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
dubbo:
scan:
base-packages: com.example.demo.service
protocol:
name: dubbo
port: 7702
registry:
address: nacos://localhost:8848
consumer:
timeout: 60000
check: false
application:
name: ${spring.application.name}
cloud:
消费者
public interface HelloService {
public String say();
}
@RestController
public class HelloController {
@Reference
HelloService helloService;
@GetMapping("/hello")
public String hello(){
return helloService.say();
}
}
dubbo:
scan:
base-packages: com.ut.msfw1a.services.demo
protocol:
name: dubbo
port: 30032
registry:
address: nacos://localhost:8848
consumer:
timeout: 60000
check: false
application:
name: ${spring.application.name}
cloud:
访问http://localhost:8433/hello
即返回hello