Dubbo实例(SpringBoot+zookeeper集群)

Dubbo实例(SpringBoot+zookeeper集群),第1张

Dubbo实例(SpringBoot+zookeeper集群) 目录结构

主项目工程:dubbo-springboot-project ,maven类型,创建后删除src即可。

接口工程:    interface ,maven类型

服务提供者:provider ,maven类型

消费者工程:consumer ,maven类型

dubbo-springboot-project
pom.xml


    4.0.0
    pom
    
        interface
        provider
        consumer
    
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.0
        
    

    com.heheda.www
    dubbo-springboot-project
    0.0.1
    dubbo-springboot-project
    Demo project for Spring Boot

    
        1.8
        
        2.7.3
    

    
    
        
            
            
                org.apache.dubbo
                dubbo-spring-boot-starter
                ${dubbo.version}
            

            
            
                org.apache.dubbo
                dubbo-dependencies-zookeeper
                ${dubbo.version}
                pom
            
        
    


    
        
            
            
                maven-compiler-plugin
                3.1
                
                    1.8
                    1.8
                
            
        
    


 interface
a.目录结构

b. pom.xml



    4.0.0

    com.heheda.www
    interface
    0.0.1

    
        dubbo-springboot-project
        com.heheda.www
        0.0.1
    

    
        UTF-8
        1.8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.apache.dubbo
            dubbo-spring-boot-starter
        
        
        
            org.apache.dubbo
            dubbo-dependencies-zookeeper
            pom
        
    


c.User.java
package com.heheda.www.model;

import java.io.Serializable;


public class User implements Serializable {

    private Integer id;

    private String username;

    private Integer age;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}
d.UserService.java
package com.heheda.www.service;

import com.heheda.www.model.User;


public interface UserService {

    
    User queryUserById(Integer id);

}
provider 
a.目录结构

b.pom.xml



    4.0.0

    com.heheda.www
    provider
    0.0.1

    
        dubbo-springboot-project
        com.heheda.www
        0.0.1
    

    
        UTF-8
        1.8
        1.8
    

    
        
            com.heheda.www
            interface
            0.0.1
        
    


c.UserServiceImpl.java
package com.heheda.www.service;

import com.heheda.www.model.User;
import org.apache.dubbo.config.annotation.Service;


@Service(version = "1.0.0")
public class UserServiceImpl implements UserService {

    @Override
    public User queryUserById(Integer id) {
        User user = new User();
        user.setId(id);
        user.setAge(11);
        user.setUsername("version-1");
        return user;
    }
}
d.UserServiceImpl2.java
package com.heheda.www.service;

import com.heheda.www.model.User;
import org.apache.dubbo.config.annotation.Service;


@Service(version = "2.0.0")
public class UserServiceImpl2 implements UserService {

    @Override
    public User queryUserById(Integer id) {
        User user = new User();
        user.setId(id);
        user.setAge(22);
        user.setUsername("version-2");
        return user;
    }
}
e.ProviderApplication.java
package com.heheda.www;

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


@SpringBootApplication
public class ProviderApplication {

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

}
f.application.yml
# 服务端口
server:
  port: 8001

# dubbo配置
dubbo:
  application:
    # dubbo中的应用名称,必须唯一
    name: provider
  registry:
    # 注册中心地址
    address: zookeeper://192.168.1.201:2181?backup=192.168.1.202:2181,192.168.1.203:2181
    # 超时时间,单位毫秒
    timeout: 6000
  metadata-report:
    # 元数据中心地址
    address: zookeeper://192.168.1.201:2181?backup=192.168.1.202:2181,192.168.1.203:2181
  protocol:
    # 协议名称
    name: dubbo
    # 协议端口
    port: 20880
  scan:
    # 扫描包的位置
    base-packages: com.heheda.www.service
 consumer
a.目录结构

