Skip to content

本章节主要介绍如何基于Spring Cloud相关组件搭建一个典型的微服务架构。 首先,创建一个Maven父项目spring-cloud-examples,用于管理项目依赖包版本。由于Spring Cloud组件很多,为保证不同组件之间的兼容性,一般通过spring-cloud-dependencies统一管理Spring Cloud组件版本,而非每个组件单独引入。 pom.xml配置如下: \<!-- 继承SpringBoot父项目,注意与SpringCloud版本的匹配 --\>\<parent\>\<groupId\>org.springframework.boot\</groupId\>\<artifactId\>spring-boot-starter-parent\</artifactId\>\<version\>2.1.4.RELEASE\</version\>\</parent\>\<properties\>\<spring.boot.version\>2.1.4.RELEASE\</spring.boot.version\>\<spring.cloud.version\>Greenwich.SR1\</spring.cloud.version\>\<lombok.version\>1.18.8\</lombok.version\>\<maven.compiler.plugin.version\>3.8.1\</maven.compiler.plugin.version\>\</properties\>\<dependencyManagement\>\<dependencies\>\<dependency\>\<groupId\>org.springframework.cloud\</groupId\>\<artifactId\>spring-cloud-dependencies\</artifactId\>\<version\>${spring.cloud.version}\</version\>\<type\>pom\</type\>\<scope\>import\</scope\>\</dependency\>\</dependencies\>\</dependencyManagement\>搭建服务配置中心

  • 在spring-cloud-examples项目下创建一个子项目spring-cloud-example-config,添加Spring Cloud Config Server端的相关依赖包:

\<dependencies\>\<dependency\>\<groupId\>org.springframework.cloud\</groupId\>\<artifactId\>spring-cloud-config-server\</artifactId\>\</dependency\>\</dependencies\>

  • 添加Spring Boot配置文件application.yml,配置如下:

spring: application: name: spring-cloud-example-config profiles: active: native # 启用本地配置文件

 cloud: config: server: native: search-locations: classpath:/configs/ # 配置文件扫描目录

  • 
  • server: port: 8000 #

服务端口

  • 启动类添加注解@EnableConfigServer通过启用Config Server服务。

@SpringBootApplication@EnableConfigServerpublicclassApplication{publicstaticvoidmain(String[]args){SpringApplication.run(Application.class,args);}}搭建服务注册中心

  • 在spring-cloud-examples项目下创建一个子项目spring-cloud-example-registry,在pom.xml中添加Eureka Server相关依赖包:

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

  • 在spring-cloud-example-config配置中心项目的src/main/resource/configs目录下添加一个服务配置文件spring-cloud-example-registry.yml,配置如下:

spring: application: name: spring-cloud-example-registry

Eureka ​

相关配置

eureka: client: register-with-eureka: false # 不注册服务

 fetch-registry: false #

不拉去服务清单

 serviceUrl: defaultZone: http://localhost😒{server.port}/eureka/ # 多个通过英文逗号分隔

  • 

  • server: port: 8001

  • 在spring-cloud-example-registry项目的src/main/resource/目录添加bootstrap.yml配置文件,配置如下:

spring: cloud: config: name: spring-cloud-example-registry # 配置文件名称,多个通过逗号分隔

 uri: http://localhost:8000 #Config Server 服务地址

  • 启动类添加注解@EnableEurekaServer通过启用Eureka Server服务。

@SpringBootApplication@EnableEurekaServerpublicclassApplication{publicstaticvoidmain(String[]args){SpringApplication.run(Application.class,args);}}搭建业务服务A

  • 在spring-cloud-examples项目下创建一个业务服务A的子项目spring-cloud-example-biz-a,在pom.xml中添加以下依赖包:

\<dependencies\>\<!-- Spring Boot Web Starter --\>\<dependency\>\<groupId\>org.springframework.boot\</groupId\>\<artifactId\>spring-boot-starter-web\</artifactId\>\</dependency\>\<!-- feign --\>\<dependency\>\<groupId\>org.springframework.cloud\</groupId\>\<artifactId\>spring-cloud-starter-openfeign\</artifactId\>\</dependency\>\<!-- Eureka Client Starter --\>\<dependency\>\<groupId\>org.springframework.cloud\</groupId\>\<artifactId\>spring-cloud-starter-netflix-eureka-client\</artifactId\>\</dependency\>\<!-- Config Client Starter --\>\<dependency\>\<groupId\>org.springframework.cloud\</groupId\>\<artifactId\>spring-cloud-starter-config\</artifactId\>\</dependency\>\</dependencies\>

  • 在spring-cloud-example-config配置中心项目的src/main/resource/configs目录下添加一个服务配置文件spring-cloud-example-biz-a.yml,配置如下:

spring: application: name: spring-cloud-example-biz-a server: port: 8010

Eureka ​

相关配置

