springBoot整合dubbo

  • 2019 年 12 月 30 日
  • 筆記

文章目錄

  1. 1. SpringBoot整合dubbo
    1. 1.1. 搭建項目
    2. 1.2. 暴露服務
    3. 1.3. 引用服務
    4. 1.4. maven聚合springBoot項目
      1. 1.4.1. maven創建springBoot工程
      2. 1.4.2. maven打包springBoot注意

SpringBoot整合dubbo

搭建項目

  • 創建springBoot項目,導入dubbo依賴

123456

<!– 添加dubbo的啟動器, 其中已經添加了zookepper的依賴–><dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version></dependency>

  • 配置dubbo的連接和zookeeper的連接,在application.properties中添加如下配置

1234567

# 啟動的端口號server.port=8001server.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)即可自動暴露。如下:

1234567891011121314151617

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 UserServiceImpl implements 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即可引用對應的服務

12345678910

@RestControllerpublic 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項目

  • 項目地址-點擊下載
  • 創建demo-parent父項目管理版本,但是在springBoot項目中也是使用父項目管理的,因此我們需要在父項目中使用springBoot的依賴管理的starter來替代之前的parent

1234567891011121314151617181920212223242526

<!–之前的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新建module
  • 在新的module的pom.xml直接添加springBoot的啟動器即可,不用指定版本

maven創建springBoot工程

1、新建一個module,打包方式為jar

2、添加依賴,如下(直接添加依賴,因為父工程demo-parent已經管理了版本):

12345678910

<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、新建一個啟動類

123456

@SpringBootApplication //標記為springBoot的啟動類public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }}

maven打包springBoot注意

1、在pom文件中添加如下依賴(如果不添加如下依賴,可能打出的jar包運行將會報錯找不到主程序清單):

12345678910111213141516

<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>