使用maven管理项目
创建maven工程
pom.xml
com.zjh.springcloud mscloud031.0-SNAPSHOT pom UTF-8 1.8 1.8 4.12 1.2.17 1.16.18 5.1.47 1.1.16 1.3.0 org.springframework.boot spring-boot-dependencies2.2.2.RELEASE pom import org.springframework.cloud spring-cloud-dependenciesHoxton.SR1 pom import com.alibaba.cloud spring-cloud-alibaba-dependencies2.1.0.RELEASE pom import mysql mysql-connector-java${mysql.version} com.alibaba druid${druid.version} org.mybatis.spring.boot mybatis-spring-boot-starter${mybatis.spring.boot.version} junit junit${junit.version} log4j log4j${log4j.version} org.projectlombok lombok${lombok.version} true org.springframework.boot spring-boot-maven-plugintrue true
复习Maven中的DependencyManagement和Dependencies
dependencyManagement
Maven 使用dependencyManagement 元素来提供了一种管理依赖版本号的方式。
通常会在一个组织或者项目的最顶层的父POM 中看到dependencyManagement 元素。
使用pom.xml 中的dependencyManagement 元素能让所有在子项目中引用一个依赖而不用显式的列出版本号。
Maven 会沿着父子层次向上走,直到找到一个拥有dependencyManagement 元素的项目,然后它就会使用这个
dependencyManagement 元素中指定的版本号。
application.ymlorg.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-actuatororg.mybatis.spring.boot mybatis-spring-boot-startercom.alibaba druid-spring-boot-starter1.1.10 mysql mysql-connector-javaorg.springframework.boot spring-boot-starter-jdbcorg.springframework.boot spring-boot-devtoolsruntime true org.projectlombok lomboktrue org.springframework.boot spring-boot-starter-testtest
server:
port: 8001
spring:
application:
name: cloud-payment-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
driver-class-name: org.gjt.mm.mysql.Driver # mysql驱动包 com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 123456
mybatis:
mapperLocations: classpath:mapper/*.xml
type-aliases-package: com.atguigu.springcloud.entities # 所有Entity别名类所在包
主启动类
@SpringBootApplication
public class PaymentMain8001
{
public static void main(String[] args)
{
SpringApplication.run(PaymentMain8001.class,args);
}
}
业务类
1.entitie——主实体类Payment
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment implements Serializable
{
private Long id;
private String serial;
}
2.entitie——Json封装体CommonResult
@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); } }
3.dao——接口PaymentDao
@Mapper //import org.apache.ibatis.annotations.Mapper;
public interface PaymentDao
{
public int create(Payment payment);
public Payment getPaymentById(@Param("id") Long id);
}
4.dao——mybaits的映射文件PaymentMapper.xml
srcmainresourcesmapperPaymentMapper.xml路径下INSERT INTO payment(SERIAL) VALUES(#{serial}); SELECT * FROM payment WHERe id=#{id};
5.service——接口PaymentService
public interface PaymentService
{
public int create(Payment payment);
public Payment getPaymentById(@Param("id") Long id);
}
6.serviceImpl——PaymentServiceImpl 实现类
@Service
public class PaymentServiceImpl implements PaymentService
{
@Resource
private PaymentDao paymentDao;
@Override
public int create(Payment payment)
{
return paymentDao.create(payment);
}
@Override
public Payment getPaymentById(Long id)
{
return paymentDao.getPaymentById(id);
}
}
7.controller
@RestController
@Slf4j
public class PaymentController
{
@Resource
private PaymentService paymentService;
@PostMapping(value = "/payment/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(444,"插入数据库失败",null);
}
}
@GetMapping(value = "/payment/get/{id}")
public CommonResult getPaymentById(@PathVariable("id") Long id)
{
Payment payment = paymentService.getPaymentById(id);
log.info("*****查询结果:{}",payment);
if (payment != null) {
return new CommonResult(200,"查询成功",payment);
}else{
return new CommonResult(444,"没有对应记录,查询ID: "+id,null);
}
}
}
cloud-consumer-order80微服务消费者订单Module模块
pom.xml
application.ymlorg.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-actuatororg.springframework.boot spring-boot-devtoolsruntime true org.projectlombok lomboktrue org.springframework.boot spring-boot-starter-testtest
server: port: 80主启动类
@SpringBootApplication
public class MainApp80
{
public static void main(String[] args)
{
SpringApplication.run(MainApp80.class,args);
}
}
业务类
1.entities——主实体Payment
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment implements Serializable
{
private Long id;
private String serial;
}
2.entities——Json封装体CommonResult
@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); } }
3.config配置类
@Configuration
public class ApplicationContextConfig
{
@Bean
public RestTemplate restTemplate()
{
return new RestTemplate();
}
}
4.controller
@RestController
public class OrderController
{
public static final String PaymentSrv_URL = "http://localhost:8001";
@Autowired
private RestTemplate restTemplate;
@GetMapping("/consumer/payment/create") //客户端用浏览器是get请求,但是底层实质发送post调用服务端8001
public CommonResult create(@RequestBody Payment payment)
{
return restTemplate.postForObject(PaymentSrv_URL + "/payment/create",payment,CommonResult.class);
}
@GetMapping("/consumer/payment/get/{id}")
public CommonResult getPayment(@PathVariable Long id)
{
return restTemplate.getForObject(PaymentSrv_URL + "/payment/get/"+id, CommonResult.class, id);
}
}
RestTemplate
RestTemplate提供了多种便捷访问远程Http服务的方法, 是一种简单便捷的访问restful服务模板类,是Spring提供的用于访问Rest服务的客户端模板工具集
使用restTemplate访问restful接口非常的简单粗暴无脑。
(url, requestMap, ResponseBean.class)这三个参数分别代表
REST请求地址、请求参数、HTTP响应转换被转换成的对象类型。
浏览器访问 http://localhost/consumer/payment/get/1
http://localhost/consumer/payment/create?serial=aaaaa1



