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)