dubbo简单demo

Dubbo角色结构

  • Provider: 服务提供者
  • Consumer: 服务消费者
  • Registry: 注册中心
  • Monitor: 监控中心
  • Container: 容器

其实看起来很像消息队列的发布-订阅模式,有提供者和消费者.
大概流程就是提供者在注册中心发布,消费者去注册中心发现服务并订阅服务

1.无注册中心模式

无注册中心,其实相当于消费者直接使用httpclient去请求提供者.

1.1Provider

1.1.1接口和服务

public interface ProService {
	String sayHello();
}
public class ProServiceImpl implements ProService{
	@Override
	public String sayHello() {
		System.out.println("==========>>>>>");
		return "HELLO";
	}
}

1.1.2xml配置

dubbo是建立在spring上的,鼓励以xml方式配置.
在classpath下写个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="providerByAaa" owner="aaa">
        <dubbo:parameter key="qos.enable" value="true"/>
        <dubbo:parameter key="qos.accept.foreign.ip" value="false"/>
        <dubbo:parameter key="qos.port" value="55555"/>
    </dubbo:application>

    <dubbo:monitor protocol="registry"/>

    <!--dubbo这个服务所要暴露的服务地址所对应的注册中心-->
    <!--<dubbo:registry address="N/A"/>-->
    <dubbo:registry address="N/A" />

    <!--当前服务发布所依赖的协议;webserovice、Thrift、Hessain、http-->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!--服务发布的配置,需要暴露的服务接口-->
    <dubbo:service
            interface="com.db.provider.service.ProService"
            ref="providerService"/>

    <!--Bean bean定义-->
    <bean id="providerService" class="com.db.provider.service.ProServiceImpl"/>

</beans>

上面的xml发布了一个服务,并且没有注册中心,发布的服务是ProService

1.1.3启动类

public class ProviderApp {
	public static void main( String[] args ) throws IOException {
        //加载xml配置文件启动
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
        context.start();
        System.in.read(); // 按任意键退出
    }
}

1.2Consumer

消费者不需要写服务,只需要配置xml

1.2.1consumer.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="consumerByAaa" owner="aaa"/>

    <!--dubbo这个服务所要暴露的服务地址所对应的注册中心-->
    <!--点对点的方式-->
    <dubbo:registry address="N/A" />
    <!--<dubbo:registry address="zookeeper://localhost:2181" check="false"/>-->

    <!--生成一个远程服务的调用代理-->
    <!--点对点方式-->
    <dubbo:reference id="providerService"
                     interface="com.db.provider.service.ProService"
                     url="dubbo://192.168.116.1:20880/com.db.provider.service.ProService"/>

    <!--<dubbo:reference id="providerService"
                     interface="com.sihai.dubbo.provider.service.ProviderService"/>-->

</beans>

由于没有使用注册中心,我们通过dubbo协议直接请求提供者暴露出来的服务.相当于使用HttpClient

1.2.2启动类

public class CsApp {
	public static void main(String[] args) throws IOException {
		ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("consumer.xml");
        context.start();
        ProService providerService = (ProService) context.getBean("providerService");
        System.out.println(providerService.sayHello());;
        System.in.read();
	}
}

运行如下结果调用了提供者提供的服务,打印了服务的返回值HELLO

