济南中建设计院网站,湘潭专业网站建设湘潭振企专业,网站正在维护中,岳阳网红初期对Spring Cloud的学习以应用搭建为主#xff0c;所以内容不会太枯燥。
一直以来#xff0c;自以为Spring全家桶的学习中#xff0c;Spring framework是基础中的基础#xff0c;部分内容也还是必须要读源码去理解底层原理#xff0c;SpringMVC、SpringBoot#xff0c…初期对Spring Cloud的学习以应用搭建为主所以内容不会太枯燥。
一直以来自以为Spring全家桶的学习中Spring framework是基础中的基础部分内容也还是必须要读源码去理解底层原理SpringMVC、SpringBoot甚至Mybatis等等都是如此。但是对于Spring Cloud来说就从应用入手以系统搭建为主。以此作为目标的话Spring Cloud比其他几个部分来讲就会简单很多。
Spring Cloud的几大组件依次学习先从Eureka入手。Eureka是netflix的服务治理组件提供服务注册和服务发现、实现注册中心服务。
Eureka包含server端和client端。
Eureka server服务端也就是注册中心每一个Eureka客户端需要向服务端注册注册成功后服务端持有客户端列表客户端定时发送心跳至服务端、汇报健康状态超时未发送心跳服务的客户端将从注册中心的客户端列表中清除。
Eureka client使用端、客户端包括服务提供者server provider和服务消费者server consumer不论服务提供者还是服务消费者都要向Eureka服务端进行注册并定时发送心跳。服务消费者向服务端获取一份服务提供者的列表、在需要服务提供者服务的之后向服务提供者发起服务请求并获取服务。
与Spring Boot版本的兼容
Spring Cloud基于SpringBoot搭建在搭建Spring Cloud项目的时候需要注意Spring Cloud和SpringBoot之间的版本兼容关系两者之间版本能兼容的情况下搭建Spring Cloud项目其实并不复杂。否则如果版本不兼容会出现各种莫名其妙的错误。 具体的版本对应关系请参考Spring官网https://spring.io/projects/spring-cloud。
项目结构
我们用一个例子来学习Spring Cloud的Eureka组件例子项目包含一个父项目、一个Eureka注册中心Eureka serverModule、一个orderServiceEureka clientservice consumermodule、一个userServiceEureka clientservice provider。
其中Eureka注册中心作为Eureka的服务端所有的Eureka client都要向注册中心进行注册。
orderService模块模拟订单模块作为微服务是Eureka的客户端启动后需要向注册中心进行注册。
userService模拟用户模块作为微服务是Eureka的客户端启动后需要向注册中心进行注册。
orderService需要通过访问userService获取用户信息这种情况下orderService就是服务消费者userSerivce是服务提供者作为Eureka的客户端两者都会从注册中心获取到Eureka客户端的信息因此可以非常方便的访问到彼此orderService当然能访问到userService提供的服务。
本案例的目的是搭建Spring Cloud的Eureka环境不做具体的业务实现也没必要进行数据库的访问、没必要做接口访问的鉴权…等等所有相关业务细节均忽略。
好的开始搭建环境。
父项目
创建SpringBoot工程pom文件如下
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionpackagingpom/packagingmodulesmoduleeureka-service/modulemoduleuserService/modulemoduleorderservice/module/modulesparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.6.5/versionrelativePath/ !-- lookup parent from repository --/parentgroupIdcom.example/groupIdartifactIdspringCloud/artifactIdversion0.0.1-SNAPSHOT/versionnamespringCloud/namedescriptionspringCloud/descriptionpropertiesjava.version17/java.versionmaven.compiler.source17/maven.compiler.sourcemaven.compiler.target17/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding!--SpringCloud 版本号2021.0.8--spring.cloud.version2021.0.8/spring.cloud.version/propertiesdependencyManagementdependencies!--spring cloud依赖--dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-dependencies/artifactIdversion${spring.cloud.version}/versiontypepom/typescopeimport/scope/dependency/dependencies/dependencyManagementbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build/project
父工程不需要有代码所以删除掉src目录。
注意选用的SpringBoot版本号为2.6.5对应的Spring Cloud的版本号为2021.0.8需要注意对照官网查询两者的兼容性。
注册中心模块
创建eureka注册中心模块命名为eureka-servicepom文件中引入spring-web与eureka-service组件
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdparentartifactIdspringCloud/artifactIdgroupIdcom.example/groupIdversion0.0.1-SNAPSHOT/version/parentmodelVersion4.0.0/modelVersionartifactIdeureka-service/artifactIdpropertiesmaven.compiler.source17/maven.compiler.sourcemaven.compiler.target17/maven.compiler.target/propertiesdependencies!--spring-cloud-starter-netflix-eureka-server 依赖--dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-eureka-server/artifactId/dependency!--spring-boot-starter-web 依赖--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency/dependencies/project在resource下创建application.yml配置文件
# eureka server
server:port: 10086# eureka server
eureka:instance:hostname: localhost # eureka服务名client:registerWithEureka: false #是否向注册中心注册自己fetchRegistry: false # 是否向注册中心获取注册信息serviceUrl: #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/server:enableSelfPreservation: false # 自我保护设置服务名称为localhost端口号为10086设置注册中心的访问地址为http://localhost:10086/eureka/
其实可以发现Eureka注册中心服务其实就是一个web服务由tomcat提供服务。
创建启动类
package com;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;SpringBootApplication
EnableEurekaServer
public class EurekaApplication {public static void main(String[] args) {SpringApplication.run(EurekaApplication.class);}
}
启动类增加注解EnableEurekaServer表明当前服务是Eureka的注册中心。
运行启动类服务正常启动后浏览器输入http://localhost:10086访问Eureka服务 可以看到服务已经正常启动不过当前尚未有任何客户端注册。
userservice
创建userserice模块pom文件如下
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdparentartifactIdspringCloud/artifactIdgroupIdcom.example/groupIdversion0.0.1-SNAPSHOT/version/parentmodelVersion4.0.0/modelVersionartifactIduserService/artifactIdpropertiesmaven.compiler.source17/maven.compiler.sourcemaven.compiler.target17/maven.compiler.target/propertiesdependenciesdependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-eureka-client/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/dependency/dependencies/project引入spring-cloud-starter-netflix-eureka-client以及spring-boot-starter-web即可。
在resource下创建application.yml文件
spring:application:name: userservice # eureka服务名称
eureka:client:service-url: # eureka注册中心路径defaultZone: http://127.0.0.1:10086/eureka/
server:port: 8080指定当前应用的name并配置eureka注册中心路径。并指定当前服务的端口号8080。
启动类加入EnableEurekaClient注解表明当前应用是eureka client
package com;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;SpringBootApplication
EnableEurekaClient
public class UserServiceApplication {public static void main(String[] args) {SpringApplication.run(UserServiceApplication.class);}
}然后创建一个User类简单一点只包含一个属性name即可
package com.example.entity;import com.fasterxml.jackson.annotation.JsonCreator;
import lombok.Data;Datapublic class User {private String name;
}创建一个UserController提供一个/user/getUser的访问为了能够在测试的时候反馈具体是哪一个userservice提供了服务log打印一句话创建一个User对象返回即可。
package com.example.controller;import com.example.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;RestController
RequestMapping(/user)
Slf4j
public class UserController {GetMapping(/getUser)public User getUser(){log.info(userControllers getuser comming......);User usernew User();user.setName(zhangsan);return user;}
}orderservice
创建orderservice模块pom文件与userservice模块大致相同
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdparentartifactIdspringCloud/artifactIdgroupIdcom.example/groupIdversion0.0.1-SNAPSHOT/version/parentmodelVersion4.0.0/modelVersionartifactIdorderservice/artifactIdpropertiesmaven.compiler.source17/maven.compiler.sourcemaven.compiler.target17/maven.compiler.target/propertiesdependenciesdependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-eureka-client/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdcom.example/groupIdartifactIduserService/artifactIdversion0.0.1-SNAPSHOT/version/dependency/dependencies/projectapplication.yml文件指定端口号9090
spring:application:name: orderservice
eureka:client:service-url: # eureka ?????defaultZone: http://127.0.0.1:10086/eureka/
server:port: 9090创建一个OrderController类提供/order/getOrder接口该接口调用OrderService的getOrder()方法:
package com.example.controller;import com.example.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;RestController
RequestMapping(/order)
public class OrderController {AutowiredOrderService orderService;GetMapping(/getOrder)public String getOrder(){System.out.println(Come here to get Order....);return orderService.getOrder();}}创建一个OrderService类增加Service注解加入Spring容器
package com.example.service;import com.example.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
Service
public class OrderService {Autowiredprivate RestTemplate restTemplate;public String getOrder(){//通过userService获取user信息String urlhttp://userservice/user/getUser;System.out.println(urlurl);User userrestTemplate.getForObject(url,User.class);System.out.println(user);return user.getName();}
}OrderService类注入RestTemplate 对象getOrder方法通过RestTemplate 对象调用userservice的/user/getuser接口获取User对象。之后返回获取到的user对象的name。
一切都是为了测试验证Eureka功能所以不管是userService还是orderService功能都非常简单。
orderservice还剩最后一步启动类。
package com;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;SpringBootApplication
EnableEurekaClient
public class OrderServiceApplication {public static void main(String[] args) {SpringApplication.run(OrderServiceApplication.class);}BeanLoadBalancedpublic RestTemplate restTemplate(){return new RestTemplate();}}
增加EnableEurekaClient注解不过发现不加这个注解也不影响功能实现等待后续解析源码学习底层原理的时候再研究这个注解的具体作用。
启动类中通过Bean的方式增加RestTemplate 对象的注入注入到Spring IoC容器中并且增加LoadBanance注解表明通过RestTemplate 对象调用userservice服务的时候需要实现负载均衡功能。
OK所有的代码准备工作已完成。
测试
第一步为了测试验证orderservice调用userservice的时候的负载均衡功能需要启动多个userservice。
idea提供了这个能力只不过需要在configuration中通过虚拟机参数指定服务端口 我们一共配置了3个userservice。
orderService无所谓其实有一个就够了。
我们顺序启动几个服务
eureka注册中心服务eureka-service其实上面我们已经启动过了。userservice服务userserviceuserservice服务userservice1userservice服务userservice2orderservice服务ordersercice
服务启动之后在idea的service监控窗口可以看到各服务的启动端口、以及状态
通过浏览器看一下eureka的工作状态 可以看到当前注册到Eureka注册中心的共有4个服务一个orderservice3个userservice。
好了可以开始测试了。
通过浏览器访问orderservice的getorder方法 可以正常访问到orderservice服务的接口/order/getorder并且orderservice通过restTemplate、以及Eureka客户端可以正常访问到微服务userservice的/user/getuser接口
说明Eureka注册中心的注册功能、已经Eureka客户端向注册中心拉取服务列表、访问其服务列表中的服务等功能已经可以正常工作了。
通过浏览器反复刷新localhost:9090/order/getOrder接口多次访问之后在idea的运行窗口观察userservice的log 发现8080端口的、8081端口的、以及8082端口的userservice服务会被轮番调用说明loadbalance功能也已经可以正常工作了。
至于负载均衡策略留待以后研究吧。
当然我们可以shutdown掉一个userservice可以发现前端访问/order/getorder接口不受影响也简单说明了通过Spring Cloud微服务功能可以提高系统服务能力、增强可靠性和可访问能力。
但是需要补充一下down掉一个userservice之后前端访问/order/getorder接口偶然会出现失败的情况说明一个问题userservice服务down掉之后并没有立即反馈给Eureka注册中心因为有心跳周期的问题之后同步给Eureka注册中心之后orderservice的本地服务列表也应该是需要时间来更新的所以userservice服务down掉一段时间之后前端访问才能正常。
let’s say it’s a day!
上一篇 SpringBoot 自动配置EnableAutoConfiguration