项目作者: wu191287278

项目描述 :
spring-boot-start-dubbo
高级语言: Java
项目地址: git://github.com/wu191287278/spring-boot-starter-dubbo.git
创建时间: 2017-04-11T10:06:03Z
项目社区:https://github.com/wu191287278/spring-boot-starter-dubbo

开源协议:

下载


spring-boot-start-dubbo

  • Dubbo是阿里开发的一套分布式通讯框架,Spring-boot是业界比较火的微服务框架,两者可以进行结合实现分布式微服务
  • 对于内部远程Rpc调用,可以借用Dubbo能力,达到服务治理的目的

增加feign protocol支持

该协议主要是为了支持老项目可以消费springcloud提供的接口,并可以利用dubbo的服务发现,构建出一个springboot rest集群,
dubbo与springboot结合时,不需要dubbo再次导出rest服务。而是由springboot提供rest服务dubbo端只负责注册,构建服务目录。

如何发布Dubbo服务

在Spring Boot项目的pom.xml中添加以下依赖:

  1. <dependency>
  2. <groupId>com.github.wu191287278</groupId>
  3. <artifactId>spring-boot-starter-dubbo</artifactId>
  4. <version>1.5.4</version>
  5. </dependency>

example


  1. //服务端,多协议发布服务
  2. @RestController
  3. @Service(version = "1.0.0", protocol = {"dubbo","feign"}, timeout = 10000) //该注解仅仅是描述接口使用,并不会造成多次实例化
  4. @RequestMapping(value = "/user/")
  5. public class UserServiceImpl implements UserService {
  6. @Autowired
  7. private UserDao userDao;
  8. public User selectByPrimaryKey(@PathVariable("id") Long id) {
  9. return userDao.selectByPrimaryKey(id);
  10. }
  11. public Object insert(@RequestBody User t) {
  12. return userDao.insert(t);
  13. }
  14. public Object updateByPrimaryKey(@RequestBody User t) {
  15. return userDao.updateByPrimaryKey(t);
  16. }
  17. public int deleteByPrimaryKey(@PathVariable("id") Long id) {
  18. return userDao.deleteByPrimaryKey(id);
  19. }
  20. }
  21. @SpringBootApplication
  22. @EnableDubboAutoConfiguration
  23. public class Application {
  24. public static void main(String[] args) {
  25. SpringApplication.run(Application.class, args);
  26. }
  27. }
  28. //消费端
  29. @FeignClient(path = "/user")
  30. @DubboClient(protocol = "feign", value = @Reference(timeout = 10000, version = "1.0.0"))
  31. public interface UserService {
  32. @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  33. User selectByPrimaryKey(@PathVariable("id") Long id);
  34. @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
  35. int deleteByPrimaryKey(@PathVariable("id") Long id);
  36. @RequestMapping(value = "/", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
  37. Object insert(@RequestBody User t);
  38. @RequestMapping(value = "/", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
  39. Object updateByPrimaryKey(@RequestBody User t);
  40. }
  41. //测试
  42. @SpringBootTest(classes = ConsumerApplication.class)
  43. @RunWith(SpringJUnit4ClassRunner.class)
  44. public class UserServiceTest {
  45. @Autowired
  46. private UserService userService;
  47. @Test
  48. public void testSelectByPrimaryKey() {
  49. User user = userService.selectByPrimaryKey(1L);
  50. System.err.println(user);
  51. }
  52. @Test
  53. public void testInsert() {
  54. userService.insert(new User()
  55. .setUsername("test")
  56. .setPassword("123456")
  57. .setCreatedTime(new Date())
  58. .setAddress("北京市"));
  59. }
  60. @Test
  61. public void testUpdate(){
  62. userService.updateByPrimaryKey(new User()
  63. .setId(2L)
  64. .setUsername("test")
  65. .setPassword("123456")
  66. .setCreatedTime(new Date())
  67. .setAddress("北京市"));
  68. }
  69. @Test
  70. public void testDelete(){
  71. userService.deleteByPrimaryKey(1L);
  72. }
  73. }

在application.properties添加Dubbo的版本信息和客户端超时信息,如下:

  1. spring:
  2. application:
  3. name: dubbo-demo-consumer
  4. dubbo:
  5. application:
  6. name: ${spring.application.name}
  7. protocol:
  8. name: feign
  9. registry:
  10. protocol: zookeeper
  11. address: localhost:2181
  12. scan: com.example

*网关支持,支持聚合dubbo rest服务同时兼容springcloud rest代理

maven 依赖

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-zuul</artifactId>
  4. </dependency>

启动类

  1. @SpringBootApplication
  2. @EnableDubboProxy
  3. public class DubboApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(DubboApplication.class, args);
  6. }
  7. }

配置文件

  1. spring:
  2. application:
  3. name: dubbo-application
  4. dubbo:
  5. application:
  6. name: ${spring.application.name}
  7. registry:
  8. protocol: hazelcast
  9. address: 224.5.6.7:1234

泛化代理Dubbo转换成rest

  1. spring:
  2. application:
  3. name: dubbo-application
  4. dubbo:
  5. application:
  6. name: ${spring.application.name}
  7. registry:
  8. protocol: hazelcast
  9. address: 224.5.6.7:1234
  10. generic-prefix: /proxy
  11. //rest请求示例 localhost:8100/proxy/
  12. {
  13. "params":[1],
  14. "method":"com.example.service.UserService.selectByPrimaryKey",
  15. "version":"1.0.0"
  16. }
  17. //返回结果
  18. {
  19. "jsonrpc": "2.0",
  20. "id": null,
  21. "result": "{\"id\":1,\"username\":\"wuyu\",\"createdTime\":1493775816000,\"address\":\"安徽省阜阳市\",\"class\":\"com.example.model.User\",\"password\":\"123456\"}"
  22. }

演示样例

https://git.oschina.net/wuyu15255872976/dubbo-demo-parent.git

新增Hazelcast 注册中心

  1. spring:
  2. application:
  3. name: dubbo-application
  4. dubbo:
  5. application:
  6. name: ${spring.application.name}
  7. registry:
  8. protocol: hazelcast
  9. address: 224.5.6.7:1234?managementCenter=http://localhost:8080/mancenter #managementCenter 是hazelcast监控地址,可以不填写

Hazelcast 监控中心

  1. 地址:https://hazelcast.org/download/