新建一个父项目,里面包含Api,Provide和Consume三个子项目。
Api:存放接口
Provide:实现接口
Consume:调用接口,返回的是Provide具体实现的内容
1.父项目maven配置:
<modules>
<module>MyDemoProvide</module>
<module>MyDemoServiceApi</module>
<module>MyDemoServiceConsume</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.MyDemo</groupId>
<artifactId>MyDemoService</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<dependencies>
<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>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>Provided</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.3</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>2.7.3</version>
<type>pom</type>
<exclusions>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
2.1 api项目maven配置:
<parent>
<artifactId>MyDemoService</artifactId>
<groupId>com.MyDemo</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>MyDemoServiceApi</artifactId>
<version>1.0.0</version>
2.2 api项目写一个接口
public interface SayHello {
public String sayHello();
}
3.1 Provide项目maven配置:
<parent>
<artifactId>MyDemoService</artifactId>
<groupId>com.MyDemo</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>MyDemoProvide</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.MyDemo</groupId>
<artifactId>MyDemoServiceApi</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
3.2 Provide项目实现SayHello接口:
@Service
@Component
public class SayHelloImp implements SayHello {
@Override
public String sayHello() {
return "I am Provide";
}
}
3.3 Provide项目启动类
@EnableDubbo
@EnableDubboConfig
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
3.4 Provide项目application.yml文件:
server:
port: 8100
dubbo:
application:
name: MyDemoProvide
registry:
protocol: zookeeper
address: 127.0.0.1:2181
4.1 Consume项目maven配置:
<parent>
<artifactId>MyDemoService</artifactId>
<groupId>com.MyDemo</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>MyDemoServiceTest</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.MyDemo</groupId>
<artifactId>MyDemoServiceApi</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
4.2 Consume项目调用SayHello接口,返回的是Provide项目具体实现的内容
@RestController
public class TestController {
@Reference
private SayHello sayHello;
@RequestMapping(value = "/sayHello.do", method = RequestMethod.GET)
public String sayHello(){
return sayHello.sayHello();//实际运行会返回"I am Provide"
}
}
4.3 Consume项目启动类
@EnableDubbo
@EnableDubboConfig
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
4.4 Consume项目application.yml文件:
server:
port: 8200
dubbo:
application:
name: MyDemoConsume
registry:
protocol: zookeeper
address: 127.0.0.1:2181