目录
1.说明
2.父工程
3.服务端
4.消费者
5.公共模块
6.RestTemplate
创建三个模块,服务者,消费者,公共api
注:spring boot和spring cloud有版本约束
约定版本号配置
注意:jdk版本,maven版本
UTF-8
1.8
1.8
4.12
1.2.17
1.18.28
8.0.33
1.2.16
2.2.2
org.springframework.boot
spring-boot-dependencies
2.2.2.RELEASE
pom
import
org.springframework.cloud
spring-cloud-dependencies
Hoxton.SR1
pom
import
com.alibaba.cloud
spring-cloud-alibaba-dependencies
2.2.0.RELEASE
pom
import
mysql
mysql-connector-java
${mysql.version}
com.alibaba
druid
${druid.verison}
org.mybatis.spring.boot
mybatis-spring-boot-starter
${mybatis.spring.boot.verison}
junit
junit
${junit.version}
log4j
log4j
${log4j.version}
org.projectlombok
lombok
${lombok.version}
true
在父工程下创建子模块
注意jdk版本
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.mybatis.spring.boot
mybatis-spring-boot-starter
com.alibaba
druid-spring-boot-starter
1.2.16
mysql
mysql-connector-java
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-devtools
runtime
true
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
org.example
cloud-api-commons
${project.version}
在主包下面创建主启动类,扫描此包及其所有子包
@SpringBootApplication
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class);
}
}
- 1.修改端口
- 2.添加mysql配置
- 3.添加mybatis配置
server:
port: 8001
spring:
application:
name: could-payment-serivce
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/springcloud
username: root
password: 123456
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.xz.springcloud.entity
dao层上一定要加@Mapper
@Mapper
public interface PaymentDao {
/**
* 增
*
* @param payment
* @return
*/
public int create(Payment payment);
/**
* 查
* @param id
* @return
*/
public Payment getPaymentById(@Param("id") Integer id);
/**
* 删除
* @param id
* @return
*/
public int deletePaymentById(@Param("id") Integer id);
}
@Service
public class PaymentServiceImpl implements PaymentService {
@Autowired
private PaymentDao paymentDao;
@Override
public int create(Payment payment) {
return paymentDao.create(payment);
}
@Override
public Payment getPaymentById(Integer id) {
return paymentDao.getPaymentById(id);
}
@Override
public int deletePaymentById(Integer id) {
return paymentDao.deletePaymentById(id);
}
}
@RestController
@RequestMapping("/payment")
@Slf4j
public class PaymentController {
@Autowired
private PaymentService paymentService;
/**
* 增添数据
* @param payment
* @return
*/
@PostMapping("/create")
public CommonResult create(@RequestBody Payment payment) {
int result = paymentService.create(payment);
log.info("插入结果:" + result);
if (result > 0) {
return new CommonResult(200, "插入数据成功!", result);
} else {
return new CommonResult(404, "插入数据失败", null);
}
}
/**
* 查询数据
* @param id
* @return
*/
@GetMapping("/selectById/{id}")
public CommonResult getPaymentById(@PathVariable("id") Integer id) {
Payment paymentResult = paymentService.getPaymentById(id);
if (paymentResult != null) {
return new CommonResult(200,"查询成功~",paymentResult);
}else {
return new CommonResult(404,"查询失败",null);
}
}
@DeleteMapping("/deleteById/{id}")
public CommonResult deleteById(@PathVariable("id") Integer id){
int result = paymentService.deletePaymentById(id);
if (result!=0){
return new CommonResult(200,"删除成功",result);
}else{
return new CommonResult(404,"删除失败",null);
}
}
}
至此服务类已经完成构建
在父工程下创建子模块
注意jdk版本
因为是消费者模块,所以不用连接数据库,只需springboot的pom文件
注:如果配置类数据库的依赖,在yml里有没有数据库的配置,启动时会报错哦~
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-devtools
runtime
true
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
org.example
cloud-api-commons
${project.version}
在主包下面创建主启动类,扫描此包及其所有子包
@SpringBootApplication
public class OrderMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderMain80.class);
}
}
因为是消费者模块,所以无需任何配置,只改端口号
server:
port: 80
要引入RestTemplate,必须将其放入ioc容器中
@Configuration
public class ApplicationContextConfig {
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
引入RestTemplate,调用服务端
@RestController
@Slf4j
@RequestMapping("/consumer")
public class OrderController {
public static final String PAYMENT_URL = "http://localhost:8001";
@Autowired
private RestTemplate restTemplate;
/**
* 增
*
* @param payment
* @return
*/
@GetMapping("/payment/create")
public CommonResult create(Payment payment) {
log.info("进入添加功能成功~");
return restTemplate.postForObject(PAYMENT_URL + "/payment/create", payment, CommonResult.class);
}
/**
* 查
*
* @param id
* @return
*/
@GetMapping("/payment/get/{id}")
public CommonResult getPayment(@PathVariable("id") Integer id) {
log.info("进入查询功能成功");
return restTemplate.getForObject(PAYMENT_URL + "payment/selectById/" + id, CommonResult.class);
}
}
在父工程下创建子模块
注意jdk版本
不做业务,无需引入其他依赖
org.projectlombok
lombok
${lombok.version}
true
cn.hutool
hutool-all
5.8.0
①实体类
其他模块必须引入此模块的pom依赖
注:一定要是实现序列化接口
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment implements Serializable {
private Integer id;
private String serial;
}
②包装类
给前传返回业务信息
/**
* 包装类
*
* @param
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult {
private Integer code;
private String message;
private T data;
public CommonResult(Integer code, String message) {
this(code, message, null);
}
}
- RestTemplate提供了多种便捷访问远程Http服务的方法
- 是一种简单便捷的访问restful服务模板类,
- 是Spring提供的用于访问Rest服务的客户端模板工具集
- url:REST请求地址
- requestMap:请求参数
- ResponseBean.class:HTTP响应转换被转换成的对象类型
- 1. 注入到spring容器中
- 2.自动导入,直接使用
@Configuration
public class ApplicationContextConfig {
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}