springcloud分布式微服务架构(一)

什么是分布式/微服务?
从分布式/微服务的角度而言:就是把我们一大的项目,分解成多个小的模块。这些小的模块组合起来,完成功能。

EurekaServer服务端安装、Eureka是什么
Eureka 是 Netflix 开发的,一个基于 REST 服务的,服务注册与发现的组件,以实现中间层服务器的负载平衡和故障转移。

它主要包括两个组件:Eureka Server 和 Eureka Client

Eureka Client:一个Java客户端,用于简化与 Eureka Server 的交互(通常就是微服务中的客户端和服务端)

Eureka Server:提供服务注册和发现的能力(通常就是微服务中的注册中心)

举个例子:
EurekaServer相当于学校进行管理
服务的提供者→ 老师
服务的消费者→ 学生 听课


话不多收,开始创建一个SpringCloud Demo

先建立项目基础父空间,用来容纳各个微服务模块。

  • 略过基础部分,直接从集群项目开始

①建立父空间
springcloud分布式微服务架构(一)_第1张图片
父空间设置pom,删掉其他东西只留pom.xml文件

  <!--子模块继承之后,提供作用:锁定版本+子module不用groupId和version-->
  <dependencyManagement>
    <dependencies>
      <!--spring boot 2.2.2-->
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>2.2.2.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <!--spring cloud Hoxton.SR1-->
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Hoxton.SR1</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <!--spring cloud alibaba 2.1.0.RELEASE-->
      <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-alibaba-dependencies</artifactId>
        <version>2.1.0.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql.version}</version>
      </dependency>
      <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>${druid.version}</version>
      </dependency>
      <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>${mybatis.spring.boot.version}</version>
      </dependency>
      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
      </dependency>
      <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>${lombok.version}</version>
        <optional>true</optional>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
          <fork>true</fork>
          <addResources>true</addResources>
        </configuration>
      </plugin>
    </plugins>
  </build>


创建实体类模块cloud-api-commons

设置pom

<artifactId>cloud-api-commons</artifactId>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>cn.hutool</groupId>
        <artifactId>hutool-all</artifactId>
        <version>5.1.0</version>
    </dependency>
</dependencies>

创建实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment implements Serializable {
     
        private Long id;
        private String serial;

}

创建服务器注册子模块7001

<artifactId>cloud-eureka-server7001</artifactId>


