Spring cloud Alibaba Nacos服务注册发现和配置中心
-
Nacos使服务易于注册自己并通过DNS或HTTP接口发现其他服务。Nacos还提供服务的实时运行状况检查,以防止向不正常的主机或服务实例发送请求。
-
动态配置管理
动态配置服务使您可以在所有环境中以集中和动态的方式管理所有服务的配置。Nacos消除了在更新配置时重新部署应用程序和服务的需求,这使配置更改更加有效和敏捷。
-
动态DNS服务
Nacos支持加权路由,使您可以更轻松地在数据中心内的生产环境中实施中间层负载平衡,灵活的路由策略,流控制和简单的DNS解析服务。它可以帮助您轻松实现基于DNS的服务发现,并防止应用程序耦合到特定于供应商的服务发现API。
-
服务和元数据管理
Nacos提供了易于使用的服务仪表板,可帮助您管理服务元数据,配置,kubernetes DNS,服务运行状况和指标统计信息。
1.下载nacos
2.打开bin目录下的startup.cmd
3.访问http://localhost:8848/nacos/
服务端:
<dependencies> <!--SpringCloud ailibaba nacos --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!-- SpringBoot整合Web组件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--日常通用jar包配置--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
server:
port: 9001
spring:
application:
name: nacos-payment-provider
cloud:
nacos:
discovery:
server-addr: localhost:8848
management:
endpoints:
web:
exposure:
include: '*'
@SpringBootApplication @Slf4j @EnableDiscoveryClient public class PaymentMain9001 { public static void main(String[] args) { SpringApplication.run( PaymentMain9001.class,args); log.info("****************PaymentMain9001 启动 ************ "); } }
@RestController @Slf4j public class PaymentController { @Value("${server.port}") private String serverPort; @GetMapping(value = "/payment/nacos/{id}") public String getPayment(@PathVariable("id") Integer id) { return "nacos registry, serverPort: " + serverPort + "\t id" + id; } }
客户端:
server:
port: 83
spring:
application:
name: nacos-order-consumer
cloud:
nacos:
discovery:
server-addr: localhost:8848
#消费者将要去访问的微服务的名称(注册成功进入nacos的微服务提供者)
service-url:
nacos-user-service: http://nacos-payment-provider
dev开发环境
test测试环境
prod生产环境
nacos 给我提供 了 dataid group namespace
<dependencies> <!--nacos-config--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <!--nacos-discovery--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!--web + actuator--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--一般基础配置--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
application.yml
spring:
profiles:
active: dev
bootstarp.xml
server:
port: 3377
spring:
application:
name: nacos-config-client
cloud:
nacos:
discovery:
server-addr: localhost:8848
config:
server-addr: localhost:8848 # nacos作为配置中心的地址
file-extension: yaml # 指定yaml格式的配置
group: DEV_GROUP
namespace: 122de11e-bb29-40e7-bc8e-06436b105ed1
# 配置规则
# ${spring.appliction.name}-${spring.profile.active}.${spring.cloud.nacos.config.file-extension}
# nacos-config-client-dev.yaml
@EnableDiscoveryClient @SpringBootApplication @Slf4j public class ConfigNacosMain3377 { public static void main(String[] args) { SpringApplication.run( ConfigNacosMain3377.class,args); log.info("****************ConfigNacosMain3377 启动 ************ "); } }
@RefreshScope @RestController @Slf4j public class ConfigController { @Value("${config.info}") private String configInfo; @GetMapping("/config/info") public String getConfigInfo() { return configInfo; } }
在nacos上更改后可实现动态刷新
namespace: