参照新建cloud-provider-payment8001`
新建cloud-consumer-order80模块
导入依赖
<dependencies>
<!--包含了sleuth+zipkin-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
<dependency>
<groupId>com.xiang.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
参照新建cloud-provider-payment8001中的entities直接copy过来。
添加主启动类
在配置文件中编写
server:
port: 80
spring:
application:
name: cloud-order-service
为什么要设置80端口?
80端口是为HTTP(HyperText Transport Protocol)即超文本传输协议开放的,此为上网冲浪使用次数最多的协议,主要用于WWW(World Wide Web)即万维网传输信息的协议。
可以通过HTTP地址(即常说的“网址”)加“: 80”来访问网站,因为浏览网页服务默认的端口号都是80,因此只需输入网址即可,不用输入“: 80”了。
cloud-consumer-order80中只有controller层
为什么呢?
现在是消费者cloud-consumer-order80去调用cloud-provider-payment8001的提供者。
为什么没有dao层,作为消费者你是不可能去操作别人的数据库的。
现在我们是两个端口,两个服务直接怎么去调用了?
以前学过httpClient
现在我们使用restTemplate,提供了多种便捷访问http服务器的方法,是一种简单的restful服务模板类,是spring提供的用于访问rest服务的客户端模板工具集,可以这么理解它把httpClient做了一次封装,实现了服务之间的横向调用。
restTemplate官方文档地址
使用restTemplate访问restful接口非常简单粗暴
(url,requestMap,responseBean.class)
rest请求地址,请求参数,http响应转换被转换成的对象类型。
将restTemplate注入到spring容器中
新建config文件夹创建ApplicationContextConfig 类
package com.xiang.springcloud.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class ApplicationContextConfig {
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
编写controller层
package com.xiang.springcloud.controller;
import com.xiang.springcloud.entities.CommonResult;
import com.xiang.springcloud.entities.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@Slf4j
@RestController
public class OrderController {
public static final String PAYMENY_URL="http://localhost:8001";
@Resource
private RestTemplate restTemplate;
@GetMapping("/consumer/payment/create")
public CommonResult<Payment> create(Payment payment){
return restTemplate.postForObject(PAYMENY_URL+"payment/create",payment,CommonResult.class);
}
@GetMapping("/consumer/payment/get/{id}")
public CommonResult<Payment> getPayment(@PathVariable("id") Long id){
return restTemplate.getForObject(PAYMENY_URL+"/payment/get"+id,CommonResult.class);
}
}
启动
测试查找没有问题
测试插入 插入serial =lisi 返回是成功了
那么我们去数据库检查一下,插入结果。
发现数据库中并没有数据,有主键,但是serial为null
这是为啥?为什么在提供端可以插入成功,这消费者端显示插入成功,但是并没有数据
不要忘记在提供者方法上加上@RequestBody
再来测试
查看一下数据库是否有数据
插入成功