资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

使用Nacos和网关中心的创建是怎样的

这期内容当中小编将会给大家带来有关使用Nacos和网关中心的创建是怎样的,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

创新互联于2013年成立,先为贵港等服务建站,贵港等地企业,进行企业商务咨询服务。为贵港企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。


1. Nacos

1.1 简介

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

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:


        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">
   
       club.attachie
       manager
       ${revision}
   


   4.0.0
   club.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
@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 版本号:

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-discovery


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

 

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

spring:
 application:
   name: gateway

 cloud:
   nacos:
     config:
       server-addr: 127.0.0.1:8848

上述就是小编为大家分享的使用Nacos和网关中心的创建是怎样的了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。


文章题目:使用Nacos和网关中心的创建是怎样的
文章出自:http://cdkjz.cn/article/ihcjch.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220