【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應用的介紹就到這裏為止了,因為個人並未對相關技術進行過多深入的研究,所以目前只能做到這些。後續研究深入了,會在這個系列中補齊的。
更多內容煩請關注我的博客《高先生小屋》
本站聲明:網站內容來源於博客園,如有侵權,請聯繫我們,我們將及時處理
※南投搬家公司費用,距離,噸數怎麼算?達人教你簡易估價知識!
搬家費用:依消費者運送距離、搬運樓層、有無電梯、步行距離、特殊地形、超重物品等計價因素後,評估每車次單