【Java Spring Cloud 实战之路】- 使用Nacos和网关中心的创建
0. 前言
在上一节中,我们创建了一个项目架构,后续的项目都会在那个架构上做补充。
1. Nacos
1.1 简介
Nacos可以用来发现、配置和管理微服务。提供了一组简单易用的特性集,可以快速实现动态服务发现、服务配置、服务元数据及流量管理。
Nacos用来更敏捷和容易地构建、交付和管理微服务平台。Nacos是构建以”服务“为中心的现代应用构架(例如微服务范式、云原生范式)的服务基础设置。
也就是通常我们所说的配置中心和服务发现中心。
1.2 搭建和启动
Nacos目前版本不支持以Spring boot的形式创建服务,必须以一个Java包的形式单独运行或者以Docker服务的形式运行,我们大概讲解一下本地运行。
下载安装包:
curl https://github.com/alibaba/nacos/releases/download/1.2.1/nacos-server-1.2.1.zip
unzip nacos-server-$version.zip 或者 tar -xvf nacos-server-$version.tar.gz
cd nacos/bin
使用源码安装:
git clone https://github.com/alibaba/nacos.git
cd nacos/
mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U
ls -al distribution/target/
// change the $version to your actual path
cd 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运行文件。
2. Spring Cloud Gateway
整个的网关服务,我们采用的Spring Cloud Gateway。在Spring Cloud微服务里,整个系统只对外公开了网关,其他的服务是对外不可见的。所以需要设置一个让我们可以用的网关服务。
在 nature/manager下创建一个gateway目录,并添加pom.xml:
<?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">
<parent>
<groupId>club.attachie</groupId>
<artifactId>manager</artifactId>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>club.attachie</groupId>
<artifactId>gateway</artifactId>
<packaging>jar</packaging>
<version>${revision}</version>
</project>
在manager下注册该模块:
<modules>
<module>gateway</module>
</modules>
2.1 添加 Gateway
创建完成项目后,需要添加依赖包:
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-gateway -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
在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
@RefreshScope
public 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 版本号:
<nacos.version>2.2.1.RELEASE</nacos.version>
然后在dependencyManagement > dependencies 下添加 nacos相关依赖管理:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>${nacos.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-starters</artifactId>
<version>${nacos.version}</version>
</dependency>
在Gateway项目中pom.xml 添加:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
然后回过头来,在bootstrap里设置:
spring:
application:
name: gateway
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848
3 总结
nacos的配置和Gateway应用的介绍就到这里为止了,因为个人并未对相关技术进行过多深入的研究,所以目前只能做到这些。后续研究深入了,会在这个系列中补齐的。
更多内容烦请关注我的博客《高先生小屋》
【Java Spring Cloud 实战之路】- 使用Nacos和网关中心的创建的更多相关文章
- 【Java Spring Cloud 实战之路】添加一个SpringBootAdmin监控
0. 前言 在之前的几章中,我们先搭建了一个项目骨架,又搭建了一个使用nacos的gateway网关项目,网关项目中并没有配置太多的东西.现在我们就接着搭建在Spring Cloud 微服务中另一个重 ...
- Java Spring Cloud 实战之路 - 1 创建项目
0. 前言 该项目使用Maven进行管理和构建,所以需要预先配置好Maven.嗯,在这个系列里就不做过多的介绍了. 1. 创建项目 先创建一个pom.xml 文件,添加以下内容: <?xml v ...
- Spring Cloud实战 | 最终篇:Spring Cloud Gateway+Spring Security OAuth2集成统一认证授权平台下实现注销使JWT失效方案
一. 前言 在上一篇文章介绍 youlai-mall 项目中,通过整合Spring Cloud Gateway.Spring Security OAuth2.JWT等技术实现了微服务下统一认证授权平台 ...
- [Spring Cloud实战 | 第六篇:Spring Cloud Gateway+Spring Security OAuth2+JWT实现微服务统一认证授权
一. 前言 本篇实战案例基于 youlai-mall 项目.项目使用的是当前主流和最新版本的技术和解决方案,自己不会太多华丽的言辞去描述,只希望能勾起大家对编程的一点喜欢.所以有兴趣的朋友可以进入 g ...
- 5w 字 | 172 图 | 超级赛亚级 Spring Cloud 实战
一.PassJava 项目简介 PassJava-Learning 项目是 PassJava(佳必过)项目的学习教程.对架构.业务.技术要点进行讲解. PassJava 是一款 Java 面试刷题 的 ...
- Spring Cloud实战: 基于Spring Cloud Gateway + vue-element-admin 实现的RBAC权限管理系统,实现网关对RESTful接口方法权限和自定义Vue指令对按钮权限的细粒度控制
一. 前言 信我的哈,明天过年. 这应该是农历年前的关于开源项目 的最后一篇文章了. 有来商城 是基于 Spring Cloud OAuth2 + Spring Cloud Gateway + JWT ...
- Spring Cloud实战 | 第十一篇:Spring Cloud Gateway 网关实现对RESTful接口权限控制和按钮权限控制
一. 前言 hi,大家好,这应该是农历年前的关于开源项目 的最后一篇文章了. 有来商城 是基于 Spring Cloud OAuth2 + Spring Cloud Gateway + JWT实现的统 ...
- Spring Cloud Alibaba基础教程:Nacos的集群部署
继续说说生产环境的Nacos搭建,通过上一篇<Spring Cloud Alibaba基础教程:Nacos的数据持久化>的介绍,我们已经知道Nacos对配置信息的存储原理,在集群搭建的时候 ...
- Spring Cloud Alibaba基础教程:Nacos配置的多文件加载与共享配置
前情回顾: <Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现> <Spring Cloud Alibaba基础教程:支持的几种服务消费方式> ...
随机推荐
- 【Scala】Actor并发编程实现单机版wordCount
文章目录 对单个文本文件进行单词计数 对多个文本文件进行单词计数 对单个文本文件进行单词计数 import scala.actors.Actor import scala.io.Source //读取 ...
- MySQL安装(linux)
Centos 安装mysql 安装mariadb yum install mariadb mariadb-server mariadb-devel 安装mysql rpm -qa | grep MyS ...
- Linux --如何新增一块硬盘并自动挂载
1. 虚拟机添加硬盘 2. 分区 fdisk /dev/sdb 3. 格式化 mkfs -t ext4 /dev/sdb1 将刚刚创建的盘格式化成 ext4格式 4. 挂载 先创建一个目录,/hom ...
- Badboy脚本开发
Badboy中的检查点 以sogo.com搜索为例演示,搜索Badboy 选中搜索框中的关键词----菜单“Tools”----“Add Assertion for Selection”添加检查点 2 ...
- 我的linux学习日记day8
链接文件 linux中链接有两种,一种是硬链接,一种是软链接 一.硬链接 硬链接是指通过索引节点来进行链接创建硬链接的方法如下:ln 源文件 硬链接文件具有相同inode号的多个文件互为硬链接文件删除 ...
- spark机器学习从0到1聚类算法 (十)
一.概念 1.1.定义 按照某一个特定的标准(比如距离),把一个数据集分割成不同的类或簇,使得同一个簇内的数据对象的相似性尽可能大,同时不再同一个簇内的数据对象的差异性也尽可能的大. 聚类属于典型 ...
- Redis 消息中间件 ServiceStack.Redis 轻量级
问题: 公司开了个新项目,算上我一共3个人.车间里机台通过流水线连通联动的玩意.一个管理控制系统连接各个机台和硬件.专机类型就有5种,个数差不多20个左右. 软件规划的时候采用总分的结构,管理控制系统 ...
- Spring 自动装配 byType
自动装配 byType,这种模式由属性类型指定自动装配. Spring 容器看作 beans,在 XML 配置文件中 beans 的 autowire 属性设置为 byType.然后,如果它的 typ ...
- GYM101635E Ingredients
题目链接:https://vjudge.net/problem/Gym-101635E 题目大意: 给定一个有 \(N\) 条边的有向无环图(有多个起点),每条边都有其费用和收益,现要从一个或多个起点 ...
- zwx_helper 只用小括号()和中括号[ ] 轻松开发wxWidgets
https://github.com/bbqz007/zhelper-wxWidgets https://github.com/bbqz007/zhelper-wxWidgets/tree/maste ...