SpringCloud配置中心+Feign+Gateway网关

发布时间:2022-06-25 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了SpringCloud配置中心+Feign+Gateway网关脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

1. 配置中心

1.1 配置中心的设置

SpringCloud配置中心也可以使用nacos来完成

SpringCloud配置中心+Feign+Gateway网关

SpringCloud配置中心+Feign+Gateway网关

nacos配置中心的配置通过 [服务名]-[类型].[后缀名] 来定位到需要读取这个配置文件的服务

1.2 配置中心的读取

  1. 给需要使用配置中心的服务 添加依赖
<!--nacos配置管理依赖-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
  1. 给需要使用配置中心的服务 添加 bootstrap.yml 配置文件并输入配置
# 通过 [服务名称]-[开发环境].[文件类型] 可以定位到 Nacos 的配置中心的具体配置
spring:
  application:
    name: userservice # 服务名称
  profiles:
    active: dev #开发环境,这里是dev 
  cloud:
    nacos:
      server-addr: localhost:8848 # Nacos地址
      config:
        file-extension: yaml # 文件后缀名
  1. 部署热更新 每次修改配置中心的文件,服务不需要再重启就能读取到最新配置 方法1. 想要热更新的Field所在class上添加 @RefreshScope 注解,这样配置中心的xx.yyy属性更新后,str的属性也会更新
@Component
@RefreshScope
class Test{

    @Value("xx.yyy")
    private String str;    
}

方法2. 使用 @ConfigurationProperties 注解获取配置中心的配置,然后添加到IOC容器中。这样配置中心的xx.yyy属性更新后,yyy的属性也会更新

@Component
@ConfigurationProperties(prefix = "xx")
class Test{
    private String yyy;
}

@Component
class Org{
    
    @Autowired
    private Test test;
}

1.3 配置中心的共享

直接使用 [服务名称].[文件类型] 为Data ID来创建配置

SpringCloud配置中心+Feign+Gateway网关

这样该服务的所有类型的开发环境下都能读取到该配置 配置的优先级:[服务名称]-[开发环境].[文件类型] > [服务名称].[文件类型] > 本地配置文件(applicaiton.yml)

2. Feign远程调用

Feign提供了一种优雅的方式调用远程接口,不需要再使用 RestTemplate 了

2.1 使用步骤

  1. 给需要使用远程调用功能的服务 添加依赖
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
  1. 给需要使用远程调用功能的服务的启动类添加 @EnableFeignClients 注解
  2. 编写Feign客户端(接口)
@FeignClient("userservice") //服务名
public interface UserFeign {

    // 请求方式、请求路径、方法参数、返回值要和 被请求的服务 的接口相同
    @GetMapping("/user/{id}")   
    User findByID(@PathVariable("id")Long id);
}
  1. 使用
@Component
class Test{

    @Autowired
    private UserFeign userFeign;

    public void test(){
        User user = userFeign.findByID(1L);
    }
}

2.2 自定义配置

  1. 主要属性

    SpringCloud配置中心+Feign+Gateway网关

  2. 配置文件方式 基于配置文件修改feign的日志级别可以针对单个服务:
feign:  
  client:
    config: 
      userservice: # 针对某个微服务的配置
        loggerLevel: FULL #  日志级别 

也可以针对所有服务:

feign:  
  client:
    config: 
      default: # 这里用default就是全局配置,如果是写服务名称,则是针对某个微服务的配置
        loggerLevel: FULL #  日志级别 
  1. 代码方式
public class DefaultFeignConfiguration  {
    @Bean
    public Logger.Level feignLogLevel(){
        return Logger.Level.BASIC; // 日志级别为BASIC
    }
}

SpringCloud配置中心+Feign+Gateway网关

2.3 Feign的性能优化

使用连接池代替默认的URLConnection

  1. 引入依赖
<!--httpClient的依赖 -->
<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-httpclient</artifactId>
</dependency>
  1. 添加配置
feign:
  httpclient:
    enabled: true # 开启feign对HttpClient的支持
    max-connections: 200 # 最大的连接数
    max-connections-per-route: 50 # 每个路径的最大连接数

2.4 最佳实践

把Feign抽取成模块,然后需要用到的服务再引入即可

  1. 创建模块 feign
  2. 引入依赖
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
  1. 抽取公共类(pojo类,Feign类,FeignConfig类等等)
  2. 给需要用到的服务 引入依赖
<dependency>
     <groupId>com.cc</groupId>
     <artifactId>feign</artifactId>
     <version>1.0</version>
</dependency>
  1. 解决包扫描问题(服务的启动类上)

    SpringCloud配置中心+Feign+Gateway网关

3. Gatewag网关

脚本宝典总结

以上是脚本宝典为你收集整理的SpringCloud配置中心+Feign+Gateway网关全部内容,希望文章能够帮你解决SpringCloud配置中心+Feign+Gateway网关所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: