Spring Cloud介绍
Spring Cloud是一个分布式的整体解决方案。Spring Cloud 为开发者提供了在分布式系统(配置管理,服务发现,熔断,路由,微代理,控制总线,一次性token,全局琐,leader选举,分布式session,集群状态)中快速构建的工具,使用Spring Cloud的开发者可以快速的启动服务或构建应用、同时能够快速和云平台资源进行对接。
SpringCloud分布式开发五大常用组件
- 服务发现——Netflix Eureka
- 客服端负载均衡——Netflix Ribbon
- 断路器——Netflix Hystrix
- 服务网关——Netflix Zuul
- 分布式配置——Spring Cloud Config
Spring Cloud搭建
Eureka注册中心
1、在idea中,新建一个Empty Project 空工程,然后在空工程中添加一个SpringBoot项目,作为Eureka注册中心服务,引入如下依赖:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.test</groupId> 8 <artifactId>test-springboot-cloud-eureka</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 12 <parent> 13 <groupId>org.springframework.boot</groupId> 14 <artifactId>spring-boot-starter-parent</artifactId> 15 <version>2.1.8.RELEASE</version> 16 </parent> 17 18 <properties> 19 <spring-cloud.version>Greenwich.SR1</spring-cloud.version> 20 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 21 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 22 <java.version>1.8</java.version> 23 </properties> 24 25 <dependencyManagement> 26 <dependencies> 27 <dependency> 28 <groupId>org.springframework.cloud</groupId> 29 <artifactId>spring-cloud-dependencies</artifactId> 30 <version>${spring-cloud.version}</version> 31 <type>pom</type> 32 <scope>import</scope> 33 </dependency> 34 </dependencies> 35 </dependencyManagement> 36 37 <dependencies> 38 39 <dependency> 40 <groupId>org.springframework.cloud</groupId> 41 <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> 42 </dependency> 43 44 <dependency> 45 <groupId>org.springframework.boot</groupId> 46 <artifactId>spring-boot-starter-test</artifactId> 47 <scope>test</scope> 48 </dependency> 49 50 </dependencies> 51 52 53 <!-- SpringBoot打包插件,可以将代码打包成一个可执行的jar包 --> 54 <build> 55 <plugins> 56 <plugin> 57 <groupId>org.springframework.boot</groupId> 58 <artifactId>spring-boot-maven-plugin</artifactId> 59 </plugin> 60 </plugins> 61 </build> 62 </project>
2、添加配置信息
1 server: 2 port: 8761 3 4 eureka: 5 instance: 6 hostname: eureka-server # eureka实例的主机名 7 client: 8 register-with-eureka: false #不把自己注册到eureka上 9 fetch-registry: false #不从eureka上来获取服务的注册信息 10 service-url: 11 defaultZone: http://localhost:8761/eureka
3、开启Eureka注册中心服务,使用注解@EnableEurekaServer
1 /** 2 * 注册中心 3 * 1、配置Eureka注册信息 4 * 2、@EnableEurekaServer 开启注册中心服务 5 */ 6 @EnableEurekaServer 7 @SpringBootApplication 8 public class Application { 9 public static void main(String[] args) { 10 SpringApplication.run(Application.class, args); 11 } 12 }
4、启动项目,查看Eureka注册中心,地址:http://localhost:8761/
服务提供者
1、在空工程中添加一个SpringBoot项目,作为服务提供者,注册到Eureka中,引入如下依赖:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.test</groupId> 8 <artifactId>test-springboot-cloud-provider</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 <parent> 12 <groupId>org.springframework.boot</groupId> 13 <artifactId>spring-boot-starter-parent</artifactId> 14 <version>2.1.8.RELEASE</version> 15 </parent> 16 17 <properties> 18 <spring-cloud.version>Greenwich.SR1</spring-cloud.version> 19 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 20 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 21 <java.version>1.8</java.version> 22 </properties> 23 24 <dependencyManagement> 25 <dependencies> 26 <dependency> 27 <groupId>org.springframework.cloud</groupId> 28 <artifactId>spring-cloud-dependencies</artifactId> 29 <version>${spring-cloud.version}</version> 30 <type>pom</type> 31 <scope>import</scope> 32 </dependency> 33 </dependencies> 34 </dependencyManagement> 35 36 <dependencies> 37 <dependency> 38 <groupId>org.springframework.boot</groupId> 39 <artifactId>spring-boot-starter-web</artifactId> 40 </dependency> 41 42 <dependency> 43 <groupId>org.springframework.cloud</groupId> 44 <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> 45 </dependency> 46 47 <dependency> 48 <groupId>org.springframework.boot</groupId> 49 <artifactId>spring-boot-starter-test</artifactId> 50 <scope>test</scope> 51 </dependency> 52 53 </dependencies> 54 55 56 <!-- SpringBoot打包插件,可以将代码打包成一个可执行的jar包 --> 57 <build> 58 <plugins> 59 <plugin> 60 <groupId>org.springframework.boot</groupId> 61 <artifactId>spring-boot-maven-plugin</artifactId> 62 </plugin> 63 </plugins> 64 </build> 65 66 </project>
2、添加提供者项目配置信息
1 spring: 2 application: 3 name: provider 4 5 server: 6 port: 8081 7 eureka: 8 instance: 9 prefer-ip-address: true # 注册服务的时候使用服务的ip地址 10 client: 11 service-url: 12 defaultZone: http://localhost:8761/eureka
3、编辑服务,编写一个controller,作为远程服务,内容如下:
1 package com.test.springboot.cloud.provider.controller; 2 3 import org.springframework.beans.factory.annotation.Value; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 import org.springframework.web.bind.annotation.RestController; 6 7 @RestController 8 public class ProviderController { 9 10 @Value("${spring.application.name}") 11 String applicationName; 12 13 @Value("${server.port}") 14 String port; 15 16 @RequestMapping("/provider") 17 public String provider(String name){ 18 return "hi " + name + "\nI'm " + applicationName + port; 19 } 20 21 }
4、启动项目,即将服务注册到Eureka,查看Eureka注册中心,地址:http://localhost:8761/
5、测试,在浏览器中使用地址(http://localhost:8081/provider?name=abc)进行访问,效果如下:
为了演示服务负载均衡方便,将项目使用不同的配置文件(不同端口)打包,一个项目用8081端口,一个项目用8082端口,
打完包后,使用java -jar 运行,Eureka注册中心,效果如下:
服务消费者
1、在空工程中添加一个SpringBoot项目,作为服务消费者,也注册到Eureka中,引入如下依赖:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.test</groupId> 8 <artifactId>test-springboot-cloud-consumer</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 <parent> 12 <groupId>org.springframework.boot</groupId> 13 <artifactId>spring-boot-starter-parent</artifactId> 14 <version>2.1.8.RELEASE</version> 15 </parent> 16 17 <properties> 18 <spring-cloud.version>Greenwich.SR1</spring-cloud.version> 19 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 20 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 21 <java.version>1.8</java.version> 22 </properties> 23 24 <dependencyManagement> 25 <dependencies> 26 <dependency> 27 <groupId>org.springframework.cloud</groupId> 28 <artifactId>spring-cloud-dependencies</artifactId> 29 <version>${spring-cloud.version}</version> 30 <type>pom</type> 31 <scope>import</scope> 32 </dependency> 33 </dependencies> 34 </dependencyManagement> 35 36 <dependencies> 37 <dependency> 38 <groupId>org.springframework.boot</groupId> 39 <artifactId>spring-boot-starter-web</artifactId> 40 </dependency> 41 42 <dependency> 43 <groupId>org.springframework.cloud</groupId> 44 <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> 45 </dependency> 46 47 <dependency> 48 <groupId>org.springframework.boot</groupId> 49 <artifactId>spring-boot-starter-test</artifactId> 50 <scope>test</scope> 51 </dependency> 52 53 </dependencies> 54 55 56 <!-- SpringBoot打包插件,可以将代码打包成一个可执行的jar包 --> 57 <build> 58 <plugins> 59 <plugin> 60 <groupId>org.springframework.boot</groupId> 61 <artifactId>spring-boot-maven-plugin</artifactId> 62 </plugin> 63 </plugins> 64 </build> 65 66 </project>
2、添加消费者项目配置信息
1 spring: 2 application: 3 name: consumer 4 5 server: 6 port: 8083 7 eureka: 8 instance: 9 prefer-ip-address: true # 注册服务的时候使用服务的ip地址 10 client: 11 service-url: 12 defaultZone: http://localhost:8761/eureka
3、在容器中注入RestTempalte对象,用来远程调用SpringCloud中的服务,使用http请求的方式
1 @LoadBalanced // 使用负载均衡机制 2 @Bean 3 public RestTemplate restTemplate(){ 4 return new RestTemplate(); 5 }
4、使用@EnableDiscoveryClient注解,开启服务发现功能
1 @EnableDiscoveryClient // 开启服务发现功能 2 @SpringBootApplication 3 public class Application { 4 public static void main(String[] args) { 5 SpringApplication.run(Application.class, args); 6 } 7 }
5、使用RestTempalte调用,远程服务,编写一个controller,内容如下:
1 @RestController 2 public class ConsumerController { 3 4 @Autowired 5 RestTemplate restTemplate; 6 7 @RequestMapping("/consumer") 8 public String consumer(String name) { 9 10 String object = restTemplate.getForObject("http://PROVIDER/provider?name=" + name, String.class); 11 12 return "consumer---> " + object; 13 } 14 }
6、启动项目,查看Eureka注册中心,地址:http://localhost:8761/
7、测试,在浏览器上使用地址(http://localhost:8083/consumer?name=fff)请求consumer服务。连续访问两次,可以看到一次请求到8081服务上,一次请求到8082上