谷粒商城笔记

1. Mybatis-plus配置:

1.1  依赖

    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.2.0</version>
    </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.17</version>
            <scope>compile</scope>
        </dependency>

1.2  Application注解

@MapperScan("com/stefan/jdmall/coupon/dao")

1.3 application.yml配置

spring:
  datasource:
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/jdmall_sms
    driver-class-name: com.mysql.cj.jdbc.Driver

mybatis-plus:
  mapper-locations: classpath:/mapper/**/*.xml
  global-config:
    db-config:
      id-type: auto

2. Nacos

2.1 nacos service mac系统启动和关闭命令

cd到bin目录下 

sh startup.sh -m standalone

sh shutdown.sh

访问地址:

127.0.0.1:8848/nacos

http://127.0.0.1:8848/nacos/#/serviceManagement?dataId=&group=&appName=&namespace=&pageSize=&pageNo=&namespaceShowName=public&serviceNameParam=&groupNameParam=

 数据库命令:(访问数据库)

mysql -u root -p -h localhost

2.2 nacos服务注册

引入依赖:(注意对应spring-cloud和spring-boot的版本)

    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2021.0.1.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

服务模块配置:

spring:
  cloud:
    nacos:
      config:
        server-addr: 127.0.0.1:8848
  application:
    name: jdmall-member
Application添加:(高版本可以省略)
@EnableDiscoveryClient

2.3 Feign

 引入依赖:

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


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

在Member服务下创建feign文件夹,创建接口

@FeignClient("jdmall-coupon") //声明feign,和目标服务
public interface CouponFeignService {

    @RequestMapping("/coupon/coupon/member/list")
    public R memberList();
}

在Application下添加注释开启Feign功能,设置扫描路径:

@EnableFeignClients(basePackages = "com.stefan.jdmall.member.feign")

在控制器调用:

    @Autowired
    private CouponFeignService couponFeignService;

    @RequestMapping("/coupon/list")
    public R couponList(){
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setNickname("张无");

        R couponR = couponFeignService.memberList();

        return R.ok().put("member", memberEntity).put("coupon", couponR.get("coupon"));
    }

2.4 nacos配置中心

引入依赖

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>

创建文件bootstrap.properties

spring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.application.name=jdmall-coupon

SpringBoot 2.4.x版本之后,对于bootstrap.properties/bootstrap.yml 配置,需要加上spring-cloud-starter-bootstrap

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

 Nacos server配置中心内创建jdmall-coupon.properties,填写配置数据:(配置中心的配置数据优先于项目内application.properties文件的配置)

coupon.user.name=kk1
coupon.user.age=19

控制器内读取文件值:

    @Value("${coupon.user.name}")
    private String name;

加上注解 @RefreshScope ,可动态加载配置中心的文件,不需要重启服务器

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值