• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

springcloudalibb框架和搭建

武飞扬头像
java源码du
帮助1

1、springcloudalibb框架

     Spring Cloud Alibaba其实是阿里的微服务解决方案,是阿里巴巴结合自身微服务实践,开源的微服务全家桶。

常用组件:

Zuul: 做服务网关,它是整个微服务的大门,可以用来实现登录、权限检查等业务。

Eureka: 做服务注册与发现,用来解决服务之间通信问题。

Ribbon/OpenFeign:用做客户端的负载均衡,也就是解决将请求路由到微服务集群的问题 。

Hystrix:断路器,它的熔断、降级策略用来解决单节点故障 。

Config:分布式配置中心,用来统一管理配置所有微服务的配置文件 。

Bus:消息总线,用来给各个微服务广播消息,可以实现各个微服务配置的自动刷新 。

Sleuth:链路追踪,用来实时监控各个微服务建的调用关系,快速定位故障节点 。

2、 springcloudalibb框架搭建原理:

学新通

案列:

学新通

1、父子项目搭建spring-cloud-alibaba-test:

 1.1、导包:

  1.  
    <!--父项目-->
  2.  
    <packaging>pom</packaging>
  3.  
     
  4.  
     
  5.  
    <!--管理springboot依赖-->
  6.  
    <parent>
  7.  
    <groupId> org.springframework.boot</groupId>
  8.  
    <artifactId>spring-boot-starter-parent</artifactId>
  9.  
    <version>2.2.5.RELEASE</version>
  10.  
    </parent>
  11.  
     
  12.  
    <!--SpringCloud依赖和SpringCloudAlibaba依赖-->
  13.  
    <dependencyManagement>
  14.  
    <dependencies>
  15.  
    <dependency>
  16.  
    <groupId>com.alibaba.cloud</groupId>
  17.  
    <artifactId>spring-cloud-alibaba-dependencies</artifactId>
  18.  
    <version>2.2.1.RELEASE</version>
  19.  
    <type>pom</type>
  20.  
    <scope>import</scope>
  21.  
    </dependency>
  22.  
    <dependency>
  23.  
    <groupId>org.springframework.cloud</groupId>
  24.  
    <artifactId>spring-cloud-dependencies</artifactId>
  25.  
    <version>Hoxton.SR3</version>
  26.  
    <type>pom</type>
  27.  
    <scope>import</scope>
  28.  
    </dependency>
  29.  
    </dependencies>
  30.  
    </dependencyManagement>
  31.  
     
  32.  
    <!--公共类-->
  33.  
    <dependencies>
  34.  
    <!--测试类-->
  35.  
    <dependency>
  36.  
    <groupId>junit</groupId>
  37.  
    <artifactId>junit</artifactId>
  38.  
    <version>4.12</version>
  39.  
    </dependency>
  40.  
     
  41.  
    <!--log4j提供了方法使我们能将日志信息分级存储-->
  42.  
    <dependency>
  43.  
    <groupId>log4j</groupId>
  44.  
    <artifactId>log4j</artifactId>
  45.  
    <version>1.2.12</version>
  46.  
    </dependency>
  47.  
     
  48.  
    <!--data用-->
  49.  
    <dependency>
  50.  
    <groupId>org.projectlombok</groupId>
  51.  
    <artifactId>lombok</artifactId>
  52.  
    </dependency>
  53.  
    </dependencies>
  54.  
     

1.2、子项目:

spring-cloud-alibaba-store
spring-cloud-alibaba-product
spring-cloud-alibaba-common-store
spring-cloud-alibaba-gateway

2、spring-cloud-alibaba-gateway网关服务搭建:

2.1导包:

  1.  
    <dependencies>
  2.  
    <!--服务网关,为了处理每个服务都要做的事情。你可以认为是切面变成了服务-->
  3.  
    <dependency>
  4.  
    <groupId>org.springframework.cloud</groupId>
  5.  
    <artifactId>spring-cloud-starter-gateway</artifactId>
  6.  
    </dependency>
  7.  
     
  8.  
    <!--服务注册-->
  9.  
    <dependency>
  10.  
    <groupId>com.alibaba.cloud </groupId>
  11.  
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  12.  
    </dependency>
  13.  
     
  14.  
    <!--配置中心客户端-->
  15.  
    <dependency>
  16.  
    <groupId>com.alibaba.cloud</groupId>
  17.  
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
  18.  
    </dependency>
  19.  
     
  20.  
    <!--Gateway聚合Swagger-->
  21.  
    <dependency>
  22.  
    <groupId>io.springfox</groupId>
  23.  
    <artifactId>springfox-swagger2</artifactId>
  24.  
    <version>2.9.2</version>
  25.  
    </dependency>
  26.  
     
  27.  
    <dependency>
  28.  
    <groupId>io.springfox</groupId>
  29.  
    <artifactId>springfox-swagger-ui</artifactId>
  30.  
    <version>2.9.2</version>
  31.  
    </dependency>
  32.  
    </dependencies>

