---
title: "基于Spring Cloud的微服务实现"
author: "theseus"
date: "2017-12-10T14:58:44Z"
tags: []
categories: ["企业架构"]
slug: "spring-cloud"
draft: false
lastmod: "2020-01-11T15:28:18Z"
id: 2404286
url: "https://theseus.iteye.com/blog/2404286"
---

微服务作为当前的流行架构设计，Spring Cloud通过与Spring的集成使微服务的应用更为简便。在Spring Cloud中集成了Netflix的多个开源组件，再通过Spring Boot的注解调用实现插件式的功能调用。

初步架构设计图

![](http://dl2.iteye.com/upload/attachment/0128/1199/b5290d06-b8a1-3397-ac41-d18e4b66024a.png)  
 

 

## 1、注册中心-Eureka

  提供服务的动态注册、发现

  启用Eureka，在pom中添加

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

  服务主类中添加@EnableEurekaServer注解即可

  指定端口 server.port=8761

  访问服务:

![](http://dl2.iteye.com/upload/attachment/0128/1108/d64a0623-4d91-3c55-9195-19a2c5129377.jpg)  
 在此能看观察到该注册中心的所有已注册服务

## 2、Rest服务-Spring Boot

  Rest服务是普通的Spring Boot构建的web应用，通过@RestController注解发布

  在配置文件中指定注册中心

```
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
```

   服务主类中通过@EnableDiscoveryClient注解在Eureka中注册服务

```
DiscoveryClient_BIZ-SERVICE/192.168.133.1:biz-service:8888: registering service...
DiscoveryClient_BIZ-SERVICE/192.168.133.1:biz-service:8888 - registration status: 204
```

 

## 3、Api网关-Zuul

  url的重写：在微服务的实现中通常服务端不会直接暴露于外部调用中，只表现为一些私有服务

  服务过滤：选择服务可为客户端调用

  授权:限制资源的访问

  Zuul是Netflix的网关实现，Zuul使用Eureka做服务发现，使用Ribbon做负载均衡，在pom中引入依赖

```
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
```

   在主类中指定@EnableZuulProxy @EnableDiscoveryClient

  配置文件设置

```
zuul:
  routes:
    biz-service:
      service-id: biz-service
      path: /book/**
      strip-prefix: false #设置为false 重定向是带book，否则则反
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
```

 

## 4、服务监控-Hystrix

   在微服务中需要查看服务的运行状态，可以通过Hystrix实现服务监控

   pom中引入依赖

```
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
```

   服务主类中引入@EnableHystrixDashboard注解

   需要监控的服务项目中(biz-service)引入hystrix  

```
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
```

   指定监控的方法

  

```
	@HystrixCommand(fallbackMethod = "queryDefaultFallback")
	public List<?> queryAllBook() {
		return BizController.bookList;
	}

	public List<?> queryDefaultFallback() { //服务失败后的请求
		return new ArrayList<>();
	}
```

   hystrix只能对一个微服务进行监控，实践中需要监控多个微服务中的方法调用，可以使用turbine组件

   pom中的依赖为

```
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-turbine</artifactId>
</dependency>
```

  turbine配置文件中指定监控的项目

 

```
turbine:
  cluster-name-expression: new String('default')
  app-config: biz-service, customer-service
```

  

  另外在同一机器上需要指定不同的域名，在此使用biz-service及customer-service作为测试样例

  bizi项目中的配置  

```
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    hostname: peer1
```

  customer项目的配置  

```
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka  
  instance:
    hostname: peer2
```

  访问http://localhost:8555/hystrix

![](http://dl2.iteye.com/upload/attachment/0128/1111/1bce34fd-2e7c-3fe9-9340-fe653f8d8a07.jpg)  
  输入监控的url:http://localhost:8555/turbine.stream,访问监控的方法

  http://localhost:8888/book/queryAll

  http://localhost:8880/customer/queryAll

  可以看到监控结果：

![](http://dl2.iteye.com/upload/attachment/0128/1113/b44a6c8c-d4da-396e-bf44-ff6fa4ddaa41.jpg)  
 

项目完整代码地址:https://github.com/hjguang/spring-cloud.git