Nacos(三)GateWay 和 Nacos结合,使用Nacos作为配置中心和注册中心
文章目录一、工程介绍1、父模块2、nacos-provider2.1 pom文件2.2 启动类2.3 controller2.4 bootstrap文件3、nacos-consumer模块3.1 pom文件3.2 启动类3.3 controller3.4 bootstrap文件4、gateway模块4.1 pom文件4.2 启动类4.3 bootstrap文件二、 nocas注册中心和配置中心相关
·
一、工程介绍
本次采用的是一个父模块,三个子模块(网关,nacosprovider,nacosconsumer)的方式来构建的。1、父模块
pom文件
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging><!--父模块打包类型必须为pom-->
<modules>
<module>nacos-provider</module>
<module>nacos-consumer</module>
<module>gateway</module>
</modules>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
</dependencies>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com._520xuzai</groupId>
<artifactId>xuzai-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>xuzai-parent</name>
<description>Demo project for Spring Boot</description>
<properties>
<!-- 在properties中统一控制依赖包的版本,更清晰-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR8</spring-cloud.version>
<spring-cloud-alibaba.version>2.2.4.RELEASE</spring-cloud-alibaba.version>
</properties>
<dependencyManagement> <!--dependencyManagement用于管理依赖版本号-->
<dependencies>
<!-- 删除spring-boot-starter和spring-boot-starter-test,
因为parent中继承的祖先中已经有了,并且一般dependencyManagement管理的依赖都要写版本号 -->
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-test</artifactId>-->
<!-- <scope>test</scope>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--nacos 客户端 注册中心-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>${spring-cloud-alibaba.version}</version>
</dependency>
<!--nacos 客户端 配置中心-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>${spring-cloud-alibaba.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<!--该插件作用是打一个可运行的包,必须要写在需要打包的项目里。这里的父模块不需要打包运行,所以删掉该插件。-->
<!-- <build>-->
<!-- <plugins>-->
<!-- <plugin>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-maven-plugin</artifactId>-->
<!-- </plugin>-->
<!-- </plugins>-->
<!-- </build>-->
</project>
2、nacos-provider
2.1 pom文件
<?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>
<artifactId>xuzai-parent</artifactId>
<groupId>com._520xuzai</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>nacos-provider</artifactId>
<groupId>com._520xuzai.nacosprovoder</groupId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--nacos 客户端 注册中心-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--nacos 客户端 配置中心-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.2 启动类
package com._520xuzai.nacosprovider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* \* Created with IntelliJ IDEA.
* \* User: 思源
* \* Date: 2021/1/30
* \* Time: 22:59
* \* To change this template use File | Settings | File Templates.
* \* Description:
* \
*/
@SpringBootApplication
@EnableDiscoveryClient
public class NacosProviderApplication {
public static void main(String[] args) {
SpringApplication.run(NacosProviderApplication.class, args);
}
}
2.3 controller
package com._520xuzai.nacosprovider.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* \* Created with IntelliJ IDEA.
* \* User: 思源
* \* Date: 2021/1/31
* \* Time: 9:02
* \* To change this template use File | Settings | File Templates.
* \* Description:
* \
*/
@RefreshScope
@Slf4j
@RestController
@RequestMapping("/provider")
public class ProviderTestController {
@RequestMapping("/get")
public String get() {
return "you get me -- provider";
}
}
2.4 bootstrap文件
spring:
application:
name: nacosprovider
cloud:
nacos:
config:
file-extension: yaml
server-addr: 192.168.76.134:8848 #这里是配置中心的地址
group: DEFAULT_GROUP
username: app
password: app
#启动环境
profiles:
active: dev
3、nacos-consumer模块
3.1 pom文件
<?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>
<artifactId>xuzai-parent</artifactId>
<groupId>com._520xuzai</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>nacos-consumer</artifactId>
<groupId>com._520xuzai.nacosconsumer</groupId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--nacos 客户端 注册中心-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--nacos 客户端 配置中心-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.2 启动类
package com._520xuzai.nacosconsumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* \* Created with IntelliJ IDEA.
* \* User: 思源
* \* Date: 2021/1/31
* \* Time: 9:01
* \* To change this template use File | Settings | File Templates.
* \* Description:
* \
*/
@SpringBootApplication
@EnableDiscoveryClient
public class NacosConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(NacosConsumerApplication.class, args);
}
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
3.3 controller
package com._520xuzai.nacosconsumer.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* \* Created with IntelliJ IDEA.
* \* User: 思源
* \* Date: 2021/1/31
* \* Time: 9:02
* \* To change this template use File | Settings | File Templates.
* \* Description:
* \
*/
@RefreshScope
@Slf4j
@RestController
@RequestMapping("/consumer")
public class ConsumerTestController {
@Autowired
private RestTemplate restTemplate;
@Value("${useLocalCache:false}")
private boolean useLocalCache;
@RequestMapping("/get")
public boolean get() {
return useLocalCache;
}
@RequestMapping("/getProvider")
public String getProvider() {
return restTemplate.getForObject("http://nacosprovider/provider/get",String.class);
}
}
3.4 bootstrap文件
spring:
application:
name: nacosconsumer
cloud:
nacos:
config:
file-extension: yaml
server-addr: 192.168.76.134:8848 #这里是配置中心的地址
group: DEFAULT_GROUP
username: app
password: app
#启动环境
profiles:
active: dev
4、gateway模块
4.1 pom文件
<?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>
<artifactId>xuzai-parent</artifactId>
<groupId>com._520xuzai</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>gateway</artifactId>
<groupId>com._520xuzai.gateway</groupId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!--nacos 客户端 注册中心-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--nacos 客户端 配置中心-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4.2 启动类
package com._520xuzai.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* \* Created with IntelliJ IDEA.
* \* User: 思源
* \* Date: 2021/1/31
* \* Time: 9:04
* \* To change this template use File | Settings | File Templates.
* \* Description:
* \
*/
@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
4.3 bootstrap文件
spring:
application:
name: gateway
cloud:
nacos:
config:
file-extension: yaml
server-addr: 192.168.76.134:8848 #这里是配置中心的地址
group: DEFAULT_GROUP
username: app
password: app
#启动环境
profiles:
active: dev
二、 nocas注册中心和配置中心相关配置
为了更好理解配置中心和注册中心不是一回事,我们设置配置中心地址为192.168.76.134:8848
注册中心为192.168.76.133:8848
1、在注册中心建立不同环境的命名空间
这里我们使用dev环境来测试
2、在配置中心编写相关配置
2.1 nacos-provider相关配置
server:
port: 9200
spring:
#nacos 服务发现
cloud:
nacos:
discovery:
server-addr: 192.168.76.133:8848 #这里是注册中心的地址
service: ${spring.application.name}
namespace: 7dfd49ba-c17d-470b-b082-34dfb9d586ff #这里是注册中心的dev环境的命名空间
username: app
password: app
2.2 nacos-consumer相关配置
server:
port: 9300
spring:
#nacos 服务发现
cloud:
nacos:
discovery:
server-addr: 192.168.76.133:8848 #这里是注册中心的地址
service: ${spring.application.name}
namespace: 7dfd49ba-c17d-470b-b082-34dfb9d586ff
username: app
password: app
2.3 gateway相关配置文件
server:
port: 9400
spring:
#nacos 服务发现
cloud:
nacos:
discovery:
server-addr: 192.168.76.133:8848 #这里是注册中心的地址
service: ${spring.application.name}
namespace: 7dfd49ba-c17d-470b-b082-34dfb9d586ff
username: app
password: app
gateway:
discovery:
locator:
enabled: true #表明gateway开启服务注册和发现的功能,并且spring cloud gateway自动根据服务发现为每一个服务创建了一个router,这个router将以服务名开头的请求路径转发到对应的服务。
lowerCaseServiceId: true #是将请求路径上的服务名配置为小写(因为服务注册的时候,向注册中心注册时将服务名转成大写的了),比如以/service-hi/*的请求路径被路由转发到服务名为service-hi的服务上。
routes:
- id: nacosconsumer-server
uri: lb://nacosconsumer #负载到nacosconsumer
predicates:
- Path=/consumer/**
- id: nacosprovider-server
uri: lb://nacosprovider #负载到nacosprovider
predicates:
- Path=/provider/**
三、启动项目
1、启动三个模块
在注册中心可以看到如下内容
2、接口访问
调用相关接口,即可转发到对应的服务上
已为社区贡献1条内容
所有评论(0)