Nacos集成Spring Cloud Gateway使用第二章:上手demo


本次demo為Nacos集成Spring Cloud Gateway,並且使用openfeign實現服務間的相互調用

如需要查看理解: 上一章:Nacos集成Spring Cloud Gateway使用第一章:理解解釋

如需引用nacos的配置中心則查看下一章:Nacos集成Spring Cloud Gateway使用第三章:nacos配置中心

nacos安裝的教程 直接根據官網的一步步來吧 https://nacos.io/zh-cn/docs/quick-start.html

1.新建一個springcloud項目

 內涵一個網關 兩個服務

1.1 父類pom

<?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>

    <groupId>org.example</groupId>
    <artifactId>nacos-gateway</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.12.RELEASE</version>
        <relativePath/>
    </parent>

    <modules>
        <module>gateway</module>
        <module>serverone</module>
        <module>servertwo</module>
    </modules>

    <dependencies>
        <!-- spring-cloud  -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Greenwich.SR3</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <!--注冊中心 實現服務的注冊與發現
        注意:版本 2.1.x.RELEASE 對應的是 Spring Boot 2.1.x 版本。版本 2.0.x.RELEASE 對應的是 Spring Boot 2.0.x 版本,版本 1.5.x.RELEASE 對應的是 Spring Boot 1.5.x 版本。
        -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.1.2.RELEASE</version>
        </dependency>

        <!-- spring-boot-test  -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>
  1.2 網關層服務

 pom文件

<?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>
    <!-- 我是一個模擬注冊nacos的一個服務 ,網關層 -->
    <parent>
        <groupId>org.example</groupId>
        <artifactId>nacos-gateway</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <groupId>com.demo</groupId>
    <artifactId>gateway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>gateway</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- 網關 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
            <version>2.1.3.RELEASE</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
  yml配置文件
server:
  port: 13008
spring:
  application:
    name: gateway
  cloud:
    nacos:
      discovery:
        server-addr: 自己nacos的地址
        ip: 127.0.0.1
        group: gatewaynacos
        namespace: demo
    gateway:
      discovery:
        locator:
          #是否與服務發現組件進行結合,通過 serviceId 轉發到具體的服務實例。默認為false,設為true便開啟通過服務中心的自動根據 serviceId 創建路由的功能
          enabled: true
          #開啟小寫,#路由訪問方式:http://Gateway_HOST:Gateway_PORT/大寫的serviceId/**,其中微服務應用名默認大寫訪問。
          lower-case-service-id: true
      routes:
      #第一個服務的路由規則
      - id: serverone
        # uri以lb://開頭(lb代表從注冊中心獲取服務),后面接的就是你需要轉發到的服務名稱
        uri: lb://serverone
        predicates:
        #以后訪問 http://127.0.0.1:13008/serverone/one/hello/two,ip:端口/自定義path/接口地址
        - Path=/serverone/**
        #第二個服務的路由規則
      - id: servertwo
        uri: lb://servertwo
        predicates:
        - Path=/servertwo/**
  gateway啟動類文件
package com.demo.gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient //開啟服務注冊發現功能
public class GatewayApplication {

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

}

  1.3 第一個服務

 pom文件
<?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>
    <!-- 我是一個模擬注冊nacos的一個服務  -->
    <parent>
        <groupId>org.example</groupId>
        <artifactId>nacos-gateway</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>com.demo</groupId>
    <artifactId>serverone</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>serverone</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>2.1.3.RELEASE</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
  ServeroneApplication
package com.demo.serverone;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient //開啟服務注冊發現功能
@EnableFeignClients
public class ServeroneApplication {

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


}
  OneHelloController
package com.demo.serverone;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "one")
public class OneHelloController {

    @Autowired
    public ServiceClient serviceClient;

    @GetMapping("/hello")
    public String hello(){
        return "hello,我是1號";
    }

    @GetMapping("/hello/two")
    public void hellotwo(){
        serviceClient.test("hello2號,我是從1號來的");
    }

}
  ServiceClient
package com.demo.serverone;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(value = "servertwo")
public interface ServiceClient {

    @GetMapping(value = "/two/hello/one")
    void test(@RequestParam(value = "test")String test);
}
application.yml
server:
  port: 9088
spring:
  application:
    name: serverone
  cloud:
    nacos:
      discovery:
        server-addr: 自己的nacos地址 改成自己的
        ip: 127.0.0.1
        group: gatewaynacos
        namespace: demo 

1.4 第二個服務

 pom
<?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>
    <!-- 我是一個模擬注冊nacos的一個服務  -->
    <parent>
        <groupId>org.example</groupId>
        <artifactId>nacos-gateway</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>com.demo</groupId>
    <artifactId>servertwo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>servertwo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>2.1.3.RELEASE</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
  application.yml
server:
  port: 9087
spring:
  application:
    name: servertwo
  cloud:
    nacos:
      discovery:
        server-addr: nacos的地址 改成自己的
        ip: 127.0.0.1
        group: gatewaynacos
        namespace: demo
  ServertwoApplication
package com.demo.servertwo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient //開啟服務注冊發現功能
public class ServertwoApplication {

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

}
  TwoHelloController
package com.demo.servertwo;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "two")
public class TwoHelloController {

    @GetMapping("/hello")
    public String hello() {
        return "hello,我是2號";
    }


    @GetMapping("/hello/one")
    public void helloOne(@RequestParam(value = "test") String test) {
        System.out.println(test);
    }
}

  測試

  創建完成后,會在nacos里面看到自己注冊的服務

  測試可以通過網關訪問到第一個服務ip+網關端口/自己定義的服務名/接口

嘗試從第一個服務調用第二個服務,直接打開網址或者自己測試類隨便  http://127.0.0.1:13008/serverone/one/hello/two

 ok 成功。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM