1.SpringCloud微服务框架

  a.概念:SpringCloud是基于SpringBoot的微服务框架

  b.五大神兽:Eureka(服务发现)、Ribbon(客服端负载均衡)、Hystrix(断路器)、Zuul(服务网关)、Spring Cloud Config(分布式配置)

2.Eureka服务发现

  a.组成:Eureka服务器和Eureka客户端

  b.Eureka服务器(注册中心)

    ①新建springboot项目,依赖选择Eureka Server

    ②pom文件关键依赖

    <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency> ...... </dependencies> ......

    ③application.yml文件

# 服务名称
spring:
application:
name: eureka-server
# 服务端口号
server:
port: 8081 #Eureka 相关配置
eureka:
instance:
hostname: localhost
client:
service-url:
defaultZone: http://localhost:${server.port}/eureka/
# 是否从其他的服务中心同步服务列表
fetch-registry: false
# 是否把自己作为服务注册到其他服务注册中心
register-with-eureka: false

    ④启动类添加注解@EnableEurekaServer

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication { public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
} }

    ⑤启动后访问 localhost:8081 可进入管理员界面

  c.Eureka客户端(生产者)

    ①新建springboot项目,依赖选择 Eureka Discovery 、Web

    ②pom文件关键依赖

    <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency> ...... </dependencies> ......

    ③application.yml文件

spring:
application:
name: eureka-client-producer
server:
port: 8082 eureka:
client:
service-url:
defaultZone: http://localhost:8081/eureka/

    ④启动类添加注解@EnableEurekaClient

@SpringBootApplication
@EnableEurekaClient
public class EurekaClientProducerApplication { public static void main(String[] args) {
SpringApplication.run(EurekaClientProducerApplication.class, args);
} }

    ⑤创建controller供外部调用

@RestController
public class CommonController { @Value("${server.port}")
String port; @Value("${spring.application.name}")
String name; @RequestMapping(value = "/printProducer")
public String printProducer(String param){
return "[" + name + "]请求参数:" + param + ",服务端口:" + port;
}
}

3.Ribbon负载均衡

  a.Eureka客户端(消费者) + Ribbon负载均衡

    ①新建springboot项目,依赖选择 Eureka Discovery 、Web 以及 Ribbon

    ②pom文件关键依赖

    <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency> ...... </dependencies> ......

    ③application.yml文件

spring:
application:
name: eureka-client-consumer
server:
port: 8085 eureka:
client:
service-url:
defaultZone: http://localhost:8081/eureka/

    ④启动类添加注解@EnableDiscoveryClient并且加入restTemplate

@SpringBootApplication
@EnableEurekaClient
public class EurekaClientConsumerApplication { public static void main(String[] args) {
SpringApplication.run(EurekaClientConsumerApplication.class, args);
} @Bean
@LoadBalanced
RestTemplate restTemplate()
{
return new RestTemplate();
} }

    ⑤使用restTemplate调用服务

@RestController
public class CommonController { @Resource
RestTemplate restTemplate; @RequestMapping(value = "/print")
public String print(String param){
String result = restTemplate.getForObject("http://EUREKA-CLIENT-PRODUCER/printProducer?param=" + param, String.class);
return result;
} }

4.Hystrix断路器

  a.消费者增加Hystrix依赖,pom中添加

        <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

  b.启动类添加注解@EnableHystrix

@SpringBootApplication
@EnableEurekaClient
@EnableHystrix
public class EurekaClientConsumerApplication { ...... }

  c.具体使用

@RestController
public class CommonController { @Resource
RestTemplate restTemplate; /* 断路器配置,当无法调用如下方法时,就会调用自定的errorCallback方法。 */
@HystrixCommand(fallbackMethod = "errorCallback")
@RequestMapping(value = "/print")
public String print(String param){
String result = restTemplate.getForObject("http://EUREKA-CLIENT-PRODUCER/printProducer?param=" + param, String.class);
return result;
} public String errorCallback(String param){
return "连接消费服务失败,请求参数:" + param;
} }

