分布式组件
spring cloud openfeign 远程调用
添加依赖
gulimall的pom.xml:
由于进行了spring boot-spring cloud-spring cloud alibaba三者的版本仲裁,所以不需要额外添加依赖控制版本。
gulimall-common中的pom.xml添加依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
由于其他微服务引入了gulimall-common包,所以其他微服务引入了服务调用依赖。
远程调用测试
1.gulimall-coupon的CouponController添加一个被调用方法。
//test-openfeign
@RequestMapping("/member/list")
public R membercoupons(){
CouponEntity couponEntity=new CouponEntity();
couponEntity.setCouponName("测试优惠卷:满100减10");
return R.ok().put("coupons",Arrays.asList(couponEntity));
}
2.调用接口可复用,所以放到gulimall-common中。新建包feign,包中新建调用接口CouponFeignService,接口详情:
接口中@RequestMapping注解的调用地址注意拼接前缀地址。
package com.atguigu.common.feign;
import com.atguigu.common.utils.R;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
//@Component
@FeignClient("gulimall-coupon")
public interface CouponFeignService {
//test-openfeign
@RequestMapping("/coupon/coupon/member/list")
public R membercoupons();
}
3.使用gulimall-member的MemberController进行调用,添加新调用接口、调用方法,然后在启动类指定调用接口包(@EnableFeignClients(basePackages = “com.atguigu.common.feign”))并添加开启调用注解(@EnableDiscoveryClient)。
@Autowired
CouponFeignService couponFeignService;
//test-openfeign
@RequestMapping("/coupons")
public R test(){
MemberEntity memberEntity = new MemberEntity();
memberEntity.setNickname("张三");
R membercoupons=couponFeignService.membercoupons();
return R.ok().put("member",memberEntity).put("coupons",membercoupons.get("coupons"));
}
package com.atguigu.gulimall.member;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableFeignClients(basePackages = "com.atguigu.common.feign")
@MapperScan("com.atguigu.gulimall.member.dao")
@SpringBootApplication
@EnableDiscoveryClient
public class GulimallMemberApplication {
public static void main(String[] args) {
SpringApplication.run(GulimallMemberApplication.class, args);
}
}
4.启动gulimall-coupon和gulimall-member并测试
测试地址:
http://127.0.0.1:8000/member/member/coupons
结果:
停掉gulimall-coupon,再访问:
nacos配置中心
1.引入依赖
放到gulimall-common中
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
2.gulimall-coupon的配置
bootstrap.yaml:
spring:
application:
name: gulimall-coupon
cloud:
nacos:
config:
server-addr: localhost:8848 #Nacos作为配置中心地址
file-extension: yaml #指定yaml格式的配置
# data id:
# ${spring.application.name}-${spring.profile.active}.${spring.cloud.nacos.config.file-extension}
CouponController中的测试方法:
类上加个实时刷新注解,类中注入配置数据:
类中添加测试方法:
//test-nacosConfig
@RequestMapping("/test")
public R test(){
return R.ok().put("name",name).put("age",age);
}
3.nacos添加外部化配置
4.测试
http://127.0.0.1:7000/coupon/coupon/test
修改nacos中的配置,编辑,发布,刷新页面看是不是动态修改。
补充:nacos配置中心的命名空间与配置分组
nacos可以用命名空间namespace和group对不同配置进行隔离。
如:
# nacos配置
server:
port: 3377
spring:
application:
name: nacos-config-client
cloud:
nacos:
discovery:
server-addr: localhost:8848 #Nacos服务注册中心地址
config:
server-addr: localhost:8848 #Nacos作为配置中心地址
file-extension: yaml #指定yaml格式的配置
group: DEV_GROUP
namespace: d79c019e-5577-4dd5-8102-0e3b3652421d
这里不展开说明,可以查阅相关资料。
网关spring cloud gateway
如果要进行鉴权、限流、日志输出
没有网关,要在每个微服务里添加相同的功能,重复开发:
有网关,直接将鉴权、限流、日志输出提取到网关里进行:
1.maven方式新建模块gulimall-gateway,引入依赖,添加启动类,配置yaml
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>gulimall</artifactId>
<groupId>com.atguigu.gulimall</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>gulimall-gateway</artifactId>
<dependencies>
<!--gateway-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!--一般基础配置类-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
启动类com.atguigu.gulimall.gateway.GulimallGatewayApplication:
package com.atguigu.gulimall.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class GulimallGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GulimallGatewayApplication.class,args);
}
}
bootstrap.yaml:
spring:
application:
name: gulimall-gateway
cloud:
nacos:
config:
server-addr: localhost:8848
file-extension: yaml
namespace: c4010893-0a68-4c9a-8819-7ea1b7668c69
# data id:
# ${spring.application.name}-${spring.profile.active}.${spring.cloud.nacos.config.file-extension}
application.yaml:
spring:
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
gateway:
routes:
- id: test_route
uri: https://www.baidu.com
predicates:
- Query=url,baidu
- id: qq_route
uri: https://www.qq.com
predicates:
- Query=url,qq
application:
name: gulimall-gateway
server:
port: 88
application.yaml中的routes指的是不同的路由规则,list结构表示。之中的示例路由规则为如果路径中带变量url=xxx,就路由到www.xxx.com。
启动网关测试:
http://127.0.0.1:88/hello?url=qq
发现404,这是因为该网址等效于访问www.qq.com/hello,没有,所以当然是404。
再访问
http://127.0.0.1:88/?url=qq
成功跳转
2.gateway详细配置
这里不叙述,可参照官网配置。