脚本宝典收集整理的这篇文章主要介绍了SpringCloud配置中心+Feign+Gateway网关,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
SpringCloud配置中心也可以使用nacos来完成
nacos配置中心的配置通过 [服务名]-[类型].[后缀名] 来定位到需要读取这个配置文件的服务<!--nacos配置管理依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
# 通过 [服务名称]-[开发环境].[文件类型] 可以定位到 Nacos 的配置中心的具体配置
spring:
application:
name: userservice # 服务名称
profiles:
active: dev #开发环境,这里是dev
cloud:
nacos:
server-addr: localhost:8848 # Nacos地址
config:
file-extension: yaml # 文件后缀名
@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;
}
直接使用 [服务名称].[文件类型] 为Data ID来创建配置
这样该服务的所有类型的开发环境下都能读取到该配置 配置的优先级:[服务名称]-[开发环境].[文件类型] > [服务名称].[文件类型] > 本地配置文件(applicaiton.yml)Feign提供了一种优雅的方式调用远程接口,不需要再使用 RestTemplate 了
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
@FeignClient("userservice") //服务名
public interface UserFeign {
// 请求方式、请求路径、方法参数、返回值要和 被请求的服务 的接口相同
@GetMapping("/user/{id}")
User findByID(@PathVariable("id")Long id);
}
@Component
class Test{
@Autowired
private UserFeign userFeign;
public void test(){
User user = userFeign.findByID(1L);
}
}
feign:
client:
config:
userservice: # 针对某个微服务的配置
loggerLevel: FULL # 日志级别
也可以针对所有服务:
feign:
client:
config:
default: # 这里用default就是全局配置,如果是写服务名称,则是针对某个微服务的配置
loggerLevel: FULL # 日志级别
public class DefaultFeignConfiguration {
@Bean
public Logger.Level feignLogLevel(){
return Logger.Level.BASIC; // 日志级别为BASIC
}
}
使用连接池代替默认的URLConnection
<!--httpClient的依赖 -->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
feign:
httpclient:
enabled: true # 开启feign对HttpClient的支持
max-connections: 200 # 最大的连接数
max-connections-per-route: 50 # 每个路径的最大连接数
把Feign抽取成模块,然后需要用到的服务再引入即可
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.cc</groupId>
<artifactId>feign</artifactId>
<version>1.0</version>
</dependency>
以上是脚本宝典为你收集整理的SpringCloud配置中心+Feign+Gateway网关全部内容,希望文章能够帮你解决SpringCloud配置中心+Feign+Gateway网关所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。