写在前面
网关的作(用)不在此赘述,『举个最常』(用)的例子,‘我们搭建了微’「服务」,前端挪(用)各「服务」接口时,由于各「服务」接口不一样,若是让前端同事划分挪(用),前端同事会疯的。 而网关就[可以解决这个问题,网关屏障了各营业「服务」(的端口),(对前端)同事来说,他们只卖力挪(用)网关「服务」端口下的「服务」就可以了。本文简朴形貌若何使(用)Spring Cloud全家桶中的网关「服务」,<再配以>Nacos。关于Nacos简朴应(用),可以看我其他博客。
「服务」提供者
“‘从’”https://start.spring.io/‘下载一’个原始的spring boot“‘工’程”,“若何下载就不在这”里说了。〖〖添加依赖〗〗:
<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-config</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
划分添加了web依赖、设置中央依赖和注册中央依赖。
(设)置文件如下:
server.port=8070
spring.application.name=service-provider
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
{启动类}如下:
package com.chris.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class MySpringbootApplication { public static void main(String[] args) { SpringApplication.run(MySpringbootApplication.class, args); } }
接口类如下:
package com.chris.springboot.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/provider") @RefreshScope public class ConfigController { @Value(value = "${Hello:123}") private String hello; @GetMapping("/helloProvider") public String helloProvider(){ return hello; } }
此「服务」为我的博客:https://www.cnblogs.com/ncwuwsh/p/12732516.html中的「服务」,可参看。
网关「服务」
“‘从’”https://start.spring.io/‘下载一’个原始的spring boot“‘工’程”,“若何下载就不在这”里说了。〖〖添加依赖〗〗:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2.2.1.RELEASE</version> </dependency>
注重,万万不要添加web依赖。
设置文件可以使(用)properties,也可以使(用)yml花样。yml『花样如下』:
server:
port: 8080
spring:
application:
name: api-gateway
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
gateway:
discovery:
locator:
enabled: true #<解>释gateway开启「服务」注册和发现的「功效」,<而且>spring cloud gateway自动凭据「服务」发现为每一个「服务」创建了一个router,这个router将以「服务」名开头的请求路径转发到对应的「服务」。
lower-case-service-id: true #是将请求路径上的「服务」{名设置为}小写(由于「服务」注册的时刻,向注册中央注册时将「服务」名转成大写的了), 『好比』以[/service-hi/* 的请求路径被路由转发[到「服务」名为service-hi的「服务」上。
routes:
- id: gateway-service
uri: lb://service-provider #此设置的值注册到Nacos中「服务」提供者的spring.application.name的值
predicates:
- Path=/provider/**
使(用)yml{的同砚},一定要去查下yml的一些规则,『好比』 :后面,{值的前面},一定要有空格,缩进不要使(用)tab键,而要(用)两个空格缩进等
下面是properties<花样设>置文件:
server.port=8080
spring.application.name=api-gateway
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
#<解>释gateway开启「服务」注册和发现的「功效」,<而且>spring cloud gateway自动凭据「服务」发现为每一个「服务」创建了一个router,这个router将以「服务」名开头的请求路径转发到对应的「服务」。
spring.cloud.gateway.discovery.locator.enabled=true
#是将请求路径上的「服务」{名设置为}小写(由于「服务」注册的时刻,向注册中央注册时将「服务」名转成大写的了), 『好比』以[/service-hi/* 的请求路径被路由转发[到「服务」名为service-hi的「服务」上。
spring.cloud.gateway.discovery.locator.lower-case-service-id=true
spring.cloud.gateway.routes[0].id=gateway-service
spring.cloud.gateway.routes[0].uri=lb://service-provider
spring.cloud.gateway.routes[0].predicates[0]=Path=/provider/**
下面是网关的启动类:
package com.chris.gatewayrouter; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.gateway.route.RouteLocator; import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; import org.springframework.context.annotation.Bean; @SpringBootApplication @EnableDiscoveryClient public class GatewayrouterApplication { public static void main(String[] args) { SpringApplication.run(GatewayrouterApplication.class, args); } @Bean public RouteLocator myRoutes(RouteLocatorBuilder builder) { return builder.routes().build(); } }
「然后启动」Nacos,「服务」提供者和网关「服务」,使(用)浏览器接见:http://127.0.0.1:8080/provider/helloProvider
{搞定}。
网关「服务」的其他高级应(用),“自己去看”官网吧。
官网是最好的先生
,
Allbet欧博官网声明:该文看法仅代表作者自己,与本平台无关。转载请注明:嘉兴娱乐:Spring Cloud Gateway+Nacos,yml+properties两种配置文件方式搭建网关服务(诚信在)线(www.ludiealliedinstitute.com)现已开放(诚信在)线手机〖版下载〗。游戏公平、『公开』、公正,(用)实力赢取信誉。