SpringCloud学习笔记023---SpringBoot集成Dubbo_依赖zookeeper实现分布式应用一致性以及远程服务调用

JAVA技术交流QQ群:170933152  

刚开始的时候zookeeper,配置的是本地127.0.0.1:2181,这个是可以连接上的,但是如果是远程的

比如:

172.19.128.67:2181

这个时候就报错了,就是报错:类似于这种错误

192.168.1.102/192.168.1.102:2181. Will not attempt to authenticate using SASL (无法定位登录配置)

网上半天没找到,都不行,后来配置了:
主机ip映射:

C:\Windows\System32\drivers\etc

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
#    127.0.0.1       localhost
#    ::1             localhost
      172.19.128.67     A28150121040057          # zookeeper server

//注意这里主要在客户端上配置,服务器端最好也配置上吧

但是刚配置好的时候,好像不起作用,后来过了一个晚上,第二个,神奇般的好了.奇怪.........

1.首先要在本地搭建一个zookeeper服务器
  zookeeper服务器可以单机,也可以配置集群,这个部分可以参考其他博文
  这里顺便说一下:
  如何启动zookeeper,其实就是:
  a.去官网下载zookeeper
  b.解压到e盘
  c.配置一下配置文件
    我这里的:
    E:\zookeeper-3.5.4-beta\conf\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=/tmp/zookeeper
# 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
#initLimit=5  
#syncLimit=2  
#server.1=192.168.211.1:2888:3888  
#server.2=192.168.211.2:2888:3888
admin.serverPort=2182  //主要是修改了这里,因为zookeeper自带的jeety服务器,占用了默认的8080端口

2.dubbo就是一个服务远程调用的工具,感觉有点像封装了一下webservice
  ejb这类的东西

3.新建springBoot项目:
  这里用maven进行统一管理

4.首先建一个父pom.xml
   注意这几个项目要在子文件夹
   E:\StsWorkSpace\spring-boot-dubbo
   都放在这里
  其实主要用到的就是:
注意主要加入Maven依赖dubbo
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.5.6</version>
</dependency>

5.项目结构
  a.E:\StsWorkSpace\spring-boot-dubbo\dubbo-api
  b.E:\StsWorkSpace\spring-boot-dubbo\dubbo-consumer
  c.E:\StsWorkSpace\spring-boot-dubbo\dubbo-provider
--------------------------------------------------------
6.父pom.xml
   /spring-boot-dubbo/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">
    <modelVersion>4.0.0</modelVersion>
         
    <groupId>io.credream.example</groupId>
    <artifactId>spring-boot-dubbo</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <modules>
        <module>dubbo-provider</module>
        <module>dubbo-consumer</module>
        <module>dubbo-api</module>
    </modules>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.7.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>

        <com.alibaba.dubbo.version>2.5.6</com.alibaba.dubbo.version>
        <fastjson_version>1.2.31</fastjson_version>
        <javassist_version>3.20.0-GA</javassist_version>
        <netty_version>3.2.5.Final</netty_version>
        <zookeeper_version>3.4.9</zookeeper_version>
        <zkclient_version>0.2</zkclient_version>
        <curator_version>2.12.0</curator_version>
        <log4j_version>1.2.16</log4j_version>
        <!-- Log libs -->
        <slf4j_version>1.7.25</slf4j_version>
        <commons-logging>1.2</commons-logging>

        <org_springframework_boot>1.5.7.RELEASE</org_springframework_boot>

    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <version>${org_springframework_boot}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>${org_springframework_boot}</version>
            </dependency>

            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
                <version>${com.alibaba.dubbo.version}</version>
                <exclusions>
                    <exclusion>
                        <groupId>commons-logging</groupId>
                        <artifactId>commons-logging</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.jboass.netty</groupId>
                        <artifactId>netty</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.javassist</groupId>
                        <artifactId>javassist</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>${fastjson_version}</version>
            </dependency>

            <dependency>
                <groupId>org.javassist</groupId>
                <artifactId>javassist</artifactId>
                <version>${javassist_version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
                <version>${zookeeper_version}</version>
                <exclusions>
                    <exclusion>
                        <artifactId>slf4j-log4j12</artifactId>
                        <groupId>org.slf4j</groupId>
                    </exclusion>
                    <exclusion>
                        <artifactId>log4j</artifactId>
                        <groupId>log4j</groupId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>com.101tec</groupId>
                <artifactId>zkclient</artifactId>
                <version>${zkclient_version}</version>
                <exclusions>
                    <exclusion>
                        <artifactId>zookeeper</artifactId>
                        <groupId>org.apache.zookeeper</groupId>
                    </exclusion>
                </exclusions>
            </dependency>

            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j_version}</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>${slf4j_version}</version>
            </dependency>
            <dependency>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
                <version>${commons-logging}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <resources>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*</include>
                </includes>
            </resource>
        </resources>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <skip>true</skip>
                        <testFailureIgnore>true</testFailureIgnore>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-source-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>attach-sources</id>
                            <goals>
                                <goal>jar-no-fork</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

