最近由于Netfix公司停止维护SpringCloud中相关的组件,造成了一些讨论,但是此时正好阿里发布了阿里维护的一些列SpringCloud的组件,比如说我本篇要讲的Nacos,此处先提供nacos中文官网的地址
可以使用Maven或者手动下载安装启动的方式。此处不做过多赘述,详细请看
https://nacos.io/zh-cn/docs/quick-start.html
首先我们先构建一个文件夹,并新建一个统一依赖的项目。
项目的pom文件
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.0.6.RELEASE
com.funtl
hello-spring-cloud-alibaba-dependencies
1.0.0-SNAPSHOT
pom
hello-spring-cloud-alibaba-dependencies
1.8
UTF-8
UTF-8
Finchley.SR2
0.2.1.RELEASE
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.cloud
spring-cloud-alibaba-dependencies
${spring-cloud-alibaba.version}
pom
import
org.apache.maven.plugins
maven-compiler-plugin
true
org.apache.maven.plugins
maven-jar-plugin
false
true
true
true
org.apache.maven.plugins
maven-resources-plugin
org.apache.maven.plugins
maven-install-plugin
org.apache.maven.plugins
maven-clean-plugin
org.apache.maven.plugins
maven-antrun-plugin
org.apache.maven.plugins
maven-dependency-plugin
org.apache.maven.plugins
maven-javadoc-plugin
prepare-package
jar
net.alchim31.maven
yuicompressor-maven-plugin
1.5.1
prepare-package
compress
UTF-8
false
true
30000
true
**/*.js
**/*.css
**/*.min.js
**/*.min.css
src/main/java
**/*.java
src/main/resources
aliyun-repos
Aliyun Repository
http://maven.aliyun.com/nexus/content/groups/public
true
false
sonatype-repos
Sonatype Repository
https://oss.sonatype.org/content/groups/public
true
false
sonatype-repos-s
Sonatype Repository
https://oss.sonatype.org/content/repositories/snapshots
false
true
spring-snapshots
Spring Snapshots
https://repo.spring.io/snapshot
true
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
aliyun-repos
Aliyun Repository
http://maven.aliyun.com/nexus/content/groups/public
true
false
然后就构建一个服务提供者的项目。项目的pom如下
4.0.0
com.funtl
hello-spring-cloud-alibaba-dependencies
1.0.0-SNAPSHOT
../hello-spring-cloud-alibaba-dependencies/pom.xml
hello-spring-cloud-alibaba-nacos-provider
jar
hello-spring-cloud-alibaba-nacos-provider
http://www.funtl.com
2018-Now
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-starter-alibaba-nacos-discovery
org.springframework.boot
spring-boot-maven-plugin
com.funtl.hello.spring.cloud.alibaba.nacos.provider.NacosProviderApplication
紧接着我们在application.yml中配置相关信息
spring:
application:
name: nacos-provider
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
server:
port: 8081
management:
endpoints:
web:
exposure:
include: "*"
添加与原生SpringCloud一样的服务提供者注解
package com.funtl.hello.spring.cloud.alibaba.nacos.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableDiscoveryClient
public class NacosProviderApplication {
public static void main(String[] args) {
SpringApplication.run(NacosProviderApplication.class, args);
}
@RestController
public class EchoController {
@GetMapping(value = "/echo/{message}")
public String echo(@PathVariable String message) {
return "Hello Nacos Discovery " + message;
}
}
}
启动服务提供者项目之前启动nacos
观察到服务提供者已经被注册到nacos
使用http://localhost:8848/nacos登录nacos项目的后台。使用用户名/密码:nacos/nacos登录。在服务列表中可以看到我们注册的服务