欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

spring/springboot整合dubbo詳細教程

 更新時間:2021年05月25日 09:43:42   作者:倪暢  
今天教大家如何使用spring/springboot整合dubbo,文中有非常詳細的圖文介紹及代碼示例,對正在學習java的小伙伴有很好地幫助,需要的朋友可以參考下

一、基本使用

需求:

某個電商系統(tǒng),訂單服務需要調用用戶服務獲取某個用戶的所有地址;
我們現(xiàn)在需要創(chuàng)建兩個服務模塊進行測試

模塊 功能
訂單服務web模塊 創(chuàng)建訂單等
用戶服務service模塊 查詢用戶地址等

測試預期結果:
訂單服務web模塊在A服務器,用戶服務模塊在B服務器,A可以遠程調用B的功能。

二、spring整合dubbo

以下使用XML 配置的方式,更多配置方式見官方文檔

2.1 spring-common模塊:

公共接口層(model,service,exception…),定義公共接口,也可以導入公共依賴

在這里插入圖片描述

UserAddress.java:用戶地址實體類

public class UserAddress implements Serializable {
    private Integer id;
    private String userAddress;
    private String userId;
    private String consignee;
    private String phoneNum;
    private String isDefault;
	....
}

IOrderService.java:訂單接口

public interface IOrderService {
    /**
     * 用戶下單
     * @param userId
     */
    UserAddress placeOrder(int userId);
}

IUserService.java:用戶接口

public interface IUserService {
    /**
     * 根據(jù)用戶id獲取用戶地址
     * @param userId
     * @return
     */
    UserAddress getUserAddById(int userId);
}

pom.xml:通用的依賴,引入dubbo和zkclient

<?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">
    <parent>
        <artifactId>dubboStudy</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>spring-common</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <!--dubbo -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.10</version>
        </dependency>

        <!--zookeeper -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.6</version>
        </dependency>
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>
    </dependencies>

</project>

2.2 spring-user模塊:

用戶業(yè)務,作為服務提供者

在這里插入圖片描述

pom.xml:引入通用模塊,可以使用定義的接口

<dependency>
  <groupId>org.example</groupId>
  <artifactId>spring-common</artifactId>
  <version>1.0-SNAPSHOT</version>
</dependency>

UserServiceImpl.xml:IUserService的實現(xiàn)類,供遠程調用

public class UserServiceImpl implements IUserService {

    @Override
    public UserAddress getUserAddById(int userId) {
        UserAddress userAddress = new UserAddress();
        userAddress.setUserAddress("上海市寶山區(qū)");
        return userAddress;
    }

}

provider.xml:dubbo配置信息

<?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://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd
		http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!--1、指定當前服務/應用的名字(同樣的服務名字相同,不要和別的服務同名)-->
    <dubbo:application name="spring-user"/>
    <!--2、指定注冊中心的位置-->
    <!--<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>-->
    <dubbo:registry address="zookeeper://192.168.31.136:2181"/>
    <!--3、指定通信規(guī)則(通信協(xié)議? 服務端口)-->
    <dubbo:protocol name="dubbo" port="20880"/>
    <!--4、暴露服務 讓別人調用 ref指向服務的真正實現(xiàn)對象-->
    <bean id="userServiceImpl" class="me.nic.service.impl.UserServiceImpl"/>
    <!--服務的實現(xiàn)-->
    <dubbo:service interface="me.nic.service.IUserService" ref="userServiceImpl"/>

</beans>

ConsumerRun.java:啟動:

public class ConsumerRun {

    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("consumer.xml");
        IOrderService orderService = applicationContext.getBean(IOrderService.class);
        orderService.placeOrder(1);
        System.in.read();
    }
}

2.3 spring-order模塊:

訂單業(yè)務,作為服務消費者

在這里插入圖片描述

pom.xml:引入通用模塊,可以使用定義的接口,同1.2
OrderServiceImpl.xml:IOrderService的實現(xiàn)類,其中遠程調用userService

@Service
public class OrderServiceImpl implements IOrderService {

    @Autowired
    private IUserService userService;

    @Override
    public UserAddress placeOrder(int userId) {
        // 遠程調用,獲取用戶地址
        UserAddress userAddById = userService.getUserAddById(userId);
        // 下單
        System.out.println("用戶地址:" + userAddById.getUserAddress());
        System.out.println("下單成功");
        return null;
    }
}

consumer.xml:dubbo配置信息

