Dubbo--基于Zookeeper服务与Spring集成

Posted 林宇风

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Dubbo--基于Zookeeper服务与Spring集成相关的知识,希望对你有一定的参考价值。

 

 

搭建dubbo微服务需要使用zookeeper作为服务注册中心,首先要搭建好zookeeper,安装方式参考zookeeper安装教程

1.部署dubbo服务管理中心

下载 dubbo-admin-2.6.0.war, 放在/apache-tomcat-8.5.34/webapps 目录下,启动tomcat服务,浏览器访问:http://192.168.37.220:8080/dubbo-admin-2.6.0/ (修改成自己ip进行访问),用户名root 密码root

 


 

 

2.搭建dubbo服务环境

搭建服务需要3个项目:

  • api项目,定义公共接口
  • dubbo-provider项目,提供服务
  • dubbo-consumer项目,消费服务

可以使用maven创建多模块项目来模拟,不会创建maven多模块,自行度娘...

pom依赖和log4j日志打印配置

2.1 pom.xml 依赖

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>lyfcode</artifactId>
        <groupId>com.lyf</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo-provider</artifactId>

    <name>dubbo-provider</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.lyf</groupId>
            <artifactId>api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!-- spring 核心jar -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>

        <!-- spring 测试jar -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>

        <!-- spring jdbc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>

        <!-- spring事物 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>

        <!-- aspectj切面编程的jar -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.9</version>
        </dependency>

        <!-- 日志打印相关的jar -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.2</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.2</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.3.3</version>
        </dependency>
        <!-- zookeeper client依赖,用于向zookeeper注册服务 -->
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>
        
    </dependencies>

    <build>
    </build>
</project>

2.2 log4j.properties 日志打印

log4j.rootLogger=DEBUG, Console  
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender  
log4j.appender.Console.layout=org.apache.log4j.PatternLayout  
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n  
log4j.logger.java.sql.ResultSet=INFO  
log4j.logger.org.apache=INFO  
log4j.logger.java.sql.Connection=DEBUG  
log4j.logger.java.sql.Statement=DEBUG  
log4j.logger.java.sql.PreparedStatement=DEBUG

3.api模块定义公共接口

package com.lyf.dubbo.service;

public interface DemoService {
    String sayHello(String name);
}

4.dubbo-provider实现接口提供服务

4.1 DemoServiceImpl.java 服务实现

package com.lyf.dubbo.service.service;

import com.lyf.dubbo.service.DemoService;

public class DemoServiceImpl implements DemoService {
 
    public String sayHello(String name) {
        System.out.println("服务接收: "+name);
        return Dubbo实战 Dubbo+Zookeeper+Spring整合应用篇-Dubbo基于Zookeeper实现分布式服务

Dubbo实战 Dubbo+Zookeeper+Spring整合应用篇-Dubbo基于Zookeeper实现分布式服务

Dubbo实战 Dubbo+Zookeeper+Spring整合应用篇-Dubbo基于Zookeeper实现分布式服务

Dubbo(Dubbo与ZookeeperSpringMVC整合)

Dubbo与Zookeeper视频教程(分布式架构+第三方支付)完整版

基于ZooKeeper的Dubbo注册中心