- springcloud springcloudalibaba springboot版本对应关系地址:https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E![image-20201127134044039]
功能 | springcloud(netfilix) | springcloud-alibaba | dubbo |
---|---|---|---|
服务注册、发现 | eureka(18年停更) | nacos | |
负载均衡 | ribbon、fegin | ribbon | |
熔断、降级 | hystrix | Sentinel | |
路由 | zuul | getway | |
链路追踪 | Sleuth | ||
分布式事务 | seata | ||
分布式配置中心 | SpringCloudConfig | nacos |
-
nacos服务端的安装方式有两种,1.下载源码自己编译、安装 2.下载编译好的源码
- 下载编译好的源码,下载地址:https://github.com/alibaba/nacos/releases
- 下载完毕,解压到相应的目录
- 进入到nacos的bin目录下
- 启动单机nacos服务
startup.cmd -m standalone
- 单机模式(standalone)和集群模式(cluster
- 启动完毕验证服务
localhost:8848/nacos
默认账号密码都是nacos
- 添加依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
- 配置 在启动类上添加@EnableDiscoverClient
/**
* @author liouwb
*/
@SpringBootApplication
@EnableDiscoveryClient
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
- application.yml配置
server:
port: 8804 # 端口号
spring:
application:
name: provider-server # 显示在nacos的服务名称
cloud:
nacos:
discovery:
server-addr: localhost:8848 # nacos地址
- 启动服务就会注册到nacos
-
无需引用jar,nacos依赖的有ribbon
- 配置ribbon负载均衡的策略
/**
* @author liouwb
*/
@Configuration
public class RibbonRuleConfig {
/**
* 自定义负载均衡算法
* ribbon负载均衡配置类IRule
* RoundRobinRule 默认的 轮询
* RandomRule随机
* WeightedResponseTimeRule 权重
*/
@Bean
public IRule myRule() {
return new RandomRule();
}
}
- 配置restTemplate
/**
* @author liouwb
*/
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
- webclient配置
/**
* @author liouwb
*/
@Configuration
public class WebClientConfig {
@Bean
@LoadBalanced
public WebClient.Builder webClientBuilder() {
return WebClient.builder();
}
}
- 通过client-server 调用provider-server
/**
* 调用provider服务 restTemplate
*
* @return
*/
@GetMapping(value = "restProvider")
public String restProvider() {
//Access through the combination of LoadBalanceClient and RestTemplate
// 发现服务
ServiceInstance serviceInstance = loadBalancerClient.choose("provider-server");
// 格式化请求路径
String path = String.format("http://%s:%s/provider/get", serviceInstance.getHost(), serviceInstance.getPort());
System.out.println("请求路径:" + path);
return restTemplate.getForObject(path, String.class);
}
/**
* 调用provider服务 webClientProvider
*
* @return
*/
@GetMapping(value = "webClientProvider")
public String webClientProvider() {
return clientBuilder
.baseUrl("http://provider-server/provider/get")
.build()
.method(HttpMethod.GET)
.retrieve()//请求结果的方法
.bodyToMono(String.class)
.block();
}
- sentinel服务端的下载地址:https://github.com/alibaba/Sentinel/releases
- 下载完毕之后,到sentinel目录下启动sentinel`
java -Dserver.port=8805 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.0.jar
- -Dserver.port=8805是sentinel服务端访问的端口
- 启动之后sentinel 登录界面
localhost:8805
默认的用户名和密码都是sentinel
- 登录之后
- 添加依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
- 配置application.yml
cloud:
nacos:
discovery:
server-addr: localhost:8848
sentinel:
transport:
dashboard: localhost:8805
- 在接口上使用@SentinelResourc value 原方法 blockHandler 异常处理 fallback 降级处理
@GetMapping(value = "get")
@SentinelResource(value = "get", blockHandler = "exceptionHandler", fallback = "fallbackFun")// value 原方法 blockHandler 异常处理 fallback 降级处理
public String get() {
return "这是一个provider服务,服务端口:" + serverPort;
}
public String fallbackFun() {
return "@sentinel:" + serverPort;
}
public String exceptionHandler(){
return "@sentinel exceptionHandler:" + serverPort;
}
- @SentinelResource` 注解用来标识资源是否被限流、降级
- 限流设置
- 添加
- 添加完限流规则就会出现在流控规则列表
- 然后在快速访问接口
- 实时监控流量
- 添加依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
- 在nacos中心添加配置文件
- 编写文件内容,并发布
- 查看配置文件列表信息
- 编写项目配置,从远程nacos获取配置文件
bootstrap.yml
文件
server:
port: 8802
spring:
application:
name: client-server
profiles:
active: dev
cloud:
nacos:
discovery:
server-addr: localhost:8848
config:
# nacos地址
server-addr: localhost:8848
# 配置文件后缀名为yaml
# 指定远程nacos 配置文件名称 这里会自动加载provider-server-dev.yml
name: provider-server
file-extension: yml
- 启动程序,验证配置是否生效
- 自定义nacos配置中心命令空间
- 新增命令空间
- 新增配置
- 代码测试