SpringCloud学习之微服务开发篇(三)1.1 - 微服务概述及治理

目录

一、微服务概述

1、初始SpringCloud及微服务

2、什么是微服务

3、什么是Spring Cloud

4、参考文章

二、Eureka-Server服务注册中心

1、Eureka-Server的pom.xml

2、配置application.yml文件

3、编辑启动类

三、Eureka-Client客户端服务提供者

1、pom.xml

2、配置application.yml文件

3、编辑启动类

4、测试说明


一、微服务概述

1、初始SpringCloud及微服务

在传统的软件架构中,我们通常采用的是单体应用来构建一个系统,一个单体应用糅合了各种业务模块。

起初在业务规模不是很大的情况下,对于单体应用的开发维护也相对容易。但随着企业的发展,业务规模与日递增,单体应用变得愈发臃肿。

由于单体应用将各种业务模块聚合在一起,并且部署在一个进程内,所以通常我们对其中一个业务模块的修改也必须将整个应用重新打包上线。

为了解决单体应用变得庞大脯肿之后产生的难以维护的问题,微服务架构便出现在了大家的视线里。

2、什么是微服务

微服务 (Microservices) 是一种软件架构风格,起源于Peter Rodgers博士于 2005 年度云端运算博览会提出的微 Web 服务 (Micro-Web-Service) 。

微服务主旨是将一个原本独立的系统 拆分成多个小型服务,这些小型服务都在各自独立的进程中运行,服务之间通过基于HTTP的RESTful API进行通信协作。

3、什么是Spring Cloud

Spring Cloud是一个基于Spring Boot实现的微服务架构开发工具。

它为微服务架构中涉及的配置管理、服务治理、断路器、智能路由、微代理、控制总线、全局锁、决策竞选、分布式会话和集群状态管理等操作提供了一种简单的开发方式。

Spring Cloud的诞生并不是为了解决微服务中的某一个问题,而是提供了一套解决微服务架构实施的综合性解决方案。

4、参考文章

https://mrbird.cc/Spring-Cloud%20and%20MicroService.html

http://blog.cuicc.com/blog/2015/07/22/microservices/

二、Eureka-Server服务注册中心

新建Eureka-Server服务注册中心项目:

新建springboot项目名称为:eureka-service,然后在pom.xml中,引入相关的依赖,如下所示(当前使用springboot1.5.1.RELEASE版本用于练习)

eureka-service为集群1,可复制一份名称为eureka-service-2的集群2(更改pom.xml中的<artifactId>eureka-service-2</artifactId>即可)

1、Eureka-Server的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.yabin</groupId>
    <artifactId>eureka-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-service</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Edgware.SR3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

2、配置application.yml文件

eureka.instance.hostname指定www.yabin.com(作为本地测试集群1的配置),

可在位置C:\Windows\System32\drivers\etc修改hosts

服务注册中心-集群1的配置:

server:
  port: 8080

spring:
  application:
    name: Eureka-Server

eureka:
  instance:
    # 指定了Eureka服务端的IP(集群1)
    hostname: www.yabin.com
  client:
    # 表示是否将服务注册到Eureka服务端,由于自身就是Eureka服务端;若集群则为true,否则设置为false
    register-with-eureka: true
    # 表示是否从Eureka服务端获取服务信息;若集群则为true,否则设置为false
    fetch-registry: true
    service-url:
      # 指定Eureka服务端的地址,若非集群模式,指定自己的地址即可;若是则指定其它集群地址
      defaultZone: http://windows10.microdone.cn:8081/eureka/
  server:
    # 保护模式:false表示关掉
    enable-self-preservation: false

服务注册中心-集群2的配置:

server:
  port: 8081

spring:
  application:
    name: Eureka-Server

