1 :右键我们的父工程建立新spring-cloud-alibaba-study-consumer的项目步骤同生产者
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.hyl.study</groupId>
<artifactId>spring-cloud-alibaba-study</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.hyl.study</groupId>
<artifactId>spring-cloud-alibaba-study-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-cloud-alibaba-study-consumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--nacos-->
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3: 修改启动类
@SpringBootApplication
@EnableDiscoveryClient //添加此标签
public class SpringCloudAlibabaStudyConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudAlibabaStudyConsumerApplication.class, args);
}
}
4:修改配置文件application.properties为application.yml并添加如下内容:
server:
port: 9530
spring:
application:
name: study-consumer
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
5:@RestController
@RequestMapping("test_consumer")
public class TestConsumerApi {
@RequestMapping("/get_test")
public String getTest(){
return "consumer测试一下";
}
}
启动项目在SpringCloudAlibabaStudyConsumerApplication
类右键运行即可,打开nacos控制台看到如下界面表示服务正确注册到服务中心。(注册中心已经 多了个服务)
本项目地址:https://gitee.com/hylgj/spring-cloud-alibaba-study.git