四.SpringBoot版
1.Provider工程
1-1 创建工程
创建一个SpringBoot工程boot-provider
选择对应的组件
- Web --> Spring Web
1-2 POM配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.itany.dubbo</groupId>
<artifactId>boot-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>boot-provider</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 基类依赖 -->
<dependency>
<groupId>com.itany.dubbo</groupId>
<artifactId>dubbo-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- dubbo starter -->
<!-- 该starter中包含了dubbo、Zookeeper服务器、Curator客户端 -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
1-3 UserServiceImpl
@Service
@com.alibaba.dubbo.config.annotation.Service
public class UserSerivceImpl implements UserService {
@Override
public User findUser() {
User user = new User();
user.setId(2);
user.setUsername("admin");
user.setPassword("123456");
user.setPhone("13812345678");
user.setAddress("江苏-南京");
return user;
}
}
1-4 配置文件
server:
port: 8081
dubbo:
application:
name: boot-provider
registry:
address: zookeeper://127.0.0.1:2181
protocol:
name: dubbo
port: 8888
1-5 主程序
在主程序中进行dubbo的启用
当启用dubbo之后,主程序也会自动扫描dubbo的注解
通过注解
@EnableDubbo
进行启用dubbo
@SpringBootApplication
@EnableDubbo
public class BootProviderApplication {
public static void main(String[] args) {
SpringApplication.run(BootProviderApplication.class, args);
}
}
2.Consumer工程
2-1 创建工程
创建一个SpringBoot工程boot-consumer
选择对应的组件
- Web --> Spring Web
- Template Engines --> Thymeleaf
2-2 POM依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 基类依赖 -->
<dependency>
<groupId>com.itany.dubbo</groupId>
<artifactId>dubbo-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- dubbo starter -->
<!-- 该starter中包含了dubbo、Zookeeper服务器、Curator客户端 -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
2-3 UserController
@Controller
@RequestMapping("/user")
public class UserController {
//远程注入
@Reference
private UserService userService;
@RequestMapping("/findUser")
public ModelAndView findUser(){
ModelAndView mav = new ModelAndView();
User user = userService.findUser();
mav.addObject("user",user);
mav.setViewName("main");
return mav;
}
}
2-4 Thymeleaf
编号:<span th:text="${user.id}"></span><br/>
用户名:<span th:text="${user.username}"></span><br/>
密码:<span th:text="${user.password}"></span><br/>
电话:<span th:text="${user.phone}"></span><br/>
地址:<span th:text="${user.address}"></span><br/>
2-5 配置文件(yaml文件)
dubbo:
application:
name: boot-consumer
registry:
address: zookeeper://127.0.0.1:2181
2-6 主程序
@SpringBootApplication
@EnableDubbo
public class BootConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(BootConsumerApplication.class, args);
}
}
3.测试
- 启动Zookeeper服务器
- 启动提供方主程序
- 启动消费者主程序
- 测试的时候访问的是消费者的服务
- 消费者通过注册中心访问提供方的服务
- 因此,访问的地址栏的端口是消费者默认的8080