eureka:
  instance:
    # 指定了Eureka服务端的IP(集群2)
    hostname: windows10.microdone.cn
  client:
    # 表示是否将服务注册到Eureka服务端,由于自身就是Eureka服务端;若集群则为true,否则设置为false
    register-with-eureka: true
    # 表示是否从Eureka服务端获取服务信息;若集群则为true,否则设置为false
    fetch-registry: true
    service-url:
      # 指定Eureka服务端的地址,若非集群模式,指定自己的地址即可;若是则指定其它集群地址
      defaultZone: http://www.yabin.com:8080/eureka/
  server:
    # 保护模式:false表示关掉
    enable-self-preservation: false

3、编辑启动类

3-1、在启动类中,增加@EnableEurekaServer注解;因本地搭建的是集群,可以在两个服务注册中心项目入口类中增加启动服务注解。

3-2、测试结果

集群1服务:http://127.0.0.1:8080

集群2服务:http://127.0.0.1:8081

3-3、项目排布

三、Eureka-Client客户端服务提供者

1、pom.xml

与eureka-server项目的pom.xml引用的jar依赖一样。

项目名称可设置未:集群1:eureka-client;集群2:eureka-client2

2、配置application.yml文件

2-1、集群1:

server:
  port: 8082

spring:
  application:
    # 指定服务名称为Server-Provider
    name: Server-Provider
  profiles:
    # 指定配置环境
    active: dev

eureka:
  client:
    # 表示是否将服务注册到Eureka服务端
    register-with-eureka: true
    # 表示是否从Eureka服务端获取服务信息
    fetch-registry: true
    service-url:
      # 指定Eureka服务端的地址
      defaultZone: http://www.yabin.com:8080/eureka/,http://windows10.microdone.cn:8081/eureka/

2-2、集群2:

server:
  port: 8083

spring:
  application:
    # 指定服务名称为Server-Provider
    name: Server-Provider
  profiles:
    # 指定配置环境
    active: dev

eureka:
  client:
    # 表示是否将服务注册到Eureka服务端
    register-with-eureka: true
    # 表示是否从Eureka服务端获取服务信息
    fetch-registry: true
    service-url:
      # 指定Eureka服务端的地址
      defaultZone: http://www.yabin.com:8080/eureka/,http://windows10.microdone.cn:8081/eureka/

3、编辑启动类

需要在启动类上加注解:@EnableDiscoveryClient

4、测试说明

① 对外提供接口让消费者调用(控制层)

@RestController
public class EurekaClientController {

    @Resource
    private Registration registration;
    @Resource
    private DiscoveryClient client;

    @GetMapping("info")
    public String info() {
        ServiceInstance instance = getLocalServiceInstance();
        if (instance == null) {
            return "服务提供者1:实例化是空的";
        }
        String info = "服务提供者1:host: "+ instance.getHost() +",serviceId: "+ instance.getServiceId() +", port: "+ instance.getPort();
        log.info(info);
        return info;
    }

    private ServiceInstance getLocalServiceInstance() {
        String instanceId = registration.getServiceId();
        log.info("服务提供者1:获取本地服务ID:{}", instanceId);
        List<ServiceInstance> list = client.getInstances(instanceId);
        if (list != null && list.size() > 0) {
            for (ServiceInstance instance : list) {
                if (instance.getPort() == 8082) {
                    return instance;
                }
            }
        }
        return null;
    }
}

② 建立一个消费者项目service-consumer,调用提供者服务eureka-server对外的接口;引用的pom.xml与eureka-server的pom.xml一样。编写控制层代码,如下:

@RestController
public class ServiceConsumerController {

    @Resource
    private RestTemplate restTemplate;

    /**
     * 使用RestTemplate对象均衡的去获取服务并消费
     *
     * 可以看到我们使用服务名称(Server-Provider)去获取服务的,而不是使用传统的IP加端口的形式。
     * 这就体现了使用Eureka去获取服务的好处,我们只要保证这个服务名称不变即可,IP和端口不再是我们关心的点。
     */
    @GetMapping("info")
    public String getInfo() {
        log.info("进入消费者方法getInfo()");
        return restTemplate.getForEntity("http://Server-Provider/info", String.class).getBody();
    }
}

③ 消费者启动类加上注解:@EnableDiscoveryClient

消费者访问地址:http://localhost:9010/info

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员的微笑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值