spring cloud gateway 注册到 nacos

本文介绍了如何将Spring Cloud Gateway服务注册到Nacos服务中心。首先,通过Docker搭建了Nacos单机版,并提供了启动命令。接着,创建了一个Spring Boot项目,配置了Spring Cloud Gateway,并详细说明了POM文件和application.yml的设置。最后,通过添加注解实现了服务注册与发现,启动后在Nacos页面上可以看到服务已成功注册。

nacos配置服务中心:https://blog.csdn.net/wsbgmofo/article/details/100558820

 

1,搭建nacos服务

nacos相关介绍请看https://nacos.io/zh-cn/docs/quick-start.html

这里选择docker版的nacos,相对简单很多

docker安装这里不多说,自行百度,安装完docker后执行:

docker pull nacos/nacos-server

然后启动:

docker run --env MODE=standalone --name nacos -d -p 8848:8848 nacos/nacos-server

MODE=standalone 是启动单机版

启动完后浏览器输入:http://xxx.xxx.xxx.xxx:8848/nacos/index.html

nacos默认端口8848,账号密码默认nacos/nacos,登录后如下图:

2,创建Spring Cloud Gateway服务

Spring Cloud Gateway是Spring Cloud官方推出的第二代网关框架,取代Zuul网关。网关作为流量的,在微服务系统中有着非常作用,网关常见的功能有路由转发、权限校验、限流控制等作用。

创建一个springboot项目,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>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.7.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.lanwon</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.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		
        <!-- hystrix 熔断器 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
            <version>2.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
            <version>2.1.1.RELEASE</version>
        </dependency>
        <!--nacos依赖-->
        <dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
			<version>0.9.0.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: 8001
spring:
  application:
    name: gateway-server
  cloud: 
    nacos: 
      discovery:
        serverAddr: 192.168.xxx.xxx:8848

启动类加上注册与发现的注解即可

@EnableDiscoveryClient
@SpringBootApplication
public class GatewayApplication {

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

}

3,启动

2019-09-05 14:02:15.401  INFO 4444 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$6c1e8505] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.7.RELEASE)

2019-09-05 14:02:15.790  INFO 4444 --- [           main] com.lanwon.gateway.GatewayApplication    : No active profile set, falling back to default profiles: default
2019-09-05 14:02:16.417  INFO 4444 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=3b2f35a0-7c00-35f3-a1d3-6fff39a8eabe
2019-09-05 14:02:16.499  INFO 4444 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$6c1e8505] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-09-05 14:02:16.660  WARN 4444 --- [           main] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.
2019-09-05 14:02:16.660  INFO 4444 --- [           main] c.n.c.sources.URLConfigurationSource     : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2019-09-05 14:02:16.668  WARN 4444 --- [           main] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.
2019-09-05 14:02:16.668  INFO 4444 --- [           main] c.n.c.sources.URLConfigurationSource     : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2019-09-05 14:02:17.911  INFO 4444 --- [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [After]
2019-09-05 14:02:17.916  INFO 4444 --- [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [Before]
2019-09-05 14:02:17.916  INFO 4444 --- [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [Between]
2019-09-05 14:02:17.917  INFO 4444 --- [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [Cookie]
2019-09-05 14:02:17.917  INFO 4444 --- [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [Header]
2019-09-05 14:02:17.917  INFO 4444 --- [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [Host]
2019-09-05 14:02:17.917  INFO 4444 --- [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [Method]
2019-09-05 14:02:17.917  INFO 4444 --- [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [Path]
2019-09-05 14:02:17.917  INFO 4444 --- [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [Query]
2019-09-05 14:02:17.917  INFO 4444 --- [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [ReadBodyPredicateFactory]
2019-09-05 14:02:17.917  INFO 4444 --- [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [RemoteAddr]
2019-09-05 14:02:17.918  INFO 4444 --- [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [Weight]
2019-09-05 14:02:17.918  INFO 4444 --- [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [CloudFoundryRouteService]
2019-09-05 14:02:18.509  INFO 4444 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService
2019-09-05 14:02:19.081  INFO 4444 --- [           main] o.s.b.web.embedded.netty.NettyWebServer  : Netty started on port(s): 8001
2019-09-05 14:02:19.575  INFO 4444 --- [           main] o.s.c.a.n.registry.NacosServiceRegistry  : nacos registry, gateway-server 192.168.121.230:8001 register finished
2019-09-05 14:02:19.577  INFO 4444 --- [           main] com.lanwon.gateway.GatewayApplication    : Started GatewayApplication in 5.237 seconds (JVM running for 5.709)

刷新nacos的页面,如下

注意:POM文件中的parent版本要2.1.x以上  2.0的版本会报以下错误:

No active profile set, falling back to default profiles: default

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值