步骤
- 新建一个springboot应用。
- 修改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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.moyang.learn</groupId>
<artifactId>01-alibaba-nacos-discovery-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>01-alibaba-nacos-discovery-server</name>
<description>服务提供者1</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--加入nacos依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.2.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.修改启动文件
@SpringBootApplication
@EnableDiscoveryClient
public class DiscoveryServerApplication {
public static void main(String[] args) {
SpringApplication.run(DiscoveryServerApplication.class, args);
}
}
4.编辑application.yml
package com.moyang.learn.alibabanacosdiscoveryserver.rest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import static com.alibaba.nacos.client.config.impl.ClientWorker.log;
@RestController
@Slf4j
public class TestRest {
@GetMapping("/hello/{name}")
public String hello(@PathVariable(name = "name", required = false) String name) {
log.debug("传入的参数:{}", name);
return "hello , " + name;
}
}
5.新建一个测试的Rest接口
package com.moyang.learn.alibabanacosdiscoveryserver.rest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import static com.alibaba.nacos.client.config.impl.ClientWorker.log;
@RestController
@Slf4j
public class TestRest {
@GetMapping("/hello/{name}")
public String hello(@PathVariable(name = "name", required = false) String name) {
log.debug("传入的参数:{}", name);
return "hello , " + name;
}
}
1
6.启动程序.
查看nacos,服务管理->服务列表:
访问:127.0.0.1:8080/hello/fangxiaobai
注意:
springboot的版本:
2.1.4.RELEASE是不会扫描类结构的。2.1.4 注册不到nacos中,也不会把Bean注到spring容器中。
2.1.0版本是可以扫描类结构,但是不会注册到nacos的。
2.0.9是可以的。
结论: 2.1.0+的版本是不可以的。(纯属自己实验结果,不保证此现象一定会出现。我这里是出现了这样的现象)
也就是说使用具体原因还在查。
最后
如果你觉得写的还不错,就关注下公众号呗,关注后,有点小礼物回赠给你。
你可以获得5000+电子书,java,springCloud,adroid,python等各种视频教程,IT类经典书籍,各种软件的安装及破解教程。
希望一块学习,一块进步!