微服务框架——SpringCloud
1.SpringCloud微服务框架
a.概念:SpringCloud是基于SpringBoot的微服务框架
b.五大神兽:Eureka(服务发现)、Ribbon(客服端负载均衡)、Hystrix(断路器)、Zuul(服务网关)、Spring Cloud Config(分布式配置)
2.Eureka服务发现
a.组成:Eureka服务器和Eureka客户端
b.Eureka服务器(注册中心)
①新建springboot项目,依赖选择Eureka Server
②pom文件关键依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> ...... </dependencies> ......
③application.yml文件
# 服务名称 spring: application: name: eureka-server # 服务端口号 server: port: 8081 #Eureka 相关配置 eureka: instance: hostname: localhost client: service-url: defaultZone: http://localhost:${server.port}/eureka/ # 是否从其他的服务中心同步服务列表 fetch-registry: false # 是否把自己作为服务注册到其他服务注册中心 register-with-eureka: false
④启动类添加注解@EnableEurekaServer
@SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
⑤启动后访问 localhost:8081 可进入管理员界面
c.Eureka客户端(生产者)
①新建springboot项目,依赖选择 Eureka Discovery 、Web
②pom文件关键依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> ...... </dependencies> ......
③application.yml文件
spring: application: name: eureka-client-producer server: port: 8082 eureka: client: service-url: defaultZone: http://localhost:8081/eureka/
④启动类添加注解@EnableEurekaClient
@SpringBootApplication @EnableEurekaClient public class EurekaClientProducerApplication { public static void main(String[] args) { SpringApplication.run(EurekaClientProducerApplication.class, args); } }
⑤创建controller供外部调用
@RestController public class CommonController { @Value("${server.port}") String port; @Value("${spring.application.name}") String name; @RequestMapping(value = "/printProducer") public String printProducer(String param){ return "[" + name + "]请求参数:" + param + ",服务端口:" + port; } }
3.Ribbon负载均衡
a.Eureka客户端(消费者) + Ribbon负载均衡
①新建springboot项目,依赖选择 Eureka Discovery 、Web 以及 Ribbon
②pom文件关键依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> ...... </dependencies> ......
③application.yml文件
spring: application: name: eureka-client-consumer server: port: 8085 eureka: client: service-url: defaultZone: http://localhost:8081/eureka/
④启动类添加注解@EnableDiscoveryClient并且加入restTemplate
@SpringBootApplication @EnableEurekaClient public class EurekaClientConsumerApplication { public static void main(String[] args) { SpringApplication.run(EurekaClientConsumerApplication.class, args); } @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } }
⑤使用restTemplate调用服务
@RestController public class CommonController { @Resource RestTemplate restTemplate; @RequestMapping(value = "/print") public String print(String param){ String result = restTemplate.getForObject("http://EUREKA-CLIENT-PRODUCER/printProducer?param=" + param, String.class); return result; } }
4.Hystrix断路器
a.消费者增加Hystrix依赖,pom中添加
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
b.启动类添加注解@EnableHystrix
@SpringBootApplication @EnableEurekaClient @EnableHystrix public class EurekaClientConsumerApplication { ...... }
c.具体使用
@RestController public class CommonController { @Resource RestTemplate restTemplate; /* 断路器配置,当无法调用如下方法时,就会调用自定的errorCallback方法。 */ @HystrixCommand(fallbackMethod = "errorCallback") @RequestMapping(value = "/print") public String print(String param){ String result = restTemplate.getForObject("http://EUREKA-CLIENT-PRODUCER/printProducer?param=" + param, String.class); return result; } public String errorCallback(String param){ return "连接消费服务失败,请求参数:" + param; } }