SpringCloud Alibaba快速入门

共 4162字,需浏览 9分钟

 ·

2021-02-02 09:24

点击上方蓝色字体,选择“标星公众号”

优质文章,第一时间送达

76套java从入门到精通实战课程分享

 
     
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
        


        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-sentinel
        


        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-seata
        


        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-config
        


        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-config-server
        

    


    
        
            
                com.alibaba.cloud
                spring-cloud-alibaba-dependencies
                2.2.0.RELEASE
                <type>pomtype>
                import
            

        

    


1、nacos注册中心

  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

@EnableDiscoveryClient
@SpringBootApplication
@MapperScan("com.sun.user.mapper")
public class RoleApplicaion {
    public static void main(String[] args) {
        SpringApplication.run(RoleApplicaion.class, args);
    }
}

  application:
    name: acgn-user

2、OpenFeign


微服务模块引入:

        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        


被调用微服务:

@RestController
@RequestMapping("coupon/coupon")
public class CouponController {
    @Autowired
    private CouponService couponService;

    @RequestMapping("/member/list")
    public R membercoupons(){
        CouponEntity couponEntity = new CouponEntity();
        couponEntity.setCouponName("100-10");
        return R.ok().put("coupons", Arrays.asList(couponEntity));
    }

调用

package com.atguigu.gulimall.member.feign;

import com.atguigu.common.utils.R;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient("gulimall-coupon")
public interface CouponFeignService {
    @RequestMapping("/coupon/coupon/member/list")
    public R membercoupons();
}


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

测试:

    @Autowired
    CouponFeignService couponFeignService;

    @RequestMapping("coupons")
    public R test1(){
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setNickname("sun");
        R memberCoupons = couponFeignService.membercoupons();
        return R.ok().put("member", memberEntity).put("coupons", memberCoupons.get("coupons"));
    }

3、nacos配置中心

   
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-config
        





3.1 nacos配置中心细节

3.1.1 命名空间:



3.1.2 配置集:

所有配置的集合

3.1.3 配置集ID:

3.1.4 配置分组:

spring:
  application:
    name: gulimall-coupon
  cloud:
    nacos:
      config:
        server-addr: 127.0.0.1:8848
        namespace: 2741d1a0-6f52-4824-ae4f-6ed82053a59f
        group: dev

3.2 加载多配置集

数据源相关:

mybatis相关:

其他:

bootstrap.yml:

spring:
  application:
    name: gulimall-coupon
  cloud:
    nacos:
      config:
        server-addr: 127.0.0.1:8848
        namespace: 2741d1a0-6f52-4824-ae4f-6ed82053a59f
        group: dev
        extension-configs[0]:
          data-id: datasource.yml
          group: dev
          refresh: true
        extension-configs[1]:
          data-id: mybatis.yml
          group: dev
          refresh: true
        extension-configs[2]:
          data-id: others.yml
          group: dev
          refresh: true


4、Gateway网关


        
            org.springframework.cloud
            spring-cloud-starter-gateway
        



application.properties:

spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.application.name=gulimall-gateway

排除数据源相关:

测试:
application.yml:

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

http://localhost:88/s?url=baidu --> https://www.baidu.com/s


版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:

https://blog.csdn.net/qq_42678055/article/details/113063740





锋哥最新SpringCloud分布式电商秒杀课程发布

👇👇👇

👆长按上方微信二维码 2 秒






感谢点赞支持下哈 

浏览 28
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报