注:框架是使用了nacos作为注册中心,要自己先搭建nacos
项目框架:
idea创建项目结构:
父亲模块创建:
File->new->新建project,选择maven(不需要选择模板),下一步,输入项目名,finish就行了
子模块创建:
新建model,选择maven(不需要选择模板),下一步,输入项目名,finish就行了
idea新建图说:
1)File->new->新建project
2)new->新建model(右键项目目录新建)
3)选择maven(不需要选择模板),下一步
4)输入项目名,finish就行了
打包方式:pom
maven管理包
<packaging>pompackaging>
<properties>
<spring-boot.version>2.2.5.RELEASEspring-boot.version>
<spring-cloud.version>Hoxton.SR8spring-cloud.version>
<spring-cloud-alibaba.version>2.2.1.RELEASEspring-cloud-alibaba.version>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>${spring-boot.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${spring-cloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>${spring-cloud-alibaba.version}version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>jcl-over-slf4jartifactId>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
dependency>
dependencies>
<dependencies>
<dependency>
<groupId>org.examplegroupId>
<artifactId>gateway-commomartifactId>
<version>${parent.version}version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<executions>
<execution>
<goals>
<goal>repackagegoal>
goals>
execution>
executions>
plugin>
<plugin>
<artifactId>maven-compiler-pluginartifactId>
<configuration>
<source>1.8source>
<target>1.8target>
configuration>
plugin>
plugins>
build>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-gatewayartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<executions>
<execution>
<goals>
<goal>repackagegoal>
goals>
execution>
executions>
plugin>
<plugin>
<artifactId>maven-compiler-pluginartifactId>
<configuration>
<source>1.8source>
<target>1.8target>
configuration>
plugin>
plugins>
build>
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.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
完整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/**