dubbo+zookeeper

zookeeper   默认端口号  2181

公共项目

public interface UserService {

    public User queryUser(Integer id);
}

提供者   pom.xml   ,普通的maven项目,不是web项目

<dependencies>
    <!--加入公共项目的依赖,在springboot-interface-api模块中-->
    <dependency>
        <groupId>org.example</groupId>
        <artifactId>springboot-interface-api</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <!--dubbo依赖-->
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>3.0.5</version>
    </dependency>
    <!-- zookeeper-->
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-dependencies-zookeeper</artifactId>
        <version>3.0.4</version>
        <type>pom</type>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>

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

提供者 service

@DubboService(interfaceClass = UserService.class,version = "1.0")
public class UserServiceImpl implements UserService {
    @Override
    public User queryUser(Integer id) {
        User user = new User();
        if(id == 1001){
            user.setId(1001);
            user.setName("张三");
            user.setPwd("123");
        }else if (id ==1002){
            user.setId(1002);
            user.setName("李四");
            user.setPwd("1234");
        }

        return user;
    }
}

提供者  启动类

@SpringBootApplication
@EnableDubbo   //启用dubbo
public class SpringbootServiceProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootServiceProviderApplication.class, args);
    }
}

提供者  application.properties

#配置dubbo名字 dubbo:application-name
spring.application.name=userservice-provider

#配置扫描包  @扫描的@DubboService
dubbo.scan.base-packages=com.abc.springbootserviceprovider.service

dubbo.protocol.name=dubbo
dubbo.protocol.port=28001

#注册中心
dubbo.registry.address=zookeeper://localhost:2181

消费者  pom.xml   web项目

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.example</groupId>
        <artifactId>springboot-interface-api</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>3.0.5</version>
    </dependency>

    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-dependencies-zookeeper</artifactId>
        <version>3.0.4</version>
        <type>pom</type>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

消费者   controller

@RestController
public class UserController {
    @DubboReference(interfaceClass = UserService.class,version = "1.0")
    private UserService userService;
    @GetMapping("/getUser")
    public String getUser(Integer id){
        User user = userService.queryUser(id);
        return user.toString();
    }
}

消费者   启动类

@SpringBootApplication
@EnableDubbo
public class SpringbootConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootConsumerApplication.class, args);
    }
}

消费者   application

#指定服务名称
spring.application.name=consumer-application

dubbo.registry.address=zookeeper://localhost:2181
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值