springcloud alibaba(1)https://developer.aliyun.com/article/1530573
基于Nacos的服务消费者
- 新建模块cloudalibaba-consumer-nacos-order83
- pom(nacos集成了ribbon,实现负载均衡)
<dependencies> <!--SpringCloud Alibaba nacos--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!-- 引用自己定义的api通用包,可以使用Payment支付Entity --> <dependency> <groupId>org.example</groupId> <artifactId>cloud-api-commons</artifactId> <version>${project.version}</version> </dependency> <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>
server: port: 83 spring: application: name: nacos-order-consumer cloud: nacos: discovery: server-addr: localhost:8848 #配置的Nacos地址(本机的写localhost:8848,服务器的写IP地址) #消费者要访问的微服务名称(成功注册进nacos的服务提供者) service-url: nacos-user-service: http://nacos-payment-provider
- 主启动
@SpringBootApplication @EnableDiscoveryClient public class OrderNacosMain83 { public static void main(String[] args) { SpringApplication.run(OrderNacosMain83.class,args); } }
- 配置类新建config.ApplicationContextConfig
@Configuration public class ApplicationContextConfig { @Bean @LoadBalanced public RestTemplate getRestTemplate(){ return new RestTemplate(); } }
- controller
@RestController public class OrderNacosController { @Resource private RestTemplate restTemplate; @Value("${service-url.nacos-user-service}") private String serverURL; @GetMapping("/consumer/payment/nacos/{id}") public String getPayment(@PathVariable("id") Integer id){ return restTemplate.getForObject(serverURL+"/payment/nacos/"+id,String.class); } }
服务注册中心对比
Nacos全景图
Nacos和CAP
AP和CP的切换
A:可用性
C:一致性
P:分区容错性
Nacos默认AP。
切换CP:
Nacos作为服务配置中心演示
Nacos作为配置中心——基础配置
- 新建模块cloudalibaba-config-nacos-client3377
- pom
<dependencies> <!-- nacos config--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <!-- openfeign --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <!--SpringCloud Alibaba nacos--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <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>
Nacos同springcloud-config一样,在项目初始化时,要保证先从配置中心进行配置拉取,拉取配置之后,才能保证项目的正常启动。
springboot中配置文件的加载是存在优先级顺序的,bootstrap优先级高于application
bootstrap
server: port: 3377 spring: application: name: nacos-config-client cloud: nacos: discovery: server-addr: 10.211.55.17:8848 #Nacos服务注册中心地址(本机的写localhost) config: server-addr: 10.211.55.17:8848 #Nacos作为配置中心地址(本机的写localhost) file-extension: yml #指定yml格式配置
application.yml
spring: profiles: active: dev #表示开发环境
controller
@RefreshScope //支持Nacos的动态刷新功能 @RestController public class ConfigClientController { @Value("${config.info}") private String configInfo; @GetMapping("/config/info") public String getConfigInfo(){ return configInfo; } }
springcloud alibaba(3)https://developer.aliyun.com/article/1530575