参考:

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学习

springcloud 学习的更多相关文章

  1. SpringCloud学习之Ribbon

    一.负载均衡与Ribbon 负载均衡,在集群中是很常见的一个“名词”,顾名思义是根据一定的算法将请求分摊至对应的服务节点上,常见的算法有如下几种: 轮询法:所有请求被依次分发到每台应用服务器上,每台服 ...

  2. SpringCloud学习之feign

    一.关于feigin feigin是一种模板化,声明式的http客户端,feign可以通过注解绑定到接口上来简化Http请求访问.当然我们也可以在创建Feign对象时定制自定义解码器(xml或者jso ...

  3. SpringCloud学习系列之三----- 断路器(Hystrix)和断路器监控(Dashboard)

    前言 本篇主要介绍的是SpringCloud中的断路器(Hystrix)和断路器指标看板(Dashboard)的相关使用知识. SpringCloud Hystrix Hystrix 介绍 Netfl ...

  4. SpringCloud学习(二):微服务入门实战项目搭建

    一.开始使用Spring Cloud实战微服务 1.SpringCloud是什么? 云计算的解决方案?不是 SpringCloud是一个在SpringBoot的基础上构建的一个快速构建分布式系统的工具 ...

  5. SpringCloud学习系列之七 ----- Zuul路由网关的过滤器和异常处理

    前言 在上篇中介绍了SpringCloud Zuul路由网关的基本使用版本,本篇则介绍基于SpringCloud(基于SpringBoot2.x,.SpringCloud Finchley版)中的路由 ...

  6. springCloud学习总览

      写完最后一篇特意去看了看第一篇是什么时候写的---2018/11/19,到现在三个月多一点,总的来说这三个月通过<Spring 微服务实战>这本书,算是对微服务进行了一次扫盲学习.   ...

  7. SpringCloud学习笔记(2):使用Ribbon负载均衡

    简介 Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡工具,在注册中心对Ribbon客户端进行注册后,Ribbon可以基于某种负载均衡算法,如轮询(默认 ...

  8. SpringCloud学习笔记(3):使用Feign实现声明式服务调用

    简介 Feign是一个声明式的Web Service客户端,它简化了Web服务客户端的编写操作,相对于Ribbon+RestTemplate的方式,开发者只需通过简单的接口和注解来调用HTTP API ...

  9. SpringCloud学习笔记(4):Hystrix容错机制

    简介 在微服务架构中,微服务之间的依赖关系错综复杂,难免的某些服务会出现故障,导致服务调用方出现远程调度的线程阻塞.在高负载的场景下,如果不做任何处理,可能会引起级联故障,导致服务调用方的资源耗尽甚至 ...

  10. SpringCloud学习笔记(5):Hystrix Dashboard可视化监控数据

    简介 上篇文章中讲了使用Hystrix实现容错,除此之外,Hystrix还提供了近乎实时的监控.本文将介绍如何进行服务监控以及使用Hystrix Dashboard来让监控数据图形化. 项目介绍 sc ...

随机推荐

  1. java----牛客练习

    1. 形式参数就是函数定义时设定的参数.例如函数头 int min(int x,int y,int z) 中 x,y,z 就是形参.实际参数是调用函数时所使用的实际的参数.   真正被传递的是实参   ...

  2. 论一类每次修改log个结点更新的线段树标记方法

    楼房重建(BZOJ2957) 多次询问一个区间中大于区间内这个数之前所有数的数的数量. 每个线段树结点维护该节点的答案c和区间内最大值m.假设有函数get(x,cm)=结点x中答案>cm的长度. ...

  3. JS 有趣的JS

    一. var arr = []; for (var i = 0; i < 3; i++) { arr[i] = function() { console.log(i+'__') // 3 3 3 ...

  4. Python学习第十四篇——类初步使用及面向对象思想

    class Restaurant(): def __init__(self,restaurant_name,cuisine_type): self.name = restaurant_name sel ...

  5. 墨者学院——密码学加解密实训(Base64转义)

    地址:https://www.mozhe.cn/bug/detail/SW5ObnVFa05vSHlmTi9pcWhRSjRqZz09bW96aGUmozhe 在靶场中找到内容 解密 访问直接得key

  6. Web系统大规模并发——秒杀与抢购 秒杀系统优化与预防措施

    电商的秒杀和抢购,对我们来说,都不是一个陌生的东西.然而,从技术的角度来说,这对于Web系统是一个巨大的考验.当一个Web系统,在一秒钟内收到数以万计甚至更多请求时,系统的优化和稳定至关重要.这次我们 ...

  7. 123. 单词搜索(DFS)

    描述 给出一个二维的字母板和一个单词,寻找字母板网格中是否存在这个单词. 单词可以由按顺序的相邻单元的字母组成,其中相邻单元指的是水平或者垂直方向相邻.每个单元中的字母最多只能使用一次. 样例 给出 ...

  8. Mybatis测试用例

    package cn.zhangxueliang.mybatis.mapper; import static org.junit.Assert.*; import java.io.InputStrea ...

  9. zTree树形菜单使用实例

    在每个节点添加 id 和 pid, id 表示当前节点编号,pid 表示父节点编号 第一步:在页面显示菜单位置,添加 ul设置 class=”ztree” 第二步:开启简单数据格式支持 第三步:编写树 ...

  10. Hbase数据表解析

    demo为表的命名空间,user为表的名字you2个列族,一个为b.一个为o. NAME 为列族名,Replication_SCOPE实现一个远程集群的复制.compression数据压缩的类型 Hb ...