微服务框架——SpringCloud的更多相关文章

  1. 微服务框架SpringCloud(Dalston版)学习 (一):Eureka服务注册与发现

    eureka-server eureka服务端,提供服务的注册与发现,类似于zookeeper 新建spring-boot工程,pom依赖: <dependency> <groupI ...

  2. 微服务框架SpringCloud与Dubbo

    #v1.0.0# 1.背景 Dubbo,是阿里巴巴服务化治理的核心框架,并被广泛应用于阿里巴巴集团的各成员站点.阿里巴巴近几年对开源社区的贡献不论在国内还是国外都是引人注目的,比如:JStorm捐赠给 ...

  3. 微服务框架——SpringCloud(三)

    1.Zuul服务网关 作用:路由转发和过滤,将请求转发到微服务或拦截请求.Zuul默认集成了负载均衡功能. 2.Zuul实现路由 a.新建springboot项目,依赖选择 Eureka Discov ...

  4. 微服务框架——SpringCloud(二)

    1.Feign声明式服务调用(负载均衡+熔断器) a.概念:Feign是一个声明式的Web Service客户端,它的目的就是让Web Service调用更加简单.Feign整合了Ribbon和Hys ...

  5. 微服务框架——SpringCloud(四)

    1.Spring Cloud Config 分布式配置 a.Config服务器 ①新建springboot项目,依赖选择Config Server ②pom文件关键依赖 <parent> ...

  6. java框架之SpringCloud(1)-微服务及SpringCloud介绍

    微服务概述 是什么 业界大牛 Martin Fowler 这样描述微服务: 参考[微服务(Microservices)-微服务原作者Martin Flower博客翻译]. 下面是关于上述博客中的部分重 ...

  7. 基于Spring-Cloud的微服务框架设计

    基于Spring-Cloud的微服务框架设计 先进行大的整体的框架整理,然后在针对每一项进行具体的详细介绍

  8. 微服务框架Dubbo与Springcloud的区别

    微服务框架Dubbo与Springcloud的区别 微服务主要的优势如下: 1.降低复杂度 将原来偶合在一起的复杂业务拆分为单个服务,规避了原本复杂度无止境的积累.每一个微服务专注于单一功能,并通过定 ...

  9. springcolud 的学习(二).SpringCloud微服务框架

    为什么选择SpringCloud因为SpringCloud出现,对微服务技术提供了非常大的帮助,因为SpringCloud 提供了一套完整的微服务解决方案,不像其他框架只是解决了微服务中某个问题. 服 ...

随机推荐

  1. 2017-12-19python全栈9期第四天第三节之iterable可迭代对象join之字符串和列表转换成字符串和range

    #!/user/bin/python# -*- coding:utf-8 -*-s = 'zd's1 = '_'.join(s)print(s1)li = ['zs','ls','ww','zl',' ...

  2. axios传参

    get //通过给定的ID来发送请求 axios.get('/user?ID=12345') .then(function(response){ console.log(response); }).c ...

  3. 080、Weave Scope 容器地图(2019-04-28 周日)

    参考https://www.cnblogs.com/CloudMan6/p/7655294.html   Weave Scope 的最大特点是会自动生成一张 Docker 容器地图,让我们能够直接的理 ...

  4. Turtle库的学习积累

    1.什么是turtle库 Python的Turtle库是一个直观有趣的图形绘制函数库,Turtle英文翻译过来是乌龟的意思,在绘图时可以想象成一只乌龟在移动. 2.绘图坐标体系 海龟的移动方向 3.绘 ...

  5. BSON数据格式

    BSON https://baike.baidu.com/item/BSON 概念 编辑 BSON()是一种类json的一种二进制形式的存储格式,简称Binary JSON,它和JSON一样,支持内嵌 ...

  6. 转载skbbuf整理笔记

    1.http://blog.csdn.net/yuzhihui_no1/article/details/38666589 2.http://blog.csdn.net/yuzhihui_no1/art ...

  7. idea2018注册

    1.在网上随便找一个注册码,lanyu的即可,注册时会被提示此注册码已被取消. 2.修改hosts文件,目录:C:\Windows\System32\drivers\etc\hosts,在文件中添加  ...

  8. Python爬虫之selenium各种注意报错

    刚刚写完第一个selenuim+BeautifulSoup实战爬虫 爬淘宝.发现代码写完后不加for 翻页的时候没什么问题 解析 操作 都没问题 也就是说第一页 的内容 完好 pagebtn=wait ...

  9. Interface Comparable<T>

    Interface Comparable<T> : 该接口对实现它的每个类的对象强加一个整体排序. 这个排序被称为类的自然排序 ,类的compareTo方法被称为其自然比较方法 . 参数 ...

  10. Python multiprocessing

     推荐教程 官方文档 multiprocess各个模块较详细介绍 廖雪峰教程--推荐 Pool中apply, apply_async的区别联系 (推荐)python多进程的理解 multiproce ...