- 下载下载链接
- 启动:进入bin目录,startup.cmd -m standalone
- 引入依赖
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>3.0.9</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>2.1.0</version>
</dependency>
4.配置application.yml
dubbo:
application:
name: dubbo-springboot-demo-provider
protocol:
name: dubbo
port: -1
registry:
id: nacos-registry
address: nacos://localhost:8848
5. 提供者:provider包下创建service服务
public interface DemoService {
String sayHello(String name);
String sayHello2(String name);
default CompletableFuture<String> sayHelloAsync(String name) {
return CompletableFuture.completedFuture(sayHello(name));
}
}
@DubboService
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
System.out.println("Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
return "Hello " + name;
}
@Override
public String sayHello2(String name) {
return "mumu";
}
}
6.消费者:创建service,需要用到demoservice
package org.mumu.project.provider;
import java.util.concurrent.CompletableFuture;
public interface DemoService {
String sayHello(String name);
String sayHello2(String name);
default CompletableFuture<String> sayHelloAsync(String name) {
return CompletableFuture.completedFuture(sayHello(name));
}
}
7.消费者方调用远程服务
@SpringBootApplication(exclude = {
DataSourceAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class,
HibernateJpaAutoConfiguration.class})
@EnableDubbo
public class ApiGatewayApplication {
@DubboReference
private DemoService demoService;
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(ApiGatewayApplication.class, args);
ApiGatewayApplication application = context.getBean(ApiGatewayApplication.class);
String result = application.doSayHello("world");
String result2 = application.doSayHello2("world");
System.out.println("result: " + result);
System.out.println("result: " + result2);
}
public String doSayHello(String name) {
return demoService.sayHello(name);
}
public String doSayHello2(String name) {
return demoService.sayHello2(name);
}
}
报错:Failed to check the status of the service org.mumu.apigateway.project.provider.DemoService. No provider available for the service org.mumu.apigateway.project.provider.DemoService from the url
需要注意的是:服务提供方和调用方需要包名一致
http://localhost:8848/nacos/index.html nacos中心