欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringCloud创建服务提供者与消费者

程序员文章站 2022-07-10 18:32:11
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> 

支付业务代码(提供者)

结构如下:
SpringCloud创建服务提供者与消费者
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); } } 

订单业务代码(消费者)

结构如下:
SpringCloud创建服务提供者与消费者
目前还没有使用注册中心来注册服务,所以先使用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

相关标签: SpringCloud