本次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 成功。