Springboot整合Apache Dubbo

Springboot整合Apache Dubbo

一、 通过Docker安装Zookeeper

docker-composeyaml文件

version: '3'
services:
  zookeeper:
    image: zookeeper:3.6.1
    container_name: zookeeper
    restart: always
    ports:
      - 2181:2181
    volumes:
      - ./zookeeper/data:/data
      - ./zookeeper/log:/datalog
    networks:
      - zk-esnet     
networks:
  zk-esnet:
    driver: bridge

启动执行:

docker-compose up -d # 后台启动

二、整合springbootApache Dubbo

2.1、创建一个maven项目

项目结果如图:
Springboot整合Apache Dubbo_第1张图片

2.2、引入依赖

root模块的pom文件


<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.0modelVersion>

    
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.3.2.RELEASEversion>
    parent>

    <groupId>com.codegroupId>
    <artifactId>code-oneartifactId>
    <packaging>pompackaging>
    <version>1.0.0version>
   
    
    <modules>
        <module>dubbo-providermodule>
        <module>dubbo-consumermodule>
    modules>

    <dependencies>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>org.apache.dubbogroupId>
            <artifactId>dubbo-spring-boot-starterartifactId>
            <version>2.7.8version>
        dependency>
        
        <dependency>
            <groupId>org.apache.dubbogroupId>
            <artifactId>dubbo-dependencies-zookeeperartifactId>
            <version>2.7.8version>
            <type>pomtype>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>
project>

服务提供者模块的pom文件


<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>code-oneartifactId>
        <groupId>com.codegroupId>
        <version>1.0.0version>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>dubbo-providerartifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
        dependency>
        <dependency>
            <groupId>org.apache.commonsgroupId>
            <artifactId>commons-lang3artifactId>
        dependency>
    dependencies>
project>

服务消费者模块的pom文件


<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>code-oneartifactId>
        <groupId>com.codegroupId>
        <version>1.0.0version>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>dubbo-consumerartifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>com.codegroupId>
            <artifactId>dubbo-providerartifactId>
            <version>1.0.0version>
        dependency>
        <dependency>
            <groupId>org.apache.commonsgroupId>
            <artifactId>commons-lang3artifactId>
        dependency>
    dependencies>
project>
2.3、添加服务提供者相关代码

添加一个实体类:(必须实现序列化接口)

package com.dubbo.entity;

import java.io.Serializable;

/**
 * @author 墨龙吟之天问
 */
public class User implements Serializable {
     

    private static final long serialVersionUID = 565515108558415476L;

    private Long id;

    private String username;

    private String password;

    private Integer age;

	// ... set / get ...

    @Override
    public String toString() {
     
        return "User[" +
                "id:" + id +
                ", username:'" + username + '\'' +
                ", password:'" + password + '\'' +
                ", age:" + age +
                ']';
    }
}

添加service接口和实现

import com.dubbo.entity.User;
import java.util.List;

/**
 * @author 墨龙吟之天问
 */
public interface UserService {
     
    /**
     * 查询列表
     * @return
     */
    List<User> queryLists();
}


import org.apache.commons.lang3.RandomStringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import com.dubbo.entity.User;
import com.dubbo.service.UserService;
import java.util.ArrayList;
import java.util.List;

/**
 * UserService的实现类
 * @author 墨龙吟之天问
 */
@DubboService(version = "${dubbo.service.version}")
public class UserServiceImpl implements UserService {
     
    @Override
    public List<User> queryLists() {
     
        // 构造一手假数据
        List<User> users = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
     
            User user = new User();
            user.setAge(i * 10);
            user.setId((long) i);
            user.setPassword(RandomStringUtils.randomAlphanumeric(10));
            user.setUsername(RandomStringUtils.randomAlphabetic(5));
            users.add(user);
        }
        return users;
    }
}

添加启动类和配置文件

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author 墨龙吟之天问
 */
@SpringBootApplication
public class ProviderApplication {
     

    public static void main(String[] args) {
     
        SpringApplication.run(ProviderApplication.class);
    }
}
server:
  port: 7777
