1
zhang
昨天 d6a98e402e4eeeec3a408f184fe11643d76b6bf3
1
2个文件已修改
17个文件已添加
857 ■■■■■ 已修改文件
.gitignore 11 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
.idea/.gitignore 10 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
.idea/misc.xml 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
LICENSE 201 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
README.md 271 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
api/pom.xml 17 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
api/src/main/java/com/zy/entity/User.java 32 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
api/src/main/java/com/zy/service/UserService.java 12 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
rpc-server/pom.xml 42 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
rpc-server/src/main/java/com/zy/SpringBootRpcServerApplication.java 17 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
rpc-server/src/main/resources/application.properties 14 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
rpc-server/target/classes/application.properties 18 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/pom.xml 76 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/src/main/java/com/zy/support/UserServiceSupport.java 26 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
web-server/pom.xml 41 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
web-server/src/main/java/com/zy/SpringBootAplication.java 17 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
web-server/src/main/java/com/zy/controller/UserController.java 22 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
web-server/src/main/resources/application.properties 12 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
web-server/target/classes/application.properties 14 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
.gitignore
@@ -1,4 +1,11 @@
# Compiled class file
*.class
# Log file
*.log
# BlueJ files
*.ctxt
# Mobile Tools for Java (J2ME)
.mtj.tmp/
@@ -6,7 +13,11 @@
# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
.idea/.gitignore
New file
@@ -0,0 +1,10 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Environment-dependent path to Maven home directory
/mavenHomeManager.xml
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
.idea/misc.xml
New file
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="KubernetesApiProvider">{}</component>
</project>
LICENSE
New file
@@ -0,0 +1,201 @@
                                 Apache License
                           Version 2.0, January 2004
                        http://www.apache.org/licenses/
   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
   1. Definitions.
      "License" shall mean the terms and conditions for use, reproduction,
      and distribution as defined by Sections 1 through 9 of this document.
      "Licensor" shall mean the copyright owner or entity authorized by
      the copyright owner that is granting the License.
      "Legal Entity" shall mean the union of the acting entity and all
      other entities that control, are controlled by, or are under common
      control with that entity. For the purposes of this definition,
      "control" means (i) the power, direct or indirect, to cause the
      direction or management of such entity, whether by contract or
      otherwise, or (ii) ownership of fifty percent (50%) or more of the
      outstanding shares, or (iii) beneficial ownership of such entity.
      "You" (or "Your") shall mean an individual or Legal Entity
      exercising permissions granted by this License.
      "Source" form shall mean the preferred form for making modifications,
      including but not limited to software source code, documentation
      source, and configuration files.
      "Object" form shall mean any form resulting from mechanical
      transformation or translation of a Source form, including but
      not limited to compiled object code, generated documentation,
      and conversions to other media types.
      "Work" shall mean the work of authorship, whether in Source or
      Object form, made available under the License, as indicated by a
      copyright notice that is included in or attached to the work
      (an example is provided in the Appendix below).
      "Derivative Works" shall mean any work, whether in Source or Object
      form, that is based on (or derived from) the Work and for which the
      editorial revisions, annotations, elaborations, or other modifications
      represent, as a whole, an original work of authorship. For the purposes
      of this License, Derivative Works shall not include works that remain
      separable from, or merely link (or bind by name) to the interfaces of,
      the Work and Derivative Works thereof.
      "Contribution" shall mean any work of authorship, including
      the original version of the Work and any modifications or additions
      to that Work or Derivative Works thereof, that is intentionally
      submitted to Licensor for inclusion in the Work by the copyright owner
      or by an individual or Legal Entity authorized to submit on behalf of
      the copyright owner. For the purposes of this definition, "submitted"
      means any form of electronic, verbal, or written communication sent
      to the Licensor or its representatives, including but not limited to
      communication on electronic mailing lists, source code control systems,
      and issue tracking systems that are managed by, or on behalf of, the
      Licensor for the purpose of discussing and improving the Work, but
      excluding communication that is conspicuously marked or otherwise
      designated in writing by the copyright owner as "Not a Contribution."
      "Contributor" shall mean Licensor and any individual or Legal Entity
      on behalf of whom a Contribution has been received by Licensor and
      subsequently incorporated within the Work.
   2. Grant of Copyright License. Subject to the terms and conditions of
      this License, each Contributor hereby grants to You a perpetual,
      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
      copyright license to reproduce, prepare Derivative Works of,
      publicly display, publicly perform, sublicense, and distribute the
      Work and such Derivative Works in Source or Object form.
   3. Grant of Patent License. Subject to the terms and conditions of
      this License, each Contributor hereby grants to You a perpetual,
      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
      (except as stated in this section) patent license to make, have made,
      use, offer to sell, sell, import, and otherwise transfer the Work,
      where such license applies only to those patent claims licensable
      by such Contributor that are necessarily infringed by their
      Contribution(s) alone or by combination of their Contribution(s)
      with the Work to which such Contribution(s) was submitted. If You
      institute patent litigation against any entity (including a
      cross-claim or counterclaim in a lawsuit) alleging that the Work
      or a Contribution incorporated within the Work constitutes direct
      or contributory patent infringement, then any patent licenses
      granted to You under this License for that Work shall terminate
      as of the date such litigation is filed.
   4. Redistribution. You may reproduce and distribute copies of the
      Work or Derivative Works thereof in any medium, with or without
      modifications, and in Source or Object form, provided that You
      meet the following conditions:
      (a) You must give any other recipients of the Work or
          Derivative Works a copy of this License; and
      (b) You must cause any modified files to carry prominent notices
          stating that You changed the files; and
      (c) You must retain, in the Source form of any Derivative Works
          that You distribute, all copyright, patent, trademark, and
          attribution notices from the Source form of the Work,
          excluding those notices that do not pertain to any part of
          the Derivative Works; and
      (d) If the Work includes a "NOTICE" text file as part of its
          distribution, then any Derivative Works that You distribute must
          include a readable copy of the attribution notices contained
          within such NOTICE file, excluding those notices that do not
          pertain to any part of the Derivative Works, in at least one
          of the following places: within a NOTICE text file distributed
          as part of the Derivative Works; within the Source form or
          documentation, if provided along with the Derivative Works; or,
          within a display generated by the Derivative Works, if and
          wherever such third-party notices normally appear. The contents
          of the NOTICE file are for informational purposes only and
          do not modify the License. You may add Your own attribution
          notices within Derivative Works that You distribute, alongside
          or as an addendum to the NOTICE text from the Work, provided
          that such additional attribution notices cannot be construed
          as modifying the License.
      You may add Your own copyright statement to Your modifications and
      may provide additional or different license terms and conditions
      for use, reproduction, or distribution of Your modifications, or
      for any such Derivative Works as a whole, provided Your use,
      reproduction, and distribution of the Work otherwise complies with
      the conditions stated in this License.
   5. Submission of Contributions. Unless You explicitly state otherwise,
      any Contribution intentionally submitted for inclusion in the Work
      by You to the Licensor shall be under the terms and conditions of
      this License, without any additional terms or conditions.
      Notwithstanding the above, nothing herein shall supersede or modify
      the terms of any separate license agreement you may have executed
      with Licensor regarding such Contributions.
   6. Trademarks. This License does not grant permission to use the trade
      names, trademarks, service marks, or product names of the Licensor,
      except as required for reasonable and customary use in describing the
      origin of the Work and reproducing the content of the NOTICE file.
   7. Disclaimer of Warranty. Unless required by applicable law or
      agreed to in writing, Licensor provides the Work (and each
      Contributor provides its Contributions) on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
      implied, including, without limitation, any warranties or conditions
      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
      PARTICULAR PURPOSE. You are solely responsible for determining the
      appropriateness of using or redistributing the Work and assume any
      risks associated with Your exercise of permissions under this License.
   8. Limitation of Liability. In no event and under no legal theory,
      whether in tort (including negligence), contract, or otherwise,
      unless required by applicable law (such as deliberate and grossly
      negligent acts) or agreed to in writing, shall any Contributor be
      liable to You for damages, including any direct, indirect, special,
      incidental, or consequential damages of any character arising as a
      result of this License or out of the use or inability to use the
      Work (including but not limited to damages for loss of goodwill,
      work stoppage, computer failure or malfunction, or any and all
      other commercial damages or losses), even if such Contributor
      has been advised of the possibility of such damages.
   9. Accepting Warranty or Additional Liability. While redistributing
      the Work or Derivative Works thereof, You may choose to offer,
      and charge a fee for, acceptance of support, warranty, indemnity,
      or other liability obligations and/or rights consistent with this
      License. However, in accepting such obligations, You may act only
      on Your own behalf and on Your sole responsibility, not on behalf
      of any other Contributor, and only if You agree to indemnify,
      defend, and hold each Contributor harmless for any liability
      incurred by, or claims asserted against, such Contributor by reason
      of your accepting any such warranty or additional liability.
   END OF TERMS AND CONDITIONS
   APPENDIX: How to apply the Apache License to your work.
      To apply the Apache License to your work, attach the following
      boilerplate notice, with the fields enclosed by brackets "[]"
      replaced with your own identifying information. (Don't include
      the brackets!)  The text should be enclosed in the appropriate
      comment syntax for the file format. We also recommend that a
      file or class name and description of purpose be included on the
      same "printed page" as the copyright notice for easier
      identification within third-party archives.
   Copyright [yyyy] [name of copyright owner]
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
       http://www.apache.org/licenses/LICENSE-2.0
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
README.md
@@ -1,4 +1,273 @@
## algo-java
## 介绍
Dubbo是一款高性能、轻量级的开源Java RPC(Remote Procedure
Call,远程过程调用)框架,它提供了分布式系统中服务之间的高效、透明化的服务调用方案。Dubbo最初由阿里巴巴公司开发并开源,现已成为Apache顶级项目。
## 架构演变
在了解dubbo之后,我们要知道dubbo在当今互联网架构中在什么样的位置,我们先来看一个架构演进图
![image-20240611185459491](img/87db65d9f5a8ff7acba7367a440ef7a5.png)
而了解过feign的同学这里就可以拿它和dubbo对比,在上面的图中,feign就是在微服务架构承担rpc框架,而dubbo就在在上面的SOA((Service-Oriented
Architecture,面向服务的架构)是一个组件模型,它将应用程序的不同功能单元(称为服务)进行拆分,并通过这些服务之间定义良好的接口和契约联系起来。)架构中承担rpc框架的角色。
SOA架构,就像是大部门制,一个部门管的事儿比较多,几个相关的工作一块儿干,这样大家协作起来方便,但是部门和部门之间还是得互相配合,有时候沟通多了就复杂了。而且,这种模式下,大家用的工具和流程尽量统一,为了好配合。
微服务架构呢,就像是小团队制,每个小团队就专攻一件事,特别专注,自己管自己那一块,出了问题也不容易影响别人。这种模式灵活多了,每个小团队还能根据自己的喜好选最好的工具干活,更新换代快。不过,这么多小团队,怎么让它们高效合作,也得好好设计一套规则。
## DUBBO
说了这么多,其实dubbo的精髓就是**把自己的api暴露出去给大家,具体的实现继承自己的api去写实现方法。**
但是feign就可以完全脱离api提供,可以提供一个restful的http接口,然后其他服务可以直接通过feign进行调用,这也导致了feign的耦合度更低,但是feign在实际开发过程中也可以模仿dubbo暴露api的方式进行架构的搭建。
之前我们快速演示一下了dubbo,[每天一个新知识之 SpringBoot+Dubbo 的快速入门](https://blog.csdn.net/csdnerM/article/details/125410232)
要使用dubbo首先要搭建一个注册中心,注册中心的目的就是为了让其他的服务调用dubbo服务,在dubbo官方,推荐使用Zookeeper作为注册中心。
[公司居然还在用zookeeper,吓得我马上学习了相关命令!](https://juejin.cn/post/7376930488738938920)
[Linux安装Zookeeper(图文解说详细版)](https://masiyi.blog.csdn.net/article/details/121848173)
> ![在这里插入图片描述](img/a39c0b1e57d24af9aa49d129a81cbe69.jpeg#pic_center)
虽然之前也写过一篇dubbo的快速入门,但是写的简单,时隔两年,这次介绍一些Dubbo的高级特性。
## 框架
我们先从框架的搭建说起
![image-20240615204929083](img/a0907aaaaa99e487ec892bab78e1efeb.png)
这次我们一共建了四个单独的模块,他们没有共同的父模块,但是他们之间有依赖关系,他们之间的关系是这样的:
![image-20240615210608867](img/3f2a3f339c9e70dc7655883a6eff85cb.png)
在service模块中引入了Dubbo的jar包,这样,一套整体的dubbo模块框架就出来了
```
      <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>
```
他们的作用分别是:
api模块提供最基础的实体类,枚举类等,最重要是需要暴露出去的抽象接口。
service写具体的接口的实现类,提供具体的实现类。
rpc-server里面是一个启动类,因为依赖service模块,可以理解为生产提供者。
web-server则是主要提供http接口给第三方,如前端调用,也可以引入其他项目的api模块,之后只要有生产者注入到注册中心就可以提供服务了,直接调用api里面的抽象接口方法。
说这么多,我们来看里面的具体实现代码,由于篇幅问题,请移至代码仓库查看代码:
[https://gitee.com/wangfugui-ma/springboot-dubbo/tree/dubbo/](https://gitee.com/wangfugui-ma/springboot-dubbo/tree/dubbo/)
那么,dubbo是需要搭配zookeeper去使用,由于上一篇文章已经讲过了,这里就不再赘述。我们怎么去使用dubbo的一些高级特性呢
## 高级特性
### 序列化
由于我们的实体类是写在api里面的,而api又是供各个项目去引用的,所以如果我们要在其他的项目中获取这个类,例如在这个项目中的queryUser方法中。
```java
package com.zy.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.zy.entity.User;
import com.zy.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
    @Reference//远程注入
    private UserService userService;
    @GetMapping("/user")
    public String user() {
        User user = userService.queryUser();
        return user.toString();
    }
}
```
我们的User对象必须实现Serializable接口,例如这样
```java
package com.zy.entity;
import java.io.Serializable;
public class User implements Serializable {
    private String name;
    private Integer age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
```
### 超时
#### 生产者超时
如果我们的生产者需要设置一个超时时间,我们可以在提供方的Service注解中加入timeout属性,就像这样:
```java
package com.zy.support;
import com.alibaba.dubbo.config.annotation.Service;
import com.zy.entity.User;
import com.zy.service.UserService;
import org.springframework.stereotype.Component;
@Service(timeout = 3000)
@Component
public class UserServiceSupport implements UserService {
    /**
     * 查询user
     */
    @Override
    public User queryUser() {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        User user = new User();
        user.setAge(18);
        user.setName("王富贵");
        return user;
    }
}
```
这样当我们的controller接口调用超过3秒的时候就会报错:
```
com.alibaba.dubbo.remoting.TimeoutException: Waiting server-side response timeout. start time: 2024-06-15 21:24:14.504, end time: 2024-06-15 21:24:17.511, client elapsed: 1 ms, server elapsed: 3006 ms, timeout: 3000 ms, request: Request [id=2, version=2.0.0, twoway=true, event=false, broken=false, data=RpcInvocation [methodName=queryUser, parameterTypes=[], arguments=[], attachments={path=com.zy.service.UserService, interface=com.zy.service.UserService, version=0.0.0, timeout=3000}]], channel: /10.8.0.136:8061 -> /10.8.0.136:20885
```
这样我们就从生产方加入了超时控制
#### 消费者超时
其实我们也可以从消费者这边控制超时时间,就像这样:
```
package com.zy.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.zy.entity.User;
import com.zy.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
    @Reference(timeout = 1000)//远程注入
    private UserService userService;
    @GetMapping("/user")
    public String user() {
        User user = userService.queryUser();
        return user.toString();
    }
}
```
在Reference注解中加入timeout属性也是可以达到一样的效果:
```
com.alibaba.dubbo.remoting.TimeoutException: Waiting server-side response timeout. start time: 2024-06-15 21:25:25.398, end time: 2024-06-15 21:25:26.399, client elapsed: 0 ms, server elapsed: 1001 ms, timeout: 1000 ms, request: Request [id=2, version=2.0.0, twoway=true, event=false, broken=false, data=RpcInvocation [methodName=queryUser, parameterTypes=[], arguments=[], attachments={path=com.zy.service.UserService, interface=com.zy.service.UserService, version=0.0.0, timeout=1000}]], channel: /10.8.0.136:8570 -> /10.8.0.136:20885
```
### @Autowired
除了使用@Reference注解注入dubbo服务外,还可以使用@Autowired去注入dubbo服务
![image-20240625163448223](img/1d70a633d9eb9f22e02faaa4283216b4.png)
### 配置文件
```
dubbo.application.name=web-server
dubbo.registry.address=zookeeper://127.0.0.1:2181
```
在配置文件中写上最基础的两个配置,dubbo.registry.address可以不带zookeeper前缀,但是要加个配置key
```
# 如果不加这个,就需要在address里面加zookeeper://
dubbo.registry.protocol=zookeeper
```
注意这个配置:
```
dubbo.protocol.port=20885
```
dubbo默认用20880端口,如果你需要在一个服务器上面启动多个dubbo服务,那就不能让他们的端口冲突
```
dubbo.registry.password=zk_password
dubbo.registry.username=zk_user
```
如果你的节点有acl权限,则可能需要加上这两个配置,替换value
如果你还不知道acl权限是什么,推荐你看一下这篇文章
[公司居然还在用zookeeper,吓得我马上学习了相关命令!](https://juejin.cn/post/7376930488738938920)
### @EnableDubbo
记得在启动类开启这个注解,否则不能使用dubbo的相关服务,你懂的。。。。
> ![在这里插入图片描述](img/31dc8042deb97d9fdd67c146bd3fac11.png)
api/pom.xml
New file
@@ -0,0 +1,17 @@
<?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.zy</groupId>
    <artifactId>api</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>
api/src/main/java/com/zy/entity/User.java
New file
@@ -0,0 +1,32 @@
package com.zy.entity;
import java.io.Serializable;
public class User implements Serializable {
    private String name;
    private Integer age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
api/src/main/java/com/zy/service/UserService.java
New file
@@ -0,0 +1,12 @@
package com.zy.service;
import com.zy.entity.User;
//todo 这里写一个自定义注解,之后通过自定义注解来获取该接口和实现类将其注册到注册中心
public interface UserService {
    /**
     * 查询user
     */
    public User queryUser();
}
rpc-server/pom.xml
New file
@@ -0,0 +1,42 @@
<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zy</groupId>
    <artifactId>rpc-server</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.zy</groupId>
            <artifactId>service</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>
rpc-server/src/main/java/com/zy/SpringBootRpcServerApplication.java
New file
@@ -0,0 +1,17 @@
package com.zy;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class SpringBootRpcServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootRpcServerApplication.class, args);
    }
}
rpc-server/src/main/resources/application.properties
New file
@@ -0,0 +1,14 @@
# ????
spring.application.name=rpc-server
server.port=8082
dubbo.application.name=rpc-server
dubbo.registry.address=zookeeper://127.0.0.1:2181
#dubbo.registry.protocol=zookeeper
#dubbo.registry.password=dreamtech
#dubbo.registry.username=zk_user
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
dubbo.monitor.protocol=registry
rpc-server/target/classes/application.properties
New file
@@ -0,0 +1,18 @@
# ????
spring.application.name=rpc-server
server.port=8082
dubbo.application.name=rpc-server
dubbo.registry.address=zookeeper://127.0.0.1:2181
#dubbo.registry.protocol=zookeeper
#dubbo.registry.password=dreamtech
#dubbo.registry.username=zk_user
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
dubbo.monitor.protocol=registry
service/pom.xml
New file
@@ -0,0 +1,76 @@
<?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.zy</groupId>
    <artifactId>service</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <dubbo.version>2.7.8</dubbo.version>
        <curator.version>4.2.0</curator.version>
        <curator-recipes.version>2.8.0</curator-recipes.version>
        <zookeeper.version>3.6.3</zookeeper.version>
    </properties>
    <dependencies>
        <!-- Dubbo Spring Boot Starter -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.8</version>
        </dependency>
        <!-- Dubbo Spring Boot Starter -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>${dubbo.version}</version>
        </dependency>
        <!-- zookeeper注册中心 需要导入zookeeper依赖  -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>${zookeeper.version}</version>
            <!-- 提示与zookeeper中的slf4j-log4j12-1.7.29.jar 包冲突-->
            <!--排除这个slf4j-log4j12-->
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- Zookeeper 客户端 -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>${curator.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>${curator-recipes.version}</version>
        </dependency>
        <dependency>
            <groupId>com.zy</groupId>
            <artifactId>api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>
service/src/main/java/com/zy/support/UserServiceSupport.java
New file
@@ -0,0 +1,26 @@
package com.zy.support;
import com.zy.entity.User;
import com.zy.service.UserService;
import org.apache.dubbo.config.annotation.DubboService;
@DubboService // 该注解用于将当前类对象存入spring容器中,既可以本项目服务使用,父接口注解能力也可以提供给其他项目使用
public class UserServiceSupport implements UserService {
    /**
     * 查询user
     */
    @Override
    public User queryUser() {
//        try {
//            Thread.sleep(5000);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
        User user = new User();
        user.setAge(18);
        user.setName("张三");
        return user;
    }
}
web-server/pom.xml
New file
@@ -0,0 +1,41 @@
<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zy</groupId>
    <artifactId>web-server</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.zy</groupId>
            <artifactId>service</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>
web-server/src/main/java/com/zy/SpringBootAplication.java
New file
@@ -0,0 +1,17 @@
package com.zy;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class SpringBootAplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootAplication.class, args);
    }
}
web-server/src/main/java/com/zy/controller/UserController.java
New file
@@ -0,0 +1,22 @@
package com.zy.controller;
import com.zy.entity.User;
import com.zy.service.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
    @DubboReference(timeout = 1000)//远程注入
    private UserService userService;
    @GetMapping("/user")
    public String user() {
        User user = userService.queryUser();
        return user.toString();
    }
}
web-server/src/main/resources/application.properties
New file
@@ -0,0 +1,12 @@
# ????
spring.application.name=web-server
# ???? WEB ????
server.port=8083
dubbo.protocol.name=dubbo
dubbo.application.name=web-server
dubbo.registry.address=zookeeper://127.0.0.1:2181
#dubbo.registry.password=dreamtech
#dubbo.registry.username=zk_user
dubbo.monitor.protocol=registry
dubbo.protocol.port=20885
web-server/target/classes/application.properties
New file
@@ -0,0 +1,14 @@
# ????
spring.application.name=web-server
# ???? WEB ????
server.port=8083
dubbo.protocol.name=dubbo
dubbo.application.name=web-server
dubbo.registry.address=zookeeper://127.0.0.1:2181
#dubbo.registry.password=dreamtech
#dubbo.registry.username=zk_user
dubbo.monitor.protocol=registry
dubbo.protocol.port=20885