https://www.bilibili.com/video/BV19T4y1g7ue
https://start.spring.io/actuator/info
查看json串返回结果
这就是我们的选型依据
本次开发选用版本如下:
cloud : Hoxton.SR1boot : 2.2.2.RELEASEcloud alibaba : 2.1.0.RELEASEjava : java8Maven : 3.5及以上Mysql : 5.7及以上
父工程步骤
1.New Project
2.聚合总父工程名字
3.Maven选版本
4.工程名字
5.字符编码
6.注解生效激活
7.java编译版本选8
8.File Type过滤
9.父工程POM
4.0.0org.springframework.boot spring-boot-starter-parent 2.2.6.RELEASEcom.top cloud2020-learn 1.0cloud2020-learnpomDemo project for cloud2020-learncloud-provider-payment8001UTF-81.81.84.121.2.171.16.185.1.471.1.162.2.2.RELEASEHoxton.SR12.1.0.RELEASE1.3.0org.springframework.boot spring-boot-dependencies ${spring.boot.version}pomimportorg.springframework.cloud spring-cloud-dependencies ${spring.cloud.version}pomimportcom.alibaba.cloud spring-cloud-alibaba-dependencies ${spring.cloud.alibaba.version}pomimportmysql mysql-connector-java ${mysql.version}com.alibaba druid ${druid.version}org.mybatis.spring.boot mybatis-spring-boot-starter ${mybatis.spring.boot.version}org.projectlombok lombok ${lombok.version}trueorg.springframework.boot spring-boot-maven-plugin true true nexus-aliyunNexus aliyunhttp://maven.aliyun.com/nexus/content/groups/publictruefalse
org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot-starter com.alibaba druid-spring-boot-starter 1.1.10mysql mysql-connector-java org.springframework.boot spring-boot-starter-jdbc org.projectlombok lombok trueorg.springframework.boot spring-boot-starter-test testorg.junit.vintage junit-vintage-engine
server: port: 8001spring: application: name: cloud-payment-service datasource: type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型 driver-class-name: org.gjt.mm.mysql.Driver # mysql驱动包 url: jdbc:mysql://localhost:3306/the_course?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: aaaaaamybatis: mapperLocations: classpath:mapper/*.xml type-aliases-package: com.top.cloud.provider.payment8001.entities # 所有Entity别名类所在包
@SpringBootApplicationpublic class CloudProviderPayment8001Application { public static void main(String[] args) { SpringApplication.run(CloudProviderPayment8001Application.class, args); }}
@Data@AllArgsConstructor@NoArgsConstructorpublic class CommonResult{ private Integer code; private String message; private T data; public CommonResult(Integer code, String message) { this(code,message,null); }}
@Data@AllArgsConstructor@NoArgsConstructorpublic class Payment implements Serializable { private Long id; private String name;}
@Mapperpublic interface PaymentDao { public int create(Payment payment); public Payment getPaymentById(@Param("id") Long id);}
public interface PaymentService{ int create(Payment payment); Payment getPaymentById(@Param("id") Long id);}
@Servicepublic 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); }}
insert into payment(name) values(#{name}); select * from payment where id=#{id};
@RestController@Slf4jpublic class PaymentController { @Resource private PaymentService paymentService; @Value("${server.port}") private String serverPort; @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,"插入数据库成功,serverPort: "+serverPort,result); }else{ return new CommonResult(444,"插入数据库失败",null); } } @GetMapping(value = "/payment/get/{id}") public CommonResult getPaymentById(@PathVariable("id") Long id) { Payment payment = paymentService.getPaymentById(id); if(payment != null) { return new CommonResult(200,"查询成功,serverPort: "+serverPort,payment); }else{ return new CommonResult(444,"没有对应记录,查询ID: "+id,null); } }}
用postman或者idea自带的HTTPClient测试接口
org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-devtools runtimetrueorg.projectlombok lombok trueorg.springframework.boot spring-boot-starter-test testorg.junit.vintage junit-vintage-engine
server: port: 80spring: application: name: cloud-order-service
@SpringBootApplicationpublic class CloudConsumerOrder80Application { public static void main(String[] args) { SpringApplication.run(CloudConsumerOrder80Application.class, args); }}
@Data@AllArgsConstructor@NoArgsConstructorpublic class CommonResult{ private Integer code; private String message; private T data; public CommonResult(Integer code, String message) { this(code,message,null); }}
@Data@AllArgsConstructor@NoArgsConstructorpublic class Payment implements Serializable { private Long id; private String name;}
@Configurationpublic class ApplicationContextConfig{ @Bean //@LoadBalanced public RestTemplate getRestTemplate() { return new RestTemplate(); }}
@RestController@Slf4jpublic class OrderController { public static final String PAYMENT_URL = "http://localhost:8001"; @Resource private RestTemplate restTemplate; @GetMapping("/consumer/payment/create") public CommonResult create(Payment payment) { return restTemplate.postForObject(PAYMENT_URL +"/payment/create",payment,CommonResult.class); } @GetMapping("/consumer/payment/get/{id}") public CommonResult getPayment(@PathVariable("id") Long id) { System.out.println("哈哈"); return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class); } @GetMapping("/consumer/payment/getForEntity/{id}") public CommonResult getPayment2(@PathVariable("id") Long id) { ResponseEntity entity = restTemplate.getForEntity(PAYMENT_URL+"/payment/get/"+id,CommonResult.class); if(entity.getStatusCode().is2xxSuccessful()){ return entity.getBody(); }else{ return new CommonResult<>(444,"操作失败"); } }}
http://localhost/consumer/payment/get/1
观察问题: 系统中有重复部分,重构
就是一个最简单的工程
4.0.0com.top cloud2020-learn 1.0com.top cloud-api-common 1.0cloud-api-commonDemo project for cloud-api-common1.8org.springframework.boot spring-boot-devtools runtimetrueorg.projectlombok lombok true
pom中添加:
com.top cloud-api-common 1.0
修改代码中的依赖路径
解决SpringBoot项目repackage failed: Unable to find main class Maven打包 install的问题
发现父项目有一个打包的插件org.springframework.boot spring-boot-maven-plugin repackage这时,问题就出现了,报打包失败错误!!! 解决:如果你的项目是一个放置通用工具类的工程,那么该项目中,就不能包括上面这个打包插件,如果你这个工具类工程依赖有父工程,那么父工程中也不能包括该打包插件,只有你的项目是一个web项目时,含有Main方法的程序入口类,要加该打包插件,我放在了父工程的pom文件中,那就是代表了所有子模块都有这个打包插件,所以报错,解决就是去掉这个插件 ,只在web工程中加入这个打包插件!