spring Cloud 配置总结

maven父项目配置

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.5.RELEASE</version>
</parent>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Hoxton.SR6</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>2.2.1.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

特别注意版本对应的问题

Nacos 注册中心

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

openFeign 依赖 

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

 开启openFeign

@EnableFeignClients

服务通信 

@FeignClient("orders")//服务名称
public interface OrderClient {//接口名称,用于注入,建议分类
    
    //要求请求地址,形参列表(必须加注释),返回值一样
    @GetMapping("/get")
    Order getOrder(@RequestParam Integer id);
    
}

 gateway 网关

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

 纯网关服务,不能导web起步依赖

server:
  port: 80
spring:
  application:
    name: gateway
  cloud:
    gateway:
      routes:
        - id: user
          uri: lb://users
          predicates:
            - Path=/user/**

nacos 配置管理

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
spring:
  application:
    name: users #服务名称
  cloud:
    nacos:
      config:
        file-extension: yaml
        namespace: c25eb97b-8c4b-4ac4-8fd7-274ca9eb1d63 #命名空间的id,一个项目一个命名空间
  profiles:
    active: dev #开发环境

特别注意: 必须写在 bootstrap.yaml 中,代表从别处获取配置

数据实时刷新 

@RefreshScope

Sentinel

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
spring:
  cloud:
    sentinel:
      transport:
        dashboard: 127.0.0.1:8083
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值