微服务现在越来火,有基于 Spring Cloud Netflix 体系的,也有基于 Spring Cloud Alibaba 为体系的。从以前的 Eureka 注册中心、Spring Cloud Config 配置中心、Spring Cloud Bus消息总线 到完全可以替代他们的 Nacos 出现,微服务技术体系的未来发展方向愈加清晰。所以,学会并了解如何使用 Nacos 是十分重要的。
Nacos 不仅仅可以作为配置中心使用,还可以作为注册中心使用,其有很多十分优秀的特性,部署起来也十分方便。
主要目的:
单机版本
# 拉取镜像
docker pull nacos/nacos-server
# 运行
docker run --env MODE=standalone --name nacos -d -p 8848:8848 nacos/nacos-server
访问
http://127.0.0.1:8848/nacos/#/login
密码和账号默认都是 nacos
配置命名空间
依赖配置
compile libs["spring-cloud-starter-alibaba-nacos-config"]
compile libs["spring-cloud-starter-alibaba-nacos-discovery"]
resources 下新建文件
bootstrap.yaml
# nacos 配置 (nacos config必须配置在bootstrap)
spring:
cloud:
nacos:
discovery:
server-addr: ${
NACOS_SERVER_ADDR:127.0.0.1:8848}
namespace: ${
NACOS_NAMESPACE:dsb-cloud}
metadata:
{
"checkSum": "${random.value}-${random.uuid}"}
config:
server-addr: ${
NACOS_SERVER_ADDR:127.0.0.1:8848}
namespace: ${
NACOS_NAMESPACE:dsb-cloud}
file-extension: yaml
服务注册中心
// 启动类开启注解
@EnableDiscoveryClient
作为 Spring Boot 项目启动后,可以看到成功注册到 Nacos 上了。
点击详情后可以看到对应的服务信息
pom
dependencies {
compile libs["spring-boot-starter-actuator"]
//nacos
compile(libs["nacos-discovery-spring-boot-starter"]) {
exclude group: 'com.alibaba.spring', module: 'spring-context-support'
}
compile 'com.alibaba.spring:spring-context-support:1.0.3'
compile project(':spring-cloud-examples:dubbo-examples:spring-cloud-dubbo-api')
compile libs['dubbo-spring-boot-starter']
testCompile "org.springframework.boot:spring-boot-starter-test"
}
application.yaml
spring:
application:
name: spring-boot-dubbo-provider
dubbo:
application:
name: spring-boot-dubbo-provider
registry: nacos://127.0.0.1:8848
protocol:
name: dubbo
port: 20880
bootstrap.yaml
# nacos 配置 (nacos config必须配置在bootstrap)
spring:
cloud:
nacos:
discovery:
server-addr: ${
NACOS_SERVER_ADDR:127.0.0.1:8848}
namespace: ${
NACOS_NAMESPACE:dsb-cloud}
metadata:
{
"checkSum": "${random.value}-${random.uuid}"}
config:
server-addr: ${
NACOS_SERVER_ADDR:127.0.0.1:8848}
namespace: ${
NACOS_NAMESPACE:dsb-cloud}
file-extension: yaml
核心类
// 启动类
@SpringBootApplication
@DubboComponentScan
public class SpringBootDubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootDubboProviderApplication.class, args);
}
}
//服务实现类
package pub.dsb.api.service.impl;
import org.apache.dubbo.config.annotation.Service;
import pub.dsb.api.service.IHelloService;
@Service
public class HelloServiceImpl implements IHelloService {
@Override
public String hi(String name) {
return "spring-cloud-dubbo-example:" + name;
}
}
启动服务提供者
dependencies {
compile libs["spring-boot-starter-web"]
compile libs["spring-boot-starter-actuator"]
//nacos
compile(libs["nacos-discovery-spring-boot-starter"]) {
exclude group: 'com.alibaba.spring', module: 'spring-context-support'
}
compile 'com.alibaba.spring:spring-context-support:1.0.3'
compile project(':spring-cloud-examples:dubbo-examples:spring-dubbo-api')
compile libs['dubbo-spring-boot-starter']
testCompile "org.springframework.boot:spring-boot-starter-test"
}
spring:
application:
name: spring-boot-dubbo-consumer
dubbo:
registry:
address: nacos://127.0.0.1:8848
@RestController
public class DubboRefController {
@Reference(check = false)
private IHelloService helloService;
@GetMapping("/hi")
public String hi(){
return helloService.hi(System.currentTimeMillis() + "");
}
}
//启动类
@SpringBootApplication
public class SpringBootDubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootDubboConsumerApplication.class, args);
}
}
服务提供者 8080, 服务消费者 8081
GET http://127.0.0.1:8081/hi
HTTP/1.1 200
Content-Type: text/plain;charset=UTF-8
Content-Length: 40
Date: Tue, 01 Sep 2020 16:55:53 GMT
spring-cloud-dubbo-example:1598979353325
ClassNotFoundException: com.alibaba.spring.util.BeanRegistrar
引用新包,剔除有问题的包 spring-context-support
。
No provider available for the service
@Reference(check = false)