1 # dubbo 与ssm 整的过程 2 3 ## 1、创建一个父工程 贺三个子工程 4 5 父工程 pom 6 7 接口工程 =》jar 8 9 服务提供者=》 jar 10 11 服务消费者=》war 12 13 ## 2、引入依赖 14 15 ### 处理父工程的依赖 16 17 ```xml 18 <!-- 定义主要版本号 --> 19 <properties> 20 <spring.version>5.1.5.RELEASE</spring.version> 21 <mybatis.version>3.4.2</mybatis.version> 22 <mybatis.spring.version>1.3.0</mybatis.spring.version> 23 <pagehelper-version>5.1.2</pagehelper-version> 24 <druid.version>1.0.9</druid.version> 25 <mysql.version>5.1.6</mysql.version> 26 <aspectj-version>1.8.0</aspectj-version> 27 28 <dubbo.version>2.7.3</dubbo.version> 29 30 <log4j.version>1.2.17</log4j.version> 31 <jackson.version>2.9.1</jackson.version> 32 <jstl.version>1.2</jstl.version> 33 <servlet-api.version>2.5</servlet-api.version> 34 <jsp-api.version>2.2</jsp-api.version> 35 36 <commons-lang3.version>3.3.2</commons-lang3.version> 37 <commons-io.version>1.3.1</commons-io.version> 38 <commons-net.version>3.3</commons-net.version> 39 <commons-fileupload.version>1.3.1</commons-fileupload.version> 40 41 <junit-version>4.12</junit-version> 42 43 <ik.version>2012_u6</ik.version> 44 <lucene.version>7.2.1</lucene.version> 45 <spring-data-elasticsearch.version>3.1.5.RELEASE</spring-data-elasticsearch.version> 46 <!-- <kafka.version>0.8.2.1</kafka.version> --> 47 <kafka.version>2.1.0</kafka.version> 48 <spring-kafka.version>2.2.4.RELEASE</spring-kafka.version> 49 <jedis.version>2.9.0</jedis.version> 50 <spring-data-redis.version>2.1.5.RELEASE</spring-data-redis.version> 51 </properties> 52 53 <dependencyManagement> 54 55 <dependencies> 56 <!-- 引入dubbo的依赖配置 --> 57 <dependency> 58 <groupId>org.apache.dubbo</groupId> 59 <artifactId>dubbo</artifactId> 60 <version>${dubbo.version}</version> 61 <exclusions> 62 <exclusion> 63 <artifactId>spring</artifactId> 64 <groupId>org.springframework</groupId> 65 </exclusion> 66 </exclusions> 67 </dependency> 68 <dependency> 69 <groupId>org.apache.dubbo</groupId> 70 <artifactId>dubbo-dependencies-zookeeper</artifactId> 71 <version>${dubbo.version}</version> 72 <type>pom</type> 73 </dependency> 74 <!-- spring-webmvc依赖 --> 75 <dependency> 76 <groupId>org.springframework</groupId> 77 <artifactId>spring-webmvc</artifactId> 78 <version>${spring.version}</version> 79 </dependency> 80 <dependency> 81 <groupId>org.springframework</groupId> 82 <artifactId>spring-web</artifactId> 83 <version>${spring.version}</version> 84 </dependency> 85 <dependency> 86 <groupId>org.springframework</groupId> 87 <artifactId>spring-context</artifactId> 88 <version>${spring.version}</version> 89 </dependency> 90 <!-- spring-jdbc依赖 --> 91 <dependency> 92 <groupId>org.springframework</groupId> 93 <artifactId>spring-jdbc</artifactId> 94 <version>${spring.version}</version> 95 </dependency> 96 <!-- spring-tx 事务依赖 --> 97 <dependency> 98 <groupId>org.springframework</groupId> 99 <artifactId>spring-tx</artifactId> 100 <version>${spring.version}</version> 101 </dependency> 102 <!-- 引入spring-test依赖 --> 103 <dependency> 104 <groupId>org.springframework</groupId> 105 <artifactId>spring-test</artifactId> 106 <version>${spring.version}</version> 107 </dependency> 108 109 <!-- mybatis核心包 --> 110 <dependency> 111 <groupId>org.mybatis</groupId> 112 <artifactId>mybatis</artifactId> 113 <version>${mybatis.version}</version> 114 </dependency> 115 <!-- mybatis-spring 整合jar --> 116 <dependency> 117 <groupId>org.mybatis</groupId> 118 <artifactId>mybatis-spring</artifactId> 119 <version>${mybatis.spring.version}</version> 120 </dependency> 121 <!-- druid数据源 --> 122 <dependency> 123 <groupId>com.alibaba</groupId> 124 <artifactId>druid</artifactId> 125 <version>${druid.version}</version> 126 </dependency> 127 <!-- Mysql数据库驱动包 --> 128 <dependency> 129 <groupId>mysql</groupId> 130 <artifactId>mysql-connector-java</artifactId> 131 <version>${mysql.version}</version> 132 </dependency> 133 <!-- 日志文件管理包 --> 134 <!-- log start --> 135 <dependency> 136 <groupId>log4j</groupId> 137 <artifactId>log4j</artifactId> 138 <version>${log4j.version}</version> 139 </dependency> 140 <!-- 单元测试 --> 141 <dependency> 142 <groupId>junit</groupId> 143 <artifactId>junit</artifactId> 144 <version>${junit-version}</version> 145 <scope>test</scope> 146 </dependency> 147 <!-- 上传组件包 --> 148 <dependency> 149 <groupId>commons-fileupload</groupId> 150 <artifactId>commons-fileupload</artifactId> 151 <version>${commons-fileupload.version}</version> 152 </dependency> 153 <!-- common-io依赖 --> 154 <dependency> 155 <groupId>commons-io</groupId> 156 <artifactId>commons-io</artifactId> 157 <version>${commons-io.version}</version> 158 </dependency> 159 <!-- 引入jstl依赖 --> 160 <dependency> 161 <groupId>jstl</groupId> 162 <artifactId>jstl</artifactId> 163 <version>${jstl.version}</version> 164 </dependency> 165 <!-- 引入jsp-api依赖 --> 166 <dependency> 167 <groupId>javax.servlet.jsp</groupId> 168 <artifactId>jsp-api</artifactId> 169 <version>${jsp-api.version}</version> 170 <scope>provided</scope> 171 </dependency> 172 <dependency> 173 <groupId>javax.servlet</groupId> 174 <artifactId>servlet-api</artifactId> 175 <version>${servlet-api.version}</version> 176 <scope>provided</scope> 177 </dependency> 178 179 <!-- 引入jackson的依赖 --> 180 <dependency> 181 <groupId>com.fasterxml.jackson.core</groupId> 182 <artifactId>jackson-databind</artifactId> 183 <version>${jackson.version}</version> 184 </dependency> 185 186 <!-- 依赖的公共包 --> 187 <dependency> 188 <groupId>org.apache.commons</groupId> 189 <artifactId>commons-lang3</artifactId> 190 <version>${commons-lang3.version}</version> 191 </dependency> 192 193 <!-- 引入org.aspectj依赖 --> 194 <dependency> 195 <groupId>org.aspectj</groupId> 196 <artifactId>aspectjweaver</artifactId> 197 <version>${aspectj-version}</version> 198 </dependency> 199 <dependency> 200 <groupId>org.aspectj</groupId> 201 <artifactId>aspectjrt</artifactId> 202 <version>${aspectj-version}</version> 203 </dependency> 204 <!-- 引入mybaits pagehelper分页助手依赖 --> 205 <dependency> 206 <groupId>com.github.pagehelper</groupId> 207 <artifactId>pagehelper</artifactId> 208 <version>${pagehelper-version}</version> 209 </dependency> 210 211 <dependency> 212 <groupId>org.apache.lucene</groupId> 213 <artifactId>lucene-core</artifactId> 214 <version>${lucene.version}</version> 215 </dependency> 216 <!-- 加入ik分词器 --> 217 <dependency> 218 <groupId>com.janeluo</groupId> 219 <artifactId>ikanalyzer</artifactId> 220 <version>${ik.version}</version> 221 <!-- 排除自带的lucene操作 --> 222 <exclusions> 223 <exclusion> 224 <groupId>org.apache.lucene</groupId> 225 <artifactId>lucene-queries</artifactId> 226 </exclusion> 227 <exclusion> 228 <groupId>org.apache.lucene</groupId> 229 <artifactId>lucene-queryparser</artifactId> 230 </exclusion> 231 </exclusions> 232 </dependency> 233 <dependency> 234 <groupId>org.springframework.data</groupId> 235 <artifactId>spring-data-elasticsearch</artifactId> 236 <version>${spring-data-elasticsearch.version}</version> 237 </dependency> 238 239 <dependency> 240 <groupId>redis.clients</groupId> 241 <artifactId>jedis</artifactId> 242 <version>${jedis.version}</version> 243 </dependency> 244 <dependency> 245 <groupId>org.springframework.data</groupId> 246 <artifactId>spring-data-redis</artifactId> 247 <version>${spring-data-redis.version}</version> 248 </dependency> 249 250 <dependency> 251 <groupId>org.apache.kafka</groupId> 252 <artifactId>kafka_2.12</artifactId> 253 <version>${kafka.version}</version> 254 <exclusions> 255 <exclusion> 256 <artifactId>jmxri</artifactId> 257 <groupId>com.sun.jmx</groupId> 258 </exclusion> 259 <exclusion> 260 <artifactId>jms</artifactId> 261 <groupId>javax.jms</groupId> 262 </exclusion> 263 <exclusion> 264 <artifactId>jmxtools</artifactId> 265 <groupId>com.sun.jdmk</groupId> 266 </exclusion> 267 </exclusions> 268 </dependency> 269 <dependency> 270 <groupId>org.springframework.kafka</groupId> 271 <artifactId>spring-kafka</artifactId> 272 <version>${spring-kafka.version}</version> 273 </dependency> 274 275 </dependencies> 276 </dependencyManagement> 277 278 <build> 279 <pluginManagement> 280 <plugins> 281 <plugin> 282 <groupId>org.apache.tomcat.maven</groupId> 283 <artifactId>tomcat7-maven-plugin</artifactId> 284 <version>2.2</version> 285 </plugin> 286 287 <plugin> 288 <groupId>org.eclipse.jetty</groupId> 289 <artifactId>jetty-maven-plugin</artifactId> 290 <version>9.4.20.v20190813</version> 291 <configuration> 292 <httpConnector> 293 <!-- 端口号 --> 294 <port>9093</port> 295 <!-- 项目访问路径 --> 296 <host>192.168.1.105</host> 297 </httpConnector> 298 <contextHandlers> 299 <jettyWebAppContext> 300 <!-- 虚拟路径 --> 301 <contextPath>/pic</contextPath> 302 <!-- 物理路径 --> 303 <resourceBase>d:/pic/</resourceBase> 304 </jettyWebAppContext> 305 </contextHandlers> 306 <scanIntervalSeconds>1</scanIntervalSeconds> 307 </configuration> 308 </plugin> 309 310 </plugins> 311 </pluginManagement> 312 313 </build> 314 <!-- 结束拷贝的位置 --> 315 ``` 316 317 ### 接口的依赖 318 319 <!-- 开始拷贝的位置 --> 320 <dependencies> 321 <dependency> 322 <groupId>com.github.pagehelper</groupId> 323 <artifactId>pagehelper</artifactId> 324 </dependency> 325 <dependency> 326 <groupId>com.fasterxml.jackson.core</groupId> 327 <artifactId>jackson-databind</artifactId> 328 </dependency> 329 330 331 ``` 332 <dependency> 333 <groupId>org.apache.lucene</groupId> 334 <artifactId>lucene-core</artifactId> 335 </dependency> 336 337 <!-- 加入ik分词器 --> 338 <dependency> 339 <groupId>com.janeluo</groupId> 340 <artifactId>ikanalyzer</artifactId> 341 <!-- 排除自带的lucene操作 --> 342 <exclusions> 343 <exclusion> 344 <groupId>org.apache.lucene</groupId> 345 <artifactId>lucene-queries</artifactId> 346 </exclusion> 347 <exclusion> 348 <groupId>org.apache.lucene</groupId> 349 <artifactId>lucene-queryparser</artifactId> 350 </exclusion> 351 </exclusions> 352 </dependency> 353 <dependency> 354 <groupId>org.springframework.data</groupId> 355 <artifactId>spring-data-elasticsearch</artifactId> 356 </dependency> 357 ``` 358 359 </dependencies> 360 <!-- <技术拷贝的位置> --> 361 362 ### 服务提供者中添加依赖 363 364 #### 添加通用的依赖 365 366 <!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils --> 367 <dependency> 368 <groupId>commons-beanutils</groupId> 369 <artifactId>commons-beanutils</artifactId> 370 <version>1.9.4</version> 371 </dependency> 372 373 374 ```xml 375 <!-- 引入dubbo的依赖配置 --> 376 <dependency> 377 <groupId>org.apache.dubbo</groupId> 378 <artifactId>dubbo</artifactId> 379 <exclusions> 380 <exclusion> 381 <artifactId>spring-context</artifactId> 382 <groupId>org.springframework</groupId> 383 </exclusion> 384 <exclusion> 385 <artifactId>netty-all</artifactId> 386 <groupId>io.netty</groupId> 387 </exclusion> 388 </exclusions> 389 </dependency> 390 <dependency> 391 <groupId>org.apache.dubbo</groupId> 392 <artifactId>dubbo-dependencies-zookeeper</artifactId> 393 <type>pom</type> 394 </dependency> 395 <!-- spring-webmvc依赖 --> 396 <dependency> 397 <groupId>org.springframework</groupId> 398 <artifactId>spring-webmvc</artifactId> 399 <exclusions> 400 <exclusion> 401 <artifactId>spring-context</artifactId> 402 <groupId>org.springframework</groupId> 403 </exclusion> 404 </exclusions> 405 </dependency> 406 <dependency> 407 <groupId>org.springframework</groupId> 408 <artifactId>spring-context</artifactId> 409 </dependency> 410 <!-- spring-jdbc依赖 --> 411 <dependency> 412 <groupId>org.springframework</groupId> 413 <artifactId>spring-jdbc</artifactId> 414 </dependency> 415 <!-- spring-tx 事务依赖 --> 416 <dependency> 417 <groupId>org.springframework</groupId> 418 <artifactId>spring-tx</artifactId> 419 </dependency> 420 <!-- 引入spring-test依赖 --> 421 <dependency> 422 <groupId>org.springframework</groupId> 423 <artifactId>spring-test</artifactId> 424 </dependency> 425 426 <!-- mybatis核心包 --> 427 <dependency> 428 <groupId>org.mybatis</groupId> 429 <artifactId>mybatis</artifactId> 430 </dependency> 431 <!-- mybatis-spring 整合jar --> 432 <dependency> 433 <groupId>org.mybatis</groupId> 434 <artifactId>mybatis-spring</artifactId> 435 </dependency> 436 <!-- druid数据源 --> 437 <dependency> 438 <groupId>com.alibaba</groupId> 439 <artifactId>druid</artifactId> 440 </dependency> 441 <!-- Mysql数据库驱动包 --> 442 <dependency> 443 <groupId>mysql</groupId> 444 <artifactId>mysql-connector-java</artifactId> 445 </dependency> 446 <!-- 日志文件管理包 --> 447 <!-- log start --> 448 <dependency> 449 <groupId>log4j</groupId> 450 <artifactId>log4j</artifactId> 451 </dependency> 452 <!-- 单元测试 --> 453 <dependency> 454 <groupId>junit</groupId> 455 <artifactId>junit</artifactId> 456 <scope>test</scope> 457 </dependency> 458 <!-- 依赖的公共包 --> 459 <dependency> 460 <groupId>org.apache.commons</groupId> 461 <artifactId>commons-lang3</artifactId> 462 </dependency> 463 464 <!-- 引入org.aspectj依赖 --> 465 <dependency> 466 <groupId>org.aspectj</groupId> 467 <artifactId>aspectjweaver</artifactId> 468 </dependency> 469 <dependency> 470 <groupId>org.aspectj</groupId> 471 <artifactId>aspectjrt</artifactId> 472 </dependency> 473 <!-- 引入mybaits pagehelper分页助手依赖 --> 474 <dependency> 475 <groupId>com.github.pagehelper</groupId> 476 <artifactId>pagehelper</artifactId> 477 </dependency> 478 479 <dependency> 480 <groupId>org.apache.lucene</groupId> 481 <artifactId>lucene-core</artifactId> 482 </dependency> 483 484 <!-- 加入ik分词器 --> 485 <dependency> 486 <groupId>com.janeluo</groupId> 487 <artifactId>ikanalyzer</artifactId> 488 <!-- 排除自带的lucene操作 --> 489 <exclusions> 490 <exclusion> 491 <groupId>org.apache.lucene</groupId> 492 <artifactId>lucene-queries</artifactId> 493 </exclusion> 494 <exclusion> 495 <groupId>org.apache.lucene</groupId> 496 <artifactId>lucene-queryparser</artifactId> 497 </exclusion> 498 </exclusions> 499 </dependency> 500 <dependency> 501 <groupId>org.springframework.data</groupId> 502 <artifactId>spring-data-elasticsearch</artifactId> 503 </dependency> 504 505 <dependency> 506 <groupId>redis.clients</groupId> 507 <artifactId>jedis</artifactId> 508 </dependency> 509 <dependency> 510 <groupId>org.springframework.data</groupId> 511 <artifactId>spring-data-redis</artifactId> 512 </dependency> 513 514 <dependency> 515 <groupId>org.apache.kafka</groupId> 516 <artifactId>kafka_2.12</artifactId> 517 <exclusions> 518 <exclusion> 519 <artifactId>jmxri</artifactId> 520 <groupId>com.sun.jmx</groupId> 521 </exclusion> 522 <exclusion> 523 <artifactId>jms</artifactId> 524 <groupId>javax.jms</groupId> 525 </exclusion> 526 <exclusion> 527 <artifactId>jmxtools</artifactId> 528 <groupId>com.sun.jdmk</groupId> 529 </exclusion> 530 </exclusions> 531 </dependency> 532 <dependency> 533 <groupId>org.springframework.kafka</groupId> 534 <artifactId>spring-kafka</artifactId> 535 <!-- <version>1.0.0.RC1</version> --> 536 </dependency> 537 ``` 538 539 #### 添加本项目中的接口的依赖 540 541 ![1587610442695](D:八维教学senior3教案senior-stage-three-hgshop第六单元 Dubbo理论技能扩展dubbo 与ssm 整的过程.assets1587610442695.png) 542 543 544 545 ### 处理服务消费者的依赖 546 547 #### 添加开通用的依赖 548 549 ```xml 550 <!-- 引入dubbo的依赖配置 --> 551 <dependency> 552 <groupId>org.apache.dubbo</groupId> 553 <artifactId>dubbo</artifactId> 554 <exclusions> 555 <exclusion> 556 <artifactId>spring-context</artifactId> 557 <groupId>org.springframework</groupId> 558 </exclusion> 559 <exclusion> 560 <artifactId>netty-all</artifactId> 561 <groupId>io.netty</groupId> 562 </exclusion> 563 </exclusions> 564 </dependency> 565 <dependency> 566 <groupId>org.apache.dubbo</groupId> 567 <artifactId>dubbo-dependencies-zookeeper</artifactId> 568 <type>pom</type> 569 </dependency> 570 <!-- spring-webmvc依赖 --> 571 <dependency> 572 <groupId>org.springframework</groupId> 573 <artifactId>spring-webmvc</artifactId> 574 </dependency> 575 <dependency> 576 <groupId>org.springframework</groupId> 577 <artifactId>spring-context</artifactId> 578 </dependency> 579 <!-- 日志文件管理包 --> 580 <!-- log start --> 581 <dependency> 582 <groupId>log4j</groupId> 583 <artifactId>log4j</artifactId> 584 </dependency> 585 <!-- 上传组件包 --> 586 <dependency> 587 <groupId>commons-fileupload</groupId> 588 <artifactId>commons-fileupload</artifactId> 589 </dependency> 590 <!-- common-io依赖 --> 591 <dependency> 592 <groupId>commons-io</groupId> 593 <artifactId>commons-io</artifactId> 594 </dependency> 595 <!-- 引入jstl依赖 --> 596 <dependency> 597 <groupId>jstl</groupId> 598 <artifactId>jstl</artifactId> 599 </dependency> 600 <!-- 引入jsp-api依赖 --> 601 <dependency> 602 <groupId>javax.servlet.jsp</groupId> 603 <artifactId>jsp-api</artifactId> 604 <scope>provided</scope> 605 </dependency> 606 <dependency> 607 <groupId>javax.servlet</groupId> 608 <artifactId>servlet-api</artifactId> 609 <scope>provided</scope> 610 </dependency> 611 612 <!-- 引入jackson的依赖 --> 613 <dependency> 614 <groupId>com.fasterxml.jackson.core</groupId> 615 <artifactId>jackson-databind</artifactId> 616 </dependency> 617 618 <!-- 依赖的公共包 --> 619 <dependency> 620 <groupId>org.apache.commons</groupId> 621 <artifactId>commons-lang3</artifactId> 622 </dependency> 623 624 <dependency> 625 <groupId>org.apache.kafka</groupId> 626 <artifactId>kafka_2.12</artifactId> 627 <exclusions> 628 <exclusion> 629 <artifactId>jmxri</artifactId> 630 <groupId>com.sun.jmx</groupId> 631 </exclusion> 632 <exclusion> 633 <artifactId>jms</artifactId> 634 <groupId>javax.jms</groupId> 635 </exclusion> 636 <exclusion> 637 <artifactId>jmxtools</artifactId> 638 <groupId>com.sun.jdmk</groupId> 639 </exclusion> 640 </exclusions> 641 </dependency> 642 <dependency> 643 <groupId>org.springframework.kafka</groupId> 644 <artifactId>spring-kafka</artifactId> 645 <!-- <version>1.0.0.RC1</version> --> 646 </dependency> 647 ``` 648 649 650 651 ```xml 652 <dependency> 653 <groupId>redis.clients</groupId> 654 <artifactId>jedis</artifactId> 655 </dependency> 656 <dependency> 657 <groupId>org.springframework.data</groupId> 658 <artifactId>spring-data-redis</artifactId> 659 </dependency> 660 ``` 661 662 #### 添加本项目接口的依赖 663 664 添加方式参考服务提供者。 665 666 667 668 #### 引入web容器插件 669 670 ```xml 671 <build> 672 <plugins> 673 674 <plugin> 675 <groupId>org.eclipse.jetty</groupId> 676 <artifactId>jetty-maven-plugin</artifactId> 677 <version>9.4.20.v20190813</version> 678 <configuration> 679 <httpConnector> 680 681 <!-- 端口号 --> 682 <port>9094</port> 683 <!-- 项目访问路径 --> 684 <host>0.0.0.0</host> 685 </httpConnector> 686 <contextHandlers> 687 <jettyWebAppContext> 688 <!-- 虚拟路径 --> 689 <contextPath>/pic</contextPath> 690 <!-- 物理路径 --> 691 <resourceBase>d:/pic/</resourceBase> 692 </jettyWebAppContext> 693 </contextHandlers> 694 <scanIntervalSeconds>1</scanIntervalSeconds> 695 </configuration> 696 </plugin> 697 698 <plugin> 699 <groupId>org.apache.tomcat.maven</groupId> 700 <artifactId>tomcat7-maven-plugin</artifactId> 701 <configuration> 702 <port>8080</port> 703 <path>/</path> 704 <uriEncoding>UTF-8</uriEncoding> 705 <contextReloadable>true</contextReloadable> 706 <staticContextDocbase>G://pic</staticContextDocbase> 707 <staticContextPath>/pic</staticContextPath> 708 <!-- 自动部署开始 --> 709 <url>http://192.168.110.134:8880/manager/text</url> 710 <username>boss</username> 711 <password>123456</password> 712 <update>true</update> 713 <path>/stu</path> 714 <!-- 自动部署结束 --> 715 </configuration> 716 </plugin> 717 </plugins> 718 </build> 719 720 ``` 721 722 ## 编写代码 723 724 ### 编写接口层 725 726 #### 编写实体bean 727 728 注意要点:一定要加序列化。 729 730 #### 编写接口service接口、 731 732 接口中的方法绝对不可以返回空值(void) 733 734 735 736 ### 编写服务层 737 738 #### 实现接口工程中所有的接口。 739 740 实现类上加Service注解,这个service 注解必须是下图中所示的,不能使用spring的。 741 742 ![1587612379885](D:八维教学senior3教案senior-stage-three-hgshop第六单元 Dubbo理论技能扩展dubbo 与ssm 整的过程.assets1587612379885.png) 743 744 745 746 #### 编写dao层 747 748 #### 引入配置文件 749 750 applicationContext-dubbo-provider.xml 751 752 ```xml 753 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 754 xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" 755 xmlns="http://www.springframework.org/schema/beans" 756 xmlns:aop="http://www.springframework.org/schema/aop" 757 xmlns:tx="http://www.springframework.org/schema/tx" 758 xmlns:p="http://www.springframework.org/schema/p" 759 xmlns:context="http://www.springframework.org/schema/context" 760 xsi:schemaLocation="http://www.springframework.org/schema/beans 761 http://www.springframework.org/schema/beans/spring-beans.xsd 762 http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd 763 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 764 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 765 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> 766 ``` 767 768 ```xml 769 <!-- 应用程序 --> 770 <dubbo:application name="dubbo-service-demo" /> 771 <!-- 注册中心 采用zookeeper 必须修改 --> 772 <dubbo:registry 773 address="zookeeper://192.168.110.134:2181" /> 774 <!-- 直连 --> 775 <!-- <dubbo:registry address="N/A" /> --> 776 <!-- 通讯协议 必须修改 --> 777 <dubbo:protocol name="dubbo" port="20881" /> 778 <!-- 注解扫描配置 指定了扫描的包 必须修改 --> 779 <dubbo:annotation 780 package="com.mmcor.pregnant.service.impl" /> 781 ``` 782 783 </beans> 784 785 786 787 788 789 applicationContext-dao.xml 790 791 <?xml version="1.0" encoding="UTF-8"?> 792 <beans xmlns="http://www.springframework.org/schema/beans" 793 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 794 xmlns:aop="http://www.springframework.org/schema/aop" 795 xmlns:context="http://www.springframework.org/schema/context" 796 xmlns:tx="http://www.springframework.org/schema/tx" 797 xmlns:p="http://www.springframework.org/schema/p" 798 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 799 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 800 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 801 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> 802 803 ```xml 804 <!--指定外部属性文件的位置 --> 805 <context:property-placeholder location="classpath:db.properties" /> 806 807 <!-- 使用druid数据源 连接池 --> 808 <bean id="dataSource" 809 class="com.alibaba.druid.pool.DruidDataSource"> 810 <property name="driverClassName" value="${jdbc.driver}"> 811 </property> 812 <property name="url" value="${jdbc.url}"> 813 </property> 814 <property name="username" value="${jdbc.username}"> 815 </property> 816 <property name="password" value="${jdbc.password}"> 817 </property> 818 </bean> 819 820 ``` 821 822 ```xml 823 <!--4、 配置mybatis SqlSessionFactory --> 824 <bean id="sqlSessionFactory" 825 class="org.mybatis.spring.SqlSessionFactoryBean" scope="singleton" 826 autowire="default"> 827 <!-- 注入数据源 --> 828 <property name="dataSource" ref="dataSource" /> 829 <!-- 关联mybatis配置文件 --> 830 <property name="configLocation" value="classpath:mybatis.xml" /> 831 <!-- mapper加载的位置 --> 832 <property name="mapperLocations" value="classpath:mappers/*" /> 833 <!-- 必须修改 xxxxxx--> 834 <property name="typeAliasesPackage" value="com.mmcor.pregnant.pojo"/> 835 </bean> 836 837 <!--8 扫描mapper --> 838 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 839 <!-- basePackage :mapper接口所在的包 必须修改 --> 840 <property name="basePackage" value="com.mmcor.pregnant.dao" /> 841 </bean> 842 843 ``` 844 845 </beans> 846 847 848 849 #### 编写测试类 850 851 ```java 852 package com.mmcor.pregnant.service.test; 853 854 import org.junit.Test; 855 import org.junit.runner.RunWith; 856 import org.springframework.beans.factory.annotation.Autowired; 857 import org.springframework.test.context.ContextConfiguration; 858 import org.springframework.test.context.junit4.SpringRunner; 859 860 import com.mmcor.pregnant.pojo.User; 861 import com.mmcor.pregnant.service.UserService; 862 863 @RunWith(SpringRunner.class) 864 @ContextConfiguration({"classpath:applicationContext-dao.xml", 865 "classpath:applicationContext-dubbo-provider.xml"}) 866 public class TestUser { 867 868 869 @Autowired 870 UserService us; 871 872 @Test 873 public void testLogin() { 874 875 User user = new User(); 876 user.setUsername("zhangsan"); 877 user.setPassword("123456"); 878 User login = us.login(user); 879 System.err.println("等路成功 ? " + (login!=null) ); 880 881 } 882 883 } 884 885 ``` 886 887 888 889 ### 完成消费者 890 891 #### 编写controller 892 893 小心referenc注解 894 895 ```java 896 package com.mmcor.pregnant.controller; 897 898 import javax.servlet.http.HttpServletRequest; 899 900 import org.apache.dubbo.config.annotation.Reference; 901 import org.springframework.stereotype.Controller; 902 903 import com.mmcor.pregnant.pojo.User; 904 import com.mmcor.pregnant.service.UserService; 905 906 @Controller 907 public class UserController { 908 909 // 特别注意 需要使用apache.dubbo的注解 910 @Reference 911 UserService us; 912 913 public String login(HttpServletRequest request,User user) { 914 User loginUser = us.login(user); 915 if(loginUser==null) { 916 //登录失败 保持在登录的页面 917 return "login"; 918 } 919 920 //登录成功 921 request.getSession().setAttribute("sessionuser", user); 922 return "index"; 923 } 924 925 } 926 927 928 929 930 931 ``` 932 933 #### 完成jsp 934 935 #### 936 937 #### 完成配置文件 938 939 web.xml 940 941 <?xml version="1.0" encoding="UTF-8"?> 942 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 943 xmlns="http://java.sun.com/xml/ns/javaee" 944 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 945 version="2.5"> 946 947 948 ```xml 949 <welcome-file-list> 950 <welcome-file>login.jsp</welcome-file> 951 </welcome-file-list> 952 953 <!-- <context-param> 954 <param-name>contextConfigLocation</param-name> 955 <param-value>classpath:applicationContext*.xml</param-value> 956 </context-param> 957 --> 958 959 <servlet> 960 <servlet-name>springDispatcherServlet</servlet-name> 961 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 962 <init-param> 963 <param-name>contextConfigLocation</param-name> 964 <param-value>classpath:spring-mvc.xml</param-value> 965 </init-param> 966 <load-on-startup>1</load-on-startup> 967 </servlet> 968 <servlet-mapping> 969 <servlet-name>springDispatcherServlet</servlet-name> 970 <url-pattern>/</url-pattern> 971 </servlet-mapping> 972 973 <filter> 974 <filter-name>CharacterEncodingFilter</filter-name> 975 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 976 <init-param> 977 <param-name>encoding</param-name> 978 <param-value>utf-8</param-value> 979 </init-param> 980 </filter> 981 <filter-mapping> 982 <filter-name>CharacterEncodingFilter</filter-name> 983 <url-pattern>/*</url-pattern> 984 </filter-mapping> 985 986 ``` 987 988 </web-app> 989 990 #### 991 992 spring-mvc.xml 993 994 995 996 ```xml 997 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 998 xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" 999 xmlns="http://www.springframework.org/schema/beans" 1000 xmlns:p="http://www.springframework.org/schema/p" 1001 xmlns:context="http://www.springframework.org/schema/context" 1002 xmlns:mvc="http://www.springframework.org/schema/mvc" 1003 xsi:schemaLocation="http://www.springframework.org/schema/beans 1004 http://www.springframework.org/schema/beans/spring-beans.xsd 1005 http://dubbo.apache.org/schema/dubbo 1006 http://dubbo.apache.org/schema/dubbo/dubbo.xsd 1007 http://www.springframework.org/schema/context 1008 http://www.springframework.org/schema/context/spring-context.xsd 1009 http://www.springframework.org/schema/mvc 1010 http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 1011 1012 ``` 1013 1014 1015 1016 ```xml 1017 <!-- 必须修改 --> 1018 <!-- <import resource="classpath:applicationContext-elasticSearch.xml"/> --> 1019 <!-- <import resource="classpath:applicationContext-kafka-consumer.xml"/> --> 1020 <!-- 扫描器 必须修改 --> 1021 <context:component-scan 1022 base-package="com.mmcor.pregnant.controller" /> 1023 <!-- 视图解析图 --> 1024 <bean 1025 class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 1026 <!-- 配置前缀 --> 1027 <property name="prefix" value="/WEB-INF/view/"></property> 1028 <!-- 配置后缀 --> 1029 <property name="suffix" value=".jsp"></property> 1030 </bean> 1031 <!-- 不拦截静态资源 --> 1032 <mvc:default-servlet-handler /> 1033 <!-- mvc注解驱动 --> 1034 <mvc:annotation-driven/> 1035 1036 ``` 1037 1038 1039 1040 ```xml 1041 <!-- 文件上传的处理类 --> 1042 <bean id="multipartResolver" 1043 class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean> 1044 1045 <dubbo:application name="pregnant-web" > 1046 <dubbo:parameter key="qos.enable" value="false"/> 1047 </dubbo:application> 1048 1049 ``` 1050 1051 1052 1053 ```xml 1054 <!-- 注册中心 必须修改 --> 1055 <dubbo:registry 1056 address="zookeeper://192.168.110.134:2181" /> 1057 <!-- 配置扫描包的路径 --> 1058 <dubbo:annotation package="com.mmcor.pregnant.controller"/> 1059 1060 ``` 1061 1062 </beans>