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 使用

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'
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章