美文网首页
spring boot 整合 dubbo

spring boot 整合 dubbo

作者: 侧耳倾听y | 来源:发表于2021-06-20 19:07 被阅读0次

项目结构

  • 父pom
<?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>
    <packaging>pom</packaging>
    <modules>
        <module>back-A</module>
        <module>bank-B</module>
        <module>common</module>
    </modules>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>org.example</groupId>
    <artifactId>dubbo-hmily-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dubbo-hmily-demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <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.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.6.3</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>2.12.0</version>
        </dependency>

    </dependencies>

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

</project>

common

  • service
package org.example.common.service;

public interface BankATransferService {

    void transfer();
}
package org.example.common.service;

public interface BankBTransferService {

    void transfer();
}

bankA

  • 配置文件
dubbo.application.name=bank-a
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=20881
dubbo.scan.base-packages=org.example.backa.service.BankATransferService
server.port=8081
  • service
package org.example.banka.service.impl;


import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.example.common.service.BankATransferService;

@Service
@Slf4j
public class BankATransferServiceImpl implements BankATransferService {

    @Override
    public void transfer() {
        log.info("...  A transfer");
    }
}
  • controller
package org.example.banka.controller;

import org.apache.dubbo.config.annotation.Reference;
import org.example.common.service.BankBTransferService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/bank/a")
public class ATransferController {

    @Reference(lazy = true)
    private BankBTransferService bankBTransferService;

    @GetMapping("/transfer")
    public String transfer(){
        bankBTransferService.transfer();
        return "success";
    }
}

bankB

  • 配置文件
dubbo.application.name=bank-b
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
dubbo.scan.base-packages=org.example.backb.service.BankATransferService
dubbo.consumer.check=false
server.port=8082
  • service
package org.example.bankb.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.example.common.service.BankBTransferService;

@Service
@Slf4j
public class BankBTransferServiceImpl implements BankBTransferService {

    @Override
    public void transfer() {
        log.info("... B transfer");
    }
}
  • controller
package org.example.bankb.controller;

import org.apache.dubbo.config.annotation.Reference;
import org.example.common.service.BankATransferService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/bank/b")
public class BTransferController {

    @Reference(lazy = true)
    private BankATransferService bankATransferService;

    @GetMapping("/transfer")
    public String transfer(){
        bankATransferService.transfer();
        return "success";
    }
}

相关文章

网友评论

      本文标题:spring boot 整合 dubbo

      本文链接:https://www.haomeiwen.com/subject/zopsyltx.html