学习资料Spring Boot

Springboot整合Dubbo - 代码集成和发布(二)

2017-09-27  本文已影响1827人  Mongogo

1. boot-dubbo-api相关

打开boot-dubbo-api项目,正在src/main/java下创建一个包,并创建你需要dubbo暴露的接口TestService.java,并创建一个实体类用于测试User.java。如下图所示:

创建文件和包结构

User.java

package com.boot.domain;

import lombok.Data;

import java.io.Serializable;

@Data
public class User implements Serializable {
    private Integer id;
    private String username;
    private String password;
    private Integer age;
    private Integer gender;
}

TestService.java

package com.boot.service;

import com.boot.domain.User;

public interface TestService {

    String sayHello(String str);

    User findUser();
}

2. boot-dubbo-provider相关

首先我们先看看总共需要编写的内容,文件的层次结构图

boot-dubbo-provider项目结构图

第一步:我们首先实现我们在boot-dubbo-api上定义的接口,创建一个TestServiceImpl类并实现TestService

package com.boot.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.boot.domain.User;
import com.boot.service.TestService;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service(version = "1.0.0")
public class TestServiceImpl implements TestService {

    @Override
    public String sayHello(String str) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        return dateFormat.format(new Date()) + ": " + str;
    }

    @Override
    public User findUser() {
        User user = new User();
        user.setId(1001);
        user.setUsername("scott");
        user.setPassword("tiger");
        user.setAge(20);
        user.setGender(0);
        return user;
    }
}

注意:代码里的@Service注解是com.alibaba.dubbo.config.annotation.Service的。

第二步:在resources下创建一个config文件夹,在config下创建spring-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="provider"/>
    <!-- 注册中心的ip地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    <!-- 扫描注解包路径,多个包用逗号分隔,不填pacakge表示扫描当前ApplicationContext中所有的类 -->
    <dubbo:annotation package="com.boot.service.impl"/>
</beans>

第三步:在com.boot包下新建Springboot的入口类,创建一个ProviderApplication.java文件。

package com.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

import java.io.IOException;

@SpringBootApplication
@ImportResource({"classpath:config/spring-dubbo.xml"})
public class ProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class, args);
        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

第四步:最后在resources文件夹下面创建application.yml,Springboot的配置文件。

# 在这里编写springboot的配置信息

3. boot-dubbo-consumer相关

首先我们先看看总共需要编写的内容,文件的层次结构图


boot-dubbo-consumer项目结构图

第一步:编写我们的Controller控制类,在com.boot.controller包下新建一个TestController类,编写访问地址。

package com.boot.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.boot.domain.User;
import com.boot.service.TestService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/")
public class TestController {
    @Reference(version = "1.0.0")
    private TestService testService;

    @GetMapping("hello")
    public String hello() {
        return testService.sayHello("Hello springboot and dubbo!");
    }

    @GetMapping("user")
    public User user() {
        return testService.findUser();
    }
}

第二步:在resources下创建一个config文件夹,在config下创建spring-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="consumer"/>
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    <dubbo:annotation package="com.boot.controller"/>
</beans>

第三步:在com.boot包下新建Springboot的入口类,创建一个ConsumerApplication.java文件。

package com.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@ImportResource({"classpath:config/spring-dubbo.xml"})
public class ConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }
}

第四步:最后在resources文件夹下面创建application.yml,Springboot的配置文件。

# 在这里编写springboot的配置信息
server:
  port: 8080
  context-path: /

至此,代码部分已经编写完毕!!!

4. 安装zookeeper注册中心到电脑中

下载地址:zookeeper下载地址
点击后下载适合自己的版本,如图所示

zookeeper的下载
下载完毕后,解压缩该文件,进入conf文件夹,拷贝一份zoo_sample.cfg,在该目录生成zoo.cfg文件。
拷贝生成zoo.cfg文件
进入bin目录,运行zkServer.cmd文件。
运行zkServer.cmd
点击运行后出现如下图所示 运行成功

5. 现在终于可以运行我们的项目了

先运行我们的ProviderApplication.java文件的main函数,再运行ConsumerApplication.java文件的main函数。
打开浏览器访问
http://localhost:8080/hello
http://localhost:8080/user
好了,激动人心的时刻到了,我们终于完成了Springboot和Dubbo的集成了。
项目地址:springboot-dubbo项目GitHub地址

上一篇 下一篇

猜你喜欢

热点阅读