<dependencies>
    <!--eureka-client-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    <!--引入自己定义的api通用包,可以使用Payment支付Entity-->   //***这里设置了就可以用公用实体类***
    <dependency>
        <groupId>com.atguigu.springcloud</groupId>
        <artifactId>cloud-api-commons</artifactId>
        <version>${project.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>

</dependencies>
*设置yml*
server:
  port: 7001

eureka:
  instance:
    hostname: eureka7001.com
  client:
    #表示是否将自己注册进EurekaServer默认为true
    register-with-eureka: false
    #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetch-registry: false
    service-url:
      #defaultZone: http://eureka7001.com:7001/eureka  #单机
      defaultZone: http://eureka7002.com:7002/eureka    #集群互相注册,这里地址用第二个注册中心的地址

*设置主启动类

@SpringBootApplication
@EnableEurekaServer//开启EurekaServer
public class EurekaApplicatin7001 {
     
    public static void main(String[] args) {
     
        SpringApplication.run(EurekaApplicatin7001.class, args);
    }
}

创建服务器注册子模块7002

*设置主启动类

@SpringBootApplication
@EnableEurekaServer//开启EurekaServer
public class EurekaApplicatin7002 {
     
    public static void main(String[] args) {
     
        SpringApplication.run(EurekaApplicatin7001.class, args);
    }
}
server:
  port: 7002

eureka:
  instance:
    hostname: eureka7002.com
  client:
    #表示是否将自己注册进EurekaServer默认为true
    register-with-eureka: false
    #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetch-registry: false
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka  #集群互相注册,这里地址用第一个注册中心的地址

创建服务提供模块8001

   <dependencies>
        <!--eureka-server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <!--mysql-connector-java-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

设置YML

server:
  port: 8001

spring:
  application:
    name: cloud-provider-service
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource      #当前数据源操作类型
    driver-class-name: com.mysql.jdbc.Driver        #mysql驱动包
    url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding-utr-8&useSSL=false
    username: root
    password: xxxx

mybatis:
    mapper-locations: classpath:mapper/*.xml
    type-aliases-package: com.miro.springcloud.entity       #所有Entity别名类所在包

eureka:
  client:
    #表示是否将自己注册进EurekaServer默认为true
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetch-registry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka

主启动类添加注解@EnableEurekaClient

@SpringBootApplication
@EnableEurekaClient
public class PaymentMain8001 {
     
    public static void main(String[] args) {
     
        SpringApplication.run(PaymentMain8001.class,args);
    }

}

创建数据库

springcloud分布式微服务架构(一)_第2张图片
创建Dao层

@Mapper
public interface PaymentDao {
     


        public int create(Payment payment);

        public Payment getPaymentById(@Param("id") Long id);

}

Serviceimpl

@Service
public class PaymentServiceImpl implements PaymentService{
     

    @Resource
    private PaymentDao paymentDao;

    public int create(Payment payment){
     
        return paymentDao.create(payment);
    }
    public Payment getPaymentById(Long id){
     
        return paymentDao.getPaymentById(id);
    }
}

PaymentService

public interface PaymentService {
     
    
    public int create(Payment payment);

    public Payment getPaymentById(@Param("id") Long id);
}

mapper.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.miro.springcloud.dao.PaymentDao">

    <insert id="create" parameterType="Payment" useGeneratedKeys="true" keyProperty="id">
        insert into payment(serial) values(#{serial})
    </insert>

    <resultMap id="BaseResultMap" type="com.miro.springcloud.entity.Payment">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="serial" property="serial" jdbcType="VARCHAR"/>
    </resultMap>
    <select id="getPaymentById" parameterType="Long" resultMap="BaseResultMap">
        select * from payment where id=#{id};
    </select>


</mapper>

Controller层

@RestController
@Slf4j
public class OrderController {
     
    //private static final String PAYMENT_URL="http://localhost:8001";
    //通过在eureka上注册过的微服务名称调用
    private static final String PAYMENT_URL = "http://CLOUD-PROVIDER-SERVICE";

    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/consumer/payment/create")
    public CommonResult<Payment> create(Payment payment){
     
        return restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment,CommonResult.class);
    }

    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult<Payment> getPayment(@PathVariable("id") Long id){
     
        return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);
    }
}

创建服务提供模块8002

其它的与8001设置一样
YML设置

server:
  port: 8002

spring:
  application:
    name: cloud-provider-service
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource      #当前数据源操作类型
    driver-class-name: com.mysql.jdbc.Driver        #mysql驱动包
    url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding-utr-8&useSSL=false
    username: root
    password: admin

mybatis:
    mapper-locations: classpath:mapper/*.xml
    type-aliases-package: com.miro.springcloud.entity       #所有Entity别名类所在包

eureka:
  client:
    #表示是否将自己注册进EurekaServer默认为true
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetch-registry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka

创建消费模块80

YML设置

<dependencies>
    <!--eureka-client-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>

    <!--引入自己定义的api通用包,可以使用Payment支付Entity-->
    <dependency>
        <groupId>com.atguigu.springcloud</groupId>
        <artifactId>cloud-api-commons</artifactId>
        <version>${project.version}</version>
    </dependency>


    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

Controller层

@RestController
@Slf4j
public class OrderController {
     

    private static final String PAYMENT_URL="http://CLOUD-PROVIDER-SERVICE";

    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/consumer/payment/create")
    public CommonResult<Payment> create(Payment payment){
     
        return restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment,CommonResult.class);
    }

    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult<Payment> getPayment(@PathVariable("id") Long id){
     
        return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);
    }
}

#LoadBalanced注解开启RestTemplate负载均衡功能。

@Configuration
public class ApplicationContextConfig {
     
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
     
        return new RestTemplate();
    }
}

启动类

@SpringBootApplication
@EnableEurekaClient
//@RibbonClient(name ="CLOUD-PROVIDER-SERVICE",configuration = MySelfRule.class)
public class OrderMain80 {
     
    public static void main(String[] args) {
     
        SpringApplication.run(OrderMain80.class,args);
    }
}

测试

当微服务器多的时候可以在
启动顺序:7001、7002、8001、8002、80
springcloud分布式微服务架构(一)_第3张图片
springcloud分布式微服务架构(一)_第4张图片

  • 测试结果
    port:8001 和port:8002 循环
    springcloud分布式微服务架构(一)_第5张图片springcloud分布式微服务架构(一)_第6张图片
    未完待续…(#.#)

你可能感兴趣的:(springcloud分布式微服务架构(一))