一步步跟着完成后就可以搭建一个简单父子微服务框架
首先创建空壳maven项目
File->New->Project
删除多余文件只保留pom.xml
现在就有了一个空壳maven项目,用作于父级项目,后续在其基础上创建springcloud项目
创建eureka(注册中心)项目选择相应服务
父级项目右击New->Module
勾选注册服务后点击next->finish完成创建
搭建gateway(网关)
父级项目右击New->Module
完成创建后项目结构
接下来完成父子项目的构建
在父级pom.xml中添加springboot依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.12.RELEASE</version>
</parent>
查看springboot对应springcloud版本
添加版本控制以便父级统一管理
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR12</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
添加子模块
<modules>
<module>eureka</module>
<module>gateway</module>
</modules>
添加后完整pom.xml文件
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.12.RELEASE</version>
</parent>
<groupId>com.own</groupId>
<artifactId>myself</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>eureka</module>
<module>gateway</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR12</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
子项目pom.xml更改父级指向
<parent>
<groupId>com.own</groupId>
<artifactId>myself</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
删除无用依赖
eureka完整pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.own</groupId>
<artifactId>myself</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.own</groupId>
<artifactId>eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
gateway完整pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.own</groupId>
<artifactId>myself</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.own</groupId>
<artifactId>gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>gateway</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
到这里我们就完成了一个简单微服务框架的基础,接下来配置启动
在EurekaApplication添加类注解
@EnableEurekaServer
配置application.yml文件
server:
port: 9800
spring:
application:
name: eureka
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:${server.port}/eureka/
register-with-eureka: false #是否将自己注册到Eureka服务中,本身就是服务,所以无需注册
fetch-registry: false # 是否从Eureka 中获取注册信息
# instance:
# prefer-ip-address: true #将自己的ip注册到eureka服务中
在GatewayApplication添加类注解
@EnableEurekaClient
配置application.yml文件
server:
port: 9801
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:9800/eureka/
instance:
prefer-ip-address: true
server:
enable-self-preservation: false
spring:
application:
name: gateway
cloud:
gateway:
globalcors:
cors-configurations:
'[/**]': #匹配所有请求
allow-credentials: true
allowedHeaders: "*"
allowedOrigins: "*" #跨域处理 允许所有的域
allowedMethods: # 支持的方法
- GET
- POST
- PUT
- DELETE
discovery:
locator:
enabled: true
lower-case-service-id: true #使用小写service-id 使用微服务的名称转发对应的服务
配置好后就可以完成我们的启动了!