Spring Cloud Alibaba
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>{project-version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Nacos作为注册中心
1.引入依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
2. 配置 Nacos Server 地址
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
3.@EnableDiscoveryClient 注解开启服务注册与发现功能
@EnableDiscoveryClient
@SpringBootApplication
public class GulimallCouponApplication {
public static void main(String[] args) {
SpringApplication.run(GulimallCouponApplication.class, args);
}
}
4.应用命名
spring.application.name=gulimall-coupon
远程调用
1.引入open-feign
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2.编写接口
方法签名一致,请求地址补全
@FeignClient("gulimall-coupon")
public interface CouponFeignService {
@RequestMapping("/coupon/coupon/member/list")
public R memberCoupons();
}
3.开启远程调用
@Autowired
private CouponFeignService couponFeignService;
@RequestMapping("/coupons")
public R test(){
MemberEntity memberEntity=new MemberEntity();
memberEntity.setNickname("Elysia");
R memberCoupons= couponFeignService.memberCoupons();
return R.ok().put("member",memberEntity).put("coupons",memberCoupons.get("coupons"));
}
Nacos作为配置中心
1.引入依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
</dependencies>
2.创建bootstrap.properties
spring.application.name=gulimall-coupon
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
3.添加一个数据集(Data Id)
gulimall-coupon.properties
默认规则,应用名.properties
4.动态获取配置
@RefreshScope:动态获取并刷新配置
配置中心的配置优先
@RefreshScope
@RestController
@RequestMapping("coupon/coupon")
public class CouponController {
@Autowired
private CouponService couponService;
@Value("${coupon.user.name}")
private String name;
@Value("${coupon.user.age}")
private String age;
@RequestMapping("/test")
public R test(){
return R.ok().put("name",name).put("age",age);
}
}
5.命名空间与分组
为某服务创建命名空间,依据不同环境创建分组 如开发环境dev
6. 同时加载多个配置集
spring.application.name=gulimall-coupon
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.cloud.nacos.config.namespace=8d8f3314-bf89-4133-a39e-047178438384
spring.cloud.nacos.config.group=dev
spring.cloud.nacos.config.ext-config[0].data-id=datasource.yml
spring.cloud.nacos.config.ext-config[0].group=dev
spring.cloud.nacos.config.ext-config[0].refresh=true
spring.cloud.nacos.config.ext-config[1].data-id=mybatis.yml
spring.cloud.nacos.config.ext-config[1].group=dev
spring.cloud.nacos.config.ext-config[1].refresh=true
spring.cloud.nacos.config.ext-config[2].data-id=other.yml
spring.cloud.nacos.config.ext-config[2].group=dev
spring.cloud.nacos.config.ext-config[2].refresh=true
GateWay
功能:路由转发,权限校验,限流控制
工作方式
断言predicate
匹配请求的信息,如请求头和请求参数。判断请求路由到哪个服务
spring:
cloud:
gateway:
routes:
- id: route_test
uri: https://www.baidu.com
predicates:
- Query=url,baidu
predicates断言数组
参数url=baidu时,转发到 uri: https://www.baidu.com
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- After=2017-01-20T17:42:47.789-07:00[America/Denver]
请求需要在指定日期时间之后发出
过滤器Filter
在请求和响应到达之前或之后,进行处理