简介:Nacos 致力于帮助您发现、配置和管理微服务。
在SpringCloud中,可以替换eureka、config
Nacos Config
一、安装启动
1、下载安装
https://github.com/alibaba/nacos/releases
2、解压
3、启动
cd到bin目录,执行:
sh startup.sh -m standalone
见到如上界面就表示启动成功
4、登陆管理界面
http://localhost:8848/nacos/index.html
二、集成SpringCloud
1、建立一个springcloud项目,依赖如下
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>${latest.version}</version>
</dependency>
2、建一个Controller,如下
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/config")
@RefreshScope
public class ConfigController {
@Value("${useLocalCache:false}")
private boolean useLocalCache;
@RequestMapping("/get")
public boolean get() {
return useLocalCache;
}
}
3、配置文件新建一个bootstrap.properties
pring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.application.name=nacos-config
spring.cloud.nacos.config.file-extension=properties
4、用命令行发一个post请求,当然其他工具也行
curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos-config.properties&group=DEFAULT_GROUP&content=useLocalCache=true"
注意:dataId的nacos-config必须和spring.application.name对应上
5、观察是否配置文件创建成功
6、启动项目
7、调用接口
http://localhost:8080/config/get
观察结果:
9、修改配置文件,然后再次调用接口观察结果
修改配置文件:把useLocalCache这个唯一的配置字段改成false
curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos-config.properties&group=DEFAULT_GROUP&content=useLocalCache=false"
观察结果:
此时返回内容为false,说明程序中的useLocalCache值已经被动态更新了
Nacos Discovery
和eureka一样:
一、Nacos server
参见上节的下载启动,保证consumer和provider启动前,Nacos Server是正常启动状态
二、Provider
1、新建一个springboot模块
2、添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>${latest.version}</version>
</dependency>
这里的version取的2.2.5.RELEASE
2021-05-22 具体版本对应关系是:
所有博主的springboot version是:2.3.2.RELEASE
3、配置文件application.properties
server.port=8070
spring.application.name=service-provider
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
通常来说,不管是eureka还是nacos,都会配置服务发现与注册服务器的地址和本身服务名
4、提供一个简单的controller
@RestController
public class DiscoveryController {
@RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
public String echo(@PathVariable String string) {
return "Hello Nacos Discovery " + string;
}
}
5、启动类加个注解@EnableDiscoveryClient
@SpringBootApplication
@EnableDiscoveryClient
public class NacosDiscoveryApplication {
public static void main(String[] args) {
SpringApplication.run(NacosDiscoveryApplication.class, args);
}
}
6、启动服务,注册到Nacos Server
三、Consumer
1、新建一个springboot模块
2、添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>${latest.version}</version>
</dependency>
版本和provider一致
3、配置文件application.properties
server.port=8080
spring.application.name=service-consumer
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
4、写一个接口调用提供者
@RestController
public class DiscoveryController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
public String echo(@PathVariable String str) {
return restTemplate.getForObject("http://service-provider/echo/" + str, String.class);
}
}
5、启动类加个注解,并且配置RestTemplate类,当然配置到其他配置类里也可以
@SpringBootApplication
@EnableDiscoveryClient
public class NacosDiscoveryApplication {
public static void main(String[] args) {
SpringApplication.run(NacosDiscoveryApplication.class, args);
}
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
6、启动消费者
7、浏览器调用消费者接口,可以看到提供者的接口内容
最后放上博主采坑整理的git地址