Spring Boot 学习笔记 10

SpringBoot集成Dubbo 代码调试

前一篇文章中,我们已经将 接口项目、消费者项目、提供者项目 配置完成,接下来就是写代码的事情了。
在Consumer项目的控制层中: StudentCOntroller.java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.bjpowernode.springboot.web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.bjpowernode.springboot.service.StudentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class StudentController {

//dubbo:reference interface="" version="" check=false
@Reference(interfaceClass = StudentService.class,version = "1.0.0",check = false)
private StudentService studentService;

@RequestMapping(value = "/student/count")
public @ResponseBody Object studentCount() {

Integer allStudentCount = studentService.queryAllStudentCount();

return "学生总人数为:"+allStudentCount;
}
}

这时候我们用到的接口private StudentService studentService;并没有被创建,所以我们要创建一个 StudentService接口,但是我们并不在本项目中创建,而是移步到Interface项目中创建这个接口。
在Interface项目中的src/java/sample/springboot/中创建一个service包:service,包内创建一个接口类StudentService.java:

1
2
3
4
5
6
7
8
9
10
package com.bjpowernode.springboot.service;

public interface StudentService {

/**
* 获取学生总人数
* @return
*/
Integer queryAllStudentCount();
}

接口创建好了,那我们在哪实现这个接口呢?按照之前的做法我们应该是在Service这个包内创建一个新的包impl来存放接口实现类,但是在Dubbo中,我们把接口实现类归于提供者(Provider)项目。
在Provider项目中的src/java/sample/springboot/service/中创建包impl,里面新建:StudentServiceImpl.java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.bjpowernode.springboot.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.bjpowernode.springboot.service.StudentService;
import org.springframework.stereotype.Component;

@Component
@Service(interfaceClass = StudentService.class,version = "1.0.0",timeout = 15000)
//dubbo:service interface="" version="" timeout=""
public class StudentServiceImpl implements StudentService {

@Override
public Integer queryAllStudentCount() {

//调用数据持久层

return 1250;
}
}

注解@Component将接口实现类加载到Spring容器中,然后再用com.alibaba.dubbo.config.annotation包中的@Service注解,添加属性interfaceClass = StudentService.class ,version = “1.0.0” , timeout = 15000,
这些注解就是dubbo中添加的dubbo:service interface=”” version=”” timeout=””标签。

配置好这个注解,这个接口就暴露出来了,然后就可以在消费者项目中进行引用了。

在Consumer项目中的StudentController.java中添加的:

1
2
//dubbo:reference interface="" version="" check=false
@Reference(interfaceClass = StudentService.class,version = "1.0.0",check = false)

这就是引用接口的注解__@Reference()__,内容为interfaceClass = StudentService.class,version = “1.0.0”,check = false,这些注解就是dubbo中添加的dubbo:reference interface=”” version=”” check=false标签。

完成代码配置后还需要在入口类添加启动Dubbo的注解:

  • Provider项目:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.bjpowernode.springboot;

import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication //开启spring配置
@EnableDubboConfiguration //开启dubbo配置
public class Application {

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

}

  • Consumer 项目:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.bjpowernode.springboot;

import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication //开启spring注解配置
@EnableDubboConfiguration //开启dubbo配置
public class Application {

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

}

完成搭建后启动各个项目,然后在浏览器完成测试!