docker 安装nacos_「Java Spring Cloud 实战之路」 使用nacos配置网关

0. 前言

在上一节中,我们创建了一个项目架构,后续的项目都会在那个架构上做补充。

1. Nacos

1.1 简介

Nacos可以用来发现、配置和管理微服务。提供了一组简单易用的特性集,可以快速实现动态服务发现、服务配置、服务元数据及流量管理。

Nacos用来更敏捷和容易地构建、交付和管理微服务平台。Nacos是构建以”服务“为中心的现代应用构架(例如微服务范式、云原生范式)的服务基础设置。

也就是通常我们所说的配置中心和服务发现中心。

4cf97cc80c77e72d933f602ea3415ed7.png

1.2 搭建和启动

Nacos目前版本不支持以Spring boot的形式创建服务,必须以一个Java包的形式单独运行或者以Docker服务的形式运行,我们大概讲解一下本地运行。

下载安装包:

curl https://github.com/alibaba/nacos/releases/download/1.2.1/nacos-server-1.2.1.zipunzip nacos-server-$version.zip 或者 tar -xvf nacos-server-$version.tar.gzcd nacos/bin

使用源码安装:

git clone https://github.com/alibaba/nacos.gitcd nacos/mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U  ls -al distribution/target/// change the $version to your actual pathcd distribution/target/nacos-server-$version/nacos/bin

启动:

Linux/Unix/Mac

启动命令(standalone代表着单机模式运行,非集群模式):

sh startup.sh -m standalone

如果您使用的是ubuntu系统,或者运行脚本报错提示[[符号找不到,可尝试如下运行:

bash startup.sh -m standalone

Windows

启动命令:

cmd startup.cmd

或者双击startup.cmd运行文件。

c399ebbd2659da08aadb6c4ad033b9a8.png

2. Spring Cloud Gateway

整个的网关服务,我们采用的Spring Cloud Gateway。在Spring Cloud微服务里,整个系统只对外公开了网关,其他的服务是对外不可见的。所以需要设置一个让我们可以用的网关服务。

在 nature/manager下创建一个gateway目录,并添加pom.xml:

<?xml version="1.0" encoding="UTF-8"?>club.attachie        manager        ${revision}4.0.0club.attachie    gateway    jar${revision}

在manager下注册该模块:

gateway

2.1 添加 Gateway

创建完成项目后,需要添加依赖包:

org.springframework.cloud    spring-cloud-starter-gateway

在gateway项目中,创建如下目录:

├── pom.xml└── src    └── main        ├── java        │   └── club        │       └── attachie        │           └── gateway        │               └── SpringGatewayApplication.java        └── resources            └── bootstrap.yml

创建 SpringGateAppliction.java文件,代码如下:

package club.attachie.gateway;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.context.config.annotation.RefreshScope;/** * @author attaching */@SpringBootApplication@EnableDiscoveryClient@RefreshScopepublic class SpringGatewayApplication {    public static void main(String[] args) {        SpringApplication.run(SpringGatewayApplication.class, args);    }}

在resource目录下创建 bootstrap.yml:

spring:  application:    name: gateway

yml 是Spring 的一种配置文件格式,其中名称有application和bootstrap,bootstrap比application先加载。

2.2 添加 nacos

先在 nature/pom.xml 添加 nacos 版本号:

2.2.1.RELEASE

然后在dependencyManagement > dependencies 下添加 nacos相关依赖管理:

com.alibaba.cloud    spring-cloud-starter-alibaba-nacos-discovery    ${nacos.version}com.alibaba.cloud    spring-cloud-alibaba-starters    ${nacos.version}

在Gateway项目中pom.xml 添加:

com.alibaba.cloud    spring-cloud-starter-alibaba-nacos-discoverycom.alibaba.cloud    spring-cloud-starter-alibaba-nacos-discovery

然后回过头来,在bootstrap里设置:

spring:  application:    name: gateway  cloud:    nacos:      config:        server-addr: 127.0.0.1:8848
387c7c26c863b9bb5f9829789072f06e.png

3 总结

nacos的配置和Gateway应用的介绍就到这里为止了,因为个人并未对相关技术进行过多深入的研究,所以目前只能做到这些。后续研究深入了,会在这个系列中补齐的。

求关注,求点赞,求转发~~有啥可以评论哟

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值