2、gateway整合nacos使用

一、父聚合项目springcloudalibaba

第一步:创建项目,pom添加依赖


<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.0modelVersion>
    <modules>
   		<module>gatewaymodule>
    modules>
    
    
    






    
    <groupId>com.examplegroupId>
    <artifactId>springcloudalibabaartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>springcloudalibabaname>
    <description>Spring Cloud Alibabadescription>
    <packaging>pompackaging>

    
    <properties>
        <java.version>1.8java.version>
        <spring.cloud.alibaba.version>2.2.5.RELEASEspring.cloud.alibaba.version>
        <spring.cloud.version>Hoxton.SR8spring.cloud.version>
        <spring.boot.version>2.3.11.RELEASEspring.boot.version>
    properties>

    <dependencyManagement>
        
        
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloudgroupId>
                <artifactId>spring-cloud-alibaba-dependenciesartifactId>
                <version>${spring.cloud.alibaba.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>

            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-parentartifactId>
                <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>
        dependencies>
    dependencyManagement>

    
    <dependencies>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
        dependency>

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>



    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>
project>

二、子模块order

第一步:创建子模块,pom.xml添加依赖

三、子模块stock

四、子模块gateway

第一步:创建子模块,pom.xml添加依赖


<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>
        <artifactId>springcloudalibabaartifactId>
        <groupId>com.examplegroupId>
        <version>0.0.1-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>gatewayartifactId>

    <dependencies>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-gatewayartifactId>
        dependency>
        
        
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
        dependency>
    dependencies>
    
project>

第二步:创建gateway启动类

package com.example.gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class GatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class);
    }
}

第三步:创建配置文件application.yml

server:
  port: 8070
spring:
  application:
    name: api-gateway
  cloud:
    #gateway的配置
    gateway:
      #路由配置 [路由 就是指定当请求满足什么条件的时候转到哪个微服务]
      routes:
        - id: order_route #路由的唯一标识,路由到order
          uri: lb://order-service #需要转发的地址,lb指的是从nacos中按照名称获取微服务,并遵循负载均衡策略
          #断言规则,就是路由转发要满足的条件
          predicates:
            - Path=/order-ser/** #当请求路径满足Path指定的规则时,才进行路由转发
            # 当客户端访问http://localhost:8070/order-ser/order/add 会路由到↓
            #  http://localhost:8081/order-ser/order/add,这个无法访问,需要filters过滤下
          filters: #过滤器,请求在传递过程中可以通过过滤器对其进行一定的修改
            - StripPrefix=1 # 转发之前去掉第一层路由
            # StripPrefix的操作会将上述http://localhost:8081/order-ser/order/add 路由到
            # http://localhost:8081/order/add
      #        - id: stock_route
    #配置nacos
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
        username: nacos
        password: nacos

第四步:启动服务,测试

启动nacos,order-service,stock-service,gateway

2、gateway整合nacos使用_第1张图片

2、gateway整合nacos使用_第2张图片

访问:http://localhost:8070/order-ser/order/add

2、gateway整合nacos使用_第3张图片

访问成功

简单配置

server:
  port: 8070
spring:
  application:
    name: api-gateway
  cloud:
    #gateway的配置
    gateway:
      discovery:
        locator:
          enabled: true #是否启动自动识别nacos服务
    #配置nacos
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
        username: nacos
        password: nacos

spring.cloud.gateway.discovery.locator.enabled=true,会拿nacos服务名为断言,也会将服务名进行过滤,从而路由到该服务的请求。所以访问:http://localhost:8070/order-service/order/add

2、gateway整合nacos使用_第4张图片

你可能感兴趣的:(微服务,gateway,spring,cloud,java)