用于作为父类为所有子工程提供整体依赖(具体步骤不详细剖析)
利用idea模块化添加实现多工程聚合在一个文件夹(具体步骤不详细剖析)
(3)利用idea查看模块关系
idea的模块关系将可以提供多个maven项目之间,依赖可以直接共享而不必经过本地仓库install的形式完成依赖,
如果模块关系丢失,则可以添加+号导入模块
选中项目就可以恢复模块关系
(3)利用maven查看模块关系
root工程中idea自动创建了子模块的引用名称,让idea可以识别这些pom为maven工程
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modules>
<module>xdoc-gatewaymodule>
<module>xdoc-service-authmodule>
<module>xdoc-service-coursemodule>
<module>xdoc-service-forummodule>
<module>xdoc-service-articlemodule>
modules>
project>
(4)子模块指定父为root依赖(手动)
记得对所有子模块都要这样操作,module仅仅实现了idea可以识别这些maven工程,但必须指定这个root工程作为父模块才实现依赖共享
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.xdocgroupId>
<artifactId>rootartifactId>
<version>1.0-SNAPSHOTversion>
<relativePath>/relativePath>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>xdoc-service-forumartifactId>
<packaging>jarpackaging>
project>
Root工程依赖
注意SpringBoot使用的是2.2.3.RELEAS版本,对应的SpringCloud的Hoxton.SR6,大家可以去网上自己查看版本对应关系,避免启动的时候报错;
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.xdocgroupId>
<artifactId>rootartifactId>
<packaging>pompackaging>
<version>1.0-SNAPSHOTversion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.2.3.RELEASEversion>
<relativePath/>
parent>
<modules>
<module>xdoc-gatewaymodule>
<module>xdoc-service-authmodule>
<module>xdoc-service-coursemodule>
<module>xdoc-service-forummodule>
<module>xdoc-service-articlemodule>
modules>
<properties>
<spring-cloud.version>Hoxton.SR6spring-cloud.version>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${spring-cloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
<version>2.2.0.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-ribbonartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-openfeign-coreartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-hystrixartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboardartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-validationartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-aopartifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger2artifactId>
<version>2.7.0version>
dependency>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger-uiartifactId>
<version>2.7.0version>
dependency>
dependencies>
<repositories>
<repository>
<id>springid>
<url>https://maven.aliyun.com/repository/url>
<releases>
<enabled>trueenabled>
releases>
<snapshots>
<enabled>trueenabled>
snapshots>
repository>
repositories>
project>
fegin代理工程的依赖
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.xdocgroupId>
<artifactId>rootartifactId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>xdoc-public-openServiceartifactId>
<dependencies>
<dependency>
<groupId>com.xdocgroupId>
<artifactId>xdoc-public-commonartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
dependencies>
project>
web网关的依赖(暂时不用网关框架)
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.xdocgroupId>
<artifactId>rootartifactId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>xdoc-gatewayartifactId>
<dependencies>
<dependency>
<groupId>com.xdocgroupId>
<artifactId>xdoc-public-commonartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>com.xdocgroupId>
<artifactId>xdoc-public-openServiceartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
dependencies>
project>
微服务的依赖(所有服务提供者)
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.xdocgroupId>
<artifactId>rootartifactId>
<version>1.0-SNAPSHOTversion>
<relativePath>/relativePath>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>xdoc-service-forumartifactId>
<packaging>jarpackaging>
<dependencies>
<dependency>
<groupId>com.xdocgroupId>
<artifactId>xdoc-public-commonartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>com.xdocgroupId>
<artifactId>xdoc-public-openServiceartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
dependencies>
project>
开启nacos服务
下载源码:https://github.com/alibaba/nacos/releases
运行源码:
注意,如果依赖拉取失败,需要重新制定idea的manve仓库、配置文件、maven核心的地址再setting中
修改变量:
关闭集群:-Dnacos.standalone=true
然後再重启就行
登录nacos
GateWay Web入口——基本配置
applivaction.yml
server:
port: 30001
spring:
profiles:
active: dev
activiti:
check-process-definitions: false
application:
name: xdoc-gateway
feign:
hystrix:
enabled: true
httpclient:
enabled: false
okhttp:
enabled: true
compression:
request:
enabled: true
mime-types: text/xml,application/xml,application/json
min-request-size: 102240
response:
enabled: true
applivaction-dev.yml
spring:
application:
name: xdoc-gateway
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
bootstrap.properties
spring.application.name=xdoc-gateway
GateWay Web入口——开启服务发现
@EnableFeignClients
@EnableDiscoveryClient//扫描Fegin的代理
@SpringBootApplication
public class XdocGateWayAppliaction {
public static void main(String[] args) {
SpringApplication.run(XdocGateWayAppliaction.class, args);
}
}
Service微服务——基本配置
applivaction.yml
server:
port: 30001
spring:
profiles:
active: dev
activiti:
check-process-definitions: false
application:
name: service-forum
feign:
hystrix:
enabled: true
httpclient:
enabled: false
okhttp:
enabled: true
compression:
request:
enabled: true
mime-types: text/xml,application/xml,application/json
min-request-size: 102240
response:
enabled: true
applivaction-dev.yml
spring:
application:
name: service-forum
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
bootstrap.properties
spring.application.name=service-forum
Service微服务——开启服务发现(服务和服务直接调用)
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class ForumServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ForumServiceApplication.class, args);
}
}
服务提供者
@RequestMapping("/feginTest")
@RestController
public class FeginTestController {
@GetMapping("/test1")
public ResponseVO feginTest(){
return ApiResult.success("我是微服务");
}
}
服务提供者代理对象
@FeignClient(value = "service-forum")
public interface WechatConsumer {
@GetMapping("/feginTest/test1")
ResponseVO getWXInvoiceCode();
}
GateWay Web(Web一般就是调用服务实现服务消费)
@RequestMapping("/feginTest")
@RestController
public class FeginTestController {
@Autowired
WechatConsumer wechatConsumer;
@GetMapping("/test1")
public ResponseVO feginTest(){
ResponseVO wxInvoiceCode = wechatConsumer.getWXInvoiceCode();
System.out.println(wxInvoiceCode.getData());
return ApiResult.success("成功2222");
}
}
Fegin代理项目包必须和SpringBoot程序入口平级(不然无法扫包)
maven导入后@EnableDiscoveryClient注解找不到
强制指定nacos-discovery的版本,因为idea自动找2.0以下的,所以强制后就不会了
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
<version>2.2.0.RELEASEversion>
dependency>
maven玄学问题解决方案
(1)改maven配置和仓库路径(新项目)
(2)rebuild reimport clean一下
(3)打包项目需要install另外的本地依赖
(4)出现于父子聚合时,父亲版本更新,子maven仍然使用缓存依赖,先删除最外部的.idea文件里的libary缓存依赖,然后reImport一下