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

Springboot整合dubbo搭建微服務(wù)的實(shí)現(xiàn)示例

 更新時(shí)間:2025年08月06日 10:02:13   作者:lianaozhe  
本文主要介紹了Springboot整合dubbo搭建微服務(wù)的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

開發(fā)前提

由于dubbo的注冊(cè)中心用的是zookeeper,所以首先需要安裝zookeeper。

構(gòu)建Springboot項(xiàng)目

第一步:選擇新建project或者module,在界面中選擇maven點(diǎn)擊next:

第二步:填上項(xiàng)目的基本信息點(diǎn)擊Finish:

第三步:右擊項(xiàng)目new -> Module:

第四步:在界面中選擇maven點(diǎn)擊next:

第五步:填上項(xiàng)目的基本信息點(diǎn)擊Finish:

第六步:重復(fù)第三,四,五步,分別創(chuàng)建項(xiàng)目需要的dubbo-api,dubbo-provider,dubbo-customer幾個(gè)模塊,如下:

第七步:導(dǎo)入父工程依賴:

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

    <groupId>com.demo</groupId>
    <artifactId>dubbo-demo</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>dubbo-provider</module>
        <module>dubbo-customer</module>
        <module>dubbo-api</module>
    </modules>

    <properties>
        <java.version>1.8</java.version>
        <source.level>1.8</source.level>
        <target.level>1.8</target.level>
        <lombok.version>1.18.16</lombok.version>
        <skip_maven_deploy>true</skip_maven_deploy>
        <spring-boot-dependencies.version>2.4.1</spring-boot-dependencies.version>
        <spring-cloud-dependencies.version>Dalston.SR4</spring-cloud-dependencies.version>
        <junit.version>4.12</junit.version>
        <dubbo.version>3.0.2.1</dubbo.version>
        <spring-dubbo.version>2.0.0</spring-dubbo.version>
        <lombok.version>1.18.16</lombok.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!-- 統(tǒng)一jar版本管理,避免使用 spring-boot-parent -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot-dependencies.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-bom</artifactId>
                <version>${dubbo.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!--dubbo 和  springboot 整合的包-->
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-spring-boot-starter</artifactId>
                <version>${dubbo.version}</version>
            </dependency>

            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
                <scope>compile</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>


</project>

開發(fā)api模塊

user實(shí)體類:

package com.demo.api.entity;


import lombok.Data;

import java.io.Serializable;

/**
 * @Author: laz
 * @CreateTime: 2022-10-26  10:56
 * @Version: 1.0
 */
@Data
public class User implements Serializable {
    

    private Long id;

    private String username;

    private String password;

}

創(chuàng)建本次測(cè)試的接口:

package com.demo.api.service;

import com.demo.api.entity.User;

public interface IUserService {

    User selectUserById(Long id);
}

開發(fā)生產(chǎn)者模塊

第一步:導(dǎo)入依賴

<?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>dubbo-demo</artifactId>
        <groupId>com.demo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo-provider</artifactId>
    <dependencies>
        <dependency>
            <groupId>com.demo</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <!--dubbo 與 spring-boot 整合包-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>
        <!--springboot 啟動(dòng)核心包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!--springboot rest -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-registry-zookeeper</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.1</version>
        </dependency>

        <!--mysql  的驅(qū)動(dòng)-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>


    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.yml</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>



</project>

第二步:添加配置

server:
  port: 8081

spring:
  application:
    name: dubbo-samples-privider-springCloud
    #配置數(shù)據(jù)源信息
  datasource:
    #配置連接數(shù)據(jù)庫的各個(gè)信息
    driver-class-name: com.mysql.cj.jdbc.Driver
    #設(shè)置字符集
    url: jdbc:mysql://8.142.127.37:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true
    username: root
    password: 123456


mybatis-plus:
  #配置類型別名所對(duì)應(yīng)的包
  type-aliases-package: com.demo.provider.entity
  #配置SQL輸出語句com.winsun.dataclean.mapper
  mapper-locations: com/demo/provider/mapper/*.xml


dubbo:
  application:
    name: ${spring.application.name}
  registry:
    address: zookeeper://43.139.86.193:2181
    timeout: 2000
  protocol:
    name: dubbo
    port: 20890
  # 掃描 @DubboService 注解
  scan:
    base-packages: com.demo.provider.service.impl

第三步:編寫啟動(dòng)類

package com.demo.provider;

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

/**
 * @Author: laz
 * @CreateTime: 2022-10-26  11:05
 * @Version: 1.0
 */
@EnableDubbo
@SpringBootApplication
@MapperScan("com.demo.provider.mapper")
public class ProviderApp {

    public static void main(String[] args) {
        SpringApplication.run(ProviderApp.class,args);
        System.out.println("生產(chǎn)者啟動(dòng)完畢");
    }
}

第四步:添加mapper接口

package com.demo.provider.mapper;

import com.demo.api.entity.User;

/**
 * @Author: laz
 * @CreateTime: 2022-10-26  11:01
 * @Version: 1.0
 */
public interface UserMapper {

    User selectUserById(Long id);
}

xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.demo.provider.mapper.UserMapper">
    <select id="selectUserById" resultType="com.demo.api.entity.User">
        select * from user where id = #{id}
    </select>
</mapper>

第五步:實(shí)現(xiàn)接口:

package com.demo.provider.service.impl;

import com.demo.api.entity.User;
import com.demo.api.service.IUserService;
import com.demo.provider.mapper.UserMapper;
import lombok.AllArgsConstructor;
import org.apache.dubbo.config.annotation.DubboService;

/**
 * @Author: laz
 * @CreateTime: 2022-10-26  11:00
 * @Version: 1.0
 */
@DubboService
@AllArgsConstructor
public class UserServiceImpl implements IUserService {

    private final UserMapper userMapper;

    public User selectUserById(Long id) {
        User user = userMapper.selectUserById(id);
        return user;
    }
}

第六步:編寫controller層接口

 package com.demo.provider.controller;

import com.demo.api.entity.User;
import com.demo.api.service.IUserService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: laz
 * @CreateTime: 2022-10-26  11:53
 * @Version: 1.0
 */
@RestController
@RequestMapping("/provider")
@AllArgsConstructor
public class UserController {

    private final IUserService userService;

    @RequestMapping("/selectUserById/{id}")
    public User selectUserById(@PathVariable("id")Long id){
        return userService.selectUserById(id);
    }
}

開發(fā)消費(fèi)者模塊

第一步:導(dǎo)入依賴

<?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>dubbo-demo</artifactId>
        <groupId>com.demo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo-customer</artifactId>
    <dependencies>
        <!--dubbo-samples-springcloud-api 項(xiàng)目 依賴-->
        <dependency>
            <groupId>com.demo</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-registry-zookeeper</artifactId>
        </dependency>
    </dependencies>

</project>

第二步:添加配置

server:
  port: 8082

spring:
  application:
    name: dubbo-samples-consumer-springCloud

dubbo:
  registry:
    address: zookeeper://43.139.86.193:2181
    timeout: 2000
  protocol:
    name: dubbo

第三步:編寫啟動(dòng)類:

package com.demo.customer;

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

@SpringBootApplication
@EnableDubbo
public class ConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
        System.out.println("消費(fèi)者啟動(dòng)完畢!");
    }
}

第四步:編寫調(diào)用生產(chǎn)者接口

package com.demo.customer.controller;

import com.demo.api.entity.User;
import com.demo.api.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/consumer")
@Slf4j
public class ConsumerUserController {

    @DubboReference( protocol = "dubbo", loadbalance = "random")
    private IUserService userService;

    @RequestMapping("/selectUserById/{id}")
    public User getUser(@PathVariable("id") Long id) {
        User user = userService.selectUserById(id);
        log.info("response from provider: {}", user);
        return user;
    }
}

整個(gè)項(xiàng)目結(jié)構(gòu)如下:

測(cè)試

分別啟動(dòng)生產(chǎn)者和消費(fèi)者,在瀏覽器分別調(diào)用以下接口:

http://localhost:8081/provider/selectUserById/1
http://localhost:8082/consumer/selectUserById/1

結(jié)果:

到此這篇關(guān)于Springboot整合dubbo搭建微服務(wù)的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)Springboot dubbo微服務(wù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

最新評(píng)論