SpringCloud Alibaba官网:https://github.com/alibaba/spring-cloud-alibaba/blob/2.2.x/README-zh.md
SpringCloud Alibaba为什么会出现?
spring cloud Netflix进入维护模式
1.有什么用处?
2.有哪些组件?
1.Nacos是一个更易于构建云远程应用的动态服务发现,配置管理和服务管理平台。
2.代替Eureka做注册中心,代替Config做配置中心,即Nacos = Eureka + Config + Bus
3.Nacos下载安装:Nacos官网下载安装
百度网盘链接:https://pan.baidu.com/s/17F4yz-GxBSqsgvaRK1ABmA
密码:1usz
尚硅谷SpringCloud Alibaba
4.Nacos支持AP模式和CP模式切换,C是所有节点在同一时间看到的数据是一致的;而A的定义是所有的请求都会收到响应。
什么是CAP理论?
C:Consistency 一致性:数据在多个副本之间是否能够保持一致的特性。
A:Availability 可用性:系统提供的服务必须一直处于可用状态,对每一个操作的请求必须在有限时间内返回结果。
P:Tolerance of network Partition 分区容错性:分布式系统在遇到网络分区故障时,仍然需要保证对外提供一致性和可用性的服务,除非整个网络都发生故障。
父项目pom.xml
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
<junit.version>4.12junit.version>
<log4j.version>1.2.17log4j.version>
<lombok.version>1.16.18lombok.version>
<mysql.version>5.1.47mysql.version>
<druid.version>1.1.16druid.version>
<mybatis.spring.boot.version>1.3.2mybatis.spring.boot.version>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>2.2.2.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>Hoxton.SR1version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>2.1.0.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>${mysql.version}version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>${druid.version}version>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>${mybatis.spring.boot.version}version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>${log4j.version}version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>${junit.version}version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>${lombok.version}version>
<optional>trueoptional>
dependency>
dependencies>
dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
1.服务提供者注册
新建服务提供者:nacos-payment-provider9001
① pom.xml
<properties>
<java.version>1.8java.version>
<spring.cloud.alibaba.version>2.2.1.RELEASEspring.cloud.alibaba.version>
properties>
<dependencies>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
② application.yml
server:
port: 9001 #端口号
spring:
application:
name: nacos-payment-provider #服务名称
cloud:
nacos:
discovery:
server-addr: localhost:8848 #配置nacos地址
management:
endpoints:
web:
exposure:
include: "*" #监控的东西
③ 启动类PaymentMain9001.java
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain9001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain9001.class,args);
}
}
2.服务消费者和负载均衡
Nacos自动集成了负载均衡。
在父工程下,再创建一个服务提供者:nacos-payment-provider9002
注册。然后创建一个服务消费者:consumer-nacos-order80
① pom.xml
<dependencies>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
② application.yml
server:
port: 80
undertow:
decode-url:
spring:
application:
name: consumer-nacos-order
cloud:
nacos:
discovery:
server-addr: localhost:8848
server-url:
nacos-user-service: http://nacos-payment-provider #消费者将要去访问的微服务名称(注册成功进nacos的微服务提供者)
③ 配置类
//配置类
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced //负载均衡
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
④ 业务类controller
@RestController
@Slf4j
public class NacosOrderController {
@Resource
private RestTemplate restTemplate;
@Value("${server-url.nacos-user-service}")
private String serverUrl; //在yml里面写的提供者服务路径, 值为:http://nacos-provider
@GetMapping(value="consumer/payment/nacos/{id}")
public String paymentInfo(@PathVariable("id") int id)
{
return restTemplate.getForObject(serverUrl+"/payment/nacos/"+id,String.class);
}
}
⑤ 启动类
@SpringBootApplication
@EnableDiscoveryClient
public class ConsumerNacosOrder80 {
public static void main(String[] args) {
SpringApplication.run(ConsumerNacosOrder80.class,args);
}
}
分别启动服务提供者9001和9002,服务消费者80,启动Nacos,访问Nacos可以看到服务注册成功。
在浏览器中访问服务消费者:http://localhost:80/consumer/payment/nacos/1001
,不断刷新访问,可以看到实现了消费者的负载均衡(轮询)。
3.Nacos作为分布式配置中心
① pom.xml
<dependencies>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-contextartifactId>
dependency>
dependencies>
② 配置文件:bootstrap.yml
# nacos 配置
server:
port: 3377
spring:
application:
name: nacos-config-client
cloud:
nacos:
discovery:
server-addr: localhost:8848 #Nacos服务注册中心地址
config:
server-addr: localhost:8848 #nacos作为配置中心的地址
file-extension: yaml # 指定yaml格式的配置
③ 配置文件:application.yml
spring:
profiles:
active: dev
需要在Nacos中新建的配置文件的名字,遵循下列规则,拼接后的结果是:nacos-config-client-dev.yaml
在Nacos中创建名为:nacos-config-client-dev.yaml
的配置文件。
④ 业务controller
@RestController
@RefreshScope //开启刷新,否则分布式配置不会生效
public class ConfigCenterController {
@Value("${config.info}")
private String configInfo;
@GetMapping("/config/info")
public String getConfigInfo()
{
return configInfo;
}
}
⑤ 启动类:
@SpringBootApplication
@EnableDiscoveryClient
public class NacosConfigMain3377 {
public static void main(String[] args) {
SpringApplication.run(NacosConfigMain3377.class,args);
}
}
启动服务配置中心微服务,访问:http://localhost:3377/config/info
,可以看到Nacos中配置的信息已经读取到。
自带动态刷新,若在Nacos中修改配置文件的信息,再次刷新,访问到的配置信息也会自动刷新。
4.Nacos之命名空间、分组和DataID的关系
是什么?
类似Java里面的package名和类名。最外层的namespace是可以用于区分部署环境的,Group和DataID逻辑上区分两个目标对象。
三者情况
默认情况:namespace=public,group=default_group,默认cluster是default。
比方说我们有三个环境:开发、测试、生产环境,我们就可以创建三个namespace,不同的namespace是隔离的。
(1)Nacos之DataID配置
(2)Nacos之group分组
(3)Nacos之namespace空间方案