微服务之Rest构建分布式微服务&服务调用Rest和Dubbo(集群版Eureka Server服务注册中心

Spring Cloud:

  • 在SpringBoot基础上提供了一套微服务的解决方案,包括注册、发现、配置中心服务网关、负载均衡、熔断器,是一系列框架的集合,利用springBoot 的开发便利性巧妙的便利简化了分布式开发。比如发现注册、配置总线 ;
  • 在Spring Cloud微服务的实际项目开发中,多个微服务之间不仅是相对独立的,而且也是相对关联的;也就是说,微服务之间需要相互访问,多个微服务之间的接口可能会被互相多次调用,我们称之为微服务;
  • 服务之间的接口实现相互调用,即服务的调用:RPC、Rest;
  • 常规的微服务有两种类型:一种是基于dubbo的微服务架构、另外一种是基于Spring Cloud的微服务架构。Dubbo是一个RPC框架,而Rest可以实现接口相互调用。也就是说,rest与dobbo是服务调用,eureka和zookeeper是服务注册与发现,rest中的服务注册和发现的功能是用eureka来实现的;dubbo 中服务注册和发现的功能是用 zookeeper 来实现的。

Spring Boot 和 Spring Cloud关系:

  • Spring Boot是Spring的一套快速配置,Spring Colud是一个基于Spring Boot实现的云应用开发;
  • Spring Boot 专注于单个个体服务开发,Spring Cloud 关注于全局的部署和开发;
  • Spring Cloud离不开Spring Boot

(目前Spring Cloud 集合21种分布式架构技术

Rest构建分布式微服务实战Demo

项目需求:以商品管理模块,做一个微服务架构的通用案例消费者Consumer(Client),通过Rest调用提供者Provider(Server) 提供商品的管理服务


Eureka服务的注册和发现:是Spring Cloud 提供服务中心来管理服务信息

为什么需要注册中心: 要进行远程调用就需要知道服务端的ip和端口,而注册中心帮助我们管理这些服务端口和ip

Spring Cloud Eureka 是Netfix公司对Eureka的二次封装,实现了服务治理功能,提供EurekaServer、EurekaClient,服务端即是服务注册中心,客户端完成微服务向Eureka服务的注册和发现
 


准备工作

1.开发环境:Spring Boot2.0.7版本+Spring Cloud的版本为Finchley SR2

2.修改Maven配置文件,在apache-maven-3.5.2\conf\settings.xml中

  在<mirrors></mirrors>标签中添加:

        <mirror>
		<id>alimaven</id>
		<mirrorOf>central</mirrorOf>
		<name>aliyun maven</name>
		<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
	</mirror>

  在<profiles></profiles>标签中添加:

    <!--在profiles 标签下指定jdk 版本-->
	<profile>

		<id>jdk-1.8</id>

		<activation>

			<activeByDefault>true</activeByDefault>

			<jdk>1.8</jdk>

		</activation>

		<properties>

			<maven.compiler.source>1.8</maven.compiler.source>

			<maven.compiler.target>1.8</maven.compiler.target>

			<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>

		</properties>

	</profile>

3.Maven的分模块架构

  1. 父工程
  2. 公共模块(子工程)
  3. 服务提供者(子工程)
  4. 服务消费者(子工程)
  5. 集群版Eureka Server 服务注册中心(子工程,两个)

注:搭建集群版Eureka Server 服务注册中心要修改C:\Windows\System32\drivers\etc下的hosts,因为要集群两个,所以添加连个eureka服务端的实例

127.0.0.1 eureka6001.com
127.0.0.1 eureka6002.com

项目预览


基本步骤

构建父工程(打包方式pom)

构建公共模块(子工程)

构建服务提供者(子工程,打包方式jar)

  1.         pom.xml文件引入依赖
  2. ​        配置application.yml
  3. ​        数据库脚本
  4. ​        创建Mapper接口
  5. ​        Mybatis的配置
  6. ​        创建服务层
  7. ​        控制层---服务者
  8. ​        创建主启动类
  9. ​        功能测试

构建服务消费者(子工程,打包方式jar)

  1. ​        创建module
  2.         pom.xml文件引入依赖
  3. ​        配置application.yml
  4. ​        自定义Rest相关配置类
  5. ​        创建控制层---消费者
  6. ​        主启动类
  7. ​        功能测试

搭建集群版Eureka Server 服务注册中心(子工程,打包方式jar,集群以两个为例)

  1.         创建module
  2.         pom.xml文件引入依赖
  3.         配置application.yml
  4.         主启动类
  5.         测试

实现细节

0.创建数据库、数据表

1.构建microService-parent父工程,pom.xml引入依赖

    <!--打包方式pom-->
    <packaging>pom</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.7.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <junit.version>4.12</junit.version>

        <spring-cloud.version>Finchley.SR2</spring-cloud.version>
    </properties>


    <!--依赖声明-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <!--maven不支持多继承,使用import来依赖管理配置-->
                <scope>import</scope>
            </dependency>
            <!--导入 mybatis 启动器-->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>1.3.2</version>
            </dependency>
            <!--druid数据源-->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.1.12</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.13</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

2.构建microService-api公共模块,创建商品的实体类

package cn.zf.springcloud.entities;

import java.io.Serializable;

public class Product implements Serializable{
    private Long pid;
    private String productName;
    private String dbSource;

    public Product(String productName) {
        this.productName = productName;
    }

    public Product(Long pid, String productName, String dbSource) {
        this.pid = pid;
        this.productName = productName;
        this.dbSource = dbSource;
    }

    public Product() {
    }

    public Long getPid() {
        return pid;
    }

    public void setPid(Long pid) {
        this.pid = pid;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public String getDbSource() {
        return dbSource;
    }

    public void setDbSource(String dbSource) {
        this.dbSource = dbSource;
    }
}

3.构建microService-provider服务提供者,pom.xml文件引入依赖,在src/main/resources下创建application.yml配置文件,在src/main/resources下创建mybatis目录并引入mybatis.cfg.xml配置文件,在src/main/resources/mybatis下创建mapper目录并引入productMapper.xml配置文件,创建mapper接口、service服务层、controller控制层、ProductProvider_8001主启动类

pom.xml文件引入依赖

    <dependencies>
        <!--依赖关系-->
        <dependency>
            <groupId>cn.zf</groupId>
            <artifactId>microService-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--添加Eureka客户端的依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--springboot web启动器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- mybatis 启动器-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
        </dependency>

    </dependencies>

在src/main/resources下创建application.yml配置文件

server:
  port: 8001  # 设置端口号
mybatis:
  config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路径
  type-aliases-package: cn.zf.springcloud.entities # 所有Entity别名类所在包
  mapper-locations: classpath:mybatis/mapper/**/*.xml # mapper映射文件
spring:
  application:
    name: microservice-product
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
    driver-class-name: com.mysql.cj.jdbc.Driver # mysql驱动包
    # springcloud_db01数据库名
    url: jdbc:mysql://127.0.0.1:3306/springcloud_db01?serverTimezone=GMT%2B8
    username: root  # 数据库用户名
    password: 123456  # 数据库密码
    dbcp2:
      min-idle: 5 # 数据库连接池的最小维持连接数
      initial-size: 5 # 初始化连接数
      max-total: 5 # 最大连接数
      max-wait-millis: 150 # 等待连接获取的最大超时时间
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    serviceUrl:     # 注册到哪个服务中心
      defaultZone: http://eureka6001.com:6001/eureka,http://eureka6002.com:6002/eureka
  instance:
    instanceId: ${spring.application.name}:${server.port} # 在注册中心隐藏主机名
    prefer-ip-address: true # 在注册中心隐藏主机名

在src/main/resources下创建mybatis目录并引入mybatis.cfg.xml配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!--开启驼峰命名-->
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
</configuration>

在src/main/resources/mybatis下创建mapper目录并引入productMapper.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="cn.zf.springcloud.mapper.ProductMapper">
    <!--
        id="findById"   findById是方法名
        resultType="Product"    SQL执行后返回的数据类型
        parameterType="Long"    参数类型
        -->
    <select id="findById" resultType="Product" parameterType="Long">
        select pid,product_name,db_source from product where pid=#{pid}
    </select>
    <select id="findAll" resultType="Product">
        select pid,product_name,db_source from product;
    </select>
    <insert id="addProduct" parameterType="Product">
        INSERT  INTO product(product_name,db_source) VALUES (#{productName},DATABASE())
    </insert>
</mapper>

创建mapper接口

package cn.zf.springcloud.mapper;

import cn.zf.springcloud.entities.Product;

import java.util.List;
/*
dao持久层接口
 */
public interface ProductMapper {
    Product findById(Long id);
    List<Product> findAll();
    boolean addProduct(Product product);
}

创建service服务层,接口、实现类

package cn.zf.springcloud.service;

import cn.zf.springcloud.entities.Product;

import java.util.List;

/*
service业务层接口
 */
public interface ProductService {
    boolean add(Product product);
    Product get(Long id);
    List<Product> list();
}
package cn.zf.springcloud.service.impl;

import cn.zf.springcloud.entities.Product;
import cn.zf.springcloud.mapper.ProductMapper;
import cn.zf.springcloud.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/*
service业务层实现类
 */
@Service
public class ProductServiceImpl implements ProductService {
    //注入dao层对象,调用dao层接口
    @Autowired
    private ProductMapper productMapper;
    @Override
    public boolean add(Product product) {
        return productMapper.addProduct(product);
    }

    @Override
    public Product get(Long id) {
        return productMapper.findById(id);
    }

    @Override
    public List<Product> list() {
        return productMapper.findAll();
    }
}

创建controller控制层

package cn.zf.springcloud.controller;

import cn.zf.springcloud.entities.Product;
import cn.zf.springcloud.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@RestController响应数据转json
@RestController
public class ProductController {
    //注入service层的对象,调用service层的方法
    @Autowired
    private ProductService productService;
    @RequestMapping(value = "/product/add",method = RequestMethod.POST)
    //@RequestBody将传过来的json数据转java中的类型数据
    public boolean add(@RequestBody Product product){
        return productService.add(product);
    }
    @RequestMapping(value = "/product/get/{id}",method = RequestMethod.GET)
    //@PathVariable获取路径中的参数
    public Product get(@PathVariable("id") Long id){
        return productService.get(id);
    }
    @RequestMapping(value = "/product/list",method = RequestMethod.GET)
    public List<Product> list(){
        return productService.list();
    }
}

创建ProductProvider_8001主启动类

package cn.zf.springcloud;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@EnableEurekaClient
//@MapperScan指定要扫描的Mapper类的包的路径
@MapperScan("cn.zf.springcloud.mapper")
@SpringBootApplication
public class ProductProvider_8001 {
    public static void main(String[] args) {
        SpringApplication.run(ProductProvider_8001.class,args);
    }
}

4.构建microService-consumer服务提供者,pom.xml文件引入依赖,在src/main/resources下创建application.yml配置文件,自定义Rest相关配置类,创建controller控制层、ProductConsumer_80主启动类

pom.xml文件引入依赖

    <dependencies>
        <!--依赖关系-->
        <dependency>
            <groupId>cn.zf</groupId>
            <artifactId>microService-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

在src/main/resources下创建application.yml配置文件

server:
  port: 80

自定义Rest相关配置类

package cn.zf.springcloud.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 * 自定义Rest相关配置类
 *
 * Rest相关配置类访问RestFull的方法(url,requestMap,ResponseBean.class)
 * url  访问路径
 * requestMap   返回值类型
 * ResponseBean.class   http响应格式
 */
@Configuration
public class ConfigBean {
    // 向容器中添加RestTemplate组件  直接通过该组件可以调用REST接口
    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

创建controller控制层

package cn.zf.springcloud.controller;

import cn.zf.springcloud.entities.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class ProductController_Consumer {
    private static final String REST_URL_PREFIX="http://localhost:8001";
    @Autowired
    private RestTemplate restTemplate;
    @RequestMapping(value = "/consumer/product/add")
    public boolean add(Product product){
        return restTemplate.postForObject(REST_URL_PREFIX+"/product/add",product,Boolean.class);
    }

    @RequestMapping(value = "/consumer/product/get/{id}")
    public Product get(@PathVariable("id") Long id){
        return restTemplate.getForObject(REST_URL_PREFIX+"/product/get/"+id,Product.class);
    }

    @RequestMapping(value = "/consumer/product/list")
    public List<Product> list(){
        return restTemplate.getForObject(REST_URL_PREFIX+"/product/list",List.class);
    }
}

ProductConsumer_80主启动类

package cn.zf.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

5.构建集群版Eureka Server服务注册中心(microService-eureka-6001、microService-eureka-6002)

5.1microService-eureka-6001模块,在src/main/resources下创建application.yml配置文件,创建EurekaServer_6001主启动类

 在src/main/resources下创建application.yml配置文件

server:
  port: 6001

eureka:
  server:
    enable-self-preservation: false     #禁用自我保护机制
  instance:
    hostname: eureka6001.com  #  eureka服务端的实例名称
  client:
    register-with-eureka: false # 不要自己注册自己
    fetch-registry: false # 不能获得自己的注册更新
    serviceUrl:
      defaultZone: http://eureka6002.com:6002/eureka/

创建EurekaServer_6001主启动类

package cn.zf.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

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

5.2microService-eureka-6001模块,在src/main/resources下创建application.yml配置文件,创建EurekaServer_6002主启动类

 在src/main/resources下创建application.yml配置文件

server:
  port: 6002

eureka:
  server:
    enable-self-preservation: false     # 禁用自我保护机制
  instance:
    hostname: eureka6002.com   #  eureka服务端的实例名称
  client:
    register-with-eureka: false
    fetch-registry: false
    serviceUrl:
      defaultZone: http://eureka6001.com:6001/eureka/

创建EurekaServer_6002主启动类

package cn.zf.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

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

6.测试,先启动两个eureka注册中心的主启动类(两个注册中心顺序无所谓),再启动服务提供者的主启动类,最后启动服务消费者的主启动类

 

over~ 


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