spring-cloud是spring提供的微服务整合开发框架。Spring Cloud 为开发者提供了在分布式系统(如配置管理、服务发现、断路器、智能路由、微代理、控制总线、一次性 Token、全局锁、决策竞选、分布式会话和集群状态)操做的开发工具。使用 Spring Cloud 开发者能够快速实现上述这些模式。java
下载 https://github.com/mykite/eureka-server.git 编译后直接运行便可,或 mvn clean install 后直接运行jar包后访问 部署后:
nginx
对配置的集中管理,使用svn or git https://github.com/mykite/configserver.git 编译后直接运行便可,或 mvn clean install 后直接运行jar包后访问
使用方式 在configserver中配置的 spring: cloud: config: server: git: uri: https://github.com/mykite/config-repostory 提交到test分支文件hell-server.yml 文件内容: test.name: kite 访问:http://localhost:8888/hello-server/profiles/test 会访问当前配置github上的test分支下的hello-server.yml(or properties文件) 对应应用中的配置 spring: cloud: config: uri: http://localhost:8888 label: test 能够实现注入
ribbon用以实现负载均衡;实现软负载均衡,核心有三点:git
服务选择规则,其中包括:github
断路器
相似nginx,提供反向代理的功能
springcloud-server 提供的服务 springcloud-client 经过feginClient调用服务 springcloud-feginclient 经过feginClient调用server springcloud-parent maven父项目web
pom.xmlspring
<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.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.5.RELEASE</version> </parent> <groupId>com.kite.test</groupId> <artifactId>springcloud-parent</artifactId> <version>1.0.0</version> <packaging>pom</packaging> <name>springcloud-parent</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <modules> <module>../springcloud-client</module> <module>../springcloud-server</module> <module>../springcloud-feginclient</module> </modules> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.SR4</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> </dependency> </dependencies> </project>
pom.xmlapache
<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.0</modelVersion> <packaging>jar</packaging> <name>springcloud-client</name> <artifactId>springcloud-server</artifactId> <url>http://maven.apache.org</url> <parent> <groupId>com.kite.test</groupId> <artifactId>springcloud-parent</artifactId> <version>1.0.0</version> </parent> </project>
提供的服务json
package com.kite.test.springcloud.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; /** * * 类HelloController.java的实现描述:暴露对外服务 * @author pengliang 2016年8月8日 下午4:23:14 */ @RestController public class HelloController { /** * rest 服务用来测试 * --@requestParam url?xxx=name * --requestBody 认定为json传输解析 url?{xxx=name} * @param name * @return */ @RequestMapping(value = "/hello", method = RequestMethod.GET) public String hello(String name) { return "{hello: '" + name + "'}"; } }
启动类app
package com.kite.test.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; //springBoot 做为主启动类 @SpringBootApplication @EnableDiscoveryClient @EnableCircuitBreaker public class ServerApplication { public static void main(String[] args) { SpringApplication.run(ServerApplication.class, args); } }
pom.xml负载均衡
<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.0</modelVersion> <packaging>jar</packaging> <name>springcloud-feginclient</name> <artifactId>springcloud-feginclient</artifactId> <url>http://maven.apache.org</url> <parent> <groupId>com.kite.test</groupId> <artifactId>springcloud-parent</artifactId> <version>1.0.0</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> </project>
feginClient提供接口
package com.kite.test.springcloud.feginclient; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; /** * feginClient接口 * 类HelloFeginClient.java的实现描述:经过feginClient自动调用 * @author pengliang 2016年8月8日 下午4:25:36 */ @FeignClient(value="HelloServer") //对应到的server端的spring.application.name public interface HelloFeginClient { @RequestMapping(value = "/hello", method=RequestMethod.POST) public String hello(@RequestParam(name="name") String name); }
pom.xml
<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.0</modelVersion> <packaging>jar</packaging> <name>springcloud-client</name> <artifactId>springcloud-client</artifactId> <url>http://maven.apache.org</url> <parent> <groupId>com.kite.test</groupId> <artifactId>springcloud-parent</artifactId> <version>1.0.0</version> </parent> <dependencies> <dependency> <groupId>com.kite.test</groupId> <artifactId>springcloud-feginclient</artifactId> <version>1.0.0</version> </dependency> </dependencies> </project>
client 调用服务类
package com.kite.test.springcloud.client.controller; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.kite.test.springcloud.feginclient.HelloFeginClient; /** * 调用测试 * 类CallHelloController.java的实现描述:调用feginClient测试 * @author pengliang 2016年8月8日 下午4:42:14 */ @RestController public class CallHelloController { private Logger log = LoggerFactory.getLogger(CallHelloController.class); @Autowired private HelloFeginClient helloFeginClient; @RequestMapping(value="/hello", method = RequestMethod.GET) public String hello(String name) { log.info("call hello parameter:{}", name); return helloFeginClient.hello(name); } }
client 启动类
package com.kite.test.springcloud.client; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.feign.EnableFeignClients; @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients(basePackages = "com.kite.test") @EnableCircuitBreaker public class ClientApplication { public static void main(String[] args) { SpringApplication.run(ClientApplication.class, args); } }
在具体的微服务用力中咱们通常采用json来做为数据传输格式,经过feginClient来对服务调用来作一层封装hystrix在对feginClient调用时对依赖失败作隔离,ribbon作负载均衡(使用feginClient时已经默认集成ribbon)