b.pom.xml



    4.0.0

    com.heheda.www
    consumer
    0.0.1

    
        dubbo-springboot-project
        com.heheda.www
        0.0.1
    

    
        UTF-8
        1.7
        1.7
    

    
        
            com.heheda.www
            interface
            0.0.1
        
    


c.UserController.java
package com.heheda.www.controller;

import com.heheda.www.model.User;
import com.heheda.www.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/user")
public class UserController {

    @Reference(version = "1.0.0")
    private UserService userService;

    @Reference(version = "2.0.0")
    private UserService userService2;

    @GetMapping("/detail")
    public User userDetail(Integer id){
        User user = userService.queryUserById(id);
        return user;
    }

    @GetMapping("/detail2")
    public User userDetail2(Integer id){
        User user = userService2.queryUserById(id);
        return user;
    }

}
d.ConsumerApplication.java
package com.heheda.www;

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


@SpringBootApplication
public class ConsumerApplication {

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

}
e.application.yml
# 服务端口
server:
  port: 8002

# dubbo配置
dubbo:
  application:
    # dubbo中的应用名称,必须唯一
    name: consumer
  registry:
    # 注册中心地址
    address: zookeeper://192.168.1.201:2181?backup=192.168.1.202:2181,192.168.1.203:2181
搭建zookeeper集群
 a.使用VMware虚拟机安装好CentOS7版本的系统

  CentOS7无界面镜像下载地址

  链接:https://pan.baidu.com/s/1UGU9CuXhDLbx3qajNAZDGA 
  提取码:4mi1

b.设置共享文件夹

c.zookeeper文件拷贝到共享文件夹内,安装好jdk环境 d.配置zookeeper

  1.进入共享文件夹,cd /mnt/hgfs/vmware_share/

  2.创建文件夹 mkdir -p /usr/local/zookeeper/

  3.解压zookeeper到文件夹中,tar -zxvf apache-zookeeper-3.5.9-bin -C /usr/local/zookeeper/

  4.cd /usr/local/zookeeper/apache-zookeeper-3.5.9-bin/,创建data文件夹+logs文件夹,mkdir data,mkdir logs

  5.配置zookeeper,cd conf,拷贝 cp zoo_sample.cfg zoo.cfg,编辑 vi zoo.cfg,修改data和log的路径

dataDir=/usr/local/zookeeper/apache-zookeeper-3.5.9-bin/data
logDir=/usr/local/zookeeper/apache-zookeeper-3.5.9-bin/logs

  6.在zoo.cfg中配置集群服务,添加:

server.1=192.168.1.201:2888:3888
server.2=192.168.1.202:2888:3888
server.3=192.168.1.203:2888:3888

  7.在data文件夹中创建文件myid,作为服务集群的id,对应的server.*,vi myid,内容:1,保存即可

  8.修改系统的ip地址为固定的192.168.1.201,先设置虚拟机网络适配器为桥接模式,然后修改文件 sudo vi /etc/sysconfig/network-scripts/ifcfg-ens33 ,根据命令ifconfig 查看自己虚拟机连接的网络,我的是ens33,所以我修改ifcfg-ens33文件,修改内容如下:

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_onLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=4d2ae117-7be8-4b0d-a9c1-3d476501bbf2
DEVICE=ens33
onBOOT=yes
IPADDR=192.168.1.201
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=114.114.114.114
NDS2=192.168.1.1
ZONE=public

e.克隆配置好的CentOS,克隆两份,然后修改对应的zookeeper里面data的myid和虚拟机网络ip

CentOS01,ip地址:192.168.1.201,data/myid 内容:1

CentOS02,ip地址:192.168.1.202,data/myid 内容:2

CentOS03,ip地址:192.168.1.203,data/myid 内容:3

f.启动zookeeper和查看zookeeper状态

启动:bin/zkServer.sh start

关闭:bin/zkServer.sh stop

状态:bin/zkServer.sh status

项目代码 

dubbo-project: 码云-呵呵哒https://gitee.com/hehedas_admin/dubbo-project

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5676974.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存