一分钟搭建SpringCloud微服务

前言 Spring Cloud是一系列框架的有序集合。它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册、配置中心、消息总线、负载均衡、断路器、数据监控等,都可以用Spring Boot的开发风格做到一键启动和部署。Spring Cloud并没有重复制造轮子,它只是将各家公司开发的比较成熟、经得起实际考验的服务框架组合起来,通过Spring Boot风格进行再封装屏蔽掉了复杂的配置和实现原理,最终给开发者留出了一套简单易懂、易部署和易维护的分布式系统开发工具包。

一分钟搭建SpringCloud微服务
文章图片
SpringCloud.png 传统架构VS微服务 在传统架构模式下一个后端服务系统只有一个war包,这个war包则是通过一个工程和一个数据生成的,在小型项目中,这种架构模式没有任何问题,虽然扩展性比较差,但因为功能不多,逻辑也不复杂,修改工作量也不会很大。对于大型互联网后端系统来说,单体架构的弊端就越发明显了,微服务因此应运而生,微服务的有点在于它可以对各个业务模块充分的解耦。
Eureka注册中心 SpringCloud的Eureka是各个模块的注册中心,在写业务模块之前,我们首先要搭建一个注册中心,我们通过Idea的File->New->Module->Spring Initializr创建一个spring module:

一分钟搭建SpringCloud微服务
文章图片
SpringInitializr.png
在依赖项中,我们选择Eureka Server:

一分钟搭建SpringCloud微服务
文章图片
eurekaServer.png
创建完成后pom文件依赖如下:

org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-netflix-eureka-server org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine

接下来我们需要在application.properties中配置注册中心的端口与IP地址
# 配置服务端口 server.port=6061 # 配置eureka服务器地址 eureka.client.service-url.defaultZone = http://localhost:6061/eureka #是否需要将自己注册到注册中心 eureka.client.register-with-eureka=false #是否需要搜索服务信息 因为自己是注册中心所以为false eureka.client.fetch-registry=false

在EurekaApplication需要添加@EnableEurekaServer注解,运行EurekaApplication后,我们可以在http://localhost:6061/查看注册中心运行情况,此时Aplication中是没有任何模块的。

一分钟搭建SpringCloud微服务
文章图片
EurekaServerWeb.png
业务模块 与注册中心类似,我们还是先new一个用户业务Module,不过此次需要选择的是Eureka Discovery Client:

一分钟搭建SpringCloud微服务
文章图片
EurekaClient.png
创建完成后pom文件依赖如下:
org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine

接下来我们需要在application.properties中配置用户模块的端口与IP地址
server.port=6062 spring.application.name=user#配置微服务 eureka.client.service-url.defaultZone = http://localhost:6061/eureka eureka.client.register-with-eureka=true eureka.client.fetch-registry=true eureka.instance.prefer-ip-address = true eureka.instance.instance-id = ${spring.cloud.client.ip-address}:${server.port}

【一分钟搭建SpringCloud微服务】在UserApplication中需要添加@EnableEurekaClient注解,运行UserApplication后,我们可以在刚刚的注册中心页面的Application下看到User服务。与User类似,我们再创建一个Project业务Module,运行后我们会在Application下看到User与Project两个服务。

一分钟搭建SpringCloud微服务
文章图片
eurekaWithClient.png 跨服务调用 因为业务之前无法做到完全独立,User Module与Project Module之间的业务交互需要Feign来协助实现,依赖如下:
org.springframework.cloud spring-cloud-starter-openfeign

同时在业务的Application入口添加@EnableFeignClients
我们在User模块写一个简单的接口
@RequestMapping(value = "https://www.it610.com/info",method = RequestMethod.GET,produces = {MediaType.APPLICATION_JSON_VALUE}) public String getUserInfo(){ return "Response================>UserInfo"; }

在Project模块中通过@FeignClient实现getUserInfo的Interface,如下:
@FeignClient("USER") @Component public interface UserFeignClient { @RequestMapping("api/v1/user/info") String getUserInfo(); }

FeignClient括号中的名字是注册中心Client注册的名字,Project中写一个测试接口,测试对User模块的调用:
@RestController @RequestMapping(value = "https://www.it610.com/article/api/v1/project") public class ProjectController {@Autowired private UserFeignClient userFeignClient; @RequestMapping(value = "https://www.it610.com/userInfo",method = RequestMethod.GET,produces = {MediaType.APPLICATION_JSON_VALUE}) public String getUserInfo(){ return userFeignClient.getUserInfo(); } }

在PostMan上我们通过使用http://localhost:6064/api/v1/project/userInfo实现Project模块userInfo接口的调用,实现对User模块的调用。

一分钟搭建SpringCloud微服务
文章图片
Response.png
示例源码 文章对SpringCloud的Demo已上传至GitHub,感兴趣的朋友可以Clone下来共同探讨学习一下。
GitHub源码

    推荐阅读