Dubbo--002--例子程序

 雖然項目中用到了dubbo,仍是想本身玩玩寫個demo。百度一搜,關於dubbo的helloworld基本都是在windows上作的。如今在Linux上玩一下。css

環境準備:html

jdk版本:1.7  前端

服務器:tomcat7java

dubbo版本:2.5.4mysql

註冊中心zookeeper版本:zookeeper-3.4.5.targit

dubbo-admin版本:dubbo-admin-2.5.4.wargithub

系統:CentOS 6.4web

準備三臺虛擬機 我用的centos6.4,具體安裝配置能夠看這裏http://www.javashuo.com/article/p-ygcxkvqh-me.html,作zookeeper集羣,部署管控臺spring

說明:sql

1,JDK版本選擇:從17年開始dubbo又開始更新後的第一個版本是2.5.4,以前版本一直停留在2.5.3,因此如今線上跑的項目不少仍是2.5.3版本的dubbo。如今不少新項目都用的jdk1.8,可是用dubbo2.5.4版本的管控臺,在jdk1.8環境上跑啓動tomcat會報錯(一個URIType類裏的getset方法致使的,緣由是這個set方法寫的不規範,雖然網上也有一些解決辦法,可是仍是換用JDK1.7吧!畢竟線上如今JDK1.7仍是不少的),因此我這裏還用JDK1.7;

