springboot與dubbo整合入門(三種方式)


Springboot與Dubbo整合三種方式詳解

整合環境:

jdk:8.0

dubbo:2.6.2

springboot:2.1.5

項目結構:

1、搭建項目環境:

  (1)創建父項目與三個子項目,創建項目時,都使用spring initializr,創建時,父項目中注意的一點:

  

  (2)創建三個子項目,在已有的父項目上右鍵,新建模塊;

  (3)創建完成后:將三個子項目在父項目pom.xml中配置:

  

  (4)修改所有子項目中的parent標簽:(刪掉之前parent中的springboot)修改為:

  

  (5)項目創建完成;

2、配置項目dubboservice,只包含接口:

 

3、第一種方式使用xml文件:

3.1、配置項目dubboserviceimpl(服務提供者):

(1)引入dubbo相關依賴,以及zookeeper客戶端依賴,由於需要與其他服務器通信,引入web依賴,pom.xml:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>top.free</groupId> <artifactId>springboot-dubbo</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>top.free</groupId> <artifactId>dubboserviceimpl</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!--依賴dubboservice接口--> <dependency> <groupId>top.free</groupId> <artifactId>dubboservice</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <!--web依賴--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--dubbo與springboot整合依賴--> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency> <!-- zookeeper依賴--> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>

 

  (2) xml文件配置 provider.xml:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd "> <!-- 發布服務到注冊中心 --> <!-- 為服務起一個別名 --> <dubbo:application name="provider" /> <!--address 注冊中心的地址 protocol 指的是注冊中心的協議的格式 --> <dubbo:registry address="XXXXXXXX:2181" protocol="zookeeper"/> <!-- interface告訴注冊中心是哪個類型 ref說明具體是哪個服務 timeout連接超時的時間 --> <dubbo:service interface="top.free.dubboservice.TestDubboService" ref="serviceImpl" timeout="60000"></dubbo:service> <!--, 配置端口,協議是dubbo,消費者消費的時候必須通過端口來消費,端口可以隨便寫,但是不能被占用,一個dubbo,獨占一個端口 --> <dubbo:protocol name="dubbo" port="20881"></dubbo:protocol> </beans>

(3) 配置實現類ServiceImpl:

      

package top.free.dubboserviceimpl; import org.springframework.stereotype.Component; import top.free.dubboservice.TestDubboService;  @Component public class ServiceImpl implements TestDubboService { @Override public String getData(String user) { return "你好,我們通信了:"+user; } }

 

(4)主啟動類上加上:@ImportResource(locations = "classpath:provider.xml")

 

package top.free; import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan; import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ImportResource; @SpringBootApplication @ImportResource(locations = "classpath:provider.xml") public class DubboserviceimplApplication { public static void main(String[] args) { SpringApplication.run(DubboserviceimplApplication.class, args); } }

(5)在application.properties中加上項目啟動的端口號: server.port=8080

3.2、配置dubboweb項目(消費者)

(1)引入dubbo相關依賴,以及zookeeper客戶端依賴,由於需要與其他服務器通信,引入web依賴,pom.xml:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>top.free</groupId> <artifactId>springboot-dubbo</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>top.free</groupId> <artifactId>dubboweb</artifactId> <version>0.0.1-SNAPSHOT</version> <name>dubboweb</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>top.free</groupId> <artifactId>dubboservice</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>

 

 (2)配置controller:

 

