谷粒商城【成神路】-【2】——alibaba组件

目录

🍕1.注册中心nacos

🍔2.feign的加入 

🥓2.nacos作为配置中心 

🍟3.网关搭建 


1.注册中心nacos

  • 1.启动nacos

小张这里启动的是三台nacos作为集群~192.168.20.129:8848, 192.168.20.130:9948,192.168.20.131:8848;(使用集群的话最低三台)

  • 2.启动nginx

在启动nginx之前,启动keepalived,用虚拟ip漂移,搭建两台nginx作为集群,ip:192.168.20.50

并配置nginx的config,方向代理和负载均衡ncaos

  • 3.测试访问 

启动nacos,和nginx后,访问浏览器192.168.20.50:1111/nacos

注:如果不会nacos和nginx的可以先看下小张笔记:come on 我的宝儿 

1.pom文件

添加nacos的依赖

直接添加到公共模块

       <!--nacos-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
   
    #添加alibaba的管控依赖
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.2.0.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

2. 配置yml

注意再启动之前一定要配置服务名称



spring:
  #数据源
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://192.168.20.129:3306/gulimall_sms
    username: root
    password: root
  #nacos
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.20.50:1111
  #服务名称
  application:
    name: gulimall-coupon



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

server:
  port: 7000



 如果启动不成功,查看一下自己的spring boot和cloud的版本号

2.feign的加入 

1.加依赖

添加feign的依赖

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

2.写接口

编写feign的接口@FeignClient(“要调用服务的名称”)

@Component
@FeignClient("gulimall-coupon")
public interface CouponFeignService {

    @RequestMapping("/coupon/coupon/members/list")
    public R memberCoupons();

}

3.业务类

在controller层注入刚刚写好的接口,调用即可

@RestController
@RequestMapping("member/member")
public class MemberController {

    @Autowired
    CouponFeignService couponFeignService;

    @RequestMapping("/coupons")
    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.主启动

添加@EnableFeignClients作为feign客户端

@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients(basePackages="com.xz.gulimall.member.feign")
public class GulimallMemberApplication {

    public static void main(String[] args) {
        SpringApplication.run(GulimallMemberApplication.class, args);
    }

}

5.测试 

如果测试不通过,报jar冲突的话,更换pom文件中的httpcore版本 

注:使用nacos的话不需要导入ribbon的jar包,因为nacos自带

2.nacos作为配置中心 

1.加pom

在公共模块添加依赖

        <!--nacos配置中心config-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>

2.改yml

  • 1.创建配置文件bootstrap.yml
spring:
  #数据源
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://192.168.20.129:3306/gulimall_sms
    username: root
    password: root
  #nacos
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.20.50:1111
      config:
        server-addr: 192.168.20.50:1111
        file-extension: yml
        namespace: ae0280bf-e9c4-4a58-904d-516b63057ff0
        group: TEST_GROUP

  application:
    name: gulimall-coupon


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

server:
  port: 7000
  • 2.application.yml添加配置
spring:
  profiles:
    active: test
  • 3.在nacos客户端添加配置

3.网关搭建 

1.建工程

  • 1.在父工程加创建网管模块
  • 2.注意jdk和maven版本
  • 3.注意spring boot和cloud的版本

2.加pom

记得排除spring-boot-starter-web,不然会与gateway冲突

 <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.2.2.RELEASE</spring-boot.version>
        <spring-cloud.version>Hoxton.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.xz.gulimall</groupId>
            <artifactId>gulimall-common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

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

    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

3.改yml


spring:
  application:
    name: guliamll-gateway
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.20.50:1111
      config:
        server-addr: 192.168.20.50:1111
        namespace: 94c7564b-624f-44f9-9567-e512ac68a97c
    gateway:
      routes:
        - id: baidu_route
          uri: https://www.baidu.com
          predicates:
            - Query=url,baidu

server:
  port: 88

4.主启动

pom里引入了公共模块,所以排除数据库的相关配置

@EnableDiscoveryClient//开启服务的注册发现
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})//排除数据库相关配置
public class GulimallGatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(GulimallGatewayApplication.class, args);
    }

}

详细了解gateway:就差你了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

会敲代码的小张

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值