zoukankan      html  css  js  c++  java
  • 使用Spring-tool-suit搭建SpringBoot+Zookeeper+dubbo(Windows)

    我的整个搭建用了三个服务器

    Zookeeper在10.160.192.164

    生产者在10.160.196.0

    消费者在10.121.252.209

    一、安装Zookeeper

    在这里下载https://zookeeper.apache.org/releases.html

    我下载的是3.6.0版本。apache-zookeeper-3.6.0-bin.tar.gz

    解压出来是apache-zookeeper-3.6.0-bin

    修改配置文件:

    在apache-zookeeper-3.6.0-binconf下面有zoo_sample.cfg,把这个文件做一个备份,然后改名为zoo.cfg.因为zookeeper启动后,只认识zoo.cfg的所有设置和配置。

    在zoo.cfg中输入:

    # The number of milliseconds of each tick
    tickTime=2000
    # The number of ticks that the initial 
    # synchronization phase can take
    initLimit=10
    # The number of ticks that can pass between 
    # sending a request and getting an acknowledgement
    syncLimit=5
    # the directory where the snapshot is stored.
    # do not use /tmp for storage, /tmp here is just 
    # example sakes.
    dataDir=D:zookeeper3.6.0data
    dataLogDir=D:zookeeper3.6.0log
    # the port at which the clients will connect
    clientPort=2181
    # the maximum number of client connections.
    # increase this if you need to handle more clients
    #maxClientCnxns=60
    #
    # Be sure to read the maintenance section of the 
    # administrator guide before turning on autopurge.
    #
    # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
    #
    # The number of snapshots to retain in dataDir
    #autopurge.snapRetainCount=3
    # Purge task interval in hours
    # Set to "0" to disable auto purge feature
    #autopurge.purgeInterval=1
    
    ## Metrics Providers
    #
    # https://prometheus.io Metrics Exporter
    #metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
    #metricsProvider.httpPort=7000
    #metricsProvider.exportJvmInfo=true

    主要是配置了dataDir的路径和dataLogDir的路径。

     然后在D盘创建这些文件夹

    D:zookeeper3.6.0data
    D:zookeeper3.6.0log

    默认不创建会自动创建,自己创建以免万一。

    使用CMD,进入apache-zookeeper-3.6.0-bin文件夹,运行zkServer.cmd,启动服务,如下图所示(port:2181是zookeeper的专用监听端口)

    创建项目生产者:
    项目结构:

    创建流程:
    File->new->other->SpringBoot->Spring Starter Project,Next
    填写相应的信息:Next

     选择依赖web,Finish

     4、pom.xml,注意:dubbo内部强制要用log4j,所以此处加上log4j的依赖

    <?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>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.2.6.RELEASE</version>
            <relativePath /> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.hzg.boot</groupId>
        <artifactId>springboot-dubbo-server</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>springboot-dubbo-server</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
            <dubbo-spring-boot>1.0.0</dubbo-spring-boot>
        </properties>
    
        <dependencies>
            <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>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
    
            <!-- 注意:dubbo强制内置log4J 如果没有则会报错 -->
            <!-- https://mvnrepository.com/artifact/log4j/log4j -->
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.17</version>
            </dependency>
    
            <!-- Spring Boot Dubbo 依赖 -->
            <dependency>
                <groupId>io.dubbo.springboot</groupId>
                <artifactId>spring-boot-starter-dubbo</artifactId>
                <version>${dubbo-spring-boot}</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>

     application.properties

    spring.dubbo.application.name=provider
    spring.dubbo.registry.address=zookeeper://10.160.192.164:2181
    spring.dubbo.protocol.name=dubbo
    spring.dubbo.protocol.port=20880
    spring.dubbo.scan=com.hzg.boot.server.dubbo

    创建这些相关类:

     Employee.java

    package com.hzg.boot.server.entity;
    
    import java.io.Serializable;
    
    public class Employee implements Serializable{
        private static final long serialVersionUID = 1L;
        private Integer id;
        private String employee_name;
        private String employee_age;
        public Integer getId() {
            return id;
        }
        public void setId(Integer id) {
            this.id = id;
        }
        public String getEmployee_name() {
            return employee_name;
        }
        public void setEmployee_name(String employee_name) {
            this.employee_name = employee_name;
        }
        public String getEmployee_age() {
            return employee_age;
        }
        public void setEmployee_age(String employee_age) {
            this.employee_age = employee_age;
        }
        @Override
        public String toString() {
            return "Employee [id=" + id + ", employee_name=" + employee_name + ", employee_age=" + employee_age + "]";
        }
        public Employee(Integer id, String employee_name, String employee_age) {
            super();
            this.id = id;
            this.employee_name = employee_name;
            this.employee_age = employee_age;
        }
        
        
    
    }

    EmployeeService.java

    package com.hzg.boot.server.dubbo;
    
    import com.hzg.boot.server.entity.Employee;
    
    public interface EmployeeService {
        
        Employee queryEmployeeByName(String employeeName);
    
    }

    EmployeeServiceImpl.java

    package com.hzg.boot.server.dubbo.impl;
    
    import com.alibaba.dubbo.config.annotation.Service;
    import com.hzg.boot.server.dubbo.EmployeeService;
    import com.hzg.boot.server.entity.Employee;
    
    @Service(version="1.0.0")
    public class EmployeeServiceImpl implements EmployeeService{
    
        @Override
        public Employee queryEmployeeByName(String employeeName) {
            // TODO Auto-generated method stub
            return new Employee(1,"上海","大城市");
        }
    
    }

    7、打包项目并运行,右击项目-Run As-Maven clean,成功之后再Run As-Maven build-Goals中输入compile

    启动SpringbootDubboServerApplication

    搭建消费者:

    项目结构:

     添加相关的类:

    application.properties

    #防止服务消费者和提供者端口冲突(一致)
    server.port=8888
    ## Dubbo 消费者  
    spring.dubbo.application.name=consumer
    spring.dubbo.registry.address=zookeeper://10.160.192.164:2181
    spring.dubbo.scan=com.hzg.boot.server.dubbo

    EmployeeService

    package com.hzg.boot.server.dubbo;
    
    import com.hzg.boot.server.entity.Employee;
    
    public interface EmployeeService {
        Employee queryEmployeeByName(String employeeName);
    }

    EmployeeConsumerService 

    package com.hzg.boot.server.dubbo;
    
    import org.springframework.stereotype.Component;
    
    import com.alibaba.dubbo.config.annotation.Reference;
    import com.hzg.boot.server.entity.Employee;
    
    @Component
    public class EmployeeConsumerService {
        
        @Reference(version="1.0.0")
        EmployeeService employeeService;
    
        public EmployeeService getEmployeeService() {
            return employeeService;
        }
    
        public void setEmployeeService(EmployeeService employeeService) {
            this.employeeService = employeeService;
        }
        
        public void printEmployee() {
            String employeeName="上海";
            if(employeeService == null) {
                System.out.println("employeeService服务对象为空");
            }else {
                Employee employee = employeeService.queryEmployeeByName(employeeName);
                System.out.println(employee.toString());
            }
        }
    
    }

    Employee

    package com.hzg.boot.server.dubbo;
    
    import org.springframework.stereotype.Component;
    
    import com.alibaba.dubbo.config.annotation.Reference;
    import com.hzg.boot.server.entity.Employee;
    
    @Component
    public class EmployeeConsumerService {
        
        @Reference(version="1.0.0")
        EmployeeService employeeService;
    
        public EmployeeService getEmployeeService() {
            return employeeService;
        }
    
        public void setEmployeeService(EmployeeService employeeService) {
            this.employeeService = employeeService;
        }
        
        public void printEmployee() {
            String employeeName="上海";
            if(employeeService == null) {
                System.out.println("employeeService服务对象为空");
            }else {
                Employee employee = employeeService.queryEmployeeByName(employeeName);
                System.out.println(employee.toString());
            }
        }
    
    }

    在SpringbootDubboClientApplication中添加代码:

    package com.hzg.boot.server;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.ConfigurableApplicationContext;
    
    import com.hzg.boot.server.dubbo.EmployeeConsumerService;
    
    //import com.hzg.boot.client.dubbo.EmployeeConsumerService;
    
    
    @SpringBootApplication
    public class SpringbootDubboClientApplication {
    
        public static void main(String[] args) {
            ConfigurableApplicationContext run =  SpringApplication.run(SpringbootDubboClientApplication.class, args);
            EmployeeConsumerService employeeService = run.getBean(EmployeeConsumerService.class);
            System.out.println(employeeService);
            employeeService.printEmployee();
        }
    
    }

    6、启动项目,和Server一致,注意先启动生产者,再启动消费者

    控制台会打印相关的信息:

  • 相关阅读:
    .Net常识 值类型和引用类型
    .net 开发人员的瓶颈和职业发展
    PPT Garbage Collection in .Net (内存管理)
    Why Sessionless Web Application ?
    一些.Net面试题 (BS 方向)
    开源一个小类库, 用于对象间灵活的拷贝属性,还有IDataReader到实体类的转换
    在 Visual Studio 单元测试中使用CallContext 导致的 Unit Test Adapter threw exception: Type is not resolved for member... 异常
    【设计原则和建议】 类
    轻量级 Lock Free 线程安全的 Queue<T> 的C#2.0实现
    实验一 命令解释程序的编写
  • 原文地址:https://www.cnblogs.com/LoganChen/p/12762252.html
Copyright © 2011-2022 走看看