建立三个工程,分别为 服务提供者 studyproduct, studyproduct-consumer服务消费者(接口暴露层),studyproductapi 公用API
在studyproduct 项目上引入dubbo 依赖
<!-- 引入公用类包 --> <dependency> <groupId>com.jiuge.common</groupId> <artifactId>studycommon</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-dubbo</artifactId> </dependency> <dependency> <groupId>com.product</groupId> <artifactId>studyproductapi</artifactId> <version>0.0.1-SNAPSHOT</version> <scope>compile</scope> </dependency>
在yml 上配置 dubbo
dubbo: scan: # 指定 Dubbo 服务实现类的扫描基准包 base-packages: com.jiuge.product.service protocol: # dubbo 协议 name: dubbo # dubbo 协议端口( -1 表示自增端口,从 20880 开始) port: -1 spring: application: name: springcloud-dubbo-provider-product main: # Spring Boot2.1及更高的版本需要设定 allow-bean-definition-overriding: true cloud: nacos: # Nacos 服务发现与注册配置 discovery: server-addr: localhost:8848 datasource: type: com.alibaba.druid.pool.DruidDataSource druid: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/sharekit?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai username: root password: 123456 initial-size: 10 max-active: 100 min-idle: 10 max-wait: 60000 pool-prepared-statements: true max-pool-prepared-statement-per-connection-size: 20 time-between-eviction-runs-millis: 60000 min-evictable-idle-time-millis: 300000 test-while-idle: true test-on-borrow: false test-on-return: false stat-view-servlet: enabled: true url-pattern: /druid/* filter: stat: log-slow-sql: true slow-sql-millis: 1000 merge-sql: false wall: config: multi-statement-allow: true #mybatis-plus mybatis-plus: mapper-locations: classpath*:/mapper/**/*.xml #实体扫描,多个package用逗号或者分号分隔 typeAliasesPackage: com.jiuge.product.*.entity global-config: #数据库相关配置 db-config: #主键类型 AUTO:"数据库ID自增" id-type: AUTO logic-delete-value: -1 logic-not-delete-value: 0 banner: false #原生配置 configuration: map-underscore-to-camel-case: true cache-enabled: false call-setters-on-nulls: true jdbc-type-for-null: 'null'
建立ProductServiceImpl 实现类,添加注解
package com.jiuge.product.service.impl; import com.jiuge.product.dao.ProductDao; import com.product.entity.ProductEntity; import com.product.service.ProductService; import org.apache.dubbo.config.annotation.DubboService; import org.springframework.beans.factory.annotation.Autowired; import java.util.List; /** * @author jiuge * @version 1.0 * @date 2021/8/6 10:29 */ @DubboService public class ProductServiceImpl implements ProductService { @Autowired private ProductDao productDao; @Override public List<ProductEntity> list() { return productDao.selectDataList(); } @Override public ProductEntity findById(Integer id) { return productDao.findById(id); } }
接口ProductService 定义放在公用的 studyproductapi工程下
在消费端 studyproduct-consumer工程下
yml 配置类如下
dubbo: cloud: # 指定需要订阅的服务提供方,默认值*,会订阅所有服务,不建议使用 subscribed-services: springcloud-dubbo-provider-product protocol: # dubbo 协议 name: dubbo # dubbo 协议端口( -1 表示自增端口,从 20880 开始) port: -1 spring: application: name: spring-cloud-dubbo-consumer-product main: # Spring Boot2.1及更高的版本需要设定 allow-bean-definition-overriding: true cloud: nacos: discovery: server-addr: localhost:8848 server: port: 8030
在controller 层添加注解 DubboReference 即可
package com.jiuge.product.controller; import com.product.entity.ProductEntity; import com.product.service.ProductService; import org.apache.dubbo.config.annotation.DubboReference; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; /** * @author jiuge * @version 1.0 * @date 2021/8/6 15:49 */ @RestController @RequestMapping("/product") public class ProductController { @DubboReference private ProductService productService; @GetMapping("/info/{id}") public ProductEntity info(@PathVariable("id") Integer id){ return productService.findById(id); } @GetMapping("/list") public List<ProductEntity> list(){ return productService.list(); } }
验证,测试 请求接口 http://localhost:8030/product/list
[{"id":1,"productName":"苹果","productType":"个","productUnit":"箱"},
从OpenFeign 迁移到 Dubbo
修改 ProductServiceImpl
package com.jiuge.product.service.impl; import com.jiuge.product.dao.ProductDao; import com.product.entity.ProductEntity; import com.product.service.ProductService; import lombok.extern.slf4j.Slf4j; import org.apache.dubbo.config.annotation.DubboService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; /** * @author jiuge * @version 1.0 * @date 2021/8/6 10:29 */ @DubboService @Slf4j @RestController @RequestMapping("/product") public class ProductServiceImpl implements ProductService { @Autowired private ProductDao productDao; @Override @GetMapping("/list") public List<ProductEntity> list() { return productDao.selectDataList(); } @Override @GetMapping("/findById") public ProductEntity findById(Integer id) { return productDao.findById(id); } }
服务消费端引入依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-dubbo</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>com.product</groupId> <artifactId>studyproductapi</artifactId> <version>0.0.1-SNAPSHOT</version> <scope>compile</scope> </dependency> </dependencies>
修改studyproduct 项目的 ProductServiceImpl 文件 提供接口访问层,暴露接口给客户端
package com.jiuge.product.service.impl; import com.jiuge.product.dao.ProductDao; import com.product.entity.ProductEntity; import com.product.service.ProductService; import lombok.extern.slf4j.Slf4j; import org.apache.dubbo.config.annotation.DubboService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; /** * @author jiuge * @version 1.0 * @date 2021/8/6 10:29 */ @DubboService @Slf4j @RestController @RequestMapping("/product") public class ProductServiceImpl implements ProductService { @Autowired private ProductDao productDao; @Override @GetMapping("/list") public List<ProductEntity> list() { return productDao.selectDataList(); } @Override @GetMapping("/findById/{id}") public ProductEntity findById(@PathVariable("id") Integer id) { return productDao.findById(id); } }
在 studyproduct-consumer 项目上添加 ProductDubboFeignService类
添加注解DubboTransported(protocol = "dubbo")
package com.jiuge.product.service; import com.alibaba.cloud.dubbo.annotation.DubboTransported; import com.product.entity.ProductEntity; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import java.util.List; @FeignClient(value="springcloud-dubbo-provider-product",path = "/product") @DubboTransported(protocol = "dubbo") public interface ProductDubboFeignService { @GetMapping("/list") List<ProductEntity> list(); @GetMapping("/findById/{id}") ProductEntity findById(@PathVariable("id")Integer id); }
修改ProductController 类
@RestController @RequestMapping("/product") public class ProductController { @DubboReference private ProductService productService; @GetMapping("/info/{id}") public ProductEntity info(@PathVariable("id") Integer id) { return productService.findById(id); } @GetMapping("/list") public List<ProductEntity> list() { return productService.list(); } /************Openfeign 转成 dubboFeign **************/ @Autowired private ProductDubboFeignService dubboFeignService; @GetMapping("/list2") public List<ProductEntity> list2() { return dubboFeignService.list(); } @GetMapping("/findById/{id}") public ProductEntity findById(@PathVariable("id") Integer id) { return dubboFeignService.findById(id); } }
注意 在StudyProductConsumerApplication 上要加注解@EnableFeignClients
@SpringBootApplication @EnableFeignClients public class StudyproductConsumerApplication { public static void main(String[] args) { SpringApplication.run(StudyproductConsumerApplication.class, args); } }
测试,验证
请求接口 http://localhost:8030/product/list2
[{"id":1,"productName":"苹果","productType":"个","productUnit":"箱"},{"id":2,"productName":"葡萄","productType":"串","productUnit":"斤"},{"id":3,"productName":"西瓜","productType":"个","productUnit":"个"}]
具体代码 可以 参考我的git 地址 欢迎star