02.Dubbo 应用之 2.7.3 版本

1. 应用环境搭建

1.1 配置文件
1. pom.xml
<dependencies>
    <!-- dubbo -->
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo</artifactId>
        <version>2.7.3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-rpc-http</artifactId>
        <version>2.7.3</version>
    </dependency>

    <!-- zookeeper -->
    <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>3.4.13</version>
    </dependency>

    <!-- curator -->
    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-framework</artifactId>
        <version>2.9.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-recipes</artifactId>
        <version>2.9.1</version>
    </dependency>

</dependencies>
2. provider.xml
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <dubbo:application name="demo-provider">
        <dubbo:parameter key="qos.enable" value="false"/>
    </dubbo:application>

    <!-- 配置中心地址 -->
    <dubbo:config-center address="zookeeper://127.0.0.1:2181"/>

    <dubbo:protocol name="dubbo" port="20880"/>
    <dubbo:protocol name="dubbo" port="20881"/>
    <dubbo:protocol name="dubbo" port="20882"/>

    <dubbo:service interface="pers.masteryourself.study.dubbo.api.DemoService" ref="demoService"/>
    <bean id="demoService" class="pers.masteryourself.study.dubbo.provider.DemoServiceImpl"/>

</beans>
3. consumer.xml
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <dubbo:application name="demo-consumer">
        <dubbo:parameter key="qos.enable" value="false"/>
    </dubbo:application>

    <!-- 配置中心地址 -->
    <dubbo:config-center address="zookeeper://127.0.0.1:2181"/>

    <!-- 配置标签路由 -->
    <dubbo:reference id="demoService" interface="pers.masteryourself.study.dubbo.api.DemoService" timeout="3000"
                   tag="test"/>

</beans>
4. log4j.properties
###set log levels###
log4j.rootLogger=info, stdout
###output to the console###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d{dd/MM/yy HH:mm:ss:SSS z}] %t %5p %c{2}: %m%n
1.2 代码
1. DemoService
public interface DemoService {

    String sayHello(String name);

}
2. DemoServiceImpl
public class DemoServiceImpl implements DemoService {

    @Override
    public String sayHello(String name) {
        URL url = RpcContext.getContext().getUrl();
        String message = String.format(" protocol is %s, address is %s", url.getProtocol(), url.getAddress());
        // cluster 策略演示
        try {
            System.out.println("调用此方法了。。。" + message);
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "你好:" + name + message;
    }

}
3. Provider
public class Provider {

    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:provider.xml");
        context.start();
        System.in.read();
    }

}
4. Consumer
public class Consumer {

    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
        context.start();
        DemoService demoService = context.getBean("demoService", DemoService.class);
        while (true){
            try {
                System.out.println(demoService.sayHello("dubbo"));
            } catch (Exception e) {
                System.out.println("报错了:" + e.getMessage());
            }
            TimeUnit.SECONDS.sleep(2);
        }
        // cluster 策略演示
        //System.out.println(demoService.sayHello("dubbo"));
    }

}

2. dubbo-admin 使用

  • 代码已经上传至 https://github.com/masteryourself/dubbo-admin.git ,分支是 dubbo-admin-2.7.x

  • dubbo-admin-ui 启动:

    cd dubbo-admin-ui
    
    npm run dev
    
  • dubbo-admin-server 启动:DubboAdminApplication

2.1 配置管理
  • 消费者和提供者可以不需要配置 dubbo.registry.addressdubbo.metadata-report.address 属性,但需要配置 dubbo:config-center 地址,然后把其他配置迁移到配置中心上

  • 配置管理 -> 创建

image

dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.metadata-report.address=zookeeper://127.0.0.1:2181
2.2 动态配置
  • 这里添加一个超时时间 6s

  • 服务治理 -> 动态配置 -> 创建

image

configVersion: v2.7
enabled: true
configs:
  - side: consumer
    addresses:
      - 0.0.0.0
    parameters:
      timeout: 6000

2.3 标签路由
  • 给服务器分组,调用方根据 tag 值路由到对应的服务提供者机器上

  • 服务治理 -> 标签路由 -> 创建

image

enabled: true
force: false
runtime: false
tags:
  - name: dev
    addresses:
      - '192.168.89.1:20880'
      - '192.168.89.1:20881'
  - name: test
    addresses:
      - '192.168.89.1:20882'
发布了37 篇原创文章 · 获赞 3 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/masteryourself/article/details/102411712