eureka: client: serviceUrl: defaultZone: http://localhost:8001/eureka/ instance: lease-renewal-interval-in-seconds: 10 # 心跳时间,即服务续约间隔时间(缺省为30s)

 lease-expiration-duration-in-seconds: 60 #

发呆时间,即服务续约到期时间(缺省为90s)

 prefer-ip-address: true instance-id: ${spring.application.name}😒{spring.application.instance_id:${server.port}}

  • 在spring-cloud-example-biz-a项目的src/main/resource/目录添加bootstrap.yml配置文件,配置如下:

spring: cloud: config: name: spring-cloud-example-biz-a # 配置文件名称,多个通过逗号分隔

 uri: http://localhost:8000 #Config Server 服务地址

  • 添加一个示例接口,代码参考:

@RestController@RequestMapping("/hello")publicclassHelloController {/** * 示例方法

 * * @return */@GetMappingpublicString sayHello(){return"Hello,This is Biz-A Service.";}} 搭建业务服务B 参考上面业务服务A搭建另外一个业务服务B。 搭建服务网关

  • 在spring-cloud-examples项目下创建一个业务服务A的子项目spring-cloud-example-gateway,在pom.xml中添加以下依赖包:

\<dependencies\>\<!-- zuul --\>\<dependency\>\<groupId\>org.springframework.cloud\</groupId\>\<artifactId\>spring-cloud-starter-netflix-zuul\</artifactId\>\</dependency\>\<!-- Eureka Client Starter --\>\<dependency\>\<groupId\>org.springframework.cloud\</groupId\>\<artifactId\>spring-cloud-starter-netflix-eureka-client\</artifactId\>\</dependency\>\<!-- Config Client Starter --\>\<dependency\>\<groupId\>org.springframework.cloud\</groupId\>\<artifactId\>spring-cloud-starter-config\</artifactId\>\</dependency\>\</dependencies\>

  • 在spring-cloud-example-config配置中心项目的src/main/resource/configs目录下添加一个服务配置文件spring-cloud-example-gateway.yml,配置如下:

spring: application: name: spring-cloud-example-gateway server: port: 8002

Eureka ​

相关配置

eureka: client: serviceUrl: defaultZone: http://localhost:8001/eureka/ instance: lease-renewal-interval-in-seconds: 10 # 心跳时间,即服务续约间隔时间(缺省为30s)

 lease-expiration-duration-in-seconds: 60 #

发呆时间,即服务续约到期时间(缺省为90s)

 prefer-ip-address: true instance-id: ${spring.application.name}😒{spring.application.instance_id:${server.port}}

  • 在spring-cloud-example-gateway项目的src/main/resource/目录添加bootstrap.yml配置文件,配置如下:

spring: cloud: config: name: spring-cloud-example-gateway # 配置文件名称,多个通过逗号分隔

 uri: http://localhost:8000 #Config Server 服务地址

  • 启动类添加注解@EnableZuulProxy通过启用网关代理服务。

@SpringBootApplication@EnableZuulProxypublicclassApplication{publicstaticvoidmain(String[]args){SpringApplication.run(Application.class,args);}}启动示例

  • 启动顺序

    spring-cloud-example-config\>\>spring-cloud-example-eureka\>\>spring-cloud-example-biz-a/spring-cloud-example-biz-b/spring-cloud-example-gateway
  • 通过网关访问服务A接口

服务A调用

  • 通过网关访问服务B接口

服务B调用

服务之间调用

  • 在业务服务A中添加一个Feign Client Bean,参考代码如下:

@FeignClient(name ="spring-cloud-example-biz-b")# 指定服务名称

publicinterfaceRemoteService {/** * 调用服务B的hello方法

 * * @return */@GetMapping("/hello")# 指定请求地址

 String sayHello();}

  • 业务服务A示例接口类增加call2b接口,代码如下:

@RestController@RequestMapping("/hello")publicclassHelloController {@AutowiredprivateRemoteService remoteService;/** * 示例方法

 * * @return */@GetMappingpublicString sayHello(){return"Hello,This is Biz-A Service.";}/** * 示例方法:调用服务

B * * @return */@GetMapping(path ="/call2b")publicString sayHello2B(){returnremoteService.sayHello();}}

  • 重启业务服务A,通过调用/hello/call2b接口:

服务之间调用

示例代码

  • 码云:

    centychen/spring-cloud-examples

下一代微服务 目前网上很多说是下一代微服务架构就是Service Mesh,Service Mesh主流框架有Linkerd和Istio,其中Istio有大厂加持所以呼声更高。Service Mesh我接触还不多,但是个人感觉并不一定能称为下一代微服务架构,可能认为是服务治理的另外一种解决方案更合适,是否能够取代当前的微服务架构还需要持续观察。

作者:centychen 链接:https://www.jianshu.com/p/7293b148028f 来源:简书 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

本站总访问 — 次