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和网关中心的创建的更多相关文章

  1. 【Java Spring Cloud 实战之路】添加一个SpringBootAdmin监控

    0. 前言 在之前的几章中,我们先搭建了一个项目骨架,又搭建了一个使用nacos的gateway网关项目,网关项目中并没有配置太多的东西.现在我们就接着搭建在Spring Cloud 微服务中另一个重 ...

  2. Java Spring Cloud 实战之路 - 1 创建项目

    0. 前言 该项目使用Maven进行管理和构建,所以需要预先配置好Maven.嗯,在这个系列里就不做过多的介绍了. 1. 创建项目 先创建一个pom.xml 文件,添加以下内容: <?xml v ...

  3. Spring Cloud实战 | 最终篇:Spring Cloud Gateway+Spring Security OAuth2集成统一认证授权平台下实现注销使JWT失效方案

    一. 前言 在上一篇文章介绍 youlai-mall 项目中,通过整合Spring Cloud Gateway.Spring Security OAuth2.JWT等技术实现了微服务下统一认证授权平台 ...

  4. [Spring Cloud实战 | 第六篇:Spring Cloud Gateway+Spring Security OAuth2+JWT实现微服务统一认证授权

    一. 前言 本篇实战案例基于 youlai-mall 项目.项目使用的是当前主流和最新版本的技术和解决方案,自己不会太多华丽的言辞去描述,只希望能勾起大家对编程的一点喜欢.所以有兴趣的朋友可以进入 g ...

  5. 5w 字 | 172 图 | 超级赛亚级 Spring Cloud 实战

    一.PassJava 项目简介 PassJava-Learning 项目是 PassJava(佳必过)项目的学习教程.对架构.业务.技术要点进行讲解. PassJava 是一款 Java 面试刷题 的 ...

  6. Spring Cloud实战: 基于Spring Cloud Gateway + vue-element-admin 实现的RBAC权限管理系统,实现网关对RESTful接口方法权限和自定义Vue指令对按钮权限的细粒度控制

    一. 前言 信我的哈,明天过年. 这应该是农历年前的关于开源项目 的最后一篇文章了. 有来商城 是基于 Spring Cloud OAuth2 + Spring Cloud Gateway + JWT ...

  7. Spring Cloud实战 | 第十一篇:Spring Cloud Gateway 网关实现对RESTful接口权限控制和按钮权限控制

    一. 前言 hi,大家好,这应该是农历年前的关于开源项目 的最后一篇文章了. 有来商城 是基于 Spring Cloud OAuth2 + Spring Cloud Gateway + JWT实现的统 ...

  8. Spring Cloud Alibaba基础教程:Nacos的集群部署

    继续说说生产环境的Nacos搭建,通过上一篇<Spring Cloud Alibaba基础教程:Nacos的数据持久化>的介绍,我们已经知道Nacos对配置信息的存储原理,在集群搭建的时候 ...

  9. Spring Cloud Alibaba基础教程:Nacos配置的多文件加载与共享配置

    前情回顾: <Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现> <Spring Cloud Alibaba基础教程:支持的几种服务消费方式> ...

随机推荐

  1. Coursera课程笔记----C程序设计进阶----Week 1&2

    C程序中的函数(Week 1&2) 函数 函数的定义 对函数的普遍认识:y=f(x) C语言中的常用函数: 平方根: r = sqrt(100.0) 底数x的y次幂:k = pow(x,y) ...

  2. Appium+Python-项目实践一

    一.前言                            前面讲了环境搭建和常用的元素定位,后续会持续以项目实践的方式去慢慢学习以及整理各方面的知识点,具体不会详细阐述,但会贴上完整代码,想要了 ...

  3. docker-compose安装rabbitmq集群(主从集群---》镜像集群)

    docker-compose安装rabbitmq集群(主从集群--->镜像集群) yls 2020/5/11 创建docker-compose.yml 文件 version: '3' servi ...

  4. Qt自定义文本输入框实现支持输入度分秒和度两种格式(简易无限制输入)

    1.自定义文本输入框 #ifndef MYLINEEDIT_H #define MYLINEEDIT_H #include <QLineEdit> #include "ui_my ...

  5. C#winform跨窗体传值和调用事件的办法

    有三个窗体,分别是Main主窗体,Form1窗体1,From2窗体2,其中Main是主窗体,Form1窗体1是一个消息通知窗体,Form2窗体2主窗体的一个子窗体,程序启动时,消息框窗体1弹出,通过消 ...

  6. Linux shell 正则表达式用法

    1.“ \  ” 用法 用于关闭其后续字符的特殊含义,恢复字符的本身含义,如:\\ 表示字符 \ 2. “ . " 用法 匹配任意单个字符 3. " * " 用法 匹配任 ...

  7. Spring源码解析02:Spring IOC容器之XmlBeanFactory启动流程分析和源码解析

    一. 前言 Spring容器主要分为两类BeanFactory和ApplicationContext,后者是基于前者的功能扩展,也就是一个基础容器和一个高级容器的区别.本篇就以BeanFactory基 ...

  8. json 格式要求

    json 格式中, 字符串类型需要使用双引号,不能为单引号

  9. 笨办法学习python-ex51自我理解笔记

    本章节主要讲的是web的工作原理,先大概熟悉记录一下,为以后写Django web框架打下基础. web工作原理: 1.用户从浏览器输入网址----->browser通过电脑中的网络设备(网卡) ...

  10. 减少 zabbix 频繁报警

    一直以来困扰的我问题是,触发器一旦触发,便会猛报警,如果你设置了email ,你的邮箱绝对会爆掉. 今天终于找到了方案,很简单,就是增加action 的steps ,从一个增加到default dur ...