[INFO ] 2021-02-26 21:13:58,594(0) --> [main] org.springframework.context.support.AbstractApplicationContext.prepareRefresh(AbstractApplicationContext.java:583): Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2b05039f: startup date [Fri Feb 26 21:13:58 CST 2021]; root of context hierarchy  
[INFO ] 2021-02-26 21:13:58,636(42) --> [main] org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:317): Loading XML bean definitions from class path resource [consumer.xml]  
[INFO ] 2021-02-26 21:13:58,730(136) --> [main] : using logger: com.alibaba.dubbo.common.logger.log4j.Log4jLoggerAdapter  
[WARN ] 2021-02-26 21:13:58,845(251) --> [main] com.alibaba.dubbo.config.spring.extension.SpringExtensionFactory.getExtension(SpringExtensionFactory.java:75):  [DUBBO] No spring extension (bean) named:defaultCompiler, try to find an extension (bean) of type java.lang.String, dubbo version: 2.6.6, current host: 192.168.116.1  
[WARN ] 2021-02-26 21:13:58,845(251) --> [main] com.alibaba.dubbo.config.spring.extension.SpringExtensionFactory.getExtension(SpringExtensionFactory.java:93):  [DUBBO] No spring extension (bean) named:defaultCompiler, type:java.lang.String found, stop get bean., dubbo version: 2.6.6, current host: 192.168.116.1  
[INFO ] 2021-02-26 21:13:59,544(950) --> [main] com.alibaba.dubbo.remoting.transport.AbstractClient.connect(AbstractClient.java:282):  [DUBBO] Successed connect to server /192.168.116.1:20880 from NettyClient 192.168.116.1 using dubbo version 2.6.6, channel is NettyChannel [channel=[id: 0x28f80f9e, L:/192.168.116.1:62231 - R:/192.168.116.1:20880]], dubbo version: 2.6.6, current host: 192.168.116.1  
[INFO ] 2021-02-26 21:13:59,544(950) --> [main] com.alibaba.dubbo.remoting.transport.AbstractClient.<init>(AbstractClient.java:91):  [DUBBO] Start NettyClient DESKTOP-7F0LPC8/192.168.116.1 connect to the server /192.168.116.1:20880, dubbo version: 2.6.6, current host: 192.168.116.1  
[INFO ] 2021-02-26 21:13:59,580(986) --> [main] com.alibaba.dubbo.config.ReferenceConfig.createProxy(ReferenceConfig.java:429):  [DUBBO] Refer dubbo service com.db.provider.service.ProService from url dubbo://192.168.116.1:20880/com.db.provider.service.ProService?application=consumerByAaa&dubbo=2.0.2&interface=com.db.provider.service.ProService&methods=sayHello&owner=aaa&pid=5692&register.ip=192.168.116.1&side=consumer&timestamp=1614345238984, dubbo version: 2.6.6, current host: 192.168.116.1  

HELLO

2.注册中心模式

没有添加注册中心,服务提供者如果变更导致url改变,那么消费者也需要改变代码.
添加一个注册中心,我们的消费者不必指向提供者,而是去注册中心去索要.提供者的变更不会影响消费者.
在这种情况下,相比于无注册中心模式,只需要改动xml即可.
当然,需要注意的,你需要提前拥有一个注册中心,这里例子里是zookeeper:192.168.18.19:2181

2.1Provider.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="providerByAaa" owner="aaa">
        <dubbo:parameter key="qos.enable" value="true"/>
        <dubbo:parameter key="qos.accept.foreign.ip" value="false"/>
        <dubbo:parameter key="qos.port" value="55556"/>
    </dubbo:application>

    <dubbo:monitor protocol="registry"/>

    <!--dubbo这个服务所要暴露的服务地址所对应的注册中心-->
    <!--<dubbo:registry address="N/A"/>-->
    <dubbo:registry address="zookeeper://192.168.18.19:2181" check="false" />

    <!--当前服务发布所依赖的协议;webserovice、Thrift、Hessain、http-->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!--服务发布的配置,需要暴露的服务接口-->
    <dubbo:service
            interface="com.db.provider.service.ProService"
            ref="proService"/>

    <!--Bean bean定义-->
    <bean id="proService" class="com.db.provider.service.ProServiceImpl"/>

</beans>

2.2 consumer.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="consumerByAaa" owner="aaa"/>

    <!--dubbo这个服务所要暴露的服务地址所对应的注册中心-->
    <!--点对点的方式-->
    <!-- <dubbo:registry address="N/A" /> -->
    <dubbo:registry address="zookeeper://192.168.18.19:2181" check="false"/>

    <!--生成一个远程服务的调用代理-->
    <!--点对点方式-->
   <!--  <dubbo:reference id="providerService"
                     interface="com.db.provider.service.ProService"
                     url="dubbo://192.168.116.1:20880/com.db.provider.service.ProService"/> -->

    <dubbo:reference id="proService"
                     interface="com.db.provider.service.ProService"/>

</beans>

2.3运行

运行后则可以调通

注意

  • 这个demo要加个日志文件log4j.properties,要不然有些日志看不出
log4j.rootLogger=info,console
log4j.additivity.org.apache=true


log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Threshold=DEBUG
log4j.appender.console.ImmediateFlush=true
log4j.appender.console.Target=System.err
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%-5p] %d(%r) --> [%t] %l: %m %x %n
  • dubbo为了可视化,有个项目叫dubbo-admin,可以供后台管理查看服务
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 程序猿惹谁了 设计师:白松林 返回首页