一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - Dubbo在Spring和Spring Boot中的使用詳解

Dubbo在Spring和Spring Boot中的使用詳解

2021-01-28 12:40自在流云 Java教程

這篇文章主要介紹了Dubbo在Spring和Spring Boot中的使用詳解,需要的朋友可以參考下

一、在Spring中使用Dubbo

1、Maven依賴

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>dubbo</artifactId>
  <version>2.5.3.6</version>
  <exclusions>
    <exclusion>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
    </exclusion>
    <exclusion>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
    </exclusion>
    <exclusion>
      <groupId>org.springframework</groupId>
      <artifactId>spring</artifactId>
    </exclusion>
    <exclusion>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
    </exclusion>
  </exclusions>
</dependency>
<dependency>
  <groupId>com.github.sgroschupf</groupId>
  <artifactId>zkclient</artifactId>
  <version>0.1</version>
</dependency>

2、DUBBO生產(chǎn)者注冊(cè)到zookeeper的xml配置方式

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?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
    ">
 <!-- 具體的實(shí)現(xiàn)bean -->
 <bean id="demoService"
 class="com.unj.dubbotest.provider.impl.DemoServiceImpl" />
 <!-- 提供方應(yīng)用信息,用于計(jì)算依賴關(guān)系 -->
 <dubbo:application name="xixi_provider" />
 <!-- 使用multicast廣播注冊(cè)中心暴露服務(wù)地址
 <dubbo:registry address="multicast://224.5.6.7:1234" />-->
 <!-- 使用zookeeper注冊(cè)中心暴露服務(wù)地址 -->
 <dubbo:registry address="zookeeper://127.0.0.1:2181" />
 <!-- 用dubbo協(xié)議在20880端口暴露服務(wù) -->
 <dubbo:protocol name="dubbo" port="20880" />
 <!-- 聲明需要暴露的服務(wù)接口 -->
 <dubbo:service interface="com.unj.dubbotest.provider.DemoService" version="mys"
 ref="demoService" />
</beans>

3、DUBBO消費(fèi)者注冊(cè)到zookeeper的xml配置方式

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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
    ">
 <!-- 消費(fèi)者應(yīng)用信息,用于提供依賴關(guān)系 -->
 <dubbo:application name="consumer-of-helloworld-app" />
 <!-- 注冊(cè)地址,用于消費(fèi)者尋找服務(wù) -->
 <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181,10.128.3.33:2181" />
 <dubbo:consumer timeout="5000" />
 <!-- 引用的服務(wù) -->
 <dubbo:reference id="demoService"interface="com.unj.dubbotest.provider.DemoService" version="mys" />
</beans>

二、在Spring Boot中使用Dubbo

在Spring Boot中使用Dubbo,不需要使用xml的方式來配置生產(chǎn)者和消費(fèi)者,需要使用@Bean注解的方式來進(jìn)行配置。

1、Maven依賴

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  <version>1.2.5.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter</artifactId>
  <version>1.2.5.RELEASE</version>
</dependency>
<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>dubbo</artifactId>
  <version>2.5.3.6</version>
  <exclusions>
    <exclusion>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
    </exclusion>
    <exclusion>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
    </exclusion>
    <exclusion>
      <groupId>org.springframework</groupId>
      <artifactId>spring</artifactId>
    </exclusion>
    <exclusion>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
    </exclusion>
  </exclusions>
</dependency>
<dependency>
  <groupId>com.github.sgroschupf</groupId>
  <artifactId>zkclient</artifactId>
  <version>0.1</version>
</dependency>

2、Dubbo基礎(chǔ)配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public class DubboBaseConfig {
  @Bean
  public RegistryConfig registry() {
    RegistryConfig registryConfig = new RegistryConfig();
    registryConfig.setAddress("127.0.0.1:2181");
    registryConfig.setProtocol("zookeeper");
    return registryConfig;
  }
  @Bean
  public ApplicationConfig application() {
    ApplicationConfig applicationConfig = new ApplicationConfig();
    applicationConfig.setName("testApp");
    return applicationConfig;
  }
  @Bean
  public MonitorConfig monitorConfig() {
    MonitorConfig mc = new MonitorConfig();
    mc.setProtocol("registry");
    return mc;
  }
  @Bean
  public ReferenceConfig referenceConfig() {
    ReferenceConfig rc = new ReferenceConfig();
    rc.setMonitor(monitorConfig());
    return rc;
  }
  @Bean
  public ProtocolConfig protocol() {
    ProtocolConfig protocolConfig = new ProtocolConfig();
    protocolConfig.setPort(20880);
    return protocolConfig;
  }
  @Bean
  public ProviderConfig provider() {
    ProviderConfig providerConfig = new ProviderConfig();
    providerConfig.setMonitor(monitorConfig());
    return providerConfig;
  }
}

3、Dubbo生產(chǎn)者配置,需要繼承Dubbo基礎(chǔ)配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Configuration
public class ExportServiceConfig extends DubboBaseConfig {
  @Bean
  public ServiceBean<Person> personServiceExport(Person person) {
    ServiceBean<Person> serviceBean = new ServiceBean<Person>();
    serviceBean.setProxy("javassist");
    serviceBean.setVersion("myversion");
    serviceBean.setInterface(Person.class.getName());
    serviceBean.setRef(person);
    serviceBean.setTimeout(5000);
    serviceBean.setRetries(3);
    return serviceBean;
  }
}

4、Dubbo消費(fèi)者配置,需要繼承Dubbo基礎(chǔ)配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
@Configuration
public class ReferenceConfig extends DubboBaseConfig {
  @Bean
  public ReferenceBean<Person> person() {
    ReferenceBean<Person> ref = new ReferenceBean<>();
    ref.setVersion("myversion");
    ref.setInterface(Person.class);
    ref.setTimeout(5000);
    ref.setRetries(3);
    ref.setCheck(false);
    return ref;
  }
}

5、直接從Spring容器中拿去Person接口即可。

總結(jié)

以上所述是小編給大家介紹的Dubbo在Spring和Spring Boot中的使用詳解,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!

原文鏈接:http://www.cnblogs.com/cksvsaaa/p/6019393.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 秋霞黄色大片 | b片在线观看 | 奇米影视久久 | 久久精视频 | 操娇妻 | 小舞同人18av黄漫网站 | 日韩免费视频播播 | 59日本人xxxxxxxxx69 | 门房秦大爷小说 | 国产美女做爰免费视频软件 | 97色伦 | 国内精品久久久久久野外 | 成人小视频在线免费观看 | 91精品乱码一区二区三区 | www.亚洲色图 | 国产精品亚洲片在线观看麻豆 | 国产亚洲精品美女久久久 | 亚洲AV福利天堂一区二区三 | 糖心vlog麻豆精东影业传媒 | 91在线老王精品免费播放 | 午夜国产 | 日本剧情片在线播放中文版 | 日韩一区二区三区四区区区 | 亚洲AV综合99一二三四区 | 精品久久久久香蕉网 | 草榴色导航| 午夜小视频免费 | 亚洲福利 影院 | 网站久久| 国产在线观看色 | 日韩一区二区三区在线 | 99亚洲| 美女禁区视频无遮挡免费看 | 美女扒下内裤让男人桶的图片 | 亚洲国产精品一区二区三区久久 | 亚州免费一级毛片 | 无人在线观看免费高清视频播放 | 国产精品色片 | 亚洲国产成人精品无码区5566 | 99热这里只有精品久久免费 | 欧美成人影院免费观 |