基于nacos搭建的父项目、spring cloud nacos provider(服务提供者)、spring cloud nacos consumer(服务消费者)结构:

1. 创建一个maven父项目。
maven父项目包含springcloudnacos-provider(服务提供者)、springcloudnacos-consumer(服务消费者)两个子模块。父模块下的src可以删除。
maven父项目的pom.xml:
- "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.0modelVersion>
- <parent>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-parentartifactId>
- <version>2.2.4.RELEASEversion>
- <relativePath/>
- parent>
- <groupId>com.examplegroupId>
- <artifactId>springcloudnacosartifactId>
- <version>0.0.1-SNAPSHOTversion>
- <name>springcloudnacosname>
- <description>Demo project for Spring Bootdescription>
- <packaging>pompackaging>
- <properties>
- <java.version>1.8java.version>
- <spring-boot-dependencies.version>2.2.4.RELEASEspring-boot-dependencies.version>
- <spring-cloud-dependencies.version>Hoxton.RELEASEspring-cloud-dependencies.version>
- <spring-cloud-alibaba.version>2.2.0.RELEASEspring-cloud-alibaba.version>
- properties>
-
- <modules>
- <module>springcloudnacos-providermodule>
- <module>springcloudnacos-consumermodule>
- modules>
-
- <dependencies>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starterartifactId>
- dependency>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- dependency>
-
- dependencies>
-
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-dependenciesartifactId>
- <version>${spring-cloud-dependencies.version}version>
- <type>pomtype>
- <scope>importscope>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-dependenciesartifactId>
- <version>${spring-boot-dependencies.version}version>
- <type>pomtype>
- <scope>importscope>
- dependency>
- <dependency>
- <groupId>com.alibaba.cloudgroupId>
- <artifactId>spring-cloud-alibaba-dependenciesartifactId>
- <version>${spring-cloud-alibaba.version}version>
- <type>pomtype>
- <scope>importscope>
- dependency>
- dependencies>
- dependencyManagement>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-maven-pluginartifactId>
- plugin>
-
- <plugin>
- <groupId>org.apache.maven.pluginsgroupId>
- <artifactId>maven-surefire-pluginartifactId>
- <configuration>
- <testFailureIgnore>truetestFailureIgnore>
- configuration>
- plugin>
- plugins>
- build>
-
- project>
2. 服务提供者:springcloudnacos-provider
(1)spring cloud nacos provider项目的pom.xml
- "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.0modelVersion>
- <parent>
- <groupId>com.examplegroupId>
- <artifactId>springcloudnacosartifactId>
- <version>0.0.1-SNAPSHOTversion>
- parent>
-
- <groupId>com.examplegroupId>
- <artifactId>springcloudnacos-providerartifactId>
- <version>0.0.1-SNAPSHOTversion>
- <name>springcloudnacos-providername>
- <description>Demo project for Spring Bootdescription>
- <properties>
- <java.version>1.8java.version>
- properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starterartifactId>
- dependency>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- 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.apache.maven.pluginsgroupId>
- <artifactId>maven-surefire-pluginartifactId>
- <configuration>
- <testFailureIgnore>truetestFailureIgnore>
- configuration>
- plugin>
- plugins>
- build>
-
-
-
- project>
(2)spring cloud nacos provider服务的启动类
- package com.example.springcloudnacosprovider;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
-
- @SpringBootApplication
- @EnableDiscoveryClient
- public class SpringcloudnacosProviderApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(SpringcloudnacosProviderApplication.class, args);
- }
-
- }
(3)spring cloud nacos provider服务的yml配置
- spring:
- cloud:
- nacos:
- discovery:
- server-addr: 127.0.0.1:8848
- application:
- name: nacos-provider
- server:
- port: 8090
(4)spring cloud nacos provider服务的测试类
- package com.example.springcloudnacosprovider.controller;
-
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- @RestController
- public class NacosProviderController {
-
- @RequestMapping("/testNacosProvider")
- public String testNacosProvider(){
- return "this is my nacos provider!";
- }
-
-
- }
(5)spring cloud nacos provider服务测试

(6)spring cloud nacos provider服务在nacos上的注册(nacos服务注册中心)

3. 服务消费者:springcloudnacos-consumer
(1)spring cloud nacos consumer服务的pom.xml
- "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.0modelVersion>
- <parent>
- <groupId>com.examplegroupId>
- <artifactId>springcloudnacosartifactId>
- <version>0.0.1-SNAPSHOTversion>
- parent>
-
- <groupId>com.examplegroupId>
- <artifactId>springcloudnacos-consumerartifactId>
- <version>0.0.1-SNAPSHOTversion>
- <name>springcloudnacos-consumername>
- <description>Demo project for Spring Bootdescription>
- <properties>
- <java.version>1.8java.version>
- properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starterartifactId>
- dependency>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- 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>
-
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-loadbalancerartifactId>
- dependency>
-
- dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.pluginsgroupId>
- <artifactId>maven-surefire-pluginartifactId>
- <configuration>
- <testFailureIgnore>truetestFailureIgnore>
- configuration>
- plugin>
- plugins>
- build>
-
- project>
(2)spring cloud nacos consumer服务的启动类
- package com.example.springcloudnacosconsumer;
-
- 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;
-
- @SpringBootApplication
- @EnableDiscoveryClient
- public class SpringcloudnacosConsumerApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(SpringcloudnacosConsumerApplication.class, args);
- }
-
- @Bean
- @LoadBalanced
- public RestTemplate restTemplate(){
- return new RestTemplate();
- }
-
- }
(3)spring cloud nacos consumer服务的yml配置
- spring:
- cloud:
- nacos:
- discovery:
- server-addr: 127.0.0.1:8848
- application:
- name: nacos-consumer
- server:
- port: 8091
-
(4)spring cloud nacos consumer服务的测试类
- package com.example.springcloudnacosconsumer.controller;
-
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import org.springframework.web.client.RestTemplate;
-
- @RestController
- public class NacosConsumerController {
-
- private RestTemplate restTemplate;
-
- @Autowired
- public NacosConsumerController(RestTemplate restTemplate){
-
- this.restTemplate = restTemplate;
-
- }
-
- @RequestMapping("/testNacosConsumer")
- public String testNacosConsumer(){
-
- return restTemplate.getForObject("http://nacos-provider/testNacosProvider",String.class);
-
- }
-
- }
(5)spring cloud nacos consumer服务测试

(6)spring cloud nacos consumer服务在nacos服务器上的注册(nacos服务注册中心)
