参考:

spring cloud 入门系列一:初识spring cloud

http://blog.didispace.com/Spring-Cloud%E5%9F%BA%E7%A1%80%E6%95%99%E7%A8%8B/

这里总结一些内容

一,spring cloud 入门系列一:初识spring cloud

了解spring cloud,不赘述

二,spring cloud 入门系列二:使用Eureka 进行服务治理

功能:创建服务注册和发现中心

首先,创建一个总的maven项目,然后里面其他的都创建 maven-module

关于pom文件,

springcloudtest总的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>com.mytest</groupId>
<artifactId>springcloudtest</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>eureak</module>
<module>helloservice</module>
<module>helloconsumer</module>
</modules> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent> <properties>
<javaVersion>1.8</javaVersion>
</properties> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies> </dependencyManagement> </project>

eureak  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">
<parent>
<artifactId>springcloudtest</artifactId>
<groupId>com.mytest</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>eureak</artifactId> <dependencies>
<!-- 引入eureka server依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency> </dependencies>
</project>

consumer  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">
<parent>
<artifactId>springcloudtest</artifactId>
<groupId>com.mytest</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>hello-consumer</artifactId> <dependencies>
<!-- 引入eureka 客户端依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- 引入ribbon 依赖 ,用来实现负载均衡,我们这里只是使用,先不作其他介绍-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency> </dependencies>
</project>

service  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">
<parent>
<artifactId>springcloudtest</artifactId>
<groupId>com.mytest</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>hello-service</artifactId> <dependencies>
<!-- 引入eureka 客户端依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency> </dependencies> </project>

关于同一个service启动两个不同端口:

1  先启动service,然后选择 edit configuration,配置如下:

2  修改application.properties中的port端口

3  启动就可以了。

结果如下:

访问页面:

PS:

如果controller和action都设置了@RequestMapping("/hello")

地址是:http://localhost:9090/hello/hello

如果只有action设置了@RequestMapping("/hello")

地址是:http://localhost:9090/hello

ConsumerApp中,

如果controller和action都设置了@RequestMapping("/hello")

restTemplate.getForObject("http://hello-service/hello/hello", String.class);

如果只有action设置了@RequestMapping("/hello")

restTemplate.getForObject("http://hello-service/hello", String.class);

三,spring cloud 入门系列三:使用Eureka 搭建高可用服务注册中心

功能:分布式

这个目前没测试。

四,spring cloud 入门系列四:使用Hystrix 实现断路器进行服务容错保护

功能:熔断措施

跟着教程配置service层,然后写入对应代码,重点是下面的注解。

@SpringCloudApplication = @EnableDiscoveryClient +@SpringBootApplication+@EnableCircuitBreaker
@HystrixCommand(fallbackMethod = "errorMsg")

值得注意的是运行顺序:

①  eureka-->helloservice1-->helloservice2-->consumer

②单独关闭helloservice2

③多次刷新页面,会出现  error!!!  字符串

五,spring cloud 入门系列五:使用Feign 实现声明式服务调用

功能:分布式和熔断措施

照着文章来 ,没什么问题。

不过这里要注意几点:

先看一下model结构:

1  首先我创建了一个 model层。所以每个运用到model的项目都要引入pom文件。

下面这个是我的feignconsumer的pom,其中model就是我创建的

<?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">
<parent>
<artifactId>springcloudtest</artifactId>
<groupId>com.mytest</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>feign-consumer</artifactId> <dependencies>
<!--引入model-->
<dependency>
<groupId>com.mytest</groupId>
<artifactId>model</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency> <!-- 引入eureka 客户端依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- 引入feign 依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency> </dependencies>
</project>

2  路由的controller问题

如果前面我们在hello-service中的controller和action上设定了

@RequestMapping("/hello")

那么在feignConsumerService中,记得要带上controller的路由

我的是如下:@RequestMapping("/hello/hello")

package com.mytest.service;

