微服务框架——SpringCloud
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的更多相关文章
- 微服务框架SpringCloud(Dalston版)学习 (一):Eureka服务注册与发现
eureka-server eureka服务端,提供服务的注册与发现,类似于zookeeper 新建spring-boot工程,pom依赖: <dependency> <groupI ...
- 微服务框架SpringCloud与Dubbo
#v1.0.0# 1.背景 Dubbo,是阿里巴巴服务化治理的核心框架,并被广泛应用于阿里巴巴集团的各成员站点.阿里巴巴近几年对开源社区的贡献不论在国内还是国外都是引人注目的,比如:JStorm捐赠给 ...
- 微服务框架——SpringCloud(三)
1.Zuul服务网关 作用:路由转发和过滤,将请求转发到微服务或拦截请求.Zuul默认集成了负载均衡功能. 2.Zuul实现路由 a.新建springboot项目,依赖选择 Eureka Discov ...
- 微服务框架——SpringCloud(二)
1.Feign声明式服务调用(负载均衡+熔断器) a.概念:Feign是一个声明式的Web Service客户端,它的目的就是让Web Service调用更加简单.Feign整合了Ribbon和Hys ...
- 微服务框架——SpringCloud(四)
1.Spring Cloud Config 分布式配置 a.Config服务器 ①新建springboot项目,依赖选择Config Server ②pom文件关键依赖 <parent> ...
- java框架之SpringCloud(1)-微服务及SpringCloud介绍
微服务概述 是什么 业界大牛 Martin Fowler 这样描述微服务: 参考[微服务(Microservices)-微服务原作者Martin Flower博客翻译]. 下面是关于上述博客中的部分重 ...
- 基于Spring-Cloud的微服务框架设计
基于Spring-Cloud的微服务框架设计 先进行大的整体的框架整理,然后在针对每一项进行具体的详细介绍
- 微服务框架Dubbo与Springcloud的区别
微服务框架Dubbo与Springcloud的区别 微服务主要的优势如下: 1.降低复杂度 将原来偶合在一起的复杂业务拆分为单个服务,规避了原本复杂度无止境的积累.每一个微服务专注于单一功能,并通过定 ...
- springcolud 的学习(二).SpringCloud微服务框架
为什么选择SpringCloud因为SpringCloud出现,对微服务技术提供了非常大的帮助,因为SpringCloud 提供了一套完整的微服务解决方案,不像其他框架只是解决了微服务中某个问题. 服 ...
随机推荐
- libiconv交叉编译提示arm-none-linux-gnueabi-gcc
title: libiconv交叉编译提示arm-none-linux-gnueabi-gcc date: 2019/3/6 17:45:48 toc: true --- libiconv交叉编译提示 ...
- JSON使用与类型转换
JSON语法与对象 JSON方法与使用 一.JSON语法与对象 JSON是英文JavaScript Object Notation(JavaScript 对象表示法)的缩写,是存储和交换文本信息的语法 ...
- JavaScript数据类型 Math对象详解
前言 javascript使用算术运算符实现基本的算术运算,如果要实现更加复杂的算术运算,需要通过Math对象定义的常量和函数来实现.和其他对象不同,Math只是一个静态对象,并没有Math()构造函 ...
- 一次多个数据库tnsping及登录单点登录需求
[环境介绍] 系统环境:Linux + Oracle 11.2.0.4.0 + python 2.7.10 [背景描述] 需求:因为涉及生产数据库较多,业务夸多个数据库使用.当收到业务有些影响时,数据 ...
- linux mint18 cinnamon 64bit 安装 docker
参考官方文档:https://docs.docker.com/engine/installation/linux/ubuntu/ 1. 安装一些使 apt 可以使用 https 的源 sudo apt ...
- UE4物理笔记
基本 物理资源随骨骼创建,可添加到骨骼网格上. 物理材质可添加到材质或组件或物理资源上. 通过配置PrimitiveComponent组件的Collision Presets值,可实现自定义的碰撞忽略 ...
- 请求数据loading
请求数据加载,CSS3实现 HTML: <!--请求数据loading--> <div class="back_loading"> <div clas ...
- 记我在github上参与的Star增长最快的十万级项目。。。
前言 GitHub作为程序员的圣地. 用了两三年,一直都觉得,他可以代码托管,项目管理,为项目建立静态主页,个人简历,找工作,面试加分. 然而>>>....昨天才认识到我还是太年轻, ...
- MSYS 编译 nginx rtmp-module
1. 下载源码 http://hg.nginx.org/nginx nginx-c74904a17021.zip https://github.com/arut/nginx-rtmp-module n ...
- 04mycat数据切分
自定义切分文件 [root@mycat mycat]# cat conf/customer-hash-int.txt 101=0 102=0 103=0 104=1 105=1 106=1 Rule. ...