Nacos集成SpringCloudGateway使⽤第⼆章:上⼿demo 本次demo为Nacos集成Spring Cloud Gateway,并且使⽤openfeign实现服务间的相互调⽤
如需要查看理解:上⼀章:
如需引⽤nacos的配置中⼼则查看下⼀章:
1.新建⼀个springcloud项⽬
内涵⼀个⽹关两个服务
1.1 ⽗类pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId&le</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>
<!--注册中⼼实现服务的注册与发现
注意:版本 RELEASE 对应的是 Spring Boot 2.1.x 版本。版本 RELEASE 对应的是 Spring Boot 2.0.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="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 我是⼀个模拟注册nacos的⼀个服务 ,⽹关层 -->
<parent>
<groupId&le</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
#开启⼩写,#路由访问⽅式:Gateway_HOST:Gateway_PORT/⼤写的serviceId/**,其中微服务应⽤名默认⼤写访问。
lower-case-service-id: true
routes:
#第⼀个服务的路由规则
- id: serverone
# uri以lb://开头(lb代表从注册中⼼获取服务),后⾯接的就是你需要转发到的服务名称
uri: lb://serverone
predicates:
#以后访问 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="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 我是⼀个模拟注册nacos的⼀个服务  -->
<parent>
<groupId&le</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;
springcloud和springboot@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
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);
}
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="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"        xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>
<!-- 我是⼀个模拟注册nacos的⼀个服务  -->
<parent>
<groupId&le</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>
  l
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+⽹关端⼝/⾃⼰定义的服务名/接⼝
ok 成功。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。