Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智能路由,微代理,控制总线)。分布式系统的协调导致了样板模式, 使用Spring Cloud开发人员可以快速地支持实现这些模式的服务和应用程序。他们将在任何分布式环境中运行良好,包括开发人员自己的笔记本电脑,裸机数据中心,以及Cloud Foundry等托管平台。
Spring Cloud专注于提供良好的开箱即用经验的典型用例和可扩展性机制覆盖。
分布式/版本化配置
服务注册和发现
路由
service - to - service调用
负载均衡
断路器
分布式消息传递
SpringCloud分布式开发五大神兽 服务发现——Netflix Eureka 客服端负载均衡——Netflix Ribbon 断路器——Netflix Hystrix 服务网关——Netflix Zuul 分布式配置——Spring Cloud Config
1:首先创建一个主maven工程
2:在1创建的的工程中创建model,名称为server,作为eureka server
创建过程如下
创建完成后的pom.xml如下
xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0modelVersion> <groupId>com.springcloudgroupId> <artifactId>serverartifactId> <version>0.0.1-SNAPSHOTversion> <packaging>jarpackaging> <name>servername> <description>Demo project for Spring Bootdescription> <parent> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-parentartifactId> <version>1.5.8.RELEASEversion> <relativePath/> parent> <properties> <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding> <java.version>1.8java.version> <spring-cloud.version>Edgware.RELEASEspring-cloud.version> properties> <dependencies> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-eureka-serverartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-testartifactId> <scope>testscope> dependency> dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-dependenciesartifactId> <version>${spring-cloud.version}version> <type>pomtype> <scope>importscope> dependency> dependencies> dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-maven-pluginartifactId> plugin> plugins> build> project>
4.启动服务注册中心,创建完成后的pom.xml如下
只需要一个注解@EnableEurekaServer,这个注解需要在springboot工程的启动application类上加
/** * 启动一个服务注册中心 * * @author wang_ */ @EnableEurekaServer @SpringBootApplication public class ServerApplication { public static void main(String[] args) { SpringApplication.run(ServerApplication.class, args); } }5:修改配置文件:
#端口号 server.port= 7080 # eureka.instance.hostname= localhost ##false来表明自己是一个eureka server eureka.client.register-with-eureka=false ##false来表明自己是一个eureka server eureka.client.fetch-registry=false eureka.client.service-url.defaultZone= http://${eureka.instance.hostname}:${server.port}/eureka/ #关闭springboot自带的ioc spring.mvc.favicon.enabled = false6:启动工程,访问http://127.0.0.1:7080/
因为现在没有注册服务,所以显示没有服务被发现
<name>clientname> <description>Demo project for Spring Bootdescription> <parent> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-parentartifactId> <version>1.5.8.RELEASEversion> <relativePath/> parent> <properties> <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding> <java.version>1.8java.version> <spring-cloud-services.version>1.5.0.RELEASEspring-cloud-services.version> <spring-cloud.version>Edgware.RELEASEspring-cloud.version> properties> <dependencies> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-eurekaartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-testartifactId> <scope>testscope> dependency> dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-dependenciesartifactId> <version>Dalston.RC1version> <type>pomtype> <scope>importscope> dependency> dependencies> dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-maven-pluginartifactId> plugin> plugins> build> <repositories> <repository> <id>spring-milestonesid> <name>Spring Milestonesname> <url>https://repo.spring.io/milestoneurl> <snapshots> <enabled>falseenabled> snapshots> repository> repositories>
/** * @author wang_ * @EnableEurekaClient 表明自己是一个eurekaclient. */ @SpringBootApplication @EnableEurekaClient @RestController public class ClientApplication { public static void main(String[] args) { SpringApplication.run(ClientApplication.class, args); } @Value("${server.port}") String port; @RequestMapping("/hi") public String home(@RequestParam String name) { return "hi "+name+",i am from port:" +port; } }修改配置文件如下:
eureka.client.service-url.defaultZone= http://localhost:7080/eureka/ server.port= 8082 spring.application.name= service-hi #在application.properties加入认证的用户名和密 security.user.name=root security.user.password=123456
,启动工程,再次访问http://127.0.0.1:7080/
可以发现有一个服务已经注册了,服务名称和端口号都显示在浏览器中
f访问:
http://127.0.0.1:8082/hi?name=%E7%8E%8B
返回结果:hi 王,i am from port:8082