<?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:context="http://www.springframework.org/schema/context"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd
		http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!--包掃描-->
    <context:component-scan base-package="me.nic.service.impl"/>

    <!--指定當前服務/應用的名字(同樣的服務名字相同,不要和別的服務同名)-->
    <dubbo:application name="spring-order"/>

    <!--指定注冊中心的位置-->
    <dubbo:registry address="zookeeper://192.168.31.136:2181"/>

    <!--調用遠程暴露的服務,生成遠程服務代理-->
    <dubbo:reference id="userService" interface="me.nic.service.IUserService"/>
</beans>

ProviderRun.java:啟動

public class ProviderRun {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext("classpath:provider.xml");
        System.in.read();
    }
}

2.4 效果

ProviderRun啟動之后阻塞,
ConsumerRun啟動之后調用orderService.placeOrder(1),placeOrder中遠程調用spring-user模塊中的userService.getUserAddById(userId)獲取用戶地址:

用戶地址:上海市寶山區(qū)
下單成功

三、springboot整合dubbo

3.1 boot-common模塊:

公共接口層(model,service,exception…),定義公共接口,也可以導入公共依賴

在這里插入圖片描述

UserAddress.java:用戶地址實體類

public class UserAddress implements Serializable {
    private Integer id;
    private String userAddress;
    private String userId;
    private String consignee;
    private String phoneNum;
    private String isDefault;
	....
}

IOrderService.java:訂單接口

public interface IOrderService {
    /**
     * 用戶下單
     * @param userId
     */
    UserAddress placeOrder(int userId);
}

IUserService.java:用戶接口

public interface IUserService {
    /**
     * 根據(jù)用戶id獲取用戶地址
     * @param userId
     * @return
     */
    UserAddress getUserAddById(int userId);
}

pom.xml:通用的依賴,引入dubbo的starter

<?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.5.0</version>
        <relativePath/> 
    </parent>
    
    <groupId>com.example</groupId>
    <artifactId>boot-common</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>boot-common</name>
    
    <properties>
        <java.version>1.8</java.version>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>
    </dependencies>

</project>

3.2 boot-user模塊:

用戶業(yè)務,作為服務提供者

在這里插入圖片描述

pom.xml:引入通用模塊,可以使用定義的接口

<dependency>
  <groupId>org.example</groupId>
  <artifactId>spring-common</artifactId>
  <version>1.0-SNAPSHOT</version>
</dependency>

UserServiceImpl.xml:IUserService的實現(xiàn)類,供遠程調用
@Service 暴露dubbo的服務

@Service
@Component
public class UserServiceImpl implements IUserService {

    @Override
    public UserAddress getUserAddById(int userId) {
        UserAddress userAddress = new UserAddress();
        userAddress.setUserAddress("上海市寶山區(qū)");
        return userAddress;
    }

}

application.properties:dubbo配置信息

dubbo.application.name=boot-user
dubbo.registry.address=192.168.31.136:2181
dubbo.registry.protocol=zookeeper
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880

BootUserApplication.java:啟動:
@EnableDubbo : 開啟基于注解的dubbo功能

@EnableDubbo
@SpringBootApplication
public class BootUserApplication {

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

}

3.3 boot-order模塊:

訂單業(yè)務,作為服務消費者

在這里插入圖片描述

pom.xml:引入通用模塊,可以使用定義的接口,同1.2

<dependency>
  <groupId>com.example</groupId>
  <artifactId>boot-common</artifactId>
  <version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>

OrderServiceImpl.xml:IOrderService的實現(xiàn)類,其中遠程調用userService
@Reference:引用遠程提供者服務

@Service
public class OrderServiceImpl implements IOrderService {

    //引用遠程提供者服務
    @Reference
    private IUserService userService;

    @Override
    public UserAddress placeOrder(int userId) {
        // 遠程調用,獲取用戶地址
        UserAddress userAddById = userService.getUserAddById(userId);
        return userAddById;
    }
}

OrderController.java:web接口,調用OrderService:

@Controller
public class OrderController {
    @Autowired
    IOrderService orderService;

    @RequestMapping("/initOrder")
    @ResponseBody
    public UserAddress initOrder(@RequestParam("uid")int userId) {
        return orderService.placeOrder(userId);
    }
}

application.properties:dubbo配置信息

server.port=8081
dubbo.application.name=boot-order
dubbo.registry.address=zookeeper://192.168.31.136:2181

BootOrderApplication.java:啟動

@EnableDubbo
@SpringBootApplication
public class BootOrderApplication {

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

}

3.4 效果

在這里插入圖片描述

到此這篇關于springboot整合dubbo詳細教程的文章就介紹到這了,更多相關springboot整合dubbo內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論