</project>
----------------------------------------------------
7.再看/spring-boot-dubbo/dubbo-api
   新建这个项目,注意这个其实就是个接口,用来给消费者服务调用的,这个跟以前的ejb,webservice挺像的把
   (好久没用,记不太清,感觉很像)定义服务接口
   /dubbo-api/src/main/java/io/credream/dubbo/api/DemoService.java
package io.credream.dubbo.api;

/**
 * 描述: 定义服务接口
 *
 * @author credream
 * @create 2018-07-27 13:20
 **/
public interface DemoService {
    
    String sayHello(String name);
}


-------------------------------------------------------------------
8./dubbo-api/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>spring-boot-dubbo</artifactId>
        <groupId>io.credream.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    
    <modelVersion>4.0.0</modelVersion>
    <artifactId>dubbo-api</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

</project>
------------------------------------------------------------------------------
9.编写服务提供方
项目:dubbo-provider,在服务提供方实现接口
/dubbo-provider/src/main/java/io/credream/dubbo/provider/service/DemoServiceImpl.java
package io.credream.dubbo.provider.service;

import com.alibaba.dubbo.rpc.RpcContext;
import io.credream.dubbo.api.DemoService;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 描述: 提供方实现
 *
 * @author credream
 * @create 2018-07-27 13:22
 **/
@Service("demoService")
public class DemoServiceImpl implements DemoService {

    @Override
    public String sayHello(String name) {
        System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
        return "Hello " + name + ", response form provider: " + RpcContext.getContext().getLocalAddress();
    }
    
}
-------------------------------------------
10.
加载 dubbo 配置
/dubbo-provider/src/main/java/io/credream/dubbo/provider/config/PropertiesConfig.java
package io.credream.dubbo.provider.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource;

/**
 * 描述: 加载配置
 *
 * @author credream
 * @create 2018-07-27 13:26
 **/
@Configuration
@PropertySource("classpath:dubbo.properties")
@ImportResource({"classpath:dubbo/*.xml"})
public class PropertiesConfig {
}

--------------------------------------------------------
11.
在提供方增加暴露服务配置 : <dubbo:service>
dubbo-provider.xml
/dubbo-provider/src/main/resources/dubbo/dubbo-provider.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="io.credream.dubbo.api.DemoService" ref="demoService"/>

</beans>
-----------------------------------------------------
11.1
服务提供方,其他不重要的代码:
/dubbo-provider/src/main/java/io/credream/dubbo/provider/run/Startup.java
package io.credream.dubbo.provider.run;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

/**
 * 描述:启动提供方服务
 *
 * @author credream
 * @create 2018-07-27 11:49
 **/
@SpringBootApplication
@ComponentScan(value = {"io.credream.dubbo"})
public class Startup {

    public static void main(String[] args) {
        SpringApplication.run(Startup.class, args);
    }
}
----------------------------------------
11.2
/dubbo-provider/src/main/resources/application.properties
spring.application.name=dubbo-provider
server.port=8084
server.tomcat.max-threads=1000
server.tomcat.max-connections=2000
---------------------------------------
11.3
/dubbo-provider/src/main/resources/log4j.properties
###set log levels###
log4j.rootLogger=debug, stdout
###\u8F93\u51FA\u5230\u63A7\u5236\u53F0###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d{dd/MM/yy hh:mm:ss:sss z}] %t %5p %c{2}: %m%n
------------------------------------------------------
11.4
/dubbo-provider/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>spring-boot-dubbo</artifactId>
        <groupId>io.credream.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>dubbo-provider</artifactId>
    <packaging>jar</packaging>

    <dependencies>
    <dependency>
            <groupId>io.credream.example</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>${project.parent.version}</version>
        </dependency> 
        <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>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
        </dependency>
        <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                    <testFailureIgnore>true</testFailureIgnore>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>