import com.mytest.User;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; /**
* Created by Tyler on 2018/8/27
*/
@FeignClient(name="hello-service")
public interface FeignConsumerService{ @RequestMapping("/hello/hello")
public void hello(); @RequestMapping("/hello/hello1")
public void hello1(@RequestBody User user);
}

3  我尝试了使用重载,既

@RequestMapping("/hello/hello")
public void hello(); @RequestMapping("/hello/hello")
public void hello(@RequestBody User user);

可是这样是错误的。

现阶段项目下载地址:(运行--eurake--helloservice--helloservice1--feignconsumer)

https://pan.baidu.com/s/1vjNM_Ud0D53U7LJEScidXw

六,spring cloud 入门系列六:使用Zuul 实现API网关服务

功能:设置路由规则

1,构建网关,配置路由

没什么问题,照着做就好了。

启动顺序:eureka---helloservice---feignconsumer--apigetway

访问地址:http://localhost:5555/api-a/feign-consumer

2,面向服务的路由

我的hello-service中设置了controller的@RequestMapping("/hello/hello"),所以地址如下

访问地址:http://localhost:5555/api-a/hello/hello

3,服务路由的默认规则

这里要访问:hello-service和feign-consumer的路径分别如下

http://localhost:5555/api-a/hello/hello

http://localhost:5555/api-b/feign-consumer

配置如下:

server.port=5555
spring.application.name=api-gateway zuul.routes.api-a.path=/api-a/**
#这里用serviceId代替url,用服务名代替ip+端口号
zuul.routes.api-a.serviceId=hello-service zuul.routes.api-b.path=/api-b/**
#这里用serviceId代替url,用服务名代替ip+端口号
zuul.routes.api-b.serviceId=feign-consumer
eureka.client.service-url.defaultZone=http://localhost:1111/eureka

现阶段项目下载地址:

https://pan.baidu.com/s/1D-ui1dMEHCAGsHWIrb94mQ

七,spring cloud 入门系列七:基于Git存储的分布式配置中心--Spring Cloud Config

首先,需要学习Git -->  Git学习

最新文章

  1. 用ShareSDK 进行第三方分享
  2. ASP.NET中的文件操作(文件信息,新建,移动,复制,重命名,上传,遍历)(亲测详细)
  3. COJ970 WZJ的数据结构(负三十)
  4. 各种element/format 在manage display 下的选项
  5. 【IOC--Common Service Locator】不依赖于某个具体的IoC
  6. [转载]JQuery的Ajax跨域请求的解决方案
  7. pureMVC java版搭建流程
  8. Web地图导图总结
  9. EF时,数据库字段和实体类不一致问题
  10. MySQL InnoDB 逻辑存储结构
  11. html5(五)拖放
  12. 算法--java实现将数字转换成人民币大写(迅雷面试题)
  13. Gym -102007 :Benelux Algorithm Programming Contest (BAPC 18) (寒假自训第5场)
  14. find_package()的查找*.cmake的顺序
  15. Ubuntu x86-64汇编(5) 控制指令
  16. matlab问题集总
  17. 重要:C/C++变量的自动初始化
  18. Seaborn-05-Pairplot多变量图
  19. [转] kerberos介绍
  20. CNN中已知input_size、kernel_size、padding、stide计算output公式的理解

热门文章

  1. 朱晔和你聊Spring系列S1E6:容易犯错的Spring AOP
  2. python第三章:循环语句--小白博客
  3. python第二章:数据类型--小白博客
  4. 使用C# HttpWebRequest进行多线程网页提交。Async httpclient/HttpWebRequest实现批量任务的发布及异步提交和超时取消
  5. Eclipse中Git的使用以及IDEA中Git的使用
  6. 微服务治理平台的RPC方案实现
  7. CSS颜色代码 颜色值 颜色名字大全
  8. Java中JDK和JRE的区别是什么?它们的作用分别是什么?
  9. Python之random模块
  10. WIN10 devtoolsuser