本帖最后由 HK仅輝 于 2022-9-13 16:56 编辑
IDEA新建一个项目,创建三个模块
注意:这里同样要先启动 zk
一、dubbo-api (单纯的maven模块)
在这个模块中创建一个接口。
[Java] 纯文本查看 复制代码 public interface DemoService {
String sayHello(String name);
}
二、dubbo-provider (springboot 模块)
在启动类同级创建一个包,然后实现api接口 ,这里使用dubbo提供的@DubboService注解
[Java] 纯文本查看 复制代码 import com.dubbo.DemoService;
import org.apache.dubbo.config.annotation.DubboService;
@DubboService(version = "1.0.0")
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
return "Hi,"+name;
}
}
三、dubbo-consumer (springboot 模块)
在启动类中,加入api接口类这里同样要用dobbo提供的 @DubboReference 注解,然后调用接口方法
[Java] 纯文本查看 复制代码 import com.dubbo.DemoService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class DubboConsumerApplication {
@DubboReference(version = "1.0.0")
private DemoService demoService;
public static void main(String[] args) {
SpringApplication.run(DubboConsumerApplication.class,args);
}
@Bean
public ApplicationRunner runner(){
return args -> System.out.println(demoService.sayHello("dubbo"));
}
}
springboot 依赖
[Java] 纯文本查看 复制代码 <dependencies>
<dependency>
<groupId>com.api</groupId>
<artifactId>dubbo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>3.0.10</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>3.0.10</version>
<type>pom</type>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>3.0.10</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
|