美文网首页
day01-分布式框架-Dubbox

day01-分布式框架-Dubbox

作者: 王杰涛 | 来源:发表于2018-10-30 20:36 被阅读0次

    1. linux环境下网络配置:

    1 桥接方式和仅主机方式的区别:本次采用仅主机的方式,并修改网段,配置子网ip网段为192.168.25.xxx

    2 SecureCRT传送Zookeeper压缩包:

    1. 文件上传模式:Alt+P;
    2. put d:\zookeeper-3.4.6.tar.gz
    3. tar -zxvf zookeeper-3.4.6.tar.gz
    4. mkdir data
    5. cd conf
    6. mv zoo_sample.cfg zoo.cfg

    3 vim修改配置:

    1. vim zoo.cfg
    2. i进入插入模式,ESC退出,:wq保存并退出

    4 ./zkServer.sh start启动服务(status/stop)

    2.Dubbox安装到本地仓库:

    1. 先将dubbo-2.8.4.jar包放到d:\setup
    2. 然后输入命令:
      mvn install:install-file -Dfile=d:\setup\dubbo-2.8.4.jar -DgroupId=com.alibaba -DartifactId=dubbo -Dversion=2.8.4 -Dpackaging=jar

    1.添加离线约束

    1.创建dtd-schema文件夹
    2.preference添加xml文件约束
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd
    3.再添加约束之后Alt+F5 Update Project

    3. Dubbox-Demo

    1.创建Maven服务方工程

    image.png
    没有web界面,但该demo是一个可运行的项目,采用war方式打包

    2.applicationContext-service.xml

    1.src/main/resources下创建applicationContext-service.xml
    2.代码如下:

    <dubbo:application name="dubboxdemo-service"/>  
    <!-- 本地注册中心,使用zookeeper协议,linux服务器ip地址 -->
    <dubbo:registry address="zookeeper://192.168.25.129:2181"/> 
     <!--配置包扫描 -->
    <dubbo:annotation package="com.itheima.demo.service.impl" /> 
    </beans> 
    

    3.@Service的位置

    image.png

    4.web.xml

    1.生成web.xml:


    image.png

    2.代码:

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

    5.pom.xml:

       <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.itheima.demo</groupId>
       <artifactId>dobboxdemo-service</artifactId>
       <version>0.0.1-SNAPSHOT</version>
       <packaging>war</packaging>
    
       <!-- 1.定义项目属性 -->
       <properties>
           <spring.version>4.2.4.RELEASE</spring.version>
       </properties>
       <!-- 2.定义项目依赖-->
       <dependencies>
           <!-- Spring相关 -->
           <dependency>
               <!--创建项目的组织或团体的唯一 Id.可视为公司名 -->
               <groupId>org.springframework</groupId>
               <!--项目的唯一 Id, 可视为项目名 . -->
               <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>
           <dependency>
               <groupId>org.springframework</groupId>
               <artifactId>spring-jms</artifactId>
               <version>${spring.version}</version>
           </dependency>
           <dependency>
               <groupId>org.springframework</groupId>
               <artifactId>spring-context-support</artifactId>
               <version>${spring.version}</version>
           </dependency>
           <!-- dubbo相关 -->
           <dependency>
               <groupId>com.alibaba</groupId>
               <artifactId>dubbo</artifactId>
               <version>2.8.4</version>
           </dependency>
           <dependency>
               <groupId>org.apache.zookeeper</groupId>
               <artifactId>zookeeper</artifactId>
               <version>3.4.6</version>
           </dependency>
           <dependency>
               <groupId>com.github.sgroschupf</groupId>
               <artifactId>zkclient</artifactId>
               <version>0.1</version>
           </dependency>
           <dependency>
               <groupId>javassist</groupId>
               <artifactId>javassist</artifactId>
               <version>3.11.0.GA</version>
           </dependency>
       </dependencies>
       <!-- 3.构建插件-->
       <build>
           <plugins>
               <plugin>
                   <groupId>org.apache.maven.plugins</groupId>
                   <artifactId>maven-compiler-plugin</artifactId>
                   <version>2.3.2</version>
                   <configuration>
                       <source>1.7</source>
                       <target>1.7</target>
                   </configuration>
               </plugin>
               <plugin>
                   <groupId>org.apache.tomcat.maven</groupId>
                   <artifactId>tomcat7-maven-plugin</artifactId>
                   <configuration>
                       <!-- 指定端口,一般为8080,这里需要修改 -->
                       <port>8081</port>
                       <!-- 请求路径 -->
                       <path>/</path>
                   </configuration>
               </plugin>
           </plugins>
       </build>
    </project>
    
    

    6.run:

    image.png
    1.服务没有界面,只有使用消费方才能看见效果
    2.每次启动服务之前需要关闭之前的服务

    7.创建Dobbox消费方工程:

    1. 创建Maven工程:dobboxdemo-web
    2. pom.xml文件:修改端口号
        <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <configuration>
                        <!-- 指定端口,一般为8080,这里需要修改 -->
                        <port>8082</port>
                        <!-- 请求路径 -->
                        <path>/</path>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    

    3.web.xml文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/javaee"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        version="2.5">
        <!-- 解决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>
            <!-- forceEncoding用来设置是否理会 request.getCharacterEncoding()方法,设置为true则强制覆盖之前的编码格式。 -->
            <init-param>
                <param-name>forceEncoding</param-name>
                <param-value>true</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>CharacterEncodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
        <servlet>
            <servlet-name>springmvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <!-- 指定加载的配置文件 ,通过参数contextConfigLocation加载 -->
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:springmvc.xml</param-value>
            </init-param>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>springmvc</servlet-name>
            <url-pattern>*.do</url-pattern>
        </servlet-mapping>
    
    </web-app>
    
    1. 创建UserController.java文件
      复制UserService到src中,代码如下:
    package com.itheima.demo.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import com.alibaba.dubbo.config.annotation.Reference;
    import com.itheima.demo.service.UserService;
    
    @Controller
    @RequestMapping("user")
    public class UserController {
        //UserServiceImpl是另一个工程的
        @Reference
        private UserService userService;
        
        @RequestMapping("/showName")
        //增加ResponseMapping表示返回的不是页面,而是值
        @ResponseBody
        public String showName() {
            return userService.getName();
        }
    }
    
    1. 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:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
        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.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">
    
        <!-- 用来转换request的内容到String的格式 -->
        <mvc:annotation-driven>
            <mvc:message-converters register-defaults="false">
                <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                    <constructor-arg value="UTF-8" />
                </bean>
            </mvc:message-converters>
        </mvc:annotation-driven>
    
        <!-- 引用dubbo 服务 -->
        <dubbo:application name="dubboxdemo-web" />
        <dubbo:registry address="zookeeper://192.168.25.128:2181" />
        <dubbo:annotation package="com.itheima.demo.controller.UserController" />
    
    </beans>
    
    1. 虚拟机的意外情况:
      该虚拟机似乎正在使用中。 如果该虚拟机未在使用,请按“获取所有权(T)”按钮获取

    解决:删除..vmk.lck


    image.png
    1. 运行:
    image.png

    8.dubbo-admin:

    1.打包dubb-admin的源码:


    image.png

    2.进入sftp文件传输模式:Alt+P
    3.传输文件:put d:/setup/dubbo-admin.war
    4.上传并解压tomcat7:tar -zxvf apache-tomcat-7.0.52.tar.gz
    5.移动war包到webapps下:mv /root/dubbo-admin.war /root/apache-tomcat-7.0.52/webapps/
    6.进入bin目录并开启服务:./startup.sh

    7.输入密码并访问 image.png

    相关文章

      网友评论

          本文标题:day01-分布式框架-Dubbox

          本文链接:https://www.haomeiwen.com/subject/yjgbtqtx.html