項(xiàng)目版本匯總
nacos | 2.1.1 |
---|---|
springCloud | 2021.0.7 |
springboot | 2.6.14 |
spring-cloud-starter-alibaba-nacos-discovery | 2021.1 |
spring-cloud-starter-alibaba-nacos-config | 2021.1 |
項(xiàng)目初始化
新建倉(cāng)庫(kù)
可使用github/gitlab/碼云等其它代碼庫(kù),建立自己的項(xiàng)目倉(cāng)庫(kù)
我這里使用的是碼云
碼云地址
拉取倉(cāng)庫(kù)項(xiàng)目
git clone 你新建的倉(cāng)庫(kù)地址
下載后,用idea打開(kāi)(這里默認(rèn)你能配置maven,jdk以及java環(huán)境變量的操作)
父工程pom初始化
用途:該pom用于管理整個(gè)微服務(wù)項(xiàng)目的依賴框架和版本以及所屬子模塊等
如圖所示:該pom.xml在工程的根目錄下創(chuàng)建
依賴版本選擇
-
進(jìn)入Spring官網(wǎng)Spring官網(wǎng)選擇Project下的Springcloud欄進(jìn)去
-
版本號(hào)對(duì)應(yīng)關(guān)系一目了然
- 選擇2021.0.x版本的cloud,那么對(duì)應(yīng)的boot版本就是2.6-2.7之間
pom文件如下
spring-cloud-dependencies | cloud依賴 |
---|---|
spring-boot-dependencies | boot依賴 |
nacos-client | nacos版本 |
spring-boot-starter-test | 單測(cè)依賴 |
spring-cloud-starter-alibaba-nacos-discovery | 注冊(cè)中心客戶端依賴 |
spring-cloud-starter-alibaba-nacos-config | 配置中心客戶端 |
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2021.0.7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.6.14</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${boot.version}</version>
<scope>test</scope>
</dependency>
<!--注冊(cè)中心客戶端-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2021.1</version>
</dependency>
<!--配置中心客戶端-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2021.1</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2021.0.7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
網(wǎng)關(guān)服務(wù)構(gòu)建
在父工程項(xiàng)目點(diǎn)擊右鍵 New-Module起名為xwl-gateway
pom文件
spring-cloud-starter-gateway | 網(wǎng)關(guān)依賴 |
---|---|
spring-cloud-starter-openfeign | rpc通信依賴 |
spring-cloud-starter-bootstrap | cloud配置文件識(shí)別依賴 |
spring-cloud-loadbalancer | cloud內(nèi)置負(fù)載均衡器(取代了ribbon) |
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>3.1.7</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>3.1.7</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
啟動(dòng)類
@SpringBootApplication
/*開(kāi)啟服務(wù)注冊(cè)中心*/
@EnableDiscoveryClient
public class XwlGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(XwlGatewayApplication.class,args);
}
}
配置文件YML
#端口號(hào)
server:
port: 10081
#服務(wù)名
spring:
application:
name: xwl-gateway
# NACOS配置鏈接注冊(cè)中心
cloud:
nacos:
discovery:
#被發(fā)現(xiàn)的服務(wù)注冊(cè)地址
server-addr: 192.168.231.1:8848
#命名空間
namespace: f212727d-d812-4945-9464-0fd0e199459b
config:
server-addr: 192.168.231.1:8848
namespace: f212727d-d812-4945-9464-0fd0e199459b
#綁定配置文件名稱前綴(與nacos注冊(cè)中心上的配置文件名稱相關(guān))
prefix: ${spring.application.name}
#組id
group: DEFAULT_GROUP
#nacos配置文件后綴
file-extension: yml
gateway:
discovery:
locator:
enabled: true #開(kāi)啟從注冊(cè)中心動(dòng)態(tài)創(chuàng)建路由功能
#路由轉(zhuǎn)發(fā)配置
routes:
- #以xwl-Authority服務(wù)名為唯一標(biāo)識(shí)id
- id: xwl-Authority
# 內(nèi)置服務(wù)轉(zhuǎn)發(fā)url 也可以是http請(qǐng)求方式
uri: lb://xwl-Authority
# 匹配器 這里設(shè)置的是以xwlAuthority前綴匹配的url都轉(zhuǎn)發(fā)到xwl-Authority服務(wù)中
predicates:
- Path=/xwlAuthority/**
nacos啟動(dòng)
注:nacos2.0后默認(rèn)以集群方式啟動(dòng),這里我們測(cè)試方便需要用單機(jī)方式啟動(dòng),需要改個(gè)配置,如圖所示
將原來(lái)的cluster改為standalone
新建命名空間
配置網(wǎng)關(guān)yml(nacos)
注意:這里的 Data ID要和配置文件中保持一致
網(wǎng)關(guān)服務(wù)啟動(dòng)
用戶服務(wù)構(gòu)建
在父工程項(xiàng)目點(diǎn)擊右鍵 New-Module起名為xwl-Authority
pom文件
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.1.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
啟動(dòng)類
@SpringBootApplication
@EnableDiscoveryClient
public class XwlAuthorityApplication {
public static void main(String[] args) {
SpringApplication.run(XwlAuthorityApplication.class,args);
}
}
配置文件YML
server:
port: 10082
spring:
application:
name: xwl-Authority
# NACOS配置鏈接注冊(cè)中心
cloud:
nacos:
discovery:
server-addr: 192.168.231.1:8848
namespace: f212727d-d812-4945-9464-0fd0e199459b
config:
server-addr: 192.168.231.1:8848
namespace: f212727d-d812-4945-9464-0fd0e199459b
prefix: ${spring.application.name}
group: DEFAULT_GROUP
file-extension: yml
compatibility-verifier:
enabled: false
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.231.1:3306/xwlmicroservice
username: root
password: root
新增url接口
package cn.xwl.controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("testAuthority")
public class AuthorityController {
@PostMapping("/getName")
@ResponseBody
public String getName(){
return "ZHANGSAN";
}
}
配置用戶服務(wù)yml(nacos)
以xwl-Authority.yml為創(chuàng)建Data ID
用戶服務(wù)啟動(dòng)
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-471078.html
測(cè)試路由轉(zhuǎn)發(fā)
- 訪問(wèn)網(wǎng)關(guān)層
- 命中g(shù)ateway配置文件中的url匹配器,從而轉(zhuǎn)發(fā)xwl-Authority服務(wù)
- 發(fā)送http請(qǐng)求到xwl-Authority服務(wù)命中具體的url
源碼地址
碼云倉(cāng)庫(kù)地址文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-471078.html
到了這里,關(guān)于微服務(wù)之以nacos注冊(cè)中心,以gateway路由轉(zhuǎn)發(fā)服務(wù)調(diào)用實(shí)例(第一篇)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!