要加强网站内容建设,力网站票网站开发,wordpress 本地 上传到服务器,湘潭做网站 都来磐石网络文章目录 Nacos Server下载启动登录创建命名空间 Nacos Client启动样例Nacos 服务发现配置项 集成 OpenFeign 远程接口调用添加 OpenFeign 依赖开启 EnableFeignClients 注解编写远程服务接口远程接口调用 集成 Sentinel 熔断降级添加 Sentinel 依赖开启 Sentinel 熔断降级编写… 文章目录 Nacos Server下载启动登录创建命名空间 Nacos Client启动样例Nacos 服务发现配置项 集成 OpenFeign 远程接口调用添加 OpenFeign 依赖开启 EnableFeignClients 注解编写远程服务接口远程接口调用 集成 Sentinel 熔断降级添加 Sentinel 依赖开启 Sentinel 熔断降级编写降级回调类添加 fallback 属性 集成 GateWay 动态路由 Nacos 官网https://nacos.io Nacos GitHub 地址https://github.com/alibaba/nacos spring-cloud-alibaba GitHub 地址https://github.com/alibaba/spring-cloud-alibaba Spring Cloud Alibaba 组件版本说明https://github.com/alibaba/spring-cloud-alibaba/wiki/版本说明 NacosNaming and Configuration Service命名和配置服务是一个动态服务发现、配置管理和服务治理的一站式解决方案致力于快速构建、部署和管理微服务平台。
Nacos的主要功能包括
服务发现Nacos提供了一个基于DNS和HTTP的服务发现机制使得服务之间能够轻松地找到对方并建立连接。服务治理Nacos提供了丰富的服务治理功能包括熔断、限流和负载均衡等帮助您构建稳定、可靠的微服务系统。配置管理Nacos支持中心化、外部化和动态化的配置管理可以为微服务应用提供实时的配置更新允许在不重启应用的情况下更新配置。
Nacos Server
下载
查看 Spring Cloud Alibaba 的各组件版本
Spring Cloud Alibaba VersionSentinel VersionNacos VersionRocketMQ VersionDubbo VersionSeata Version2022.0.0.0-RC21.8.62.2.14.9.4~1.7.0-native-rc22021.0.5.01.8.62.2.04.9.4~1.6.12.2.10-RC11.8.62.2.04.9.4~1.6.12022.0.0.0-RC11.8.62.2.1-RC4.9.4~1.6.12.2.9.RELEASE1.8.52.1.04.9.4~1.5.2
进入 Nacos GitHub点击【Tags】 进入对应的【Tag】下载对应的版本文件 Nacos 下载完成 启动
将压缩文件进行解压目录如下 创建数据库【nacos】 导入 \nacos\conf 目录下的 nacos-mysql.sql 数据库文件 查看数据库内容包含和配置文件命名空间用户有关的数据表 修改 \nacos\conf 目录下的 application.properties 配置文件 修改启动模式将集群模式【cluster】改为单机模式【standalone】 Windows 运行 nacos\bin 目录下的 startup.cmd 文件 登录
访问链接http://localhost:8848/nacos使用默认账号密码登录【nacos:nacos】 创建命名空间
依次点击左侧菜单【命名空间→新建命名空间】输入表格内容点击【确定】即可 Nacos Client
启动样例
创建对应版本的 SpringBoot 项目 选择相关依赖如 web、Nacos Service Discovery Pom.xml 中依赖如下
properties!--spring-boot 版本--spring-boot.version2.3.12.RELEASE/spring-boot.version!--spring-cloud-alibaba 版本--spring-cloud-alibaba.version2.2.9.RELEASE/spring-cloud-alibaba.version
/properties
dependencies!--web 依赖--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!--spring-cloud-alibaba-nacos 依赖--dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-discovery/artifactId/dependency
/dependencies
dependencyManagementdependencies!-- spring-boot 依赖管理--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-dependencies/artifactIdversion${spring-boot.version}/versiontypepom/typescopeimport/scope/dependency!-- spring-cloud-alibaba 依赖管理--dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-alibaba-dependencies/artifactIdversion${spring-cloud-alibaba.version}/versiontypepom/typescopeimport/scope/dependency/dependencies
/dependencyManagementYAML 配置如下
server:port: 8080
spring:application:name: nacos-client-acloud:nacos:server-addr: localhost:8848 # nacos server 的地址 默认localhost:8848username: nacospassword: nacosdiscovery: # 注册相关配置namespace: 9889d187-8b66-41f9-a554-d3d0635c0ee9 # 命名空间的 idgroup: A_GROUP # 分组名称默认为DEFAULT_GROU启动项目显示 nacos 注册, 默认组 nacos-client-a 192.168.152.1:8080 注册完成 查看 Nacos Server 中的服务列表 Nacos 服务发现配置项
nacos.discovery 是 Nacos 中与服务注册与发现相关的配置项前缀包含以下配置
配置项Key默认值说明服务注册中心地址spring.cloud.nacos.discovery.server-addr无多个地址可以使用逗号隔开命名空间spring.cloud.nacos.discovery.namespace实现多租户的注册与发现区分隔离服务名spring.cloud.nacos.discovery.service${spring.application.name}服务列表的服务名称服务分组spring.cloud.nacos.discovery.groupDEFAULT_GROUP服务的分组信息权重spring.cloud.nacos.discovery.weight1取值范围1-100数值越大权重越大网卡名spring.cloud.nacos.discovery.network-interface无当 IP 未配置时注册的 IP 为此网卡所对应的IP地址如果此项也未配置则默认取第一块网卡的地址注册 IP 地址spring.cloud.nacos.discovery.ip无优先级最高注册端口spring.cloud.nacos.discovery.port-1默认情况下不用配置会自动探测阿里云账号spring.cloud.nacos.discovery.access-key无当要上阿里云时阿里云上面的一个云账号名阿里云密码spring.cloud.nacos.discovery.secret-key无当要上阿里云时阿里云上面的一个云账号密码元数据spring.cloud.nacos.discovery.metadata无服务实例的元数据信息使用 Map 格式配置日志文件名spring.cloud.nacos.discovery.log-name无
集成 OpenFeign 远程接口调用
添加 OpenFeign 依赖
添加 spring-cloud 和 spring-cloud-openfeign 依赖
properties!--spring-cloud 版本--spring-cloud.versionHoxton.SR12/spring-cloud.version
/properties
dependencies!--spring-cloud-openfeign 依赖--dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-openfeign/artifactId/dependency
/dependencies
dependencyManagementdependencies!-- spring-cloud 依赖管理--dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-dependencies/artifactIdversion${spring-cloud.version}/versiontypepom/typescopeimport/scope/dependency/dependencies
/dependencyManagement开启 EnableFeignClients 注解
EnableFeignClients //开启 Feign
EnableDiscoveryClient //开启服务发现
SpringBootApplication
public class NacosApplication {public static void main(String[] args) {SpringApplication.run(NacosApplication.class, args);}}编写远程服务接口
FeignClient(value nacos-user-service) //调用服务的服务名称
public interface UserFeignService {GetMapping(info) //调用的服务接口String getInfo();}远程接口调用
编写 UserController 接口调用服务
RestController
public class UserController {Autowiredprivate UserFeignService userFeignService;GetMapping(userInfo)public String getUserInfo(){return userFeignService.getInfo();}}注服务之前需要在同一个命名空间和分组下否则不能发现服务环境隔离。 集成 Sentinel 熔断降级
添加 Sentinel 依赖
!--sentinel 依赖--
dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-sentinel/artifactId
/dependency开启 Sentinel 熔断降级
在 YAML 配置文件中开启 Sentinel 熔断降级
feign:sentinel:enabled: true编写降级回调类
编写 UserFeignService 服务的接口实现类 UserFeignFallbackService
/*** UserFeignService 降级回调类*/
Component
public class UserFeignFallbackService implements UserFeignService {public String getInfo() {return UUID.randomUUID().toString();}
}注当 UserFeignService 中的接口调用出现异常或服务提供者下线时就会调用 UserFeignFallbackService 类中对应得方法进行降级处理。
添加 fallback 属性
在 UserFeignService 接口的注解 FeignClient 上添加 fallback 属性指定降级回调类
FeignClient(value nacos-user-service,fallback UserFeignFallbackService.class) //调用服务的服务名称
public interface UserFeignService {GetMapping(info)String getInfo();}集成 GateWay 动态路由
添加 Gateway 依赖
!--gateway 依赖--
dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-gateway/artifactId
/dependencyYAML 文件中添加配置
server:port: 8081spring:application:name: nacos-client-bcloud:nacos:server-addr: localhost:8848 # nacos server 的地址username: nacos # Nacos 用户名password: nacos # Nacos 密码discovery: # 注册相关配置namespace: 9889d187-8b66-41f9-a554-d3d0635c0ee9 # 命名空间的 idgroup: A_GROUP # 分组名称默认为DEFAULT_GROUPgateway:discovery:locator:enabled: true # 开启动态路由lower-case-service-id: true添加 EnableDiscoveryClient 注解
EnableDiscoveryClient
SpringBootApplication
public class NacosGateWayApplication {public static void main(String[] args) {SpringApplication.run(NacosGateWayApplication.class, args);}}