文章目录
创建一个springcloud项目框架
搭建项目框架
注:框架是使用了nacos作为注册中心,要自己先搭建nacos
项目框架:
第一步:创建项目结构
idea创建项目结构:
父亲模块创建:
File->new->新建project,选择maven(不需要选择模板),下一步,输入项目名,finish就行了
子模块创建:
新建model,选择maven(不需要选择模板),下一步,输入项目名,finish就行了
idea新建图说:
1)File->new->新建project
2)new->新建model(右键项目目录新建)
3)选择maven(不需要选择模板),下一步
4)输入项目名,finish就行了
第二步:springcloud-gateway-example的pom.xml
打包方式:pom
maven管理包
<packaging>pom</packaging>
<properties>
<spring-boot.version>2.2.5.RELEASE</spring-boot.version>
<spring-cloud.version>Hoxton.SR8</spring-cloud.version>
<spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
第三步:gateway-common的pom.xml
<dependencies>
<!-- stringBoot begin-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- stringBoot end-->
<!-- nacos服务注册和配置中心 begin-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- nacos服务注册和配置中心 begin-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- log4日志 begin-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
<!-- log4日志 end-->
</dependencies>
第四步:discovery-business1-server和discovery-business2-server的pom.xml
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>gateway-commom</artifactId>
<version>${parent.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
第五步:discovery-gateway-server的pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
4.对服务discovery-business1-server和discovery-business2-server创建api接口
4.1 创建包org.example
4.2 创建启动类:Business1application
@SpringBootApplication
@EnableDiscoveryClient
@RestController
@RequestMapping("/bus1")
public class Business1Application {
public static void main (String [] args){
SpringApplication.run(Business1Application.class, args);
}
@RequestMapping("/1")
public String getBus1() {
return "你好:bus1";
}
}
4.3 创建启动:Business2application
@SpringBootApplication
@EnableDiscoveryClient
@RestController
@RequestMapping("/bus2")
public class Business2application {
public static void main (String [] args){
SpringApplication.run(Business1Application.class, args);
}
@RequestMapping("/1")
public String getBus1() {
return "你好:bus2";
}
}
5.对服务discovery-business1-server和discovery-business2-server创建bootstrap.yml
5.1discovery-business1-server创建yml
server:
port: 8181
# 设置应用名称,便于在注册中心中查看
spring:
application:
name: business1-provider
# 设置注册中心
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
5.2discovery-business2-server创建yml
server:
port: 8182
# 设置应用名称,便于在注册中心中查看
spring:
application:
name: business2-provider
# 设置注册中心
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
6.discovery-gateway-server服务
初次体验gateway
完整yml配置文件:
server:
port: 8180
# 设置应用名称,便于在注册中心中查看
spring:
application:
name: gateway-server
# 设置注册中心
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
gateway:
routes:
- id: example-server
uri: https://example.org/
predicates:
- Path=/test
- id: business1-provider
uri: http://localhost:8181/
predicates:
- Path=/bus2/**
- id: business2-provider
uri: lb://business2-provider
predicates:
- Path=/bus2/**
访问
-
浏览器直接输入:http://127.0.0.1:8180/test 就会转发到:https://example.org/test
-
浏览器直接输入:http://127.0.0.1:8180/bus1/1 就会转发到: http://localhost:8181/bus1/1
规则就是在predicates断言,如果判断断言为true,就会路由并打多出来的path拼接到uri的后面转发
主要配置说明
spring.cloud:
gateway:
routes:
- id: example-server
uri: https://example.org/
predicates:
- Path=/test
- id: business1-provider
uri: http://localhost:8181/
predicates:
- Path=/bus2/**
- id: business2-provider
uri: lb://business2-provider
predicates:
- Path=/bus2/**
- id:就是唯一值,可以随便写,确保唯一就行
- uri:就是你的请求需要转发后的地址
- predicates:断言,就是满足条件,满足就转发到uri
注:lb://business2-provider
在注册中心才能使用,lb是LoadBalanced的缩写,负载均衡的意思,后面是服务名