dubbo学习日志(一)--搭建简单的dubbo框架(hello world)

简单了解一下dubbo:

1:简单说dubbo是一个由阿里巴巴开发的高性能、透明化的RPC框。

2:RPC又是什么:也就是远程调用。比如服务器A要调用服务器B上的方法,不在一个机器上要怎么调用,这时我们想到的是网络。rpc就干这事,就跟我们调用本地方法一样,不像调用web接口那么麻烦。

通过百度,又了解到dubbo的特点

  透明化的远程方法调用:  像调用本地方法一样调用远程方法;只需简单配置,没有任何API侵入

  软负载均衡及容错机制:  可在内网替代f5等硬件负载均衡器

  服务注册中心自动注册 & 配置管理:  不需要写死服务提供者地址,注册中心基于接口名自动
  查询提供者ip.使用类似zookeeper等分布式协调服务作为服务注册中心,可以将绝大部分项
  目配置移入zookeepeer集群

  服务接口监控与治理: Dubbo-admin与Dubbo-monitor提供了完善的服务接口管理与监控功能
  针对不同应用的不同接口,可以进行 多版本,多协议,多注册中心管理。

duubo架构

理论知识很多。本文主要讲搭建一个demo工程。

首先创建一个maven工程用于当作父级工程。把dubbo和spring依赖进来,还有注册中心zookeeper(当然也可以选择其他的)

<properties>
    <spring.version>4.3.11.RELEASE</spring.version>
</properties>
<dependencies>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo</artifactId>
        <version>2.5.3</version>
        <exclusions>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <!--dubbo注册中心-->
    <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>3.4.13</version>
    </dependency>
    <!--zookeeper客户端-->
    <dependency>
        <groupId>com.github.sgroschupf</groupId>
        <artifactId>zkclient</artifactId>
        <version>0.1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1.3</version>
    </dependency>
</dependencies>

既然使用了zookeeper,那么在此之前得去把zookeeper下载下来。修改了zookeeper的一些配置之后点击zkServer.cmd运行zk

切记pom文件添加

<packaging>pom</packaging>

下面创建子工程,肯定分为三个子模块分别为服务、内容提供者、消费者

ok。点击父工程 右键-->new-->module  分别创建三个子模块   我分别命名为 myService myProvider、myConsumer

消费者和内容提供者的pom文件要依赖于myService。因为两者都引用了service的代码(service提供了接口,myProvider进行了实现,myConsumer则调用了那个接口)

所以两者都添加以下依赖

<dependencies>
    <dependency>
        <groupId>dwj</groupId>
        <artifactId>myService</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
</dependencies>

groupId则是你的项目的groupId别跟我的写一样了 artifactId为service模块的名称

service模块的pom则添加以下配置

<packaging>jar</packaging>

然后开始写第一个程序:

写一个hello world:

1:现在service创建一个服务:

public interface First {
    String firstMethod(String str);
}

2:创建在myProvider项目(提供者)创建相应的实现:

public class FirstImpl implements First{

    public String firstMethod(String str) {
        return "hello: "+str;
    }
}

创建配置文件provider.xml。名字随意,只要知道就好,但是最好还是遵循见名知意的规则

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
                   http://code.alibabatech.com/schema/dubbo
                       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <dubbo:application name="hello-provider"/>
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    <dubbo:protocol name="dubbo" port="20880"/>
    <bean id="FirstImpl" class="com.serviceImpl.StringOperationImpl"/>
    <dubbo:service interface="First" ref="FirstImpl"/>
</beans>

interface为service的类全路径,ref为接口的实现

 

然后编写main程序去运行服务端,

public class ProviderServer {
    public static void main(String[] args){
        ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("provider.xml");
        applicationContext.start();
        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

没什么问题的话现在服务端已经正常提供服务了,现在要做的,只是让消费者去调用这些服务。

首先在myConsumer工程编写配置文件cunsumer.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans.xsd
                   http://code.alibabatech.com/schema/dubbo
                       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    <dubbo:application name="hello-consumer"/>
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>

    <dubbo:reference interface="First" id="helloWorld"/>
</beans>

配置zk 和接口的bean  interface就是那个service的类全路径,id则是bean name

在myConsumer工程编写一个main程序去调用服务

public class ConsumerClient {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext xaa = new ClassPathXmlApplicationContext("consumer.xml");
        First sss = (First) xaa.getBean("helloWorld");
        String s= sss.firstMethod("tracy !!!!!!!");
        System.out.println(s);
    }
}

运行,如果不报错的话,会发现控制台打印了 hello tracy !!!!!

那么hello world程序就这么写完了。还蛮简单的,这种简单程序

 

 

 

 

 

 

 

### IntelliJ IDEA 中通义 AI 功能介绍 IntelliJ IDEA 提供了一系列强大的工具来增强开发体验,其中包括与通义 AI 相关的功能。这些功能可以帮助开发者更高效地编写代并提高生产力。 #### 安装通义插件 为了使用通义的相关特性,在 IntelliJ IDEA 中需要先安装对应的插件: 1. 打开 **Settings/Preferences** 对话框 (Ctrl+Alt+S 或 Cmd+, on macOS)。 2. 导航到 `Plugins` 页面[^1]。 3. 在 Marketplace 中搜索 "通义" 并点击安装按钮。 4. 完成安装后重启 IDE 使更改生效。 #### 配置通义服务 成功安装插件之后,还需要配置通义的服务连接信息以便正常使用其提供的各项能力: - 进入设置中的 `Tools | Qwen Coding Assistant` 菜单项[^2]。 - 填写 API Key 和其他必要的认证参数。 - 测试连接以确认配置无误。 #### 使用通义辅助编程 一旦完成上述准备工作,就可以利用通义来进行智能编支持了。具体操作如下所示: ##### 自动补全代片段 当输入部分语句时,IDE 将自动提示可能的后续逻辑,并允许一键插入完整的实现方案[^3]。 ```java // 输入 while 循环条件前半部分... while (!list.isEmpty()) { // 激活建议列表选择合适的循环体内容 } ``` ##### 解释现有代含义 选中某段复杂的表达式或函数调用,右键菜单里会有选项可以请求通义解析这段代的作用以及优化意见。 ##### 生产测试案例 对于已有的业务逻辑模块,借助于通义能够快速生成单元测试框架及初始断言集,减少手动构建的成本。 ```python def test_addition(): result = add(2, 3) assert result == 5, f"Expected 5 but got {result}" ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值