文章目录
- 一、网关模块搭建
- 1. 引入依赖
- 2. 配置
- 3. 启动类
- 二、产品服务模块搭建
- 2.1. 引入依赖
- 2.2. 配置
- 2.3. 控制层
- 2.4. 启动类
- 2.5. 启动产品模块
- 三、启动中间件
- 3.1. nacos启动
- 3.2. 启动gateway
- 3.3. 配置域名映射
- 四、测试验证
- 4.1. 测试产品服务
- 4.2. 网关访问产品
一、网关模块搭建
1. 引入依赖
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><spring.cloud-version>Hoxton.SR9</spring.cloud-version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency></dependencies><dependencyManagement><!--https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E--><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>
2. 配置
访问product.gblfy.com**
就会路由到http://localhost:9000
这里的9000端口我们的产品服务模块
server:port: 8081
spring:cloud:gateway:routes:- id: producturi: http://localhost:9000predicates:- Host=product.gblfy.com**
3. 启动类
package com.gblfy.gatewayserv;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class GatewayServApplication {public static void main(String[] args) {SpringApplication.run(GatewayServApplication.class, args);}}
二、产品服务模块搭建
2.1. 引入依赖
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--服务注册发现--><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-sentinel</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency></dependencies><dependencyManagement><dependencies><!--spring-cloud-alibaba 版本控制--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>2.2.6.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>
2.2. 配置
server:port: 9000
management:endpoints:web:exposure:include: '*'
spring:cloud:nacos:discovery:service: product-servserver-addr: localhost:8848
2.3. 控制层
package com.gblfy.controller;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;@RestController
public class ProductController {//http://localhost:9000/product/" + productId@GetMapping("/product/{productId}")public String getProductName(@PathVariable Integer productId) {return "IPhone 12";}
}
2.4. 启动类
package com.gblfy;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class ProductAplication {public static void main(String[] args) {SpringApplication.run(ProductAplication.class);}
}
2.5. 启动产品模块
三、启动中间件
3.1. nacos启动
3.2. 启动gateway
3.3. 配置域名映射
四、测试验证
4.1. 测试产品服务
http://localhost:9000/product/1
4.2. 网关访问产品
通过网关gateway访问产品模块服务
http://product.gblfy.com:8081/product/1