2.2、启动类配置:
 

  1.  
    package com.jd;
  2.  
     
  3.  
    import org.springframework.boot.SpringApplication;
  4.  
    import org.springframework.boot.autoconfigure.SpringBootApplication;
  5.  
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  6.  
     
  7.  
    @SpringBootApplication
  8.  
    //注册客户端
  9.  
    @EnableDiscoveryClient
  10.  
    public class GatewayStarter {
  11.  
    public static void main(String[] args) {
  12.  
    SpringApplication.run(GatewayStarter.class);
  13.  
    }
  14.  
    }

  2.3、xml注册 :

application.xml配置:可以配远程或本地自己选择

  1.  
    server:
  2.  
    port: 10010
  3.  
    spring:
  4.  
    application:
  5.  
    name: service-gateway #服务名
  6.  
    cloud: #注册到Nacos
  7.  
    nacos:
  8.  
    discovery:
  9.  
    server-addr: 127.0.0.1:8848
  10.  
    gateway:
  11.  
    discovery:
  12.  
    locator:
  13.  
    enabled: false #开放服务名访问方式
  14.  
    lower-case-service-id: true #服务名小写
  15.  
    routes: #路由配置
  16.  
    - id : application-product #指定服务名
  17.  
    uri: lb://service-product #去注册中心找这个服务名
  18.  
    predicates: #断言,匹配访问的路径
  19.  
    - Path=/product/** #服务访问路径
  20.  
    filters:
  21.  
    - StripPrefix=1
  22.  
     
  23.  
    - id: application-store #指定服务名
  24.  
    uri: lb://service-store #去注册中心找这个服务名
  25.  
    predicates: #断言,匹配访问的路径
  26.  
    - Path=/store/** #服务访问路径
  27.  
    filters:
  28.  
    - StripPrefix=1
  29.  
     
  30.  
    globalcors: #跨域配置
  31.  
    cors-configurations:
  32.  
    '[/**]':
  33.  
    allowedOrigins: "*"
  34.  
    allow-credentials: true
  35.  
    allowed-headers: "*"
  36.  
    allowedMethods:
  37.  
    - GET
  38.  
    - POST
  39.  
    - DELETE
  40.  
    - PUT
  41.  
    - PATCH
  42.  
    - OPTIONS
  43.  
    - HEAD
  44.  
    - CONNECT
  45.  
    - TRACE
  46.  
    #允许Bean覆盖
  47.  
    main:
  48.  
    allow-bean-definition-overriding: true

 bootstrap.xml配置 :

  1.  
    server:
  2.  
    port: 10010
  3.  
    spring:
  4.  
    profiles:
  5.  
    active: dev
  6.  
    application:
  7.  
    name: product-gateway
  8.  
    cloud:
  9.  
    nacos:
  10.  
    discovery:
  11.  
    server-addr: localhost:8848 #注册中心
  12.  
    config:
  13.  
    server-addr: localhost:8848 #配置中心
  14.  
    file-extension: yaml #配置文件格式
  15.  
    prefix: application-gateway #配置前缀 ,默认使用sring.application.name
  16.  
    group: DEFAULT_GROUP #默认分组
  17.  
    namespace: b18ab1b5-485a-4d43-a4ff-fe7da3488dbb #命名空间的ID

3、 spring-cloud-alibaba-store库存模块:

3.1 pom:

  1.  
    <dependencies>
  2.  
    <!--springbootnacos依赖-->
  3.  
    <dependency>
  4.  
    <groupId>com.alibaba.cloud </groupId>
  5.  
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  6.  
    </dependency>
  7.  
     
  8.  
    <!--加入WEB依赖是为了方便后面写Controller-->
  9.  
    <dependency>
  10.  
    <groupId>org.springframework.boot</groupId>
  11.  
    <artifactId>spring-boot-starter-web</artifactId>
  12.  
    </dependency>
  13.  
     
  14.  
    <!-- 配置中心客户端-->
  15.  
    <dependency>
  16.  
    <groupId>com.alibaba.cloud</groupId>
  17.  
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
  18.  
    </dependency>
  19.  
     
  20.  
    <!--客户端接入导入sentinel依赖-->
  21.  
    <dependency>
  22.  
    <groupId>com.alibaba.cloud</groupId>
  23.  
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
  24.  
    </dependency>
  25.  
     
  26.  
    <!--Sentinel和Nacos做持久的-->
  27.  
    <dependency>
  28.  
    <groupId>com.alibaba.csp</groupId>
  29.  
    <artifactId>sentinel-datasource-nacos</artifactId>
  30.  
    <version>1.5.2</version>
  31.  
    </dependency>
  32.  
     
  33.  
    <!-- &lt;!&ndash;,在SpringBoot启动时,去扫描所有Mapper接口,然后为其增加一个代理实现类,在调用的过程中,-->
  34.  
    <!--我们实际调用的是这个代理对象 可以写,可以写在父类,建议写在当前子类&ndash;&gt;-->
  35.  
    <dependency>
  36.  
    <groupId>org.mybatis.spring.boot</groupId>
  37.  
    <artifactId>mybatis-spring-boot-starter</artifactId>
  38.  
    <version>1.1.1</version>
  39.  
    </dependency>
  40.  
     
  41.  
    <!-- &lt;!&ndash;连接数据库 可以写,可以写在父类,建议写在当前子类 注意版本号 &ndash;&gt;-->
  42.  
    <dependency>
  43.  
    <groupId>mysql</groupId>
  44.  
    <artifactId>mysql-connector-java</artifactId>
  45.  
    <version>8.0.13</version>
  46.  
    </dependency>
  47.  
     
  48.  
     
  49.  
    <!--jdbc连接配置-->
  50.  
    <dependency>
  51.  
    <groupId>org.springframework.boot</groupId>
  52.  
    <artifactId>spring-boot-starter-jdbc</artifactId>
  53.  
    </dependency>
  54.  
     
  55.  
    <dependency>
  56.  
    <groupId>com.alibaba</groupId>
  57.  
    <artifactId>druid</artifactId>
  58.  
    <version>1.1.10</version>
  59.  
    </dependency>
  60.  
     
  61.  
    <!--公共依赖-->
  62.  
    <dependency>
  63.  
    <groupId>com.jd</groupId>
  64.  
    <artifactId>spring-cloud-alibaba-common-store</artifactId>
  65.  
    <version>1.0-SNAPSHOT</version>
  66.  
    </dependency>
  67.  
     
  68.  
    <!--导入openfeign包-->
  69.  
    <dependency>
  70.  
    <groupId>org.springframework.cloud</groupId>
  71.  
    <artifactId>spring-cloud-starter-openfeign</artifactId>
  72.  
    </dependency>
  73.  
     
  74.  
    <!--Feign整合Sentinel熔断-->
  75.  
    <dependency>
  76.  
    <groupId>com.alibaba.cloud</groupId>
  77.  
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
  78.  
    </dependency>
  79.  
     
  80.  
     
  81.  
    <!--引入swagger支持-->
  82.  
    <dependency>
  83.  
    <groupId>io.springfox</groupId>
  84.  
    <artifactId>springfox-swagger2</artifactId>
  85.  
    <version>2.9.2</version>
  86.  
    </dependency>
  87.  
     
  88.  
    <dependency>
  89.  
    <groupId>io.springfox</groupId>
  90.  
    <artifactId>springfox-swagger-ui</artifactId>
  91.  
    <version>2.9.2</version>
  92.  
    </dependency>
  93.  
     
  94.  
    <!--引入swagger支持-->
  95.  
    <dependency>
  96.  
    <groupId>io.springfox</groupId>
  97.  
    <artifactId>springfox-swagger2</artifactId>
  98.  
    <version>2.9.2</version>
  99.  
    </dependency>
  100.  
     
  101.  
    <dependency>
  102.  
    <groupId>io.springfox</groupId>
  103.  
    <artifactId>springfox-swagger-ui</artifactId>
  104.  
    <version>2.9.2</version>
  105.  
    </dependency>
  106.  
    </dependencies>

3.2 启动类基础:

  1.  
    package com.jd;
  2.  
    import org.mybatis.spring.annotation.MapperScan;
  3.  
    import org.springframework.boot.SpringApplication;
  4.  
    import org.springframework.boot.autoconfigure.SpringBootApplication;
  5.  
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  6.  
    import org.springframework.cloud.openfeign.EnableFeignClients;
  7.  
     
  8.  
    @SpringBootApplication
  9.  
    //注册客户端
  10.  
    @EnableDiscoveryClient
  11.  
    @MapperScan("com.jd.mapper")
  12.  
    @EnableFeignClients
  13.  
    public class StoreStarter {
  14.  
    public static void main(String[] args) {
  15.  
     
  16.  
    SpringApplication.run(StoreStarter.class);
  17.  
    }
  18.  
    }

3.3 yml:

application:

  1.  
    #端口号
  2.  
    server:
  3.  
    port: 10030
  4.  
    spring:
  5.  
    #服务名
  6.  
    application:
  7.  
    name: service-store
  8.  
    cloud:
  9.  
    nacos:
  10.  
    server-addr: 127.0.0.1:8848 #注册中心地址
  11.  
    #Sentinel 依赖
  12.  
    sentinel:
  13.  
    transport:
  14.  
    dashboard: localhost:1111
  15.  
    #连接数据库
  16.  
    datasource:
  17.  
    url: jdbc:mysql://localhost:3306/jd-store?serverTimezone=GMT+8&characterEncoding=utf-8&useSSL=false
  18.  
    driver-class-name: com.mysql.cj.jdbc.Driver
  19.  
    username: root
  20.  
    password: java
  21.  
     
  22.  
    # 配置别名
  23.  
    mybatis:
  24.  
    type-aliases-package: com.jd.domain
  25.  
     
  26.  
    #配置sentinel:熔断
  27.  
    feign:
  28.  
    sentinel:
  29.  
    enabled: true #熔断
  30.  
     
  31.  
    #测试用的,不用管
  32.  
    temp:
  33.  
    notify: 2222
  34.  
     
  35.  
     
  36.  
     

bootstrap.yml:

  1.  
    server:
  2.  
    port: 10010
  3.  
    spring:
  4.  
    profiles:
  5.  
    active: dev
  6.  
    application:
  7.  
    name: store-server
  8.  
    cloud:
  9.  
    nacos:
  10.  
    discovery:
  11.  
    server-addr: localhost:8848 #注册中心
  12.  
    config:
  13.  
    server-addr: localhost:8848 #配置中心
  14.  
    file-extension: yaml #配置文件格式
  15.  
    prefix: application-store #配置前缀 ,默认使用sring.application.name
  16.  
    group: DEFAULT_GROUP #默认分组
  17.  
    namespace: b18ab1b5-485a-4d43-a4ff-fe7da3488dbb #命名空间的ID

3.4 xml:

  1.  
    <?xml version="1.0" encoding="UTF-8" ?>
  2.  
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  3.  
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  4.  
    <mapper namespace="com.jd.mapper.StoreMapper">
  5.  
     
  6.  
    <!-- Store findOneByStore(Long id) ;;-->
  7.  
    <select id="findOneByStore" resultType="com.jd.domain.Store">
  8.  
    select * from t_store
  9.  
    where id = #{id};
  10.  
    </select>
  11.  
     
  12.  
    <!--保存对象-->
  13.  
    <!-- void insert(Store store);-->
  14.  
    <insert id="insert" parameterType="com.jd.domain.Store">
  15.  
    insert into t_store(
  16.  
    productId,
  17.  
    number
  18.  
    ) values(
  19.  
    #{productId},
  20.  
    #{number})
  21.  
    </insert>
  22.  
     
  23.  
     
  24.  
    </mapper>

3.5 controller:

  1.  
    package com.jd.controller;
  2.  
    import com.jd.domain.Store;
  3.  
    import com.jd.feigh.ProductFeign;
  4.  
    import com.jd.server.IStoreServer;
  5.  
    import org.springframework.beans.factory.annotation.Autowired;
  6.  
    import org.springframework.web.bind.annotation.*;
  7.  
     
  8.  
    @RestController
  9.  
    @RequestMapping("/store")
  10.  
    public class StoreController {
  11.  
     
  12.  
    @Autowired
  13.  
    private IStoreServer storeServer;
  14.  
    @Autowired
  15.  
    private ProductFeign productFeign;
  16.  
     
  17.  
    @GetMapping("/{id}")
  18.  
    public Store findOneByStore(@PathVariable Long id){
  19.  
    Store store = storeServer.findOneByStore(id);
  20.  
     
  21.  
     
  22.  
     
  23.  
    return store;
  24.  
    }
  25.  
    @PostMapping("/add")
  26.  
    public void insert(@RequestBody Store store){
  27.  
    storeServer.insert(store);
  28.  
    }
  29.  
     
  30.  
    @PostMapping("/insert")
  31.  
    public void insertNumber(@RequestBody Store store){
  32.  
    Integer id = productFeign.insert(store.getProduct());
  33.  
    store.setId(id);
  34.  
    storeServer.insert(store);
  35.  
     
  36.  
    }
  37.  
    }

4、其他server 、domain mapper省略

4、spring-cloud-alibaba-product 商品模块:

1、基础依赖:

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <project xmlns="http://maven.apache.org/POM/4.0.0"
  3.  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.  
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5.  
    <parent>
  6.  
    <artifactId>spring-cloud-alibaba-test</artifactId>
  7.  
    <groupId>com.jd</groupId>
  8.  
    <version>1.0-SNAPSHOT</version>
  9.  
    </parent>
  10.  
    <modelVersion>4.0.0</modelVersion>
  11.  
     
  12.  
    <artifactId>spring-cloud-alibaba-product</artifactId>
  13.  
     
  14.  
    <dependencies>
  15.  
    <!--springbootnacos依赖-->
  16.  
    <dependency>
  17.  
    <groupId>com.alibaba.cloud </groupId>
  18.  
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  19.  
    </dependency>
  20.  
     
  21.  
    <!--加入WEB依赖是为了方便后面写Controller-->
  22.  
    <dependency>
  23.  
    <groupId>org.springframework.boot</groupId>
  24.  
    <artifactId>spring-boot-starter-web</artifactId>
  25.  
    </dependency>
  26.  
     
  27.  
    <!-- 配置中心客户端-->
  28.  
    <dependency>
  29.  
    <groupId>com.alibaba.cloud</groupId>
  30.  
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
  31.  
    </dependency>
  32.  
     
  33.  
    <!-- &lt;!&ndash;客户端接入导入sentinel依赖&ndash;&gt;-->
  34.  
    <!-- <dependency>-->
  35.  
    <!-- <groupId>com.alibaba.cloud</groupId>-->
  36.  
    <!-- <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>-->
  37.  
    <!-- </dependency>-->
  38.  
     
  39.  
    <!-- &lt;!&ndash;Sentinel和Nacos做持久的&ndash;&gt;-->
  40.  
    <!-- <dependency>-->
  41.  
    <!-- <groupId>com.alibaba.csp</groupId>-->
  42.  
    <!-- <artifactId>sentinel-datasource-nacos</artifactId>-->
  43.  
    <!-- <version>1.5.2</version>-->
  44.  
    <!-- </dependency>-->
  45.  
     
  46.  
     
  47.  
    <!--,在SpringBoot启动时,去扫描所有Mapper接口,然后为其增加一个代理实现类,在调用的过程中,
  48.  
    我们实际调用的是这个代理对象 可以写,可以写在父类,建议写在当前子类-->
  49.  
    <dependency>
  50.  
    <groupId>org.mybatis.spring.boot</groupId>
  51.  
    <artifactId>mybatis-spring-boot-starter</artifactId>
  52.  
    <version>1.1.1</version>
  53.  
    </dependency>
  54.  
     
  55.  
    <dependency>
  56.  
    <groupId>org.springframework.boot</groupId>
  57.  
    <artifactId>spring-boot-starter-jdbc</artifactId>
  58.  
    </dependency>
  59.  
     
  60.  
    <!--连接数据库 可以写,可以写在父类,建议写在当前子类 注意版本号 -->
  61.  
    <dependency>
  62.  
    <groupId>mysql</groupId>
  63.  
    <artifactId>mysql-connector-java</artifactId>
  64.  
    <version>8.0.13</version>
  65.  
    </dependency>
  66.  
     
  67.  
     
  68.  
    <dependency>
  69.  
    <groupId>com.alibaba</groupId>
  70.  
    <artifactId>druid</artifactId>
  71.  
    <version>1.1.10</version>
  72.  
    </dependency>
  73.  
     
  74.  
    <!--导入openfeign包-->
  75.  
    <dependency>
  76.  
    <groupId>org.springframework.cloud</groupId>
  77.  
    <artifactId>spring-cloud-starter-openfeign</artifactId>
  78.  
    </dependency>
  79.  
     
  80.  
    <!--Feign整合Sentinel熔断-->
  81.  
    <dependency>
  82.  
    <groupId>com.alibaba.cloud</groupId>
  83.  
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
  84.  
    </dependency>
  85.  
     
  86.  
    <!--公共依赖-->
  87.  
    <dependency>
  88.  
    <groupId>com.jd</groupId>
  89.  
    <artifactId>spring-cloud-alibaba-common-store</artifactId>
  90.  
    <version>1.0-SNAPSHOT</version>
  91.  
    </dependency>
  92.  
     
  93.  
     
  94.  
    <!--引入swagger支持-->
  95.  
    <dependency>
  96.  
    <groupId>io.springfox</groupId>
  97.  
    <artifactId>springfox-swagger2</artifactId>
  98.  
    <version>2.9.2</version>
  99.  
    </dependency>
  100.  
     
  101.  
    <dependency>
  102.  
    <groupId>io.springfox</groupId>
  103.  
    <artifactId>springfox-swagger-ui</artifactId>
  104.  
    <version>2.9.2</version>
  105.  
    </dependency>
  106.  
    </dependencies>
  107.  
    </project>

2、启动类:

  1.  
    package com.jd;
  2.  
    import org.mybatis.spring.annotation.MapperScan;
  3.  
    import org.springframework.boot.SpringApplication;
  4.  
    import org.springframework.boot.autoconfigure.SpringBootApplication;
  5.  
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  6.  
    import org.springframework.cloud.openfeign.EnableFeignClients;
  7.  
     
  8.  
    @SpringBootApplication
  9.  
    //注册客户端
  10.  
    @EnableDiscoveryClient
  11.  
    @EnableFeignClients
  12.  
    @MapperScan("com.jd.mapper")
  13.  
    public class ProductStarter {
  14.  
    public static void main(String[] args) {
  15.  
    SpringApplication.run(ProductStarter.class);
  16.  
    }
  17.  
    }

3、yaml:

application.yaml:

  1.  
    #端口号
  2.  
    server:
  3.  
    port: 10020
  4.  
    spring:
  5.  
    main:
  6.  
    allow-bean-definition-overriding: true
  7.  
    #服务名
  8.  
    application:
  9.  
    name: service-product
  10.  
    cloud:
  11.  
    nacos:
  12.  
    server-addr: 127.0.0.1:8848 #注册中心地址
  13.  
    #Sentinel 依赖
  14.  
    sentinel:
  15.  
    transport:
  16.  
    dashboard: localhost:1111
  17.  
    #连接数据库
  18.  
    datasource:
  19.  
    url: jdbc:mysql://localhost:3306/jd-product?serverTimezone=GMT+8&characterEncoding=utf-8&useSSL=false
  20.  
    driver-class-name: com.mysql.cj.jdbc.Driver
  21.  
    username: root
  22.  
    password: java
  23.  
     
  24.  
     
  25.  
     
  26.  
    # 配置别名
  27.  
    mybatis:
  28.  
    type-aliases-package: com.jd.domain
  29.  
     
  30.  
    #配置sentinel:熔断
  31.  
    feign:
  32.  
    sentinel:
  33.  
    enabled: true #熔断
  34.  
     
  35.  
    #测试用的,不用管
  36.  
    temp:
  37.  
    notify: 2222
  38.  
     
  39.  
     
  40.  
     
  41.  
     

bootstrap.yml:

  1.  
    server:
  2.  
    port: 10020
  3.  
    spring:
  4.  
    profiles:
  5.  
    active: dev
  6.  
    application:
  7.  
    name: product-server
  8.  
    cloud:
  9.  
    nacos:
  10.  
    discovery:
  11.  
    server-addr: localhost:8848 #注册中心
  12.  
    config:
  13.  
    server-addr: localhost:8848 #配置中心
  14.  
    file-extension: yaml #配置文件格式
  15.  
    prefix: application-product #配置前缀 ,默认使用sring.application.name
  16.  
    group: DEFAULT_GROUP #默认分组
  17.  
    namespace: b18ab1b5-485a-4d43-a4ff-fe7da3488dbb #命名空间的ID

4、xm:

ProductMapper.xml:

  1.  
    <?xml version="1.0" encoding="UTF-8" ?>
  2.  
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  3.  
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  4.  
    <mapper namespace="com.jd.mapper.ProductMapper">
  5.  
     
  6.  
    <!-- Product findOneByProduct(Long id)-->
  7.  
    <select id="findOneByProduct" resultMap="ProductMap">
  8.  
    select p.*,s.id cid,s.number
  9.  
    from `jd-product`.t_product p
  10.  
    left join `jd-store`.t_store s
  11.  
    on p.id = s.productId
  12.  
    where p.id = #{id}
  13.  
    </select>
  14.  
    <resultMap id="ProductMap" type="com.jd.domain.Product">
  15.  
    <id column="id" property="id" />
  16.  
    <result column="name" property="name"/>
  17.  
    <result column="price" property="price"/>
  18.  
     
  19.  
    <!-- private Store store;;-->
  20.  
    <association property="store" javaType="com.jd.domain.Store">
  21.  
    <id column="cid" property="id" />
  22.  
    <result column="number" property="number"/>
  23.  
    </association>
  24.  
    </resultMap>
  25.  
     
  26.  
    <!--保存对象-->
  27.  
    <!-- Product insert(Product product);;-->
  28.  
    <insert id="insert" parameterType="com.jd.domain.Product" >
  29.  
    insert into t_product(
  30.  
    name,
  31.  
    price
  32.  
    ) values(
  33.  
    #{name},
  34.  
    #{price})
  35.  
    </insert>
  36.  
     
  37.  
    <!-- <insert id="insert" parameterType="com.jd.domain.Product">-->
  38.  
    <!-- &lt;!&ndash;AFTER :insert 先执行. &ndash;&gt;-->
  39.  
    <!-- <selectKey order="AFTER" keyProperty="id" resultType="int">-->
  40.  
    <!-- &lt;!&ndash; 获取最后一次执行insert语句的 key &ndash;&gt;-->
  41.  
    <!-- select LAST_INSERT_ID()-->
  42.  
    <!-- </selectKey>-->
  43.  
    <!-- insert into `jd-store`.t_store(id, name, gender, birthday, phone, idCard)-->
  44.  
    <!-- values(#{id}, #{name}, #{gender},#{birthday},#{phone},#{idcard})-->
  45.  
    <!-- </insert>-->
  46.  
     
  47.  
     
  48.  
     
  49.  
    <!-- &lt;!&ndash;删除对象&ndash;&gt;-->
  50.  
    <!-- <delete id="delete" parameterType="long">-->
  51.  
    <!-- delete from t_user where id = #{id}-->
  52.  
    <!-- </delete>-->
  53.  
     
  54.  
    <!-- &lt;!&ndash;更新对象&ndash;&gt;-->
  55.  
    <!-- <update id="update" parameterType="User">-->
  56.  
    <!-- update t_user-->
  57.  
    <!-- set-->
  58.  
     
  59.  
    <!-- username = #{username},-->
  60.  
    <!-- phone = #{phone},-->
  61.  
    <!-- email = #{email},-->
  62.  
    <!-- salt = #{salt},-->
  63.  
    <!-- password = #{password},-->
  64.  
    <!-- state = #{state},-->
  65.  
    <!-- age = #{age},-->
  66.  
    <!-- createtime = #{createtime},-->
  67.  
    <!-- headImg = #{headImg},-->
  68.  
    <!-- logininfo_id = #{logininfoId} where id = #{id}-->
  69.  
    <!-- </update>-->
  70.  
     
  71.  
    </mapper>

5、controller:

  1.  
    package com.jd.controller;
  2.  
    import com.jd.domain.Product;
  3.  
    import com.jd.domain.Store;
  4.  
    import com.jd.feigh.StoreFeign;
  5.  
    import com.jd.feigh.StoreInsertFeign;
  6.  
    import com.jd.server.IProductServer;
  7.  
    import org.springframework.beans.factory.annotation.Autowired;
  8.  
    import org.springframework.web.bind.annotation.*;
  9.  
     
  10.  
    @RestController
  11.  
    @RequestMapping("/product")
  12.  
    public class ProductController {
  13.  
     
  14.  
    @Autowired
  15.  
    private IProductServer productServer;
  16.  
     
  17.  
    @Autowired
  18.  
    private StoreFeign storeFeign;
  19.  
     
  20.  
     
  21.  
    @Autowired
  22.  
    private StoreInsertFeign storeInsertFeign;
  23.  
     
  24.  
     
  25.  
    @GetMapping("/{id}")
  26.  
    public Product findOneByStore(@PathVariable Long id){
  27.  
    Product product = productServer.findOneByProduct(id);
  28.  
    Store oneByStore = storeFeign.findOneByStore(id);
  29.  
    product.setStore(oneByStore);
  30.  
    return product;
  31.  
     
  32.  
    }
  33.  
     
  34.  
    @PostMapping("/add")
  35.  
    public void insert(@RequestBody Product product ){
  36.  
    productServer.insert(product);
  37.  
    Integer productId = product.getId();
  38.  
    Integer number = product.getNumber();
  39.  
     
  40.  
    Store store = new Store();
  41.  
    store.setProductId(productId);
  42.  
    store.setNumber(number);
  43.  
     
  44.  
    storeInsertFeign.insert(store);
  45.  
     
  46.  
    }
  47.  
     
  48.  
    }

对于服务之间的调用当然是通过rpc调用,要比我们自己手动拼接url的好:

Spring Cloud Alibaba整合了Feign,使用Fegin实现服务之间的调用,默认集成Ribbon,可以实现负载均衡。

 什么是Feign

Feign是一个声明式的http客户端,使用Feign可以实现声明式REST调用,它的目的就是让Web Service调用更加简单。Feign整合了Ribbon和SpringMvc注解,这让Feign的客户端接口看起来就像一个Controller。Feign提供了HTTP请求的模板,通过编写简单的接口和插入注解,就可以定义好HTTP请求的参数、格式、地址等信息。而Feign则会完全代理HTTP请求,我们只需要像调用方法一样调用它就可以完成服务请求及相关处理。同时Feign整合了Hystrix,可以很容易的实现服务熔断和降级 。


Nacos很好的兼容了Feign,Feign默认集成了Ribbon,所以在Nacos下使用Fegin默认实现负载均衡的效果。

Fegin原理:

学新通

本项目实战为例带你快速了解Fegin基本使用:

分析需求:需要通过Feign调用库存服务把商品的数量保存到库存表。

1、导包:

  1.  
     
  2.  
    <!--导入openfeign包-->
  3.  
    <dependency>
  4.  
    <groupId>org.springframework.cloud</groupId>
  5.  
    <artifactId>spring-cloud-starter-openfeign</artifactId>
  6.  
    </dependency>
  7.  
     
  8.  
    <!--Feign整合Sentinel熔断-->
  9.  
    <dependency>
  10.  
    <groupId>com.alibaba.cloud</groupId>
  11.  
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
  12.  
    </dependency>

2、启动类加上:

开启fegin客户端

@EnableFeignClients

3、构建一个Fegin类

  1.  
    package com.jd.feigh;
  2.  
    import com.jd.domain.Store;
  3.  
    import org.springframework.cloud.openfeign.FeignClient;
  4.  
    import org.springframework.web.bind.annotation.PostMapping;
  5.  
    import org.springframework.web.bind.annotation.RequestBody;
  6.  
    @FeignClient(value = "store-server",fallbackFactory = ProductFeignStoreFactoryInsert.class)
  7.  
    public interface StoreInsertFeign {
  8.  
    @PostMapping("/store/add")
  9.  
    void insert(@RequestBody Store store);
  10.  
    }

4、降级处理:

  1.  
    package com.jd.feigh;
  2.  
    import com.jd.domain.Store;
  3.  
    import feign.hystrix.FallbackFactory;
  4.  
    import org.springframework.stereotype.Component;
  5.  
     
  6.  
    @Component
  7.  
    public class ProductFeignStoreFactoryInsert implements FallbackFactory<StoreInsertFeign> {
  8.  
    @Override
  9.  
    public StoreInsertFeign create(Throwable e) {
  10.  
    return new StoreInsertFeign() {
  11.  
    @Override
  12.  
    public void insert(Store store) {
  13.  
    e.printStackTrace();
  14.  
    System.out.println(new Store(-1,-2,-3,null));
  15.  
    }
  16.  
    };
  17.  
    }
  18.  
    }

4、业务代码思路:在商品构建一个  number字段接受前段参数,以商品id为productId保存到库存中,这样两个表添加时,使用一个id。(由于业务很少,就在Controller处理结果,本文章主要带大家了解springcloudalibb微服务代建以及Fegin基本使用

代码:

  1.  
    @PostMapping("/add")
  2.  
    public void insert(@RequestBody Product product ){
  3.  
    productServer.insert(product);
  4.  
    Integer productId = product.getId();
  5.  
    Integer number = product.getNumber();
  6.  
     
  7.  
    Store store = new Store();
  8.  
    store.setProductId(productId);
  9.  
    store.setNumber(number);
  10.  
     
  11.  
    storeInsertFeign.insert(store);
  12.  
     
  13.  
     
  14.  
    }

注意事项:Feign接口的参数是一个对象的时候需要使用:@RequestBody 标签

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgbhaef
系列文章
更多 icon
同类精品
更多 icon
继续加载