一.商城项目总体架构
二.环境搭建代码
1.项目架构
建立父工程 pom文件
<description> 聚合服务</description>
<packaging> pom</packaging>
<modules>
<module> gulimall- coupon</module>
<module> gulimall- member</module>
<module> gulimall- order</module>
<module> gulimall- product</module>
<module> gulimall- ware</module>
<module> renren- fast</module>
<module> renren- generator</module>
<module> gulimall- common</module>
<module> gulimall- common</module>
<module> gulimall- gateway</module>
</modules>
建立每个子工程(微服务) 建立公共服务模块,即可以将公共工具类,公共pom引入放入公共工具类
<parent>
<artifactId> gulimall</artifactId>
<groupId> com.atguigu.gulimall</groupId>
<version> 0.0.1- SNAPSHOT</version>
</parent>
<modelVersion> 4.0.0</modelVersion>
<artifactId> gulimall- common</artifactId>
<description> 每一个微服务公共的依赖,bean,工具类等</description>
其他服务引入该公共服务模块
<dependency>
<groupId> com.atguigu.gulimall</groupId>
<artifactId> gulimall- common</artifactId>
<version> 0.0.1- SNAPSHOT</version>
</dependency>
2.Nacos注册中心(微服务注册到Nacos中)
Nacos依赖引入
<!-- 服务注册/发现- - >
<dependency>
<groupId> com.alibaba.cloud</groupId>
<artifactId> spring- cloud- starter- alibaba- nacos- discovery</artifactId>
</dependency>
开启注解驱动
@EnableDiscoveryClient
@SpringBootApplication
public class GulimallCouponApplication {
public static void main(String[ ] args) {
SpringApplication.run(GulimallCouponApplication.class, args);
}
}
配置Nacos注册中心地址以及该微服务名称
spring :
cloud :
nacos :
discovery :
server-addr : 127.0.0.1: 8848
application :
name : gulimall- coupon
Nacos中会有名称对应地址的注册
3.远程调用Feign
引入pom文件
<dependency>
<groupId> org.springframework.cloud</groupId>
<artifactId> spring- cloud- starter- openfeign</artifactId>
</dependency>
注意被调用服务需要在Nacos注册中心中注册 调用者编写接口
@FeignClient ( "gulimall-coupon" )
public interface CouponFeignService {
@RequestMapping ( "/coupon/coupon/member/list" )
public R memberCoupons ( ) ;
}
被调用者注册在Nacos中的映射 和其controller gulimall-coupon:127.0.0.1:8848
@RequestMapping ( "coupon/coupon" )
public class CouponController {
@RequestMapping ( "/member/list" )
public R memberCoupons ( ) {
CouponEntity couponEntity = new CouponEntity ( ) ;
couponEntity. setCouponName ( "满一百减十" ) ;
return R . ok ( ) . put ( "coupons" , Arrays . asList ( couponEntity) ) ;
}
}
开启注解驱动并指定某包下全为feign
@EnableFeignClients ( "com.atguigu.gulimall.member.feign" )
@EnableDiscoveryClient
@SpringBootApplication
public class GulimallMemberApplication {
public static void main ( String [ ] args) {
SpringApplication . run ( GulimallMemberApplication . class , args) ;
}
}
调用者调用服务接口
public R test ( ) {
MemberEntity memberEntity = new MemberEntity ( ) ;
memberEntity. setNickname ( "张三" ) ;
R r = couponFeignService. memberCoupons ( ) ;
return R . ok ( ) . put ( "member" , memberEntity) . put ( "coupons" , r. get ( "coupons" ) ) ;
}
4.Nacos作为配置中心
引入pom文件
< dependency>
< groupId> com.alibaba.cloud</ groupId>
< artifactId> spring-cloud-starter-alibaba-nacos-config</ artifactId>
</ dependency>
利用bootstrap.properties动态拉取Nacos中的配置
spring.application.name=gulimall- coupon
spring.cloud.nacos.config.server- addr=127.0.0.1: 8848
spring.cloud.nacos.config.namespace=66f939a7- 03fb- 4a3a- 9e8c- 663cc37fd464
spring.cloud.nacos.config.group=prod
spring.cloud.nacos.config.ext- config[ 0 ] .dataId=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 ] .dataId=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 ] .dataId=other.yml
spring.cloud.nacos.config.ext- config[ 2 ] .group=dev
spring.cloud.nacos.config.ext- config[ 2 ] .refresh=true
配置也可在controller中获得
@Value ( "${coupon.user.name}" )
private String name;
@Value ( "${coupon.user.age}" )
private Integer age;
命名空间和配置分组的使用与配置 命名空间:每个微服务创建自己的命名空间 配置分组:区分环境(dev,test,prop)
5.gateway作为API网关(route->断言->过滤)
引入依赖
< dependency>
< groupId> org.springframework.cloud</ groupId>
< artifactId> spring-cloud-starter-gateway</ artifactId>
</ dependency>
配置routes规则
spring :
cloud :
gateway :
routes :
- id : baidu_route
uri : https: //www.baidu.com
predicates :
- Query=url, baidu
- id : qq_route
uri : https: //www.qq.com
predicates :
- Query=url, qq
访问即可