package top.free.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import top.free.dubboservice.TestDubboService; @RestController public class TestDubboWeb { //serviceImpl會顯示紅色,這個是因為編譯時找不到bean注入,不影響結果  @Autowired private TestDubboService servcieImpl; @RequestMapping("/test/{user}") public String getData(@PathVariable("user")String user){ String data =servcieImpl.getData(user); return data; } }

 (3)配置消費者的consumer.xml文件:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd "> <!-- 消費者起一個別名 --> <dubbo:application name="consumer"/> <!--address 注冊中心的地址 protocol 指的是注冊中心的協議的格式 --> <dubbo:registry address="XXXXXXXX:2181" protocol="zookeeper"/> <!-- 告訴注冊中心我需要什么 --> <dubbo:reference interface="top.free.dubboservice.TestDubboService" id="serviceImpl"></dubbo:reference> </beans>

(4)在主啟動類上加:@ImportResource(locations = "classpath:consumer.xml")

package top.free; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ImportResource; @SpringBootApplication @ImportResource(locations = "classpath:consumer.xml") public class DubbowebApplication { public static void main(String[] args) { SpringApplication.run(DubbowebApplication.class, args); } }

 (5)在application.properties文件中加上啟動的端口號:server.port=8081

 (6)、第一種xml方式配置完成;

(7)、首先在本地安裝這三個項目:

 (8)、安裝完成后就可以起動dubboserviceimpl與dubboweb兩個項目,並測試:

 4、第二種使用配置類方式(API):pom.xml文件不修改,application.properties文件不做修改(只配置了端口號)

  4.1、服務提供者(項目dubboserviceimpl):配置類DubboProviderConfig(注意與主程序類XXXApplication的位置)

  

  (1)、配置類代碼:

package top.free.config; import com.alibaba.dubbo.config.ApplicationConfig; import com.alibaba.dubbo.config.ProtocolConfig; import com.alibaba.dubbo.config.RegistryConfig; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class DubboProviderConfig { /*相當於xml中服務提供者的別名:<dubbo:application name="provider" />*/ @Bean public ApplicationConfig myApplicationConfig(){ ApplicationConfig ac = new ApplicationConfig(); ac.setName("provider3"); return ac; } /*相當於注冊中心配置:<dubbo:registry address="XXXXXXXX:2181" protocol="zookeeper"/>*/ @Bean public RegistryConfig myregistryConfig(){ RegistryConfig rc = new RegistryConfig(); rc.setAddress("XXXXXXXX:2181"); rc.setProtocol("zookeeper"); return rc; } /*相當於暴露服務的協議以及端口:<dubbo:protocol name="dubbo" port="20881"></dubbo:protocol>*/ @Bean public ProtocolConfig myProtocolConfig(){ ProtocolConfig pc = new ProtocolConfig(); pc.setName("dubbo"); pc.setPort(20888); return pc; } /*在實現類上加注解來代替:暴露的服務<dubbo:service interface="top.free.dubboservice.TestDubboService" ref="serviceImpl" timeout="60000"></dubbo:service>*/ }

(2)、實現類:注意這里使用的@Service注解是Dubbo的service注解,標志這個類時提供服務的類

(3)、主程序類上加入@EnableDubbo掃描所有包下使用dubbo提供的注解類(或者@EnableDubbo(scanBasePackages = "top.free.dubboserviceimpl")只掃描特定包下有沒有使用dubbo提供的注解),注意:注釋掉第一種方式加載的xml文件;

4.2、配置消費者(dubboweb項目):配置類DubboConsumerConfig

(1)、配置類代碼:

package top.free.config;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DubboConsumerConfig {
    /*相當於consumer.xml中的:<dubbo:application name="consumer"/>*/
    @Bean
    public ApplicationConfig myapplicationConfig(){
        ApplicationConfig ac = new ApplicationConfig();
        ac.setName("consumer3");
        return ac;
    }
    /*相當於:<dubbo:registry address="39.108.125.227:2181" protocol="zookeeper"/>*/
    @Bean
    public RegistryConfig myregistryConfig(){
        RegistryConfig rc = new RegistryConfig();
        rc.setAddress("39.108.125.227:2181");
        rc.setProtocol("zookeeper");
        return rc;
    }
/*使用注解方式來代替:<dubbo:reference interface="top.free.dubboservice.TestDubboService" id="serviceImpl"></dubbo:reference>*/
}
消費者配置類代碼

(2)、在Controller的屬性上加注解@Reference:注意,是dubbo提供的注解,用於標志需要引用的服務

(3)、在主啟動類上加:@EnableDubbo掃描使用dubbo提供的注解,注釋第一種方式加載的xml(我試過,消費者好像不加@EnableDubbo也可以,提供者必須加);

(4)、啟動dubboserviceimpl和dubboweb兩個程序,並測試:

5、第三種方式:屬性文件方式:

  5.1、服務提供者(dubboserviceimpl項目):

  (1)把第二種方式中,配置類文件讓它不要被加載:(注釋掉這個注解,不需要這個類)

  (2)實現類不變:如下圖:使用@Service來標志提供的服務

  

  (3)主程序類不變,與第二種方式一致,如下:

  

  (4)在application.properties文件中加入如下:

#別名 dubbo.application.name=prodiver2 #注冊中心zookeeper地址 dubbo.registry.address=XXXXXXXX:2181 dubbo.registry.protocol=zookeeper #暴露的服務端口與協議 dubbo.protocol.name=dubbo dubbo.protocol.port=20880 server.port=8080

 

5.2、消費者(dubboweb項目):

(1)把第二種方式中,配置類文件讓它不要被加載:(注釋掉這個注解,不需要這個類)

(2)、Controller服務調用與第二種方式一致,如下:使用@Reference來表明需要引用服務的屬性

(3)主配置類不變(與第二種方式一致),如下:

(4)application.properties屬性文件中:

#消費者別名 dubbo.application.name=consumer #注冊中心zookeeper地址與協議 dubbo.registry.address=zookeeper://39.108.125.227:2181 dubbo.registry.protocol=zookeeper server.port=8081

 (5)啟動測試:

6、至此三種方式整合完成;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM