目录结构如下所示:
getway调用application是http协议,而application1调用服务和服务内部调用则是dubbo协议,dubbo协议比http协议快,service-11-api里面写接口,service-22-service里面写实现。application的一个简单服务调用效果如下:
最外层父包nacosservice的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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>nacosService</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>nacos-micro-service</module>
</modules>
<parent>
<artifactId>spring-boot-dependencies</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.3.2.RELEASE</version>
<relativePath />
</parent>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<alibaba-cloud.version>2.2.5.RELEASE</alibaba-cloud.version>
<springcloud.version>Hoxton.SR8</springcloud.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${springcloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${alibaba-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
nocos-micro-service的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>
<artifactId>nacosService</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>nacos-micro-service</artifactId>
<packaging>pom</packaging>
<modules>
<module>application-1</module>
<module>service11</module>
<module>service22</module>
<module>api-getway</module>
</modules>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
api-getway的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>
<artifactId>nacos-micro-service</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>api-getway</artifactId>
<dependencies>
<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-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
application-1的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>
<artifactId>nacos-micro-service</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>application-1</artifactId>
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>service-11-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>service-22-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.0.6.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
service-11-service的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>
<artifactId>service11</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>service-11-service</artifactId>
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>service-11-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<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-config</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
service-22-service
<?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>service22</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>service-22-service</artifactId>
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>service-22-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<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-config</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
apgetway的bootstrap.yml
server:
port: 1000
spring:
application:
name: api-getway
main:
allow-bean-definition-overriding: true #springboot 2.1需要设定
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
namespace: 6cec7a10-3a34-4bfb-9753-0e9e5b318195
cluster-name: DEFAULT
config:
server-addr: 127.0.0.1:8848
namespace: 6cec7a10-3a34-4bfb-9753-0e9e5b318195
file-extension: yaml
group: NACOS_DEFAULT_GROUP
application1的配置文件bootstrap.yml
server:
port: 5600 #启动端口,命令行注入
servlet:
context-path: /application
spring:
application:
name: application
main:
allow-bean-definition-overriding: true #springboot 2.1需要设定
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
namespace: 6cec7a10-3a34-4bfb-9753-0e9e5b318195
cluster-name: DEFAULT
config:
server-addr: 127.0.0.1:8848
namespace: 6cec7a10-3a34-4bfb-9753-0e9e5b318195
file-extension: yaml
group: NACOS_DEFAULT_GROUP
service-11-service的bootstrap.yml
server:
port: 5601 #启动端口,命令行注入
spring:
application:
name: service11
main:
allow-bean-definition-overriding: true #springboot 2.1需要设定
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
namespace: 6cec7a10-3a34-4bfb-9753-0e9e5b318195
cluster-name: DEFAULT
config:
server-addr: 127.0.0.1:8848
namespace: 6cec7a10-3a34-4bfb-9753-0e9e5b318195 #开发环境
file-extension: yaml
group: NACOS_DEFAULT_GROUP
dubbo:
scan:
#dubbo 服务扫面的包
base-packages: com.edu.stu
protocol:
#dubbo协议
name: dubbo
#dubbo协议端口
port: ${dubbo_port:2000}
registry:
address: nacos://127.0.0.1:8848
application:
qos-enable: false #dubbo运维服务是否开启
consumer:
check: false #启动时是否检查依赖的服务
service-22-service的bootstrap.yml
server:
port: 5602 #启动端口,命令行注入
spring:
application:
name: service22
main:
allow-bean-definition-overriding: true #springboot 2.1需要设定
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
namespace: 6cec7a10-3a34-4bfb-9753-0e9e5b318195
cluster-name: DEFAULT
config:
server-addr: 127.0.0.1:8848
namespace: 6cec7a10-3a34-4bfb-9753-0e9e5b318195 #开发环境
file-extension: yaml
group: NACOS_DEFAULT_GROUP
dubbo:
scan:
#dubbo 服务扫面的包
base-packages: com.edu.stu
protocol:
#dubbo协议
name: dubbo
#dubbo协议端口
port: ${dubbo_port:2001}
registry:
address: nacos://127.0.0.1:8848
application:
qos-enable: false #dubbo运维服务是否开启
consumer:
check: false #启动时是否检查依赖的服务