1.前言:
1.1.以下内容是我通过阅读官方文档,并成功实践后的经验总结,希望能帮助你更快地理解和使用spring Cloud.
1.2.默认读者已经熟练掌握Spring 全家桶,Spring Boot和注解开发.
1.3.陆续更新
2.开发环境:
2.1.开发工具:idea
2.2.开发环境:jdk1.7
2.3.Spring版本:
2.3.1.Spring Boot :1.4.0 release
2.3.2.Spring Cloud : Camden SR2
3.demo:(献给急于速成的各位大兄弟): demo地址: https://github.com/leoChaoGlut/spring-cloud-demo
3.1.服务注册demo:
3.1.1.创建工程模块,如图所示
3.1.2.将官方提供的maven依赖,加入pom.
- <?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.0</modelVersion>
-
- <groupId>demo</groupId>
- <artifactId>spring-cloud-demo</artifactId>
- <packaging>pom</packaging>
- <version>1.0-SNAPSHOT</version>
-
- <modules>
- <module>discovery</module>
- <module>service0</module>
- <module>service1</module>
- </modules>
-
-
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.4.0.RELEASE</version>
- </parent>
-
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-dependencies</artifactId>
- <version>Camden.SR2</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.boot</groupId>
- <artifactId>spring-boot-devtools</artifactId>
- <optional>true</optional>
- </dependency>
- </dependencies>
-
-
- </project>
3.1.3.如图步骤,完成Discovery
3.1.4.如图步骤完成Service0,Service1类似
3.1.5.简单到爆炸有没有...........,接下来先启动Discovery,然后启动Service0和Service1
3.1.6.打开浏览器,访问 localhost:8080 ,8080是Discovery里配置的端口号.一切顺利的话,可以看到:
3.1.7.已经成功注册了service0,service1两个服务
3.2.网关demo: 光是注册了服务还不行,这里可以再配一个网关,让服务调用有统一的入口.
3.2.1.通过上图配置后,首先启动Discovery,其次的服务和网关启动顺序随意.通过访问localhost:8083/service0/service0,即可看到,gateway帮我们转发了请求.
3.3.Ribbon负载均衡(未完待续)