【微服务专题】——SpringCloud——nacos微服务项目搭建
SpringCloudAlibaba——项目搭建
(0)模块整体观
(1)先建立一个root工程
用于作为父类为所有子工程提供整体依赖(具体步骤不详细剖析)
(2)idea模块化操作
利用idea模块化添加实现多工程聚合在一个文件夹(具体步骤不详细剖析)
(3)利用idea查看模块关系
idea的模块关系将可以提供多个maven项目之间,依赖可以直接共享而不必经过本地仓库install的形式完成依赖,
如果模块关系丢失,则可以添加+号导入模块
选中项目就可以恢复模块关系
(3)利用maven查看模块关系
root工程中idea自动创建了子模块的引用名称,让idea可以识别这些pom为maven工程
<?xml version="1.0" encoding="UTF-8"?>
<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-gateway</module>
<module>xdoc-service-auth</module>
<module>xdoc-service-course</module>
<module>xdoc-service-forum</module>
<module>xdoc-service-article</module>
</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.xdoc</groupId>
<artifactId>root</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>/</relativePath>
</parent>
<!--自身定位配置-->
<modelVersion>4.0.0</modelVersion>
<artifactId>xdoc-service-forum</artifactId>
<packaging>jar</packaging>
</project>
SpringCloudAlibaba——依赖管理
Root工程依赖
注意SpringBoot使用的是2.2.3.RELEAS版本,对应的SpringCloud的Hoxton.SR6,大家可以去网上自己查看版本对应关系,避免启动的时候报错;
<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
<groupId>com.xdoc</groupId>
<artifactId>root</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<!--【maven父模块】-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.3.RELEASE</version>
<relativePath/>
</parent>
<!--【maven子模块】-->
<modules>
<module>xdoc-gateway</module>
<module>xdoc-service-auth</module>
<module>xdoc-service-course</module>
<module>xdoc-service-forum</module>
<module>xdoc-service-article</module>
</modules>
<!--【变量管理】 -->
<properties>
<spring-cloud.version>Hoxton.SR6</spring-cloud.version>
</properties>
<!--【SpringCloud版本约束配置】 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!--【root工程依赖】 -->
<dependencies>
<!--SpringCloud基础组件-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-openfeign-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<!--SpirngBoot共用组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId><!--执行数据分析-->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId><!--SpringMVC-->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId><!-- SpringMVC参数校验 -->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId><!-- Spring aop -->
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId><!--lombok-->
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId><!-- swagger -->
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId><!-- swagger-ui -->
<version>2.7.0</version>
</dependency>
</dependencies>
<!--【局部*仓库配置】 -->
<repositories>
<repository>
<id>spring</id>
<url>https://maven.aliyun.com/repository/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</project>
fegin代理工程的依赖
<?xml version="1.0" encoding="UTF-8"?>
<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.xdoc</groupId>
<artifactId>root</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>xdoc-public-openService</artifactId>
<dependencies>
<dependency>
<groupId>com.xdoc</groupId>
<artifactId>xdoc-public-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
web网关的依赖(暂时不用网关框架)
<?xml version="1.0" encoding="UTF-8"?>
<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.xdoc</groupId>
<artifactId>root</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>xdoc-gateway</artifactId>
<dependencies>
<dependency>
<groupId>com.xdoc</groupId>
<artifactId>xdoc-public-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.xdoc</groupId>
<artifactId>xdoc-public-openService</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
微服务的依赖(所有服务提供者)
<?xml version="1.0" encoding="UTF-8"?>
<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.xdoc</groupId>
<artifactId>root</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>/</relativePath>
</parent>
<!--自身定位配置-->
<modelVersion>4.0.0</modelVersion>
<artifactId>xdoc-service-forum</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.xdoc</groupId>
<artifactId>xdoc-public-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.xdoc</groupId>
<artifactId>xdoc-public-openService</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
SpringCloudAlibaba——nacos搭建
开启nacos服务
下载源码:https://github.com/alibaba/nacos/releases
运行源码:
注意,如果依赖拉取失败,需要重新制定idea的manve仓库、配置文件、maven核心的地址再setting中
修改变量:
关闭集群:-Dnacos.standalone=true
然後再重启就行
登录nacos
http://localhost:8848/nacos/
默认账号密码:nacos、nacos
SpringCloudAlibaba——项目配置
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);
}
}
SpringCloudAlibaba——测试用例
服务提供者
@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");
}
}
SpringCloudAlibaba——注意事项
Fegin代理项目包必须和SpringBoot程序入口平级(不然无法扫包)
maven导入后@EnableDiscoveryClient注解找不到
强制指定nacos-discovery的版本,因为idea自动找2.0以下的,所以强制后就不会了
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
maven玄学问题解决方案
(1)改maven配置和仓库路径(新项目)
(2)rebuild reimport clean一下
(3)打包项目需要install另外的本地依赖
(4)出现于父子聚合时,父亲版本更新,子maven仍然使用缓存依赖,先删除最外部的.idea文件里的libary缓存依赖,然后reImport一下