spring boot +mybatis注解+dubbo整合,spring boot 2.0.0

为了在不改变公司项目架构的基础上,使用新技术,所以想到了spring boot整合dubbo,服务注册调了整整两天,增加了一个注解解决了,下面直接上代码,希望能够帮助大家。

test-api

pom.xml

    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.test</groupId>
    <artifactId>test_api</artifactId>
    <version>dev-SNAPSHOT</version>
    <name>test_api</name>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

因为仅作api的jar包,所以删除测试类和启动类,创建bean包和service包,还有配置文件。

testbean

/**
 * @author john
 * @date 2019/2/1
 */
@AllArgsConstructor
@NoArgsConstructor
@Setter
@Getter
public class TestBean implements Serializable {
    private  static final long serialVersionUID = 1L;

    private int id;
    private String keyword;
}

testSetvice

/**
 * @author john
 * @date 2019/2/1
 */
public interface TestService {
    /**
     * 查询列表
     * @param page
     * @param pageSize
     * @return
     * @throws Exception
     */
    List<TestBean> qry(int page,int pageSize)throws Exception;

    /**
     * 保存
     * @param bean
     * @return
     * @throws Exception
     */
    int save(TestBean bean)throws Exception;

    /**
     * 更新
     * @param bean
     * @return
     * @throws Exception
     */
    int update(TestBean bean)throws Exception;
}

至此,api完成

服务提供者:test-service

pom.xml

<modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.test</groupId>
    <artifactId>test_service</artifactId>
    <version>dev-SNAPSHOT</version>
    <name>test_service</name>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba.boot/dubbo-spring-boot-starter -->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.test</groupId>
            <artifactId>test_api</artifactId>
            <version>dev-SNAPSHOT</version>
        </dependency>
    </dependencies>

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

application.properties
说明一下,整合dubbo的项目这的.properties暂时不能更换未.yml。
新版本的spring boot的mysql类名已经更新为com.mysql.cj.jdbc.Driver。

dubbo.registry.address=zookeeper://ip:port
dubbo.application.name=test-service
dubbo.application.id=test-service
dubbo.application.version=1.0.0
dubbo.registry.port=21999
dubbo.scan.base-packages=com.test.service
dubbo.protocol.name=dubbo
dubbo.protocol.port=-1
dubbo.protocol.threads=1000
dubbo.protocol.id=dubbo
dubbo.registry.id=register

server.port=8992

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.name=8888
spring.datasource.password=888
spring.datasource.url=jdbc\:mysql\://ip\:3306/test?autoReconnect\=true\&failOverReadOnly\=false&characterEncoding\=utf8

启动类:TestApplication
@EnableDubbo这个是dubbo注解千万记得加,不然dubbo注册不上
@MapperScan(“com.test.dao”) 这个是mybatis扫描

@MapperScan("com.test.dao")
@EnableDubbo
@SpringBootApplication
public class TestApplication {

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

}

TestServiceImpl
@Component为了注入spring。
@Service此为dubbo注解
(import com.alibaba.dubbo.config.annotation.Service),
不是spring注解,里面的参数,部分可以不用给定。

/**
 * 测试
 * @author john
 * @date 2019/2/1
 */
@Service(version = "1.0.0",timeout = 5000,application = "${dubbo.application.id}",protocol = "${dubbo.protocol.id}",
        registry = "${dubbo.registry.id}",interfaceClass = TestService.class)
@Component
public class TestServiceImpl implements TestService {

    @Autowired
    TestMapper testMapper;

    @Override
    public List<EmotionBean> qry(int page, int pageSize) throws Exception {
        int start=(page-1)*pageSize;
        return testMapper.qryList(start,pageSize);
    }

    @Override
    public int save(TestBean bean) throws Exception {
        return testMapper.save(bean);
    }

    @Override
    public int update(TestBean bean) throws Exception {
        return testMapper.update(bean);
    }
}

TestMapper

/**
 * @author john
 * @date 2019/2/1
 */
public interface TestMapper {
    /**
     * 查询列表
     * @param start
     * @param size
     * @return
     * @throws Exception
     */
    @Select("select * from t_test order by id asc limit #{start},#{end}")
    List<TestBean> qryList(int start,int size)throws Exception;

    /**
     * 保存
     * @param bean
     * @return
     * @throws Exception
     */
    @Insert("insert into t_emtion(keyword) values(#{bean.keyword})")
    int save(TestBean bean)throws Exception;

    /**
     * 更新
     * @param emotionBean
     * @return
     * @throws Exception
     */
    @Update("update t_test set keyword=#{bean.keyword} where id=#{bean.id}")
    int update(TestBean bean)throws Exception;
}

消费者:test-consumer

pom.xml

<modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.test</groupId>
    <artifactId>test</artifactId>
    <version>dev-SNAPSHOT</version>
    <name>test</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba.boot/dubbo-spring-boot-starter -->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>
        <dependency>
            <groupId>com.edujia</groupId>
            <artifactId>test_api</artifactId>
            <version>dev-SNAPSHOT</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

## 避免和 server 工程端口冲突
server.port=8991

## Dubbo 服务消费者配置
spring.scan=org.spring.springboot.dubbo
dubbo.application.name=test-consumer
dubbo.registry.address=zookeeper://ip:2181
dubbo.registry.port=-1

启动类TestApplication,自动生成,无需改动

TestCtr

/**
 * @author john
 * @date 2019/1/30
 */
@RestController
@RequestMapping("/test")
public class TestCtr {

    @Autowired
    ITestBiz testBiz;

    @GetMapping("/qryList/{page}/{pageSize}")
    public String qryList(@PathVariable(name = "page") int page,@PathVariable(name = "pageSize") int pageSize)throws Exception{
        System.out.println("page:"+page);
        return testBiz.qryList(page,pageSize);
    }
}

ITestBiz

/**
 * @author john
 * @date 2019/1/30
 */
public interface ITestBiz {
    /**
     * 查询列表
     * @param page
     * @param pageSize
     * @return
     * @throws Exception
     */
    String qryList(int page, int pageSize)throws Exception;
}

服务相关的service,TestBiz
这里使用@Reference(version = “1.0.0”)调用dubbo服务。

/**
 * @author john
 * @date 2019/1/30
 */
@Service
public class TestBiz implements ITestBiz {

    @Reference(version = "1.0.0")
    TestService testService;

    @Override
    public String qryList(int page, int pageSize) throws Exception {
        System.out.println("请求dubbo");
        List<TestBean> qry = emotionService.qry(1, 10);
        System.out.println(JSONObject.toJSONString(qry));
        JSONObject data=new JSONObject();
        System.out.println("返回");
        return JsonUtils.getSuccessDataJson(data,"查询成功");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值