栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring-cloud-alibaba项目搭建以及集成数据库

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring-cloud-alibaba项目搭建以及集成数据库

1、spring-cloud-alibaba

pom文件:


pom


     org.springframework.boot
    spring-boot-starter-parent
    2.2.5.RELEASE



    
        
            com.alibaba.cloud
            spring-cloud-alibaba-dependencies
            2.2.1.RELEASE
            pom
            import
        
        
            org.springframework.cloud
            spring-cloud-dependencies
            Hoxton.SR3
            pom
            import
        
    


    junit
    junit
    4.12


    log4j
    log4j
    1.2.12


    org.projectlombok
    lombok


2、子服务:

spring-cloud-alibaba-user

pom文件:

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

        
        
            org.springframework.boot
            spring-boot-starter-web
        

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

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

        
        
            com.alibaba.csp
            sentinel-datasource-nacos
            1.5.2
        

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

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


        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.1.1
        

        
        
            mysql
            mysql-connector-java
            5.1.26
        
    

启动类:

@SpringBootApplication

//开启feign客服端
@EnableFeignClients
//注册客户端
@EnableDiscoveryClient
//扫描包路径
@MapperScan("com.jd.mapper")
public class UserStarter {
    public static void main(String[] args) {
        SpringApplication.run(UserStarter.class);
    }
}

file-extension: yaml # 配置文件的后缀  只有两种可能  properties  yaml
file-extension: yaml做持久化,如果连接数据库需要配置四大金刚
spring:
  cloud:
    sentinel:
      transport:
        #修改yml配置,添加senticel服务控制台地址
        dashboard: localhost:1111
      datasource:
        flow: #限流的配置
          nacos: #限流持久配置
            #使用nacos的持久
            server-addr: localhost:8848
            #获取限流的数据源的dataId
            dataId: application-store-dev
            groupId: DEFAULT_GROUP
            #类型:限流
            rule-type: flow
  datasource:
    username: root
    password: java
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql:///itsourcesstore?useUnicode=true&characterEncoding=UTF-8

#  //配置别名
mybatis:
  type-aliases-package: com.jd.domain

#配置sentinel:熔断
feign:
  sentinel:
    enabled: true #熔断
bootstrap.yml配置文件:
spring:
  cloud:
    nacos:
      server-addr: 127.0.0.1:8848   #nacos的地址
      config:
        name: user-serve   # 配置文件的dataId的前面的部分
        file-extension: yaml # 配置文件的后缀  只有两种可能  properties  yaml
        group: DEFAULT_GROUP  #文件的分组  默认DEFAULT_GROUP
#        namespace: AricChen #namespace如果不写  那么默认会读取public里面的配置文件
  profiles:
    active: dev # 环境名

controller配置:

//刷新范围
@RefreshScope
@RestController
//获取远程配置文件的值,看需求写
@Value("${testTemp}")
private  String testTemp;
@GetMapping("/getUserById/{id}")
@SentinelResource(value = "根据用户ID获取用户信息"
        //限流,配置规则
        ,blockHandler="getUserByIdBlockHandler",
        //熔断
        fallback = "getUserByFallback")
public User getUserById(@PathVariable Long id){
        int i  = 1/0;
    return new User(id,"xx","xx"+testTemp);

}
 //限流,配置规则

public User getUserByFallback(@PathVariable Long id,Throwable e){
    return new User(-1L, "xx", "xx(fallback)");
}

//限流,配置规则


public User getUserByIdBlockHandler(@PathVariable Long id, BlockException ex){
    //打印异常
    ex.printStackTrace();
    //FlowException流异常  instanceof实列
    if (ex instanceof FlowException) {
        return new User(-1L,"xx","xx1(FlowException)");
        //降级异常DegradeException
    }else if (ex instanceof DegradeException){
        return new User(-1L,"xx","xx2(DegradeException)");
        //参数流异常ParamFlowException
    }else if (ex instanceof ParamFlowException){
        return new User(-1L,"xx","xx3(ParamFlowException)");
        //系统块异常SystemBlockException
    }else if (ex instanceof SystemBlockException){
        return new User(-1L,"xx","xx4(ParamFlowException)");
        //权限例外
    }else if (ex instanceof AuthorityException){
        return new User(-1L,"xx","x5(AuthorityException)");
    }
    return new User(-1L, "xx", "xx6(block)");
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1002739.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号