--------------------------------------------------------------------------
12.
服务消费方
/dubbo-consumer/src/main/java/io/credream/dubbo/consumer/service/ConsumerDemoService.java
项目:dubbo-consumer ,消费消费远程方法
package io.credream.dubbo.consumer.service;

import io.credream.dubbo.api.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 描述: 消费远程方法
 *
 * @author credream
 * @create 2018-07-27 13:22
 **/
@Service("consumerDemoService")
public class ConsumerDemoService {

    @Autowired
    private DemoService demoService;

    public void sayHello(String name) {
        String hello = demoService.sayHello(name); // 执行消费远程方法
        System.out.println(hello); // 显示调用结果
    }

}
------------------------------------------------------------------
13.
加载 dubbo 配置
/dubbo-consumer/src/main/java/io/credream/dubbo/consumer/config/PropertiesConfig.java
package io.credream.dubbo.consumer.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource;

/**
 * 描述:  加载配置
 *
 * @author credream
 * @create 2018-07-27 13:26
 **/
@Configuration
@PropertySource("classpath:dubbo.properties")
@ImportResource({"classpath:dubbo/*.xml"})
public class PropertiesConfig {
}
-----------------------------------------------------------
14.
在消费方增加引用服务配置: <dubbo:reference>
dubbo-consumer.xml
/dubbo-consumer/src/main/resources/dubbo/dubbo-consumer.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 增加引用远程服务配置 可以和本地bean一样使用demoService -->
    <dubbo:reference id="demoService" check="false" interface="io.credream.dubbo.api.DemoService"/>

</beans>
-----------------------------------------------
14.1
其他不重要的代码
/dubbo-consumer/src/main/java/io/credream/dubbo/consumer/run/Startup.java
package io.credream.dubbo.consumer.run;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

/**
 * 描述: 启动服务
 *
 * @author credream
 * @create 2018-07-27 11:49
 **/
@SpringBootApplication
@ComponentScan(value = {"io.credream.dubbo"})
public class Startup {

    public static void main(String[] args) {
        SpringApplication.run(Startup.class, args);
    }
}
----------------------------------------------------------
14.2
/dubbo-consumer/src/main/resources/application.properties
spring.application.name=dubbo-consumer
server.port=8085
server.tomcat.max-threads=1000
server.tomcat.max-connections=2000
-------------------------
14.3
/dubbo-consumer/src/main/resources/log4j.properties
###set log levels###
log4j.rootLogger=debug, stdout
###\u8F93\u51FA\u5230\u63A7\u5236\u53F0###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d{dd/MM/yy hh:mm:ss:sss z}] %t %5p %c{2}: %m%n
-----------------------------------------------------------
14.4
/dubbo-consumer/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>spring-boot-dubbo</artifactId>
        <groupId>io.credream.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>dubbo-consumer</artifactId>
    <packaging>jar</packaging>

    <dependencies>
  <dependency>
            <groupId>io.credream.example</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>${project.parent.version}</version>
        </dependency> 
        <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>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
        </dependency>
        <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                    <testFailureIgnore>true</testFailureIgnore>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>
---------------------------------------------------
15.
远程服务 Dubbo 配置
项目:dubbo-provider ,dubbo-consumer 一样配置
dubbo.xml
/dubbo-consumer/src/main/resources/dubbo/dubbo.xml
/dubbo-provider/src/main/resources/dubbo/dubbo.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="${spring.application.name}"  />

    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <dubbo:registry protocol="zookeeper" address="${zookeeper.connect}"  file="${dubbo.cache}"/>

    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="${dubbo.protocol.port}"  threadpool="${dubbo.protocol.threadpool}"  threads="${dubbo.protocol.threads}"/>

    <!-- 提供方的缺省值,当ProtocolConfig和ServiceConfig某属性没有配置时,采用此缺省值,可选。-->
    <dubbo:provider connections="${dubbo.provider.connections}" timeout="${dubbo.provider.timeout}" retries="${dubbo.provider.retries}" version="${dubbo.provider.version}" />

    <!-- 消费方缺省配置,当ReferenceConfig某属性没有配置时,采用此缺省值,可选。-->
    <dubbo:consumer version="${dubbo.provider.version}" />

    <!-- 监控中心配置,用于配置连接监控中心相关信息,可选。-->
    <dubbo:monitor protocol="registry"/>

</beans>
-------------------------------------------------------------
16.
/dubbo-consumer/src/main/resources/dubbo.properties
/dubbo-provider/src/main/resources/dubbo.properties

