springBoot整合dubbo
- 建立springBoot專案,匯入dubbo依賴
<!-- 新增dubbo的啟動器, 其中已經添加了zookepper的依賴--> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency>
- 配置dubbo的連線和zookeeper的連線,在application.properties中新增如下配置
# 啟動的埠號 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
暴露服務
-
在spring中使用的
<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/">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; } }
引用服務
-
在spring的配置檔案中使用
<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); } }
maven聚合springBoot專案
-
ofollow,noindex">專案地址-點選下載
-
建立
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
maven建立springBoot工程
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); } }
maven打包springBoot注意
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>