前一篇文章小编介绍了什么是微服务、为什么选择微服务,本篇文章我们来介绍如何使用Spring Cloud来进行微服务的开发。

微服务架构中针对不同应用场景的各种问题目前已经有了更中解决方案和开源框架,如下图所示:

Cloud微服务架构进阶 Spring pdf spring cloud微服务架构开发_spring


大部分的开源框架都是重点解决微服务中某一个场景的问题,只有Spring Cloud框架能解决微服务架构实施过程中所有的问题。Spring Cloud框架是微服务综合性解决框架,它整合了诸多被广泛实践并证明过的框架为实施的基础部件(如Netfix的服务治理框架Eureka),并在此基础上创建了一些非常优秀的边缘组件。Spring Cloud框架有极高的社区活跃度,一些开发工具也集成了Spring Cloud,对初学微服务的开发人员来说是最好的一个选择。

Spring Cloud是基于Spring Boot实现的微服务架构开发工具包,为开发者提供了微服务开发中涉及到配置管理、服务发现、断路器、智能路由、微代理、控制总线、一次性Token、全局锁、决策竞选、分布式会话或集群状态监控等操作提供了一种简单的开发方式。

通过Spring Cloud框架搭建的典型的系统架构如下图所示:

Cloud微服务架构进阶 Spring pdf spring cloud微服务架构开发_Spring Cloud_02


此典型架构中涉及注册中心Eureka、智能路由Zuul、负载均衡Ribbon、配置中心Spring Cloud Config及服务间调用Feign。此外Spring Cloud中还包含了其他子项目,如断路器Hystrix、服务跟踪器Sleuth等,主要子项目如下:

Cloud微服务架构进阶 Spring pdf spring cloud微服务架构开发_Cloud_03


微服务实践

使用Spring Cloud进行开发,常用的开发工具有:传统的Eclipse、近来流行的IntelliJ IDEA以及Spring自带的Spring Tool Suite。

使用传统的Eclipse工具,可以前往https://start.spring.io/生成一个Spring Cloud工程,如下图:

Cloud微服务架构进阶 Spring pdf spring cloud微服务架构开发_微服务_04


可以选择工程的构建方式、工程语言、Spring Boot版本,工程信息(如的Group、Artifact等),工程高级配置(生成jar包还是war包,jdk版本选择),Spring Cloud组件选择(在选择框中输入组件名称,在结果中选择需要的组件或者点击列表展示Spring Cloud组件进行选择),如下图:

Cloud微服务架构进阶 Spring pdf spring cloud微服务架构开发_Cloud_05


工程的配置信息完成后,可以点击下方左侧按钮” Generate the project”生成工程并下载,点击下方右侧按钮“Explore the project”查看工程代码,如下图所示:

Cloud微服务架构进阶 Spring pdf spring cloud微服务架构开发_Spring Boot_06


可以看到右侧工程目录结果,点击相应的文件可以查看文件代码。由于IntelliJ IDEA已经集成了Spring Cloud,可以直接创建工程,

1、创建工程,选择Spring Initializr,如下图所示:

Cloud微服务架构进阶 Spring pdf spring cloud微服务架构开发_Spring Cloud_07


2、配置工程信息,如Group、Artifact等,如下图所示:

Cloud微服务架构进阶 Spring pdf spring cloud微服务架构开发_微服务_08


Cloud微服务架构进阶 Spring pdf spring cloud微服务架构开发_微服务_09


通过IntelliJ IDEA工具开发也比较简单,可是IntelliJ IDEA是商业软件,需要收费,不过有30天的试用期,大家如果感兴趣可以下载下来使用试试看(IntelliJ IDEA提供了免费的社区版,不过功能较少

Spring Tool Suite工具创建工程此处小编就不再赘述了,感兴趣的同学可以自行下载使用,下载地址:https://spring.io/tools。

按照上文中典型的Spring Cloud架构,我们通过一个简单的示例来体验下Spring Cloud架构的魅力。
1、注册中心
创建包含Eureka Server的工程,工程中的pom.xml会自动引入Eureka Server依赖的包。
在代码中加入注解,声明是Eureka Server服务,
@EnableEurekaServer
在配置文件中增加配置如下:

server.port=8761
eureka.instance.hostname=localhost
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
eureka.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

启动工程,打开页面http://localhost:8761/可以查看服务注册情况,如下图所示:

Cloud微服务架构进阶 Spring pdf spring cloud微服务架构开发_spring_10


2、测试服务

创建三个测试服务,两个order-service和一个product-service,在代码中加入注解,声明是Eureka客户端及引入Rest:

@EnableDiscoveryClient

@RestController

加入Rest调用:

@RequestMapping(value = "/hello", method = RequestMethod.GET)
    public ResponseEntity<String> hello() {
        return new ResponseEntity<>("hello order service", HttpStatus.OK);
}

order-service1的配置文件如下:

spring.application.name=order-service
server.port=3331
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

order-service2的配置文件如下:

spring.application.name=order-service
server.port=3332
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

product-service的配置文件如下:

spring.application.name=product-service
server.port=2221
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

3、负载均衡
创建包含Zuul的工程,在代码中加入注解,声明是Eureka客户端
@EnableDiscoveryClient
在代码中引入负载均衡:

@Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

在另一个文件RibbonController实现负载均衡,如下代码:

@RestController
public class RibbonController {

    public static Logger logger = LoggerFactory.getLogger(RibbonController.class);

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping(value = "/product/hello", method = RequestMethod.GET)
    public String productHello() {
        logger.info("ribbon call product service");
        return restTemplate.getForEntity("http://PRODUCT-SERVICE/hello", String.class).getBody();
    }

    @RequestMapping(value = "/order/hello", method = RequestMethod.GET)
    public String orderHello() {
        logger.info("ribbon call order service");
        return restTemplate.getForEntity("http://ORDER-SERVICE/hello", String.class).getBody();
    }

}

配置文件如下:

spring.application.name=ribbon-service
server.port=8763
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

4、智能路由
创建包含Zuul的工程,在代码中加入注解,声明是Eureka客户端和Zuul服务

@EnableDiscoveryClient
@EnableZuulProxy

在配置文件中增加配置如下:

spring.application.name=zuul-service
server.port=8765
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
#针对各个服务实现路由
zuul.routes.product-service.path=/product-service/**
zuul.routes.product-service.serviceId=product-service

zuul.routes.order-service.path=/order-service/**
zuul.routes.order-service.serviceId=order-service

zuul.routes.ribbon-service.path=/ribbon-service/**
zuul.routes.ribbon-service.serviceId=ribbon-service

各工程完成后,启动刷新服务治理查看页面http://localhost:8761/,已经看到服务,如下图所示:

Cloud微服务架构进阶 Spring pdf spring cloud微服务架构开发_微服务_11


结果验证如下:

[root@vos ~]# curl 'http://172.30.18.71:3331/hello'
hello order service
[root@vos ~]# curl 'http://172.30.18.71:3332/hello'
hello order service2
[root@vos ~]# curl 'http://172.30.18.71:2221/hello'
hello product service
#验证路由功能
[root@vos ~]# curl 'http://172.30.18.71:8765/order-service/hello'
hello order service
[root@vos ~]# curl 'http://172.30.18.71:8765/product-service/hello'
hello product service
#验证负载均衡,每一次分别分配到不同节点的服务上
[root@vos ~]# curl 'http://172.30.18.71:8763/order/hello'
hello order service
[root@vos ~]# curl 'http://172.30.18.71:8763/order/hello'
hello order service2

通过以上示例不难看出使用Spring Cloud实现微服务的服务治理、智能路由、负载均衡等都比较简单。