dubbo相关配置

释放双眼,带上耳机,听听看~!

在上一篇dubbo入门这篇文章中,我们主要通过xml配置的方式,来演示了Dubbo框架的基本使用。在这一篇中,我们详细介绍一下有关dubbo框架的详细细节以及其它配置方式。


  • 属性配置

在dubbo框架中,我们除了使用xml配置的方式外,还可以通过dubbo.properties配置文件的方式,来配置dubbo相关参数,并且服务启动时dubbo会自动加载classpath根⽬录下的dubbo.properties文件,我们也可以通过JVM启动参数设置:Ddubbo.properties.file=xxx.properties。属性配置和xml配置基本一样,就是将xml配置中的标签名加属性名修改成⽤点分隔。例如,我们上篇文章中介绍的

<dubbo:application name="jilinwula" />
<dubbo:registry address="zookeeper://127.0.0.1:2181" />

标签我们改成配置方式配置如下:

dubbo.application.name=jiinwula
dubbo.registry.address=zookeeper://127.0.0.1:2181

下面我们看一下这3种配置参数之间的优先级。JVM参数优先、XML配置其次, 最后才是dubbo.properties文件的配置。


  • API配置

因为API配置都是代码编码方式,所以我们不做过多介绍,而是直接看代码,具体代码如下:

/**
 * 启动Dubbo提供方
 */
public class Provider {
    public static void main(String[] args) throws IOException, InterruptedException {
        // 服务实现
        HelloWorld helloWorld = new HelloWorldImpl();

        // 提供方信息
        ApplicationConfig application = new ApplicationConfig();
        application.setName("jilinwula");

        // 连接注册中⼼配置
        RegistryConfig registry = new RegistryConfig();
        registry.setAddress("zookeeper://127.0.0.1:2181");

        // 服务提供者协议配置
        ProtocolConfig protocol = new ProtocolConfig();
        protocol.setName("dubbo");
        protocol.setPort(20880);

        // 服务提供者暴露服务配置
        ServiceConfig<HelloWorld> service = new ServiceConfig<HelloWorld>();
        service.setApplication(application);
        service.setRegistry(registry);
        service.setProtocol(protocol);
        service.setInterface(HelloWorld.class);
        service.setRef(helloWorld);
        service.setVersion("1.0.0");

        // 暴露及注册服务
        service.export();

        Thread.currentThread().join();
    }
}
/**
 * 服务调用方
 */
public class Consumer {
    public static void main(String[] args) {
        // 调用方信息
        ApplicationConfig application = new ApplicationConfig();
        application.setName("jilinwula");

        // 连接注册中心
        RegistryConfig registry = new RegistryConfig();
        registry.setAddress("zookeeper://127.0.0.1:2181");

        ReferenceConfig<HelloWorld> reference = new ReferenceConfig<HelloWorld>();
        reference.setApplication(application);
        reference.setRegistry(registry);
        reference.setInterface(HelloWorld.class);
        reference.setVersion("1.0.0");

        // 调用接口
        HelloWorld helloWorld = reference.get();
        System.out.println(helloWorld.getHelloWorld());
    }
}
img

执行结果:


下面我们了解一下怎么用API方式设置方法级别的调用具体代码如下:

/**
 * 服务调用方
 */
public class Consumer {
    public static void main(String[] args) {
        // 调用方信息
        ApplicationConfig application = new ApplicationConfig();
        application.setName("jilinwula");

        // 连接注册中心
        RegistryConfig registry = new RegistryConfig();
        registry.setAddress("zookeeper://127.0.0.1:2181");

        // 方法级配置
        List<MethodConfig> methods = new ArrayList<MethodConfig>();
        MethodConfig method = new MethodConfig();
        method.setName("getHelloWorld");
        methods.add(method);

        ReferenceConfig<HelloWorld> reference = new ReferenceConfig<HelloWorld>();
        reference.setApplication(application);
        reference.setRegistry(registry);
        reference.setInterface(HelloWorld.class);
        reference.setVersion("1.0.0");
        reference.setMethods(methods);

        // 调用接口
        HelloWorld helloWorld = reference.get();
        System.out.println(helloWorld.getHelloWorld());
    }
}

  • 点对点直连

使用点对点直连的方式,将直接绕过注册中心。下面为具体的代码:

/**
 * 服务调用方
 */
public class Consumer {
    public static void main(String[] args) {
        // 调用方信息
        ApplicationConfig application = new ApplicationConfig();
        application.setName("jilinwula");

        ReferenceConfig<HelloWorld> reference = new ReferenceConfig<HelloWorld>();
        reference.setUrl("dubbo://192.168.5.179:20880/com.jilinwula.api.HelloWorld");
        reference.setApplication(application);
        reference.setInterface(HelloWorld.class);
        reference.setVersion("1.0.0");

        HelloWorld helloWorld = reference.get();
        System.out.println(helloWorld.getHelloWorld());
    }
}

  • 注解配置
/**
 * 接口实现类
 */
@Service(version = "1.0.0")
public class HelloWorldImpl implements HelloWorld {
    public String getHelloWorld() {
        return "Hello World Dubbo";
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 提供方信息-->
    <dubbo:application name="jilinwula" />

    <!-- 使用zookeeper暴露服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />

    <!-- 暴露服务端口 -->
    <dubbo:protocol name="dubbo" port="20880" />

    <!-- 自动扫描注解 -->
    <dubbo:annotation package="com.jilinwula.provider"/>

</beans>
<?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="jilinwula" />

<!-- 使用zookeeper查找服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />

<!-- 自动扫描注解 -->
<dubbo:annotation package="com.jilinwula.consumer"/>

</beans>
/**
 * 服务调用方
 */
@Component
public class Consumer {

    @Reference(version = "1.0.0")
    private static HelloWorld helloWorld;

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"consumer.xml"});
        context.start();
        System.out.println(helloWorld.getHelloWorld());

    }
}

执行结果:

img
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