pom文件
<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.0modelVersion>
<packaging>pompackaging>
<modules>
<module>nacos-providermodule>
<module>nacos-consumermodule>
<module>gatewaymodule>
modules>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-autoconfigureartifactId>
dependency>
dependencies>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.3.2.RELEASEversion>
<relativePath/>
parent>
<groupId>com._520xuzaigroupId>
<artifactId>xuzai-parentartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>xuzai-parentname>
<description>Demo project for Spring Bootdescription>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
<java.version>1.8java.version>
<spring-cloud.version>Hoxton.SR8spring-cloud.version>
<spring-cloud-alibaba.version>2.2.4.RELEASEspring-cloud-alibaba.version>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${spring-cloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
<version>${spring-cloud-alibaba.version}version>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
<version>${spring-cloud-alibaba.version}version>
dependency>
dependencies>
dependencyManagement>
project>
<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-parentartifactId>
<groupId>com._520xuzaigroupId>
<version>0.0.1-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>nacos-providerartifactId>
<groupId>com._520xuzai.nacosprovodergroupId>
<version>0.0.1-SNAPSHOTversion>
<packaging>jarpackaging>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-contextartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-commonsartifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
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);
}
}
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";
}
}
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
<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-parentartifactId>
<groupId>com._520xuzaigroupId>
<version>0.0.1-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>nacos-consumerartifactId>
<groupId>com._520xuzai.nacosconsumergroupId>
<version>0.0.1-SNAPSHOTversion>
<packaging>jarpackaging>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-contextartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-commonsartifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
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();
}
}
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);
}
}
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
<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-parentartifactId>
<groupId>com._520xuzaigroupId>
<version>0.0.1-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>gatewayartifactId>
<groupId>com._520xuzai.gatewaygroupId>
<version>0.0.1-SNAPSHOTversion>
<packaging>jarpackaging>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-gatewayartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
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);
}
}
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
为了更好理解配置中心和注册中心不是一回事,我们设置配置中心地址为192.168.76.134:8848
注册中心为192.168.76.133:8848
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
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
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/**
在注册中心可以看到如下内容
调用相关接口,即可转发到对应的服务上