alibab仓库 idea_01.微服务架构编码、构建
教學(xué)視頻傳送:
springBoot和springCloud的版本選型https://start.spring.io/actuator/info
查看json串返回結(jié)果
這就是我們的選型依據(jù)
本次開發(fā)選用版本如下:
cloud ? ? ? ? : Hoxton.SR1boot ? ? ? ? ?: 2.2.2.RELEASEcloud alibaba : 2.1.0.RELEASEjava ? ? ? ? ?: java8Maven ? ? ? ? : 3.5及以上Mysql ? ? ? ? : 5.7及以上
關(guān)于Cloud各種組件的停更、升級、替換
約定>配置>編碼
IDEA新建project工作空間
微服務(wù)cloud整體聚合工程
父工程步驟New Project
聚合總父工程名字
Maven選版本
工程名字
字符編碼
注解生效激活
java編譯版本選8
File Type過濾
父工程POM
<?xml version="1.0" encoding="UTF-8"?>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.2.6.RELEASE
com.top
cloud2020-learn
1.0
cloud2020-learn
pom
Demo project for cloud2020-learn
cloud-provider-payment8001
UTF-8
1.8
1.8
4.12
1.2.17
1.16.18
5.1.47
1.1.16
2.2.2.RELEASE
Hoxton.SR1
2.1.0.RELEASE
1.3.0
org.springframework.boot
spring-boot-dependencies
${spring.boot.version}
pom
import
org.springframework.cloud
spring-cloud-dependencies
${spring.cloud.version}
pom
import
com.alibaba.cloud
spring-cloud-alibaba-dependencies
${spring.cloud.alibaba.version}
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}
org.projectlombok
lombok
${lombok.version}
true
org.springframework.boot
spring-boot-maven-plugin
true
true
nexus-aliyun
Nexus aliyun
http://maven.aliyun.com/nexus/content/groups/public
true
false
Maven工程落地細(xì)節(jié)復(fù)習(xí)
Maven中的DependencyManagement和Dependencies
maven中跳過單元測試
父工程創(chuàng)建完成執(zhí)行mvn:insall將父工程發(fā)布到倉庫方便子工程繼承
Rest微服務(wù)工程搭建
構(gòu)建步驟
1. Cloud-provider-payment8001 微服務(wù)提供者M(jìn)odule模塊
- 建module
- 改POM
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
com.alibaba
druid-spring-boot-starter
1.1.10
mysql
mysql-connector-java
org.springframework.boot
spring-boot-starter-jdbc
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
- 寫YML
server:port: 8001spring:application:name: cloud-payment-servicedatasource:type: com.alibaba.druid.pool.DruidDataSource ? ? ? ? ? ?# 當(dāng)前數(shù)據(jù)源操作類型driver-class-name: org.gjt.mm.mysql.Driver ? ? ? ? ? ? ?# mysql驅(qū)動包url: jdbc:mysql://localhost:3306/the_course?useUnicode=true&characterEncoding=utf-8&useSSL=falseusername: rootpassword: aaaaaamybatis:mapperLocations: classpath:mapper/*.xmltype-aliases-package: com.top.cloud.provider.payment8001.entities ? ?# 所有Entity別名類所在包
- 主啟動
@SpringBootApplicationpublic class CloudProviderPayment8001Application {
public static void main(String[] args) {
SpringApplication.run(CloudProviderPayment8001Application.class, args);
}
}
- 業(yè)務(wù)類CommonResult
@Data@AllArgsConstructor@NoArgsConstructorpublic class CommonResult
{ ? ?private Integer code; ? ?private String ?message; ? ?private T ? ? ? data; ? ?public CommonResult(Integer code, String message)
{ ? ? ? ?this(code,message,null);
}
}Payment
@Data@AllArgsConstructor@NoArgsConstructorpublic class Payment implements Serializable { ? ?private Long id; ? ?private String name;
}PaymentDao
@Mapperpublic interface PaymentDao { ? ?public int create(Payment payment); ? ?public Payment getPaymentById(@Param("id") Long id);
}PaymentService
public interface PaymentService{ ? ?int create(Payment payment); ? ?Payment getPaymentById(@Param("id") Long id);
}PaymentServiceImpl
@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);
}
}PaymentMapper.xml
insert into payment(name) ?values(#{name}); ? ?
select * from payment where id=#{id}; ? ?PaymentController
@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("*****插入結(jié)果:"+result); ? ? ? ?if(result > 0)
{ ? ? ? ? ? ?return new CommonResult(200,"插入數(shù)據(jù)庫成功,serverPort: "+serverPort,result);
}else{ ? ? ? ? ? ?return new CommonResult(444,"插入數(shù)據(jù)庫失敗",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,"沒有對應(yīng)記錄,查詢ID: "+id,null);
}
}
}
- 測試
用postman或者idea自帶的HTTPClient測試接口
2. cloud-consumer-order80 微服務(wù)消費者訂單Module模塊
- 建cloud-consumer-order80
- 改POM
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.junit.vintage
junit-vintage-engine
- 寫YML
server:port: 80spring:application:name: cloud-order-service
- 主啟動
@SpringBootApplicationpublic class CloudConsumerOrder80Application {
public static void main(String[] args) {
SpringApplication.run(CloudConsumerOrder80Application.class, args);
}
}
- 業(yè)務(wù)類CommonResult
@Data@AllArgsConstructor@NoArgsConstructorpublic class CommonResult
{ ? ?private Integer code; ? ?private String ?message; ? ?private T ? ? ? data; ? ?public CommonResult(Integer code, String message)
{ ? ? ? ?this(code,message,null);
}
}Payment
@Data@AllArgsConstructor@NoArgsConstructorpublic class Payment implements Serializable { ? ?private Long id; ? ?private String name;
}ApplicationContextConfig
@Configurationpublic class ApplicationContextConfig{ ? ?@Bean
//@LoadBalanced
public RestTemplate getRestTemplate(){ ? ? ? ?return new RestTemplate();
}
}OrderController
@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
3. 工程重構(gòu)觀察問題: 系統(tǒng)中有重復(fù)部分,重構(gòu)新建cloud-api-common
就是一個最簡單的工程改pom
<?xml version="1.0" encoding="UTF-8"?>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.top
cloud2020-learn
1.0
com.top
cloud-api-common
1.0
cloud-api-common
Demo project for cloud-api-common
1.8
org.springframework.boot
spring-boot-devtools
runtime
true
org.projectlombok
lombok
true
提取公共部分maven命令clean install
其他模塊依賴 刪除各自的原先的entities文件夾
pom中添加:
com.top
cloud-api-common
1.0
修改代碼中的依賴路徑解決SpringBoot項目repackage failed: Unable to find main class Maven打包 install的問題
發(fā)現(xiàn)父項目有一個打包的插件
org.springframework.boot
spring-boot-maven-plugin
repackage
這時,問題就出現(xiàn)了,報打包失敗錯誤!!!解決:如果你的項目是一個放置通用工具類的工程,那么該項目中,就不能包括上面這個打包插件,如果你這個工具類工程依賴有父工程,那么父工程中也不能包括該打包插件,只有你的項目是一個web項目時,含有Main方法的程序入口類,要加該打包插件,我放在了父工程的pom文件中,那就是代表了所有子模塊都有這個打包插件,所以報錯,解決就是去掉這個插件 ,只在web工程中加入這個打包插件!
目前工程樣圖
總結(jié)
以上是生活随笔為你收集整理的alibab仓库 idea_01.微服务架构编码、构建的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 美团公布去年反腐成果:抓到110人 清退
- 下一篇: 时隔近四年!波音737 MAX今日国内复