nacos主要就是配置管理与服务发现,如果你不是很明白这两个概念的情况下建议你可以先去了解一下配置管理与服务发现的基本原理。本文基于理解这些概念的情况下来书写的
在nacos官网下载nacos下载 nacos-server-$version.zip 包(当启动nacos的时候账户名和密码都是nacos)
Linux/Unix/Mac
启动命令(standalone代表着单机模式运行,非集群模式):
sh startup.s h -m standalone
Windows
启动命令:
cmd startup.cmd
或者双击startup.cmd运行文件。
1.这里我选择一个简单的创建springboot项目的方法https://start.spring.io。直接在里面创建好项目然后下载到本地,然后在本地解压。
2.打开idea然后打开该项目。
3.然后打开pox文件添加相应的依赖
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
com.alibaba.boot
nacos-config-spring-boot-starter
0.1.1
com.alibaba.boot
nacos-discovery-spring-boot-starter
0.2.1
com.alibaba.nacos
nacos-client
1.0.0
org.springframework.boot
spring-boot-maven-plugin
4.在resources文件夹下创建一个application.properties文件,在里面添加访问路径、服务名、服务地址等。
server.port= 8080
#访问的根路径
server.servlet.context-path=/springboot-nacos
#Nacos服务名
spring.application.name=UserService
#nacos服务端的地址
nacos.config.server-addr=127.0.0.1:8848
#discovery地址
nacos.discovery.server-addr=127.0.0.1:8848
这里的服务地址就是当你启动nacos的时候你自己的nacos地址。
1.创建一个实体类
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
import org.springframework.context.annotation.Configuration;
@Configuration
//加载 dataId 为 example 的配置源,并开启自动更新
@NacosPropertySource(dataId = "example",autoRefreshed = true)
public class AppConfig {
@NacosValue(value = "${username:null}", autoRefreshed = true)
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
2.配置管理
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;
@Controller
@RequestMapping("/config")
public class ConfigController {
@Autowired
AppConfig appConfig;
@RequestMapping(value = "/get")
@ResponseBody
public Object test(){
Mapmap =new HashMap<>();
map.put("username",appConfig.getUsername());
return map;
}
}
3.服务发现
import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
@Controller
@RequestMapping("/discover")
public class DiscoverController {
@NacosInjected
NamingService namingService;
@RequestMapping(value = "/get", method = GET)
@ResponseBody
public List get(@RequestParam String serviceName) throws NacosException {
return namingService.getAllInstances(serviceName);
}
}
4.application
import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import javax.annotation.PostConstruct;
@SpringBootApplication
public class SpringbootNacosApplication {
@NacosInjected
private NamingService namingService;
@Value("${server.port}")
private int serverPort;
@Value("${spring.application.name}")
private String applicationName;
//curl -X PUT 'http://127.0.0.1:8848/nacos/v1/ns/instance?serviceName=xxx&ip=127.0.0.1&port=8080'
@PostConstruct
public void registerInstance() throws NacosException {
namingService.registerInstance(applicationName,"127.0.0.1",serverPort);
// if(CollectionUtils.isEmpty(namingService.getAllInstances(applicationName))){
// namingService.registerInstance(applicationName,"127.0.0.1",serverPort);
// }
}
public static void main(String[] args) {
SpringApplication.run(SpringbootNacosApplication.class, args);
}
}
最后就可以试一试你自己的项目啦!!!!!!!!!!!!