SpringBoot结合Dubbo+Zokeeper实现服务调用

一、依赖

	<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
    </parent>
    
	<dependency>
     	<groupId>com.alibaba.boot</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>0.2.0</version>
    </dependency>


    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.16</version>
        <scope>compile</scope>
    </dependency>

二、Server服务端

在这里插入图片描述

2.1 启动类

@EnableDubbo开启基于注解的Dubbo功能

package com.lsh;

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

/**
 * @author :LiuShihao
 * @date :Created in 2021/3/8 10:26 上午
 * @desc :
 */
//开启基于注解的Dubbo功能
@EnableDubbo
@SpringBootApplication
public class DubboServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(DubboServerApplication.class);
    }
}

2.2 yml配置文件

server:
  port: 8081

spring:
  application:
    name: Dubbo_server
  datasource:
    url: jdbc:mysql://localhost:3306/day27?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2b8
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
    hikari:
      connection-timeout: 60000
      maximum-pool-size: 5
  jpa:
    show-sql: true
dubbo:
  application:
    #服务应用名称
    name: provider-8081
  protocol:
    name: dubbo
    #手动配置服务端口
    port: 20880
    #表明这是一个server
    status: server
    #扫描哪些包下的服务要被注册
  scan:
    base-packages: com.lsh.service
  # 连接监控中心
  monitor:
    protocol: registry
# 2.6写法 ubbo.registry.address:  zookeeper://192.168.103.10:2181   使用com.alibaba.boot的包也需要使用这个写法
# 2:7的配置 dubbo.config.center: 推荐用法,填写配置中心的地址,并且在配置中心的相应目录下配置注册中心和元数据中心的地址:
#  config-center:
#    address: zookeeper://192.168.103.10:2181
  registry:
    address: zookeeper://192.168.103.10:2181

2.3 Controller

@RestController
public class ProviderController {
    @Autowired
    UserService userService;

    @GetMapping("/findAll")
    public List<User> findAll(){
        return  userService.findAll();
    }
}

2.4 Server

public interface UserService {

    List<User> findAll();
}

2.5 ServerImpl

package com.lsh.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.lsh.entity.User;
import com.lsh.repository.UserRepository;
import com.lsh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author :LiuShihao
 * @date :Created in 2021/3/8 9:23 上午
 * @desc :
 * @Service注解需要使用com.alibaba.dubbo.config.annotation.Service;包下的注解
 *
 */
//注意!这里的@Service注解是dubbo包下的,不是Spring包下的,千万不要导错,这里是将此服务放到dubbo服务运行容器
@Service
@Component("UserService")
public class UserServiceImpl implements UserService {
    @Autowired
    UserRepository userRepository;

    @Override
    public List<User> findAll() {
        return userRepository.findAll();
    }
}

三、Client调用端

在这里插入图片描述

3.1 启动类

启动类同上

3.2 yml配置文件

server:
  port: 8082

spring:
  application:
    name: Dubbo_client
  datasource:
    url: jdbc:mysql://localhost:3306/day27?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2b8
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
    hikari:
      connection-timeout: 60000
      maximum-pool-size: 5
  jpa:
    show-sql: true
dubbo:
  application:
    #指定当前服务/应用的名字(不要和别的服务名字相同)
    name: consumer-8082
    #指定注册中心的地址
#    registry:
#      address: zookeeper://192.168.103.10:2181
  protocol:
    # 指定通信规则(通信协议、通信端口)
    name: dubbo
    port: 20880
  # 包扫描
  # 指定监控中心
  monitor:
    protocol: monitor
#  config-center:
#    address: zookeeper://192.168.103.10:2181
  scan:
    base-packages: com.lsh.service
  registry:
    address: zookeeper://192.168.103.10:2181

3.3 Controller

package com.lsh.controller;

import com.lsh.entity.User;
import com.lsh.service.ClientServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author :LiuShihao
 * @date :Created in 2021/3/8 1:35 下午
 * @desc :服务调用端
 */
@RestController
public class UserController {
    @Autowired
    ClientServer clientServer;

    @GetMapping("/findAll")
    public List<User> findAll(){
        return  clientServer.findAll();
    }
}

3.4 Server

ClientServer

package com.lsh.service;

import com.lsh.entity.User;

import java.util.List;

/**
 * @author :LiuShihao
 * @date :Created in 2021/3/8 1:51 下午
 * @desc :
 */

public interface ClientServer {
    List<User> findAll();
}

UserServer

package com.lsh.service;

import com.lsh.entity.User;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author :LiuShihao
 * @date :Created in 2021/3/8 9:20 上午
 * @desc : 服务端像消费端提供接口,因此我们需要编写与服务端包名,接口名完全相同的接口
 */
@Component("UserService")
public interface UserService {

    List<User> findAll();

}

3.5 ServerImpl

package com.lsh.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.lsh.entity.User;
import com.lsh.service.ClientServer;
import com.lsh.service.UserService;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author :LiuShihao
 * @date :Created in 2021/3/8 1:51 下午
 * @desc :@Reference 是com.alibaba.dubbo.config.annotation.Reference包下的注解
 */
@Component("ClientServer")
public class ClientServerImpl implements ClientServer {
    //这个注解是dubbo下的,这里的userService是在zookeeper注册中心取到的
    @Reference
    UserService userService;

    @Override
    public List<User> findAll() {
        return userService.findAll();
    }
}

四、测试

启动服务端和调用端,调用调用端ClientfindAll接口,有返回数据就说明服务调用成功。

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

总结

@Service注解暴露服务
@Reference注解引用服务,声明需要调用的远程服务的接口,生成远程服务代理

注意:这两个注解都是Dubbo包下的

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Spring Boot是一个开源的Java框架,用于快速构建独立的、基于Spring的应用程序。它简化了Spring应用程序的配置和部署过程,并提供了许多嵌入式服务器,如Tomcat、Jetty等。Spring Boot还提供了自动配置的特性,可以根据类路径中的依赖自动配置Spring应用程序。 Dubbo是一款高性能的分布式服务框架,也是阿里巴巴开源的项目。它提供了服务治理、远程通讯和分布式调用等功能,帮助开发人员轻松构建分布式服务化的应用。 Zookeeper是一个开源的分布式协调服务,可以用于实现分布式应用程序的一致性和可靠性。它提供了一个类似于文件系统的层次化的命名空间,并允许开发人员注册、协调和共享各种资源,如配置信息、服务注册和发现等。 当使用Spring Boot结合DubboZookeeper时,可以构建一个高性能、可扩展和可靠的微服务架构。Spring Boot提供了便利的开发和部署方式,Dubbo提供了分布式服务框架的支持,而Zookeeper则提供了分布式协调服务。开发人员可以使用Spring Boot快速构建独立的微服务应用程序,使用Dubbo进行服务间的通信和管理,同时通过Zookeeper进行服务的注册和发现。这样的架构可以方便地实现服务架构中的资源共享和服务治理等功能,大大简化了开发人员的负担。 综上所述,Spring Boot结合DubboZookeeper可以构建高效、可靠的微服务架构,并提供了便利的开发和部署方式,帮助开发人员构建高性能的分布式应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Liu_Shihao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值