#########################################################
# dubbo config
#暴露服务端口
dubbo.protocol.port=20880
#提供方超时时间
dubbo.provider.timeout=10000
#提供方版本
dubbo.provider.version=1.0
#表示该服务使用独的五条条长连
dubbo.provider.connections=5
# 固定大小线程池,启动时建立线程,不关闭,一直持有。(缺省)
dubbo.protocol.threadpool=fixed
# 线程数量
dubbo.protocol.threads=500
#配置重试次数,最好只用于读的重试,写操作可能会引起多次写入  默认retries="0"
dubbo.provider.retries=0
# dubbo缓存文件
dubbo.cache=/data/dubbo/cache/dubbo-consumer
#########################################################

# zookeeper config
#zookeeper.connect=127.0.0.1:2181
zookeeper.connect=172.19.128.67:2181
--------------------------------------------------
17.
测试 Dubbo
该接口需单独打包,在服务提供方和消费方共享 (在这里体现在:提供者,消费者的pom.xml中,引用了api那个项目,这样就可以了)
对服务消费方隐藏实现 (只引入接口就可以了)

也可以使用 IoC 注入 (注入后直接用也行)
-------------------------------------
18.
启动 ZooKeeper
启动服务 这里是windows,自己认为windows比较方便,当然也可以用centos
E:\zookeeper-3.5.4-beta\bin>zkServer

或者用:
E:\zookeeper-3.5.4-beta\bin>zkServer start 也行,不过我用这种方式:
报错:
java.lang.NumberFormatException: For input string: "E:\zookeeper-3.5.4-beta\bin\..\conf\zoo.cfg"
        at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
        at java.lang.Integer.parseInt(Integer.java:580)
        at java.lang.Integer.parseInt(Integer.java:615)
        at org.apache.zookeeper.server.ServerConfig.parse(ServerConfig.java:63)
        at org.apache.zookeeper.server.ZooKeeperServerMain.initializeAndRun(ZooKeeperServerMain.java:103)
        at org.apache.zookeeper.server.ZooKeeperServerMain.main(ZooKeeperServerMain.java:64)
        at org.apache.zookeeper.server.quorum.QuorumPeerMain.initializeAndRun(QuorumPeerMain.java:128)
        at org.apache.zookeeper.server.quorum.QuorumPeerMain.main(QuorumPeerMain.java:82)
2018-07-25 20:27:03,921 [myid:] - INFO  [main:ZooKeeperServerMain@67] - Usage: ZooKeeperServerMain configfile | port datadir [ticktime] [maxcnxns]

这个错误,网上也没有好的解决方案,就是把zkServer start 换成zkServer就好了,不过用
zkServer stop的时候也报这个错误,不过能关上服务
-------------------------------------
19.
启动提供方服务
/dubbo-provider/src/main/java/io/credream/dubbo/provider/run/Startup.java
package io.credream.dubbo.provider.run;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

/**
 * 描述:启动提供方服务
 *
 * @author credream
 * @create 2018-07-27 11:49
 **/
@SpringBootApplication
@ComponentScan(value = {"io.credream.dubbo"})
public class Startup {

    public static void main(String[] args) {
        SpringApplication.run(Startup.class, args);
    }
}
-----------------------------------------------
20.
测试消费远程服务
/dubbo-consumer/src/test/java/io/credream/dubbo/test/ConsumerTest.java
package io.credream.dubbo.test;

import io.credream.dubbo.consumer.run.Startup;
import io.credream.dubbo.consumer.service.ConsumerDemoService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * 描述: 测试消费远程服务
 *
 * @author credream
 * @create 2018-07-27 14:15
 **/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Startup.class)
public class ConsumerTest {

    @Autowired
    private ConsumerDemoService consumerDemoService;

    @Test
    public void sayHello(){
        consumerDemoService.sayHello("Credream Test。。。。");
    }
}
---------------------------------------------------------------
这样就可以直接执行,打印出:
可以在消费者那方看到:
2018-07-27 09:34:07.193  INFO 11136 --- [           main] io.credream.dubbo.provider.run.Startup   : Started Startup in 4.093 seconds (JVM running for 5.684)
[09:34:23] Hello Credream Test。。。。, request from consumer: /172.19.128.41:62666
打印出了,消费信息
-------------------------------------------------------
 

猜你喜欢

转载自blog.csdn.net/lidew521/article/details/81232057