spring:
  application:
    name: dubbo-provider-service
dubbo:
  service:
    version: 1.0.0
  application:
    name: dubbo-provider-service
  scan:
    base-packages: com.dubbo.service.impl
  # 注册中心的地址  
  registry:
    address: zookeeper://192.168.31.25:2181
  protocol:
    name: dubbo
    port: 20880
2.4、添加服务消费者者相关代码

编写一个controller来调用远程service

import com.dubbo.entity.User;
import com.dubbo.service.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 墨龙吟之天问
 */
@RestController
public class IndexController {
     

    @DubboReference(version = "${dubbo.service.version}", loadbalance = "roundrobin")
    private UserService userService;


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

}

编写配置文件将服务提供者的服务注册到zookeeper中,然后写一个配置文件

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author 墨龙吟之天问
 */
@SpringBootApplication
public class ConsumerApplication {
     

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

}
server:
  port: 7778
spring:
  application:
    name: dubbo-consumer-service
dubbo:
  service:
    version: 1.0.0
  application:
    name: dubbo-consumer-service
  registry:
    address: zookeeper://192.168.31.25:2181
2.5、启动服务

将IDEA的仪表板(dashboard)启动,这个可以方便我们去统一管理服务。
Springboot整合Apache Dubbo_第2张图片
访问接口:localhost:7778/user

[
    {
     "id":0,"username":"oBhxn","password":"5VIogUrCG3","age":0},		{
     "id":1,"username":"DqYti","password":"XDShtZrszU","age":10},		{
     "id":2,"username":"FvRTn","password":"S7hug0oRWz","age":20},		{
     "id":3,"username":"BMVRN","password":"apwhk8llZw","age":30},		{
     "id":4,"username":"vHnWF","password":"14fp8mWbs8","age":40},		{
     "id":5,"username":"SNHkl","password":"hA5J0509YY","age":50},		{
     "id":6,"username":"yOAtC","password":"NQo50bo9mq","age":60},		{
     "id":7,"username":"QrgpW","password":"S49VOeOrk3","age":70},		{
     "id":8,"username":"zJSXK","password":"rR31TAuXJA","age":80},		{
     "id":9,"username":"TwQMM","password":"3Y3c0JlcpJ","age":90}
]

三、zookeeperdubbo一些工具

3.1、zookeeper监控工具:ZooInspector

下载地址:https://issues.apache.org/jira/secure/attachment/12436620/ZooInspector.zip

解压之后的build目录下有一个start.bat的脚本,双击启动。

Springboot整合Apache Dubbo_第3张图片

连接之后就可以看到我们注册的服务了

Springboot整合Apache Dubbo_第4张图片

这个版本比较旧,新版本的下载地址就在后面连接里面。

链接:https://pan.baidu.com/s/1jk5a1Wv_PPkQaJ8rmZERog
提取码:kqh4

3.2、Dubbo监控工具dubbo-admin

这个工具我们依然在docker中启动,我们在上面那个docker-compose.yml文件中增加一个apache-dubbo-admin的容器。

version: '3'
services:
  zookeeper:
    image: zookeeper:3.6.1
    container_name: zookeeper
    restart: always
    ports:
      - 2181:2181
    volumes:
      - ./zookeeper/data:/data
      - ./zookeeper/log:/datalog
    networks:
      - zk-esnet  
  dubbo-admin:
    container_name: dubbo-admin
    image: apache/dubbo-admin
    links:
      - zookeeper:zookeeper
    depends_on:
      - zookeeper
    ports:
      - "8080:8080"
    environment:
      - admin.registry.address=zookeeper://zookeeper:2181
      - admin.config-center=zookeeper://zookeeper:2181
      - admin.metadata-report.address=zookeeper://zookeeper:2181
    restart: always
    networks:
      - zk-net 
networks:
  zk-esnet:
    driver: bridge

启动之后访问:IP:8080就可以进入管理页面
Springboot整合Apache Dubbo_第5张图片

你可能感兴趣的:(SpringBoot,dubbo,zookeeper,dubbo,dubbo-admin,docker-compose)