Nacos【Dynamic Naming and Configuration Service】提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。
下载地址:Releases · alibaba/nacos · GitHub
在window环境学习我下载了zip包
复制cluster.conf.example 去掉后缀【.example】生成配置文件
启动成功
在浏览器上访问
账号密码默认为:nacos
Nacos环境搭建完成
2.0.4.RELEASE
Finchley.RELEASE
0.2.2.RELEASE
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-dependencies
${spring-boot.version}
pom
import
org.springframework.cloud
spring-cloud-alibaba-dependencies
${nacos.version}
pom
import
org.springframework.boot
spring-boot-starter
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
2.2.5.RELEASE
为了springboot版本和springcloud版本兼容,这里我修改了
org.springframework.boot
spring-boot-starter-parent
2.3.9.RELEASE
@EnableDiscoveryClient
server.port=8087
spring.application.name=provider
spring.cloud.nacos.discovery.server-addr=192.168.110.179:8848
@RestController
public class ProviderController {
@GetMapping("/helloProvider")
public String helloProvider(){
return "你好,我是提供服务者";
}
}
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
2.2.5.RELEASE
com.netflix.archaius
archaius-core
0.7.6
com.google.guava
guava
org.springframework.cloud
spring-cloud-starter-openfeign
2.1.1.RELEASE
为了springboot版本和springcloud版本兼容,这里我修改了
org.springframework.boot
spring-boot-starter-parent
2.3.9.RELEASE
@EnableDiscoveryClient
@EnableFeignClients
server.port=8099
spring.application.name=consumer
spring.cloud.nacos.discovery.server-addr=192.168.110.179:8848
@FeignClient(name = "provider", fallback = ConsumerServiceImpl.class)
@Service
public interface ConsumerService {
@GetMapping("/helloProvider")
String getHello();
}
@Component
public class ConsumerServiceImpl implements ConsumerService {
@Override
public String getHello() {
return "服务出错";
}
}
@RestController
public class ConsumerController {
@Resource
private ConsumerService consumerService;
@GetMapping("/getHello")
public String getHello(){
return consumerService.getHello();
}
}
把创建的提供者服务与消费者服务运行起来,再回到nacos服务管理后台页面看
我们会看到有两个服务在nacos服务上注册成功了
我们创建一个网页访问消费者服务的接口
我们会发现成功调用了提供者服务
在这里我把消费者实现配置中心功能
在nacos服务后台中的配置列表中创建一个配置文件相当于application.properties或者yaml等配置
这里我使用的是application.properties并配置了
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-config
2.2.1.RELEASE
com.alibaba.nacos
nacos-client
1.2.0
org.apache.commons
commons-lang3
3.7
@NacosPropertySource(dataId = "test-dev.properties", autoRefreshed = true)
spring.cloud.nacos.config.server-addr=192.168.110.179:8848
spring.profiles.active=dev
spring.cloud.nacos.config.file-extension=properties
spring.cloud.nacos.config.group=DEFAULT_GROUP
@RestController
@RefreshScope
@Validated
public class ConsumerController {
@NacosValue(value = "${datasource.url:123}", autoRefreshed = true)
private String url;
@NacosValue(value = "${datasource.username:123}", autoRefreshed = true)
private String username;
@NacosValue(value = "${datasource.password:123}", autoRefreshed = true)
private String password;
@Autowired
private RestTemplate restTemplate;
@Resource
private ConsumerService consumerService;
@GetMapping("/getHello")
public String getHello(){
System.out.println(url);
System.out.println(username);
System.out.println(password);
return consumerService.getHello();
}
}
服务输出为
服务输出为
项目源码
链接:https://pan.baidu.com/s/12yC3cjK12_izzFO9imcPjQ
提取码:k5sj