SpringCloud创建服务提供者与消费者
程序员文章站
2022-04-15 17:57:56
SpringCloud创建服务提供者与消费者,首先是创建相关依赖...
相关依赖
提供者:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.22</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>com.hht.cloud</groupId> <artifactId>cloud-api-commons</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
消费者:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency>
支付业务代码(提供者)
结构如下:
yml配置:
spring: application: name: cloud-payment-service datasource: driver-class-name: com.mysql.cj.jdbc.Driver username: root password: 123456 url: jdbc:mysql://localhost:3306/cloutstudy #指定数据源为druid type: com.alibaba.druid.pool.DruidDataSource devtools: restart: #增加热部署检测时间,防止出现404 poll-interval: 3s mybatis: mapper-locations: classpath:mapper/*.xml server: port: 8011
主要业务服务代码:
创建和查询操作:
@Service public class PaymentServiceImpl implements PaymentService { @Resource PaymentMapper paymentMapper; @Override public int create(Payment payment) { return paymentMapper.create(payment); } @Override public Payment getPaymentById(Long id) { return paymentMapper.getPaymentById(id); } }
订单业务代码(消费者)
结构如下:
目前还没有使用注册中心来注册服务,所以先使用restTemplate来进行远程调用提供者的业务服务
容器配置类:
@Configuration public class ApplicationContextConfig { //注册restTemplate进容器 @Bean public RestTemplate getRestTemplate(){ return new RestTemplate(); } }
控制器如下:
@RestController @Slf4j @RequestMapping("/consumer/payment") public class OrderController { public static final String PAYMENT_URL="http://localhost:8011"; @Autowired private RestTemplate restTemplate; @PostMapping("/create") public CommonResult<Payment> create(Payment payment){ return restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment,CommonResult.class); } @GetMapping("/get/{id}") public CommonResult<Payment> getPayment(@PathVariable Long id){ return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class); } }
本文地址:https://blog.csdn.net/qq_41120971/article/details/107873807
上一篇: 前端 h5 文件下载重命名
推荐阅读
-
SpringCloud之服务注册与发现Spring Cloud Eureka实例代码
-
springcloud干货之服务注册与发现(Eureka)
-
SpringCloud之服务注册与发现Spring Cloud Eureka实例代码
-
SpringCloud-创建服务消费者-Feign方式(附代码下载)
-
VS2013创建Windows服务与调试服务的图文方法
-
Java--IDEA创建多服务模块的SpringCloud微服务项目
-
SpringCloud教程 | 第三篇: 服务消费者(Feign)(Finchley版本)
-
Express与NodeJs创建服务器的两种方法
-
springcloud使用之服务的注册发现与消费
-
跟我学SpringCloud | 第三篇:服务的提供与Feign调用