2,dubbo版本:爲了和dubbo版本保持一致,因此管控臺也用2.5.4 。最新版本的dubbo源碼項目結構已經跟之前不同了(詳看github),2.5.4版本的管控臺dubbo-admin項目也不存在了,因此要從dubbo-admin的源碼構建出war包也構建不了了(我上傳到了百度網盤一份dubbo-admin-2.5.4.war:https://pan.baidu.com/s/1iUzL2YuDU0I2MBQU5jnG4w  提取碼:b4t1)。

一:安裝Zookeeper集羣

http://www.cnblogs.com/lihaoyang/p/8358153.html

安裝好zookeeper後, zkServer.sh start 啓動每臺zk,zkServer.sh status 查看每一個節點狀態,能夠看到一個leader,兩個follower

  

二:安裝管控臺

把 dubbo-admin-2.5.4.war 上傳到其中的一臺機器,copy到tomcat7的webapps下,啓動tomcat解壓war包,或者 unzip dubbo-admin-2.5.4.war -d dubbo-admin 命令解壓,刪除tomcat的webapps下的ROOT,並把解壓後的管控臺命名爲ROOT這樣訪問的時候就不用輸入項目名了。

進入 /lhy/software/apache-tomcat-7.0.70/webapps/ROOT/WEB-INF  修改dubbo管控臺配置

vim dubbo.properties:用戶名密碼都改成了root,修改zk集羣地址

保存並退出wq! 。

啓動tomcat,關閉每臺機器的防火牆,在windows機器上訪問tomcat機器地址,輸入配置的root/root用戶

能夠看到dubbo管控檯安裝沒問題:

因爲用的是dubbo-2.5.4版本,因此這一步特別注意JDK的版本必定要是JDK1.7 ,若是你是jdk1.8 啓動管控臺會報錯。

三:寫java程序

項目結構:比較接近實際項目結構了

lhy-parent父工程,定義jar包版本。

lhy-facede:定義dubbo接口,爲了省事把 實體類放在了lhy-facade,這樣生產者和消費者都能引用了。現公司的接口層叫xxx-api,實現叫xxx-service,而裏邊的接口和實現類的定義叫UserFacade、UserFacadeImpl。其實接口定義爲XXXFacade比較科學,這樣在消費者項目裏,一看就知道是dubbo接口,而區別於本項目的service。

lhy-service:接口實現

lhy-consumer:消費者

 

 lhy-parent 父項目pom工程:

其中zookeeper註冊中心依賴的jar包是zkClient,官方文檔說了。從2.2.0版本zk註冊中心默認使用zkclient,需依賴

<dependency>
       <groupId>com.github.sgroschupf</groupId>
  <artifactId>zkclient</artifactId>
  <version>0.1</version>
</dependency>

或者從2.3.0版本開始,支持zk註冊中心支持curator實現,需依賴

 

<dependency>
  <groupId>com.netflix.curator</groupId>
  <artifactId>curator>framework</artifactId>
  <version>1.1.10</version>
</dependency>

本例子使用zkClient

 

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>com.lhy</groupId>
    <artifactId>lhy-parent</artifactId>
    <version>1.0</version>
    <packaging>pom</packaging>

    <modules>
        <module>../lhy-facade</module>
        <module>../lhy-service</module>
        <module>../lhy-consumer</module>
    </modules>


    <!-- 集中定義依賴版本號 -->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>

        <junit.version>4.12</junit.version>
        <spring.version>4.1.3.RELEASE</spring.version>
        <mysql.version>5.1.32</mysql.version>
        <slf4j.version>1.6.4</slf4j.version>
        <jackson.version>2.4.2</jackson.version>
        <druid.version>1.0.9</druid.version>
         <httpclient.version>4.3.5</httpclient.version>
        <jstl.version>1.2</jstl.version>
        <servlet-api.version>2.5</servlet-api.version>
        <jsp-api.version>2.0</jsp-api.version>
        <joda-time.version>2.5</joda-time.version>
        <commons-lang3.version>3.3.2</commons-lang3.version>
         <commons-io.version>1.3.2</commons-io.version>
         <commons-net.version>3.3</commons-net.version>
        <pagehelper.version>3.4.2-fix</pagehelper.version>
         <jsqlparser.version>0.9.1</jsqlparser.version>
         <commons-fileupload.version>1.3.1</commons-fileupload.version>
         <jedis.version>2.7.2</jedis.version>
         <solrj.version>4.10.3</solrj.version>
        <shiro.version>1.3.2</shiro.version>
        <commons-logging.version>1.2</commons-logging.version>
        <log4j.version>1.2.17</log4j.version>
        <slf4j-log4j12.version>1.7.5</slf4j-log4j12.version>
        <zkclient.version>0.1</zkclient.version>
        <dubbo.version>2.5.4</dubbo.version>
        <mybatis-plus.version>3.0.5</mybatis-plus.version>
    </properties>

    <!-- 只定義依賴的版本,並不實際依賴 -->
    <dependencyManagement>
        <dependencies>
            <!-- MySql -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
            </dependency>
            <!--mysql end-->

            <!-- 鏈接池 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>${druid.version}</version>
            </dependency>
            <!--鏈接池end-->

            <!-- Spring -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aspects</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <!--spring end-->

            <!-- JSP相關 -->
            <dependency>
                <groupId>jstl</groupId>
                <artifactId>jstl</artifactId>
                <version>${jstl.version}</version>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>servlet-api</artifactId>
                <version>${servlet-api.version}</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jsp-api</artifactId>
                <version>${jsp-api.version}</version>
                <scope>provided</scope>
            </dependency>
            <!--Jsp end-->

            <!-- 文件上傳組件 -->
            <dependency>
                <groupId>commons-fileupload</groupId>
                <artifactId>commons-fileupload</artifactId>
                <version>${commons-fileupload.version}</version>
            </dependency>

            <!-- 工具類 -->
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
                <version>${commons-lang3.version}</version>
            </dependency>

            <!-- Jackson Json處理工具包 -->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>${jackson.version}</version>
            </dependency>


            <!--<dependency>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
                <version>${commons-logging.version}</version>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>${slf4j-log4j12.version}</version>
            </dependency>

            &lt;!&ndash; Jackson Json處理工具包 &ndash;&gt;
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>${jackson.version}</version>
            </dependency>
            &lt;!&ndash;模板語言支持包&ndash;&gt;
            &lt;!&ndash; velocity模板語言支持包 &ndash;&gt;
            <dependency>
                <groupId>org.apache.velocity</groupId>
                <artifactId>velocity</artifactId>
                <version>1.7</version>
            </dependency>
            &lt;!&ndash; velocity-tools &ndash;&gt;
            <dependency>
                <groupId>org.apache.velocity</groupId>
                <artifactId>velocity-tools</artifactId>
                <version>2.0</version>
            </dependency>
            &lt;!&ndash; 不加這個配置volecity視圖解析器報錯 &ndash;&gt;
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>4.3.0.RELEASE</version>
            </dependency>-->

            <!-- 噹噹elastic job -->
            <!--<dependency>
                <groupId>com.dangdang</groupId>
                <artifactId>elastic-job-lite-core</artifactId>
                <version>2.1.5</version>
            </dependency>
            &lt;!&ndash; 使用springframework自定義命名空間時引入 &ndash;&gt;
            <dependency>
                <groupId>com.dangdang</groupId>
                <artifactId>elastic-job-lite-spring</artifactId>
                <version>2.1.5</version>
            </dependency>-->

            <!--mybatis-plus-->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus</artifactId>
                <version>${mybatis-plus.version}</version>
            </dependency>

            <!--dubbo-->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
                <version>${dubbo.version}</version>
                <exclusions>
                    <exclusion>
                        <artifactId>spring</artifactId>
                        <groupId>org.springframework</groupId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>com.github.sgroschupf</groupId>
                <artifactId>zkclient</artifactId>
                <version>${zkclient.version}</version>
            </dependency>

        </dependencies>
    </dependencyManagement>




    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <!-- 資源文件拷貝插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <!-- java編譯插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
        <pluginManagement>
            <plugins>
                <!-- 配置Tomcat插件 -->
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <version>2.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

接口層 lhy-facade

是一個jar包工程,只提供接口,供現實層lhy-service 和 消費者 lhy-consumer 依賴,爲了簡便,把pojo也放這裏了,這樣實現和消費者項目二者都能依賴。

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>


    <packaging>jar</packaging>
    <artifactId>lhy-facade</artifactId>

    <parent>
        <groupId>com.lhy</groupId>
        <version>1.0</version>
        <artifactId>lhy-parent</artifactId>
        <relativePath>../lhy-parent/pom.xml</relativePath>
    </parent>



</project>

User.java:

package com.lhy.service.pojo;

import java.io.Serializable;
//實現序列化接口
public class User implements Serializable{

    private int id;

    private String name;

    private int sex;
   
    //省略getset


}

接口 UserFacade.java 

package com.lhy.service.user;

import com.lhy.service.pojo.User;

import java.util.List;

public interface UserFacade {

    public User getById(int id);

    public int addUser(User user);

    public List<User> getUsers();
}

接口實現層 lhy-service:

war包工程,依賴父工程和接口層。dao層爲了沒有單獨出來,這裏依賴了一個mybatis-plus的mybatis 插件,爲了簡便沒有查庫,可是dao的配置沒有少,爲了完整性、

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>com.lhy</groupId>
  <artifactId>lhy-service</artifactId>
  <version>1.0</version>
  <packaging>war</packaging>

  <name>lhy-service</name>

  <parent>
    <groupId>com.lhy</groupId>
    <version>1.0</version>
    <artifactId>lhy-parent</artifactId>
    <relativePath>../lhy-parent/pom.xml</relativePath>
  </parent>



  <dependencies>

    <!--接口層-->
    <dependency>
      <groupId>com.lhy</groupId>
      <version>1.0</version>
      <artifactId>lhy-facade</artifactId>
    </dependency>


    <!-- Spring -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aspects</artifactId>
    </dependency>

    <!-- JSP相關 -->
    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jsp-api</artifactId>
      <scope>provided</scope>
    </dependency>
    <!-- 文件上傳組件 -->
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
    </dependency>


    <!--數據源相關-->
    <!--鏈接池-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
    </dependency>
    <!-- MySql -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <!--mysql end-->
    <!--mybatis-plus-->
    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus</artifactId>
    </dependency>

    <!--dubbo-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
    </dependency>
    <!--zkClient-->
    <dependency>
      <groupId>com.github.sgroschupf</groupId>
      <artifactId>zkclient</artifactId>
    </dependency>

    <!-- Jackson Json處理工具包 -->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
    </dependency>




  </dependencies>

  <build>

    <resources>
      <resource>
        <directory>src/main/resource</directory>
        <includes>
          <include>**/*.properties</include>
          <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
      </resource>
    </resources>

    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.4</version>
      </plugin>
      <!-- 配置Tomcat插件 -->
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <configuration>
          <!-- 這裏配置端口號和訪問路徑 -->
          <port>8080</port>
          <path>/</path>
        </configuration>
      </plugin>
    </plugins>
  </build>



</project>

web.xml :

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
>
  <display-name>lhy-service</display-name>
  <!-- 加載spring容器 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/spring-context.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!-- 解決post亂碼 -->
  <filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>utf-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <!-- springmvc的前端控制器 -->
  <servlet>
    <servlet-name>lhy-consumer</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- contextConfigLocation不是必須的, 若是不配置contextConfigLocation, springmvc的配置文件默認在:WEB-INF/servlet的name+"-servlet.xml" -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring/springmvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>lhy-consumer</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
 <!-- <error-page>
    <error-code>404</error-code>
    <location>/static/errorPage/404.html</location>
  </error-page>
  <error-page>
    <error-code>403</error-code>
    <location>/static/errorPage/403.html</location>
  </error-page>-->
</web-app>

Spring配置:

spring-context.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:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    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-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">


    <!-- 掃描包加載Service實現類 -->
    <context:component-scan base-package="com.lhy.service.impl"></context:component-scan>

    <!-- 數據庫鏈接池 -->
    <!-- 加載配置文件 -->
    <context:property-placeholder location="classpath:db.properties" />
    <!-- 數據庫鏈接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
        destroy-method="close">
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="maxActive" value="10" />
        <property name="minIdle" value="5" />
    </bean>

    <!-- 配置掃描包,加載mapper代理對象 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.lhy.service.mapper"></property>
    </bean>
    <!--調整 SqlSessionFactory 爲 MyBatis-Plus 的 SqlSessionFactory-->
    <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
    </bean>




    <!-- 定義事務 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!-- 配置 Annotation 驅動,掃描@Transactional註解的類定義事務  -->
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>


    <!--dubbo配置-->
    <import resource="dubbo-provider.xml"/>

</beans>

springmvc.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

    <context:component-scan base-package="com.lhy.service.controller" />
    <mvc:annotation-driven />
    
    
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/page/" />
        <property name="suffix" value=".jsp" />
    </bean>
    
    <!--配置velocity   前端的模板語言-->
  <!--velocity配置-->
    <!-- <bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
        <property name="resourceLoaderPath" value="/template/"/>
        <property name="velocityProperties">
            <props>
                <prop key="input.encoding">utf-8</prop>
                <prop key="output.encoding">utf-8</prop>
                <prop key="file.resource.loader.cache">false</prop>
                <prop key="file.resource.loader.modificationCheckInterval">1</prop>
                <prop key="velocimacro.library.autoreload">false</prop>
            </props>
        </property>
    </bean> -->

    <!--velocity模板語言-->
    <!-- <bean class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
        <property name="suffix" value=".vm"/>
        <property name="contentType" value="text/html;charset=utf-8"/>
        <property name="dateToolAttribute" value="date"/>日期函數名稱
    </bean> -->
    
    
    <!-- 靜態資源映射 -->
    <mvc:resources mapping="/static/**" location="/static/"/>
    <mvc:resources mapping="/css/**" location="/css/"/>
    
    <!-- 定義文件上傳解析器 -->
    <!-- <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        設定默認編碼
        <property name="defaultEncoding" value="UTF-8"></property>
        設定文件上傳的最大值5MB,5*1024*1024
        <property name="maxUploadSize" value="5242880"></property>
    </bean> -->
     
    <!-- 開啓aop代理 ,spring-shirl裏就能夠不用配置lifecycleBeanPostProcessor了--> 
    <aop:config proxy-target-class="true"></aop:config>
    
    <!-- 全局異常處理 ,只要實現了HandlerExceptionResolver接口的就是全局異常處理器-->
   <!--  <bean class="com.lhy.shiro.exception.CustomExceptionResolver"/> -->
    
</beans>

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-2.5.xsd
    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">


    <!--提供方應用信息,用戶計算依賴關係-->
    <dubbo:application name="lhy-service"/>

    <!--使用zookeeper註冊中心暴露服務地址-->
    <dubbo:registry address="zookeeper://192.168.85.3:2181?backup=192.168.85.4:2181,192.168.85.5:2181"/>

    <!-- 用dubbo協議在20880端口暴露服務 -->
    <dubbo:protocol name="dubbo" port="20880" />

    <!-- 聲明須要暴露的服務接口 ,寫操做可設置retries="0"避免重複調用SOA服務-->
    <dubbo:service retries="0" interface="com.lhy.service.user.UserFacade" ref="userFacade" />


</beans>

db.properties :

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/lhy?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root

接口實現類:UserFacade.java 

package com.lhy.service.impl;

import com.lhy.service.pojo.User;
import com.lhy.service.user.UserFacade;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("userFacade")
public class UserFacadeImpl implements UserFacade{



    @Override
    public User getById(int id) {
        User user = new User();
        user.setId(id);
        user.setName("張三");
        return user;
    }

    @Override
    public int addUser(User user) {
        return 1;
    }

    @Override
    public List<User> getUsers() {
        List<User> list = new ArrayList<>();

        User u1 = new User();
        u1.setId(1);
        u1.setName("阿皮");

        User u2 = new User();
        u2.setId(2);
        u2.setName("喜覺");

        User u3 = new User();
        u3.setId(3);
        u3.setName("虛竹");

        User u4 = new User();
        u3.setId(4);
        u3.setName("wooubeibei");

        list.add(u1);
        list.add(u2);
        list.add(u3);
        list.add(u4);
        return list;
    }
}

消費者 lhy-consumer :

war包工程,依賴父工程和接口層,配置和接口實現差很少,

不同的是dubbo配置:

dubbo-consumer.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-2.5.xsd
    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

<!-- 消費方應用名,用於計算依賴關係,不是匹配條件,不要與提供方同樣 -->
    <dubbo:application name="lhy-consumer"  />

    <!--使用zookeeper註冊中心暴露服務地址-->
    <dubbo:registry address="zookeeper://192.168.85.3:2181?backup=192.168.85.4:2181,192.168.85.5:2181"/>

    <!-- 生成遠程服務代理,能夠和本地bean同樣使用demoService -->
    <dubbo:reference id="userFacade" interface="com.lhy.service.user.UserFacade" />
</beans>

 

 定義一個UserService,實現類裏調用 dubbo接口:

package com.lhy.consumer.service.impl;

import com.lhy.consumer.service.UserService;
import com.lhy.service.pojo.User;
import com.lhy.service.user.UserFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserFacade userFacade;

    @Override
    public List<User> getUsers() {
        return userFacade.getUsers();
    }
}

UserController來調用一下:

package com.lhy.consumer.controller;


import com.lhy.consumer.service.UserService;
import com.lhy.service.pojo.User;
import com.lhy.service.user.UserFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

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

    @Autowired
    private UserService userService;

    @RequestMapping("/hello")
    @ResponseBody
    public String hello(){
        return "hello";
    }

    @RequestMapping("/toHelloPage")
    public String toHelloPage(){
        return "hello";
    }


    @RequestMapping("/getUsers")
    public @ResponseBody List<User> getUsers(){
        List<User> list = userService.getUsers();
        return list;
    }
}

代碼打好後,先把接口層mvn install到本地倉庫,依次啓動 生產者 lhy-service 、消費者lhy-consumer :

訪問一下消費者:http://localhost:8081/user/getUsers

能夠看到,成功調用了提供者的接口,再看dubbo管控臺:有了一個服務

能夠利用管控臺對服務進行各類治理了

只用zk查看工具看zk上註冊的信息:

相關文章
相關標籤/搜索