<!-- 添加dubbo的启动器, 其中已经添加了zookepper的依赖--> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency>
# 启动的端口号 server.port=8001 server.servlet.context-path=/provider # 暴露的服务名称 dubbo.application.name=user-service # zookeeper注册中心的地址 dubbo.registry.address=zookeeper://39.105.123.197:2181
@EnableDubbo
注解,开启dubbo <dubbo:service>
暴露服务,但是在springBoot中只需要使用dubbo的注解 @Service
(com.alibaba.dubbo.config.annotation.Service)即可自动暴露。如下: import org.springframework.stereotype.Component;
import com.alibaba.dubbo.config.annotation.Service;
import cn.tedu.demo.beans.User;
import cn.tedu.demo.service.UserService;
@Service //暴露服务,只需要在对应的服务类上添加这个注解即可
@Component //注入到IOC容器中
public class UserServiceImplimplements UserService{
@Override
public User getUser(Integer userId){
User user=new User();
user.setAge(22);
user.setUserId(userId);
user.setUserName("陈加兵");
return user;
}
}
<dubbo:reference>
引用服务,但是在springBoot中只需要使用dubbo的注解 @Reference
即可引用对应的服务 @RestController
public class UserController{
@Reference //消费者引用提供者提供的服务,相当于<dubbo:reference>
private UserService userService;
@GetMapping("/user/get/{id}")
public User get(@PathVariable("id")Integer userId){
return userService.getUser(userId);
}
}
项目地址-点击下载
创建 demo-parent
父项目管理版本,但是在springBoot项目中也是使用父项目管理的,因此我们需要在父项目中使用springBoot的依赖管理的starter来替代之前的parent
<!--之前的parent,在springBoot创建的时候将会添加,但是这里不需要
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath />
</parent>
-->
<properties>
<springBoot-version>2.0.5.RELEASE</springBoot-version>
</properties>
<dependencyManagement>
<dependencies>
<!-- 直接使用这个依赖管理springBoot的版本即可 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${springBoot-version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
demo-parent
1、新建一个module,打包方式为jar
2、添加依赖,如下(直接添加依赖,因为父工程demo-parent已经管理了版本):
<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>
3、在 src/main/resources
包的下新建springBoot的配置文件application.properties
4、新建一个启动类
@SpringBootApplication //标记为springBoot的启动类
public class DemoApplication{
public static void main(String[] args){
SpringApplication.run(DemoApplication.class, args);
}
}
1、在pom文件中添加如下依赖(如果不添加如下依赖,可能打出的jar包运行将会报错找不到主程序清单):
<build> <!-- <finalName>batman-web</finalName> --> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build>