SpringCloud
SpringBoot+SpringCloud应用,实现微服务架构。
SpringBoot:快速开发SpringMVC服务API
SpringCloud:服务管理,例如服务注册、服务查找、服务调用、集群、容错机制、监控、统一配置、消息总线等。
搭建SpringCloud中心
SpringBoot 2.0.1和SpringCloud Finchley.RELEASE
- 在pom.xml添加eureka-server包定义
org.springframework.boot spring-boot-starter-parent 2.0.1.RELEASE
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <!-- eureka-server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <!-- spring-cloud-parent --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
在application.properties定义eureka参数
server.port=1111 eureka.client.registerWithEureka=false eureka.client.fetchRegistry=false eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka
编写启动类,追加启动注解标记
@EnableEurekaServer @SpringBootApplication public class EurekaBootApplication { public static void main(String[] args) { SpringApplication.run(EurekaBootApplication.class, args); } }
启动应用,打开浏览器测试
http://localhost:1111
Eureka服务中心(将服务注册到Eureka中心)
在pom.xml追加eureka-client定义
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <!-- spring-cloud-parent --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
在application.properties定义eureka参数
spring.application.name=USER-SERVICE eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka
在启动类前,追加启动标记
@EnableDiscoveryClient//这是启动标记 @SpringBootApplication @MapperScan(basePackages={"cn.yixue.user.dao"}) @ServletComponentScan public class UserBootApplication { public static void main(String[] args) { SpringApplication.run(UserBootApplication.class, args); } }
Ribbon负载均衡(查找Eureka服务调用)
采用集群部署服务时,需要使用ribbon负载均衡调用机制。SpringCloud提供的ribbon默认机制为轮询模式。此外还支持随机、权重等模式。
ribbon可以和RestTemplate结合,也可以和feign接口模式结合。
采用RestTemplate模式
在pom.xml添加netflix-ribbon负载定义
org.springframework.cloud
spring-cloud-starter-netflix-ribbon
在SpringBootApplication中创建RestTemplate对象时追加@LoadBalaced
@Bean
@LoadBalanced//启用ribbon负载
public RestTemplate restTemplate(){return new RestTemplate();
}
使用RestTemplate调用远程服务
String url = “http://YIXUE-USER/user/ticket";
MultiValueMap<String, Object> params =new LinkedMultiValueMap<>();
params.set(“userId”, userId);
params.set(“ticket”, ticket);
OvlsResult result = restTemplate.postForObject(url, params, ReturnResult.class);
采用Feign远程接口模式(Fegin接口调用)
ribbon完全内置,jar包内部依赖,自动作用接口。
在pom.xml添加openfeign定义
org.springframework.cloud
spring-cloud-starter-netflix-ribbon
org.springframework.cloud
spring-cloud-starter-openfeign
编写远程服务的Remote接口(定义Fegin接口)
@FeignClient(name=”YIXUE-USER”)
public interface UserRemote {@PostMapping("/user/ticket") public OvlsResult checkTicket( @RequestParam(name="userId",required=false)String userId, @RequestParam(name="ticket",required=false)String token);
}
注意:参数多余一个,一定要使用@RequestParam标记
注入Remote接口对象使用(注入Feigin接口对象,实现远程服务调用)
@Autowired
private UserRemote userRemote;//Feign接口调用模式
ReturnResult result = userRemote.ticket(Integer.parseInt(userId), ticket);在启动类前添加@EnableFeignClients标记
@EnableFeignClients//启用feign
@EnableDiscoveryClient//Eureka服务中心启动标记
@SpringBootApplication
@MapperScan(basePackages={“cn.yixue.video.dao”})
@ServletComponentScan
public class VideoBootApplication {public static void main(String[] args) { SpringApplication.run(VideoBootApplication.class, args); }
}
Hystrix断路器
SpringCloud提供了一个hystrix套件,俗称断路器,可以在服务间调用,被调用方处理缓慢或瘫痪,对调用者提供保护机制。
在pom.xml中添加hystrix工具包
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
在启动类前面加@EnableCircuitBreaker
@EnableCircuitBreaker//启用断路器hystrix
在Controller方法前使用@HystrixCommand指定延迟或异常后调用的方法
@RestController public class VideoController { @Autowired private VideoService videoService; @HystrixCommand(fallbackMethod="defaultLoad") @GetMapping("/video/{id}") public OvlsResult load(@PathVariable("id")int id){ return videoService.load(id); } public OvlsResult defaultLoad(int id){ ReturnResult result = new OvlsResult(); result.setStatus(OvlsConstant.ERROR1); result.setMsg(OvlsConstant.SELECT_EMPTY_MSG); return result; } }
提示:默认超时设置为1秒,超过一秒就调用fallbackMethod处理。