SpringBoot开发案例之整合Dubbo提供者(一)

既然是开发案例,显然不会扯那么多老婆舌,有不清楚这两个东东的请自行百度。

0.jpg

开发环境

JDK1.7、Maven、Eclipse、SpringBoot1.5.1、Dubbo2.8.4

项目结构

1.png

相关配置

pom.xml:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.itstyle.dubbo</groupId>
  <artifactId>spring-boot-dubbo-provider</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  
  <name>springBoot_dubbo_provider</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.7</java.version>
  </properties>
  
  <!-- spring-boot-starter-parent包含了大量配置好的依赖管理,在自己项目添加这些依赖的时候不需要写<version>版本号 -->
  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
        <relativePath/>
  </parent>
  <!-- 配置依赖 -->
  <dependencies>
        <!-- springboot相关 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- Dubbo相关 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <!-- 这里使用最新的2.8.4版本,中央仓库不存在,请自行打入本地仓库 -->
            <version>2.8.4</version>
            <exclusions>
                <exclusion>
                    <artifactId>spring</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- zookeeper 注册中心 -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.6</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- zookeeper 第三方操作工具类 -->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.6</version>
        </dependency>
        <!-- javassist 字节码类库 -->
        <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
        </dependency>
  </dependencies>
  <build>
        <plugins>
            <!-- 打包项目 mvn clean package -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <dependencies>
                    <!-- mvn spring-boot:run 热部署启动 -->
                    <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>springloaded</artifactId>
                        <version>1.4.0.RELEASE</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

User.java 一定要实现序列化接口

public class User implements Serializable {

    private static final long serialVersionUID = -1L;

    private String username;
    private Integer age;

    public User(String username, Integer age) {
        this.username = username;
        this.age = age;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

IUserService.java 接口

public interface IUserService {
    void saveUser(User user);
}

UserServiceImpl.java 实现

@Service("userService")
public class UserServiceImpl implements IUserService {
    @Override
    public void saveUser(User user) {
        System.out.println("保存用户:"+user.getUsername());
    }
}

dubbo.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="springBoot_dubbo_provider"  />

    <!-- 注册中心服务地址 -->
    <dubbo:registry  protocol="zookeeper" address="${dubbo.registry.address}"  check="false" />

    <!-- 用dubbo协议-->
    <dubbo:protocol name="dubbo" port="-1" dispather="all" check="false" />
    
    <dubbo:provider timeout="10000"  threads="10" threadpool="fixed"   loadbalance="roundrobin"/>
    
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.itstyle.dubbo.service.IUserService" ref="userService"/>


</beans>

application.properties

# 项目contextPath,一般在正式发布版本中,我们不配置
server.context-path=/provider
# 错误页,指定发生错误时,跳转的URL。请查看BasicErrorController源码便知
server.error.path=/error
# 服务端口
server.port=8080
# session最大超时时间(分钟),默认为30
server.session-timeout=60
# 该服务绑定IP地址,启动服务器时如本机不是该IP地址则抛出异常启动失败,只有特殊需求的情况下才配置
# server.address=192.168.16.11

# tomcat最大线程数,默认为200
server.tomcat.max-threads=800
# tomcat的URI编码
server.tomcat.uri-encoding=UTF-8

#ZooKeeper
dubbo.registry.address=192.168.1.180:2181
logging.level.root=INFO

SpringbootDubboApplication.java 测试类

@SpringBootApplication
@ComponentScan(basePackages={"com.itstyle.dubbo"})
@ImportResource({"classpath:dubbo.xml"})
public class SpringbootDubboApplication implements CommandLineRunner {
    @Autowired
    private IUserService userService;

    public static void main(String[] args) {
        SpringApplication.run(SpringbootDubboApplication.class, args);
    }
    @Override
    public void run(String... args) throws Exception {
       User user = new User("张三",19);
       userService.saveUser(user);
    }
}

此时,我们运行SpringbootDubboApplication类,如下结果说明成功:
2.png

然后使用ZooInspector查看zookeeper信息,会发现注册了一个提供者:
3.png

代码:https://git.oschina.net/52itstyle/springboot_dubbo_provider

qrcode_for_gh_bf7a27ade681_258.jpg

作者: 小柒

出处: https://blog.52itstyle.com

分享是快乐的,也见证了个人成长历程,文章大多都是工作经验总结以及平时学习积累,基于自身认知不足之处在所难免,也请大家指正,共同进步。

本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出, 如有问题, 可邮件(345849402@qq.com)咨询。