手把手教你Dubbo與SpringBoot經常使用兩種方式整合

1、Dubbo整合SpringBoot的方式(1)


 

1)直奔主題,方式一:

  pom.xml中引入dubbo-starter依賴,在application.properties配置屬性,使用@Service【暴露服務】使用@Reference【引用服務】,選取的是application.properties+部分註解的方式來完成。java

 

2)建立ego_interface接口模塊,被提供者和消費者所使用

此模塊目錄結構以下:web

一、實體類UserAddressspring

package com.sxt.domain;
import java.io.Serializable;

//實體類 必須實現序列化
public class UserAddress implements Serializable {

    private Integer id;
    private String address;
    private String userId;

    public UserAddress() {
    }
    public UserAddress(Integer id, String address, String userId) {
        this.id = id;
        this.address = address;
        this.userId = userId;
    }

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getUserId() {
        return userId;
    }
    public void setUserId(String userId) {
        this.userId = userId;
    }
}

二、提供者接口UserServiceapache

package com.sxt.service;
import com.sxt.domain.UserAddress;
import java.util.List;
//提供者接口
public interface UserService {

    //根據用戶id查詢用戶地址
    public List<UserAddress> getUserAddressByUserId(String userId);
}

三、消費者接口OrderServiceapi

package com.sxt.service;
import com.sxt.domain.UserAddress;
import java.util.List;
//消費者接口
public interface OrderService{

    //初始化訂單
    public List<UserAddress> initOrder(String userId);
}

3)建立boot-ego-user-service-provider提供者模塊

此模塊目錄結構以下(config是第二種方式用的,此處請先忽略):springboot

4)修改pom.xml加入依賴

添部分關鍵依賴(其他自動生成):服務器

    <!--加入對ego_interface依賴-->
  <!--這個是上面第二步建立的你所要使用的接口類的依賴-->
<dependency> <groupId>com.sxt</groupId> <artifactId>ego_interface</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
<!-- Dubbo Spring Boot Starter --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>${dubbo.version}</version> </dependency> <!-- 使用zk 作註冊中心,Dubbo 須要的依賴 --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-dependencies-zookeeper</artifactId> <version>${dubbo.version}</version> <type>pom</type> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> </exclusions> </dependency>

一、編寫UserServiceImplapp

package com.sxt.service.impl;

import com.sxt.domain.UserAddress;
import com.sxt.service.UserService;
import org.apache.dubbo.config.annotation.Service;
import java.util.ArrayList;
import java.util.List;

//此處service是apache.dubbo的 代替了建立和暴露對象
@Service
public class UserServiceImpl implements UserService {

    public static List<UserAddress> address=new ArrayList<>();

    static {
        address.add(new UserAddress(1, "天安門廣場", "bj"));
        address.add(new UserAddress(2, "上海迪士尼", "sh"));
    }


    @Override
    public List<UserAddress> getUserAddressByUserId(String userId) {
        return address;
    }
}

二、修改application.propertiesdom

#application-name 本模塊名字
dubbo.application.name=boot-ego-user-service-provider
#registry 指定註冊中心地址(www.lcbxiuxiu.tech是我阿里雲地址 請換成你本身的服務器地址)
dubbo.registry.address=zookeeper://www.lcbxiuxiu.tech:2181
#dubbo protocol 指定dubbo協議 將服務暴露在20880端口
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880

三、修改啓動類並啓動maven

@SpringBootApplication
//此註解爲了自動開啓dubbo
@EnableDubbo
public class BootEgoUserServiceProviderApplication {
  public static void main(String[] args) {
        SpringApplication.run(BootEgoUserServiceProviderApplication.class, args);
    }
}

四、啓動成功後畫面在遠程dubbo訪問

5)建立boot-ego-order-service-comsumer消費者模塊

此模塊目錄結構以下(config是第二種方式用的,此處請先忽略):

 

 

 

一、修改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 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.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.sxt</groupId>
    <artifactId>boot-ego-order-service-comsumer</artifactId>
    <version>1.0</version>
    <name>boot-ego-order-service-comsumer</name>
    <description>springboot集成dubbo的消費者</description>

    <properties>
        <java.version>1.8</java.version>
        <dubbo.version>2.7.3</dubbo.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </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>
        <!--加入對ego_interface依賴-->
        <dependency>
            <groupId>com.sxt</groupId>
            <artifactId>ego_interface</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>

        <!-- Dubbo Spring Boot Starter -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>${dubbo.version}</version>
        </dependency>
        <!-- 使用zk 作註冊中心,Dubbo 須要的依賴 -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <version>${dubbo.version}</version>
            <type>pom</type>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

二、建立OrderServiceImpl

package com.sxt.service.impl;

import com.sxt.domain.UserAddress;
import com.sxt.service.OrderService;
import com.sxt.service.UserService;
import org.springframework.stereotype.Service;
import org.apache.dubbo.config.annotation.Reference;

import java.util.List;

@Service  //此處是spring的 幫你自動建立對象與注入
public class OrderServiceImpl implements OrderService {

    @Reference //此處是apache.dubbo 代替引入遠程對象
    private UserService userService;

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @Override
    public List<UserAddress> initOrder(String userId) {
        return this.userService.getUserAddressByUserId(userId);
    }

}

三、修改啓動類並啓動

package com.sxt;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableDubbo
public class BootEgoOrderServiceComsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(BootEgoOrderServiceComsumerApplication.class, args);
    }
}

四、在測試類中測試

package com.sxt;

import com.sxt.domain.UserAddress;
import com.sxt.service.OrderService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.List;

@SpringBootTest
class BootEgoOrderServiceComsumerApplicationTests {

    @Autowired
    OrderService orderService;

    @Test
        void contextLoads() throws IOException {
        List<UserAddress> userAddresses = orderService.initOrder("sxt");
        for (UserAddress userAddress : userAddresses) {
            System.out.println(userAddress.getId()+" "+userAddress.getAddress());
        }
        //想在dubbo首頁顯示消費者 阻止程序中止
        System.in.read();
    }

}

五、修改application.properties後啓動上面的測試類便可

#application.name
dubbo.application.name=boot-ego-order-service-comsumer
#address
dubbo.registry.address=zookeeper://本身的服務器地址:2181

小結第一種方式:

  1.提供者只須要在application.properties中聲明:模塊名字,註冊中心地址,鏈接規則(使用什麼協議,暴露什麼端口)。其他的由service實現類中的@service(apache.dubbo)註解幫忙建立和暴露了對象

  2.消費者只須要在application.properties中聲明:模塊名字,註冊中心地址。其他由service實現類中的@service(spring的)註解幫忙建立和注入對象,@Reference(apache.dubbo)幫忙引入遠程服務,在測試類中就能夠使用@Autowired裝配對象使用其方法

相關文章
相關標籤/搜索