转载请注明作者及出处:
作者:银河架构师
原文链接:https://www.cnblogs.com/luas/p/12073156.html
1 创建父项目
以前文所述,以spring boot 2.1.7.RELEASE为基,spring cloud版本为Greenwich.SR2,spring cloud alibaba版本为2.1.0.RELEASE。后续均以此为版本参照,不再赘述。
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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.luas.cloud</groupId> <artifactId>java-boot-parent-2.1</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>pom</packaging> <name>java-boot-parent-2.1</name> <description>Spring Cloud Learning, parent project by spring boot 2.1.x</description> <properties> <java.version>1.8</java.version> <hutool.version>4.6.13</hutool.version> <spring-cloud.version>Greenwich.SR2</spring-cloud.version> <spring-cloud-alibaba.version>2.1.0.RELEASE</spring-cloud-alibaba.version> </properties> <dependencies> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>${hutool.version}</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${spring-cloud-alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project>
2 创建商品工程
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> <parent> <groupId>com.luas.cloud</groupId> <artifactId>java-boot-parent-2.1</artifactId> <version>1.0.0-SNAPSHOT</version> <relativePath>../../java-boot-parent-2.1</relativePath> </parent> <groupId>com.luas.cloud</groupId> <artifactId>xmall-product</artifactId> <version>1.0.0-SNAPSHOT</version> <name>xmall-product</name> <description>Spring Cloud Learning,nacos-client</description> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- nacos cloud --> <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-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
注意,父工程便是上一步创建的父项目,特别注意一下relativePath,不如特殊指定,需要将父工程deploy到maven私服,子工程才能正常引用。
application配置
注意,spring.application.name将作为xmall-product服务在nacos注册后的服务名称,该服务名称非常重要,后续无论是通过feign调用,还是rest调用,均需要此名称。
商品服务
启动类
关于@EnableDiscoveryClient注解,此处并没有添加,而服务依然能正常注册到nacos。什么原因呢?看如下两段代码即可明了:
此3处重要逻辑,均不依赖于是否配置了开启参数,所以会自动注册并监听。
Nacos服务列表
打开nacos控制台,发现访问已然在列表中,注册成功!
可以点击详情查看服务元数据
访问
访问http://localhost:8080/sku/1000000,得出现如下结果:
源码
欢迎点赞
- github:https://gitee.com/xbd521/SpringCloudLearning
- gitee: https://github.com/liuminglei/SpringCloudLearning
微信搜索【银河架构师】,发现更多精彩内容。
技术资料领取方法:关注公众号,回复微服务,领取微服务相关电子书;回复MK精讲,领取MK精讲系列电子书;回复JAVA 进阶,领取JAVA进阶知识相关电子书;回复JAVA面试,领取JAVA面试相关电子书,回复JAVA WEB领取JAVA WEB相关电子书。