springBoot整合dubbo集成项目

    技术2022-07-10  214

    springBoot整合dubbo集成

    传统Spring 整合dubbo,需要繁琐的编写一堆堆的*.xml 配置文件 而springBoot整合dubbo后,不在需要写*.xml,通过jar包引用,完 成整合,通过注解的形式完成配置。提高我们的开发效率

    目录结构 1 服务层生产者开发(hs-ldm-server-service) 1.1添加dubbo依赖包

    <dependencies> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>2.8.0</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>2.8.0</version> </dependency> <dependency> <groupId>com.alibaba.spring.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.9</version> </dependency>

    如果不加curator-recipes和curator-framework依赖启动java.lang.NoClassDefFoundError: org/apache/curator/RetryPolicy 问题。 启动如果报错,请检查依赖版本是否过低! 1.2 配置application.properties server.port=8050 #dubbo配置 spring.dubbo.application.name=hs-ldm-server-service #注册中心地址 spring.dubbo.registry.address=zookeeper://192.168.228.52:2181 #配置了这个后,就会一直有守护京城,而不是启动就怪盗了 spring.dubbo.server=true #扫描的包 spring.dubbo.scan=com.gy.ldm.server #Dubbo Protocol spring.dubbo.protocol.name=dubbo spring.dubbo.protocol.port=29999 spring.dubbo.protocol.version=1.0.0 #DemoService version service.version=1.0.0 1.3服务类

    package com.gy.ldm.server.service; import org.springframework.stereotype.Component; import com.alibaba.dubbo.config.annotation.Service; @Component @Service(version="${service.version}" ,interfaceClass = DubboServiceApi.class) public class DubboService implements DubboServiceApi{ public String getDubbo() { // TODO Auto-generated method stub return "dubbo"; } }

    @component (把对象实例化到spring容器中) 注意这里的service引用!(com.alibaba.dubbo.config.annotation.Service) 1.4 服务启动类

    @SpringBootApplication @EnableDubboConfiguration public class LdmServerApp { public static void main(String[] args) { SpringApplication.run(LdmServerApp.class, args); } }

    启动成功! 2.接入层消费者开发 添加依赖和服务层提供者依赖一样 2.1配置application.properties

    server.port=8051 #dubbo配置 #服务名称 spring.dubbo.application.name=ldm-client #注册中心地址 spring.dubbo.registry.address=zookeeper://192.168.228.52:2181 #扫描的包 spring.dubbo.scan=com.gy.ldm.client ## DemoService version service.version=1.0.0

    2.2Controller层服务

    package com.gy.ldm.client.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.alibaba.dubbo.config.annotation.Reference; import com.gy.ldm.server.service.DubboServiceApi; @Controller public class TestController { @Reference(version="${service.version}",check = false) private DubboServiceApi dubboServiceApi; @RequestMapping("customer") @ResponseBody public String customer(){ return dubboServiceApi.getDubbo(); } }

    2.3启动服务

    package com.gy.ldm.client; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration; @EnableDubboConfiguration @SpringBootApplication public class LdmClientApp { public static void main(String[] args) { SpringApplication.run(LdmClientApp.class, args); } }

    3.测试访问 4.注册中心服务 可以看见提供者发布的服务

    springboot整合dubbo项目会上传,可以在我主页去下载

    Processed: 0.085, SQL: 9