微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Spring Boot整合Dubbo

Spring Boot整合dubbo

服务端

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.baizhi</groupId>
    <artifactId>dubbo05</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.16.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.1.0</version>
        </dependency>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.24.Final</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.7</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

application.properties

dubbo.application.name = dubbo-demo-server

# Base packages to scan dubbo Component: @com.alibaba.dubbo.config.annotation.Service
dubbo.scan.basePackages  = com.baizhi

## RegistryConfig Bean
dubbo.registry.id = my-registry
dubbo.registry.address = zookeeper://192.168.128.128:2181?client=curator

## 配置协议 dubbo 端口 20880
dubbo.protocol.name= dubbo
dubbo.protocol.port= 20880
dubbo.protocol.server= netty4


## Legacy QOS Config
dubbo.application.qosEnable=true
dubbo.application.qosPort= 22222
dubbo.application.qosAcceptForeignIp=false

dubbo示例学习:

dubbo所有的配置优先级别:客户端>服务端>缺省

  • 集群容错(failover/failfast/failsafe/forking/broadcast)
<dubbo:reference cluster="failsafe" />
  • 负载均衡(random/roundrobin/leastactive/consistenthash)
<dubbo:reference weight="100" loadbalance="roundrobin"/>
  • 多协议、多版本、服务分组、多注册中心
<dubbo:service  interface="xx.xx.xx.IXxxService" ref="qqUserService1" version="1.1.1"    />
<dubbo:service  interface="xx.xx.xx.IXxxService" ref="qqUserService2" version="1.1.2"  />
---
<dubbo:reference id="" interface="xx.xx.xx.IXxxService" version="1.1.2"/>

<dubbo:service  interface="xx.xx.xx.IXxxService" ref="qqUserService1" group="qq"  />
<dubbo:service  interface="xx.xx.xx.IXxxService" ref="wxUserService2" group="wx"  />
---
<dubbo:reference id="" interface="xx.xx.xx.IXxxService" group="qq"/>

<dubbo:protocol name="dubbo" port="20880" />
<dubbo:protocol name="rmi" port="1099" />
<!--暴露服务接口-->
<dubbo:service interface="xx.xx.xx.IXxxService" ref="qqUserService" protocol="dubbo,rmi"   />
<dubbo:reference id="" protocol="rmi"  interface="xx.xx.xx.IXxxService" /> 
  • 结果缓存
<dubbo:reference interface="com.foo.BarService" cache="lru" />
<dubbo:service interface="com.foo.BarService" cache="lru" />
  • 上下文信息、隐式参数(日志调用追踪)

    客户端

//传送附件参数 隐式参数
RpcContext.getContext().setAttachment("user", "lisi");

User user = qqUserSerivice.queryUserById(1);
//获取服务端信息
String serverIP = RpcContext.getContext().getRemoteHost();
System.out.println(serverIP);

服务端

 public User queryUserById(Integer id) {
     //获取附件信息
        System.out.println(RpcContext.getContext().getAttachment("user"));
        return new User(id,"QQ用户1",new Date());
     //获取客户端端信息
	 String clientIP = RpcContext.getContext().getRemoteHost();
	System.out.println(clientIP);
 }
  • 令牌验证(防止消费端绕过注册中心,直连服务提供者)
<dubbo:service interface="com.foo.BarService" token="true" />
  • 路由规则、配置规则、dubbo Keeper|dubbo Admin(服务治理UI)

dubbo Keeper

1)下载dubbo-keeper安装包

https://github.com/dubboclub/dubbokeeper/archive/dubbokeeper-1.0.1.tar.gz

2)解压该文件

3)在window是执行install-MysqL.bat

4)将dubbo-keeper目录下的target下的MysqL-dubbokeeper-ui/dubbokeeper-ui-1.0.1.war拷贝到tomcat的webapps下

5)启动tomcat ,修改上面的war包解压出来后对其中WEB-INF/classes/dubbo.properties文件中的配置项进行调整。

dubbo.application.name=common-monitor
dubbo.application.owner=bieber
dubbo.registry.address=zookeeper://192.168.128.128:2181

#use netty4
dubbo.reference.client=netty4

#peeper config
peeper.zookeepers=192.168.128.128:2181
peeper.zookeeper.session.timeout=60000

#logger
monitor.log.home=/monitor-log

monitor.collect.interval=6000

6)访问dubbo-keeper

http://localhost:8080/dubbokeeper-ui-1.0.1

dubbo Admin(略)

1)安装部署一台tomcat-6

2)解压dubbo-admin.zip文件到tomcat的webapps

3)修改dubbo-admin-2.5.3\WEB-INF\dubbo.properties

dubbo.registry.address=zookeeper://192.168.128.128:2181
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest
  • QOS(在线运维)
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo-qos</artifactId>
    <version>2.6.2</version>
</dependency>
---
dubbo.properties

dubbo.application.qos.port=33333
[root@centos ~]# telnet 192.168.253.1 22222

上一篇Dubbo的简单使用

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