码迷,mamicode.com
首页 > 编程语言 > 详细

使用Spring-tool-suit搭建SpringBoot+Zookeeper+dubbo(Windows)

时间:2020-04-23 20:57:25      阅读:61      评论:0      收藏:0      [点我收藏+]

标签:prot   web   reporting   from   super   添加   div   exporter   clu   

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

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-bin\conf下面有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.0\data
dataLogDir=D:\zookeeper3.6.0\log
# 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.0\data
D:\zookeeper3.6.0\log

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

使用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一致,注意先启动生产者,再启动消费者

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

技术图片

 

 

使用Spring-tool-suit搭建SpringBoot+Zookeeper+dubbo(Windows)

标签:prot   web   reporting   from   super   添加   div   exporter   clu   

原文地址:https://www.cnblogs.com/LoganChen/p/12762252.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!