SPRINGCLOUD 开发学习记录
一个简单的微服务系统:服务注册和发现,服务消费,负载均衡,断路器,智能路由,配置管理
服务注册中心:
eureka是一个高可用组件,没有后端缓存,每一个实例注册后向注册中心发送心跳,默认情况下,erureka server也是一个eureka clinet,必须指定server
引入依赖:
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-eureka-server</artifactId>
- </dependency>
启用注册中心:
- @EnableEurekaServer
- @SpringBootApplication
- public class App
- {
- public static void main( String[] args )
- {
- SpringApplication.run(App.class, args);
- }
- }
YML配置:
- server:
- port: 8761
- spring:
- application:
- name: microservice-registry
- profiles:
- active: native
- eureka:
- instance:
- prefer-ip-address: true
- client:
- registerWithEureka: false
- fetchRegistry: false
#serverUrl:
# defaultZone: http://otherRegistry:port/eureka/
eureka客户端:
依赖:
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-eureka</artifactId>
- </dependency>
启动:
- @SpringBootApplication
- @EnableDiscoveryClient
- @RestController
- public class App
- {
- public static void main( String[] args )
- {
- SpringApplication.run(App.class, args);
- }
- @RequestMapping("/hi")
- public String home(@RequestParam String name) {
- return "hi "+name+",i am from port: " +port;
- }
- }
YML配置:
- spring:
- application:
- name: microservice-producer
- profiles:
- active: native
- eureka:
- instance:
- prefer-ip-address: true
- client:
- serviceUrl:
- defaultZone: http://localhost:8761/eureka/
在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务间基于http restful
springcloud有两种服务调用方式:ribbon+restTemplate和feign,ribbon是一个负载均衡客户端,可以很好控制http和tcp的一些行为,feign默认集成也ribbon
ribbon+restTemplate:
依赖:
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-eureka</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-ribbon</artifactId>
- </dependency>
启动:
- @SpringBootApplication
- @EnableDiscoveryClient
- public class App
- {
- public static void main( String[] args )
- {
- SpringApplication.run(App.class, args);
- }
- @Bean
- @LoadBalanced
- RestTemplate restTemplate() {
- return new RestTemplate();
- }
- }
调用:
- @Service
- public class HelloService {
- @Autowired
- RestTemplate restTemplate;
- public String hiService(String name) {
- return restTemplate.getForObject("http://eureka-producer/hi?name="+name,String.class);
- }
- }
YML配置:
- spring:
- application:
- name: microservice-ribbon-consumer
- profiles:
- active: native
- eureka:
- instance:
- prefer-ip-address: true
- client:
- serviceUrl:
- defaultZone: http://localhost:8761/eureka/
feign:
依赖:
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-eureka</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-feign</artifactId>
- </dependency>
启动:
- @SpringBootApplication
- @EnableDiscoveryClient
- @EnableFeignClients
- public class App
- {
- public static void main( String[] args )
- {
- SpringApplication.run(App.class, args);
- }
- }
调用:
- @RestController
- public class HiController {
- @Autowired
- SchedualServiceHi schedualServiceHi;
- @RequestMapping(value = "/hi",method = RequestMethod.GET)
- public String sayHi(@RequestParam String name){
- return schedualServiceHi.sayHiFromClientOne(name);
- }
- }
- @FeignClient(value = "eureka-producer")
- public interface SchedualServiceHi {
- @RequestMapping(value = "/hi",method = RequestMethod.GET)
- String sayHiFromClientOne(@RequestParam(value = "name") String name);
- }
yml配置:
- spring:
- application:
- name: microservice-feign-consumer
- profiles:
- active: native
- eureka:
- instance:
- prefer-ip-address: true
- client:
- serviceUrl:
- defaultZone: http://localhost:8761/eureka/
断路器:应付故障传播问题
ribbon+restTemplate:
依赖:
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-hystrix</artifactId>
- </dependency>
启动:
- @SpringBootApplication
- @EnableDiscoveryClient
- @EnableHystrix
- public class App
- {
- public static void main( String[] args )
- {
- SpringApplication.run(App.class, args);
- }
- @Bean
- @LoadBalanced
- RestTemplate restTemplate() {
- return new RestTemplate();
- }
- }
使用:
- @Service
- public class HelloService {
- @Autowired
- RestTemplate restTemplate;
- @HystrixCommand(fallbackMethod = "hiError")
- public String hiService(String name) {
- return restTemplate.getForObject("http://eureka-producer/hi?name="+name,String.class);
- }
- public String hiError(String name) {
- return "hi,"+name+",sorry,error!";
- }
- }
YML中不需要额外配置
feign:
依赖同上
启动:
- @SpringBootApplication
- @EnableDiscoveryClient
- @EnableFeignClients
- @EnableCircuitBreaker
- public class App
- {
- public static void main( String[] args )
- {
- SpringApplication.run(App.class, args);
- }
- }
使用:
- @FeignClient(value = "eureka-producer", fallback = SchedualServiceHiHystric.class)
- public interface SchedualServiceHi {
- @RequestMapping(value = "/hi",method = RequestMethod.GET)
- String sayHiFromClientOne(@RequestParam(value = "name") String name);
- }
- @Component
- public class SchedualServiceHiHystric implements SchedualServiceHi {
- @Override
- public String sayHiFromClientOne(String name) {
- return "sorry "+name;
- }
- }
yml配置:
额外添加:
- feign:
- hystrix:
- enabled:true
路由网关:
依赖:
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-zuul</artifactId>
- </dependency>
启动:
- @EnableZuulProxy
- @EnableDiscoveryClient
- @SpringBootApplication
- public class App
- {
- public static void main( String[] args )
- {
- SpringApplication.run(App.class, args);
- }
- }
yml配置:
- spring:
- application:
- name: microservice-gateway
- profiles:
- active: native
- eureka:
- instance:
- prefer-ip-address: true
- client:
- serviceUrl:
- defaultZone: http://localhost:8761/eureka/
- zuul:
- routes:
- microservice-producer:
- path: /produce/**
- serviceId: microservice-producer
配置管理:
服务器依赖:
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-config-server</artifactId>
- </dependency>
客户端依赖:
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-config</artifactId>
- </dependency>
服务器端启动:
- @EnableDiscoveryClient
- @SpringBootApplication
- @EnableConfigServer
- public class App
- {
- public static void main( String[] args )
- {
- SpringApplication.run(App.class, args);
- }
- }
客户端启动不需要额外添加
服务器端yml配置:
- server:
- port: 8000
- spring:
- application:
- name: microservice-config
- cloud:
- config:
- server:
- native:
- search-locations: classpath:/repo
- profiles:
- active: native
- eureka:
- client:
- serviceUrl:
- defaultZone: http://localhost:8761/eureka/
客户端yml配置:
- spring:
- application:
- name: microservice-gateway
- cloud:
- config:
- uri: http://localhost:8000
- fail-fast: true
- profiles:
- active: native
- eureka:
- instance:
- prefer-ip-address: true
- client:
- serviceUrl:
- defaultZone: http://localhost:8761/eureka/
监控管理:
依赖:
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-turbine</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-netflix-turbine</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
- </dependency>
启动:
- @SpringBootApplication
- @EnableTurbine
- @EnableDiscoveryClient
- @EnableHystrixDashboard
- public class App
- {
- public static void main( String[] args )
- {
- SpringApplication.run(App.class, args);
- }
- }
yml配置:
- server:
- port: 8770
- security.basic.enabled: false
- turbine:
- aggregator:
- clusterConfig: default # 指定聚合哪些集群,多个使用","分割,默认为default。可使用http://.../turbine.stream?cluster={clusterConfig之一}访问
- appConfig: microservice-feign-consumer,microservice-ribbon-consumer ### 配置Eureka中的serviceId列表,表明监控哪些服务
- clusterNameExpression: new String("default")
- # 1. clusterNameExpression指定集群名称,默认表达式appName;此时:turbine.aggregator.clusterConfig需要配置想要监控的应用名称
- # 2. 当clusterNameExpression: default时,turbine.aggregator.clusterConfig可以不写,因为默认就是default
- # 3. 当clusterNameExpression: metadata['cluster']时,假设想要监控的应用配置了eureka.instance.metadata-map.cluster: ABC,则需要配置,同时turbine.aggregator.clusterConfig: ABC
消息总线:将分布式节点用轻量的消息代理连接起来,可用于广播配置文件的更改或者服务之间的通讯,也可用于监控
SPRINGCLOUD 开发学习记录的更多相关文章
- SpringCloud开发学习总结(七)—— 声明式服务调用Feign(一)
在实践的过程中,我们会发现在微服务架构中实现客户端负载均衡的服务调用技术Spring Cloud Ribbon<SpringCloud开发学习总结(四)—— 客户端负载均衡Ribbon> ...
- SpringCloud开发学习总结(四)—— 客户端负载均衡Ribbon
通过上一章<SpringCloud开发学习总结(三)—— 服务治理Eureka>,我们已经搭建起微服务架构中的核心组件——服务注册中心(包括单点模式和高可用模式).同时还注册了一个服务,命 ...
- 寒假安卓app开发学习记录(3)
今天终于开始正式的安卓软件开发学习.开始用了大约一个小时的时间把创建第一个软件的学习视频观看了一下.跟着视频一边学习一边操作. 首先是创建项目,创建的过程和之前创建Java项目的过程相似.先给app起 ...
- SpringCloud的学习记录(1)
最近一段时间重新学习一边SpringCloud(有半年不用了),这里简单记录一下. 我用的是IntelliJ IDEA开发工具, SpringBoot的版本是2.1.3.RELEASE. 1. 构建M ...
- SpringCloud开发学习总结(六)—— 结合注解的AOP示例
面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型.利用AOP ...
- 微信小程序开发学习记录
两天撸了一遍小程序的文档,跟网页相似,个人感觉是简化版.但是因为开放了很多微信自带的接口又使得部分功能开发起来相对方便 思维导图如下: 目前我的理解大概是这么个逻辑,以后深入学习后可能会有更改 跟着大 ...
- SpringCloud开发学习总结(三)—— 服务治理Eureka
在最初开始构建微服务系统的时候可能服务并不多,我们可以通过做一些静态配置来完成服务的调用.比如,有两个服务A和B,其中服务A需要调用服务B来完成一个业务操作时,为了实现服务B的高可用,不论采用服务端负 ...
- SpringCloud开发学习总结(一)—— 基础知识
1:Dubbo和Spring Cloud的关系 就我个人对这两个框架的使用经验和理解,打个不恰当的比喻:使用Dubbo构建的微服务架构就像组装电脑,各环节我们的选择自由度很高,但是最终结果很有可能因为 ...
- 寒假安卓app开发学习记录(1)
今天是安卓软件开发的第一天.虽然之前有了对javaweb的学习,但是对基于安卓的软件开发还是一无所知.所以,第一步就是寻找学习资源,从慕课网上还有菜鸟教程上都找到了对应的教程.然后就开始了开发的第一步 ...
随机推荐
- 配置程序成为Linux服务
最近写了个程序需要随Linux启动时自动运行起来, 查了一些方法后, 通过配置程序成为系统的服务实现了这个需求, 在此记录一下. 测试程序 #! /bin/sh while [ true ] do e ...
- IOS学习5——属性与成员变量
[转]iOS中属性与成员变量的区别 ios中属性修饰符的作用 1. 属性用property声明 2. 简而言之,对于目前的ios开发,属性和成员变量的区别,完全可以不管. 3. 这个是历史原因造成的. ...
- 使用 webpack 打包 font 字体的问题
之前在使用 Vue 做项目的时候使用了 font 字体,然而在打包的时候 font 字体的引用路径不正确. 解决办法就是在 webpack 的配置文件中设置根路径 目录在 \config\index. ...
- C++11新语法糖之尾置返回类型
C++11的尾置返回类型初衷是为了方便复杂函数的声明和定义,但是当复杂度稍微提升一些的时候很明显能注意到这种设计的作用微乎其微. 首先考虑如下代码: C++ //返回指向数组的指针 auto func ...
- NP完整性| 集1(简介)
我们一直在写关于高效算法来解决复杂问题,如最短路径,欧拉图,最小生成树等.这些都是算法设计者的成功故事. 在这篇文章中,讨论了计算机科学的失败故事. 计算机可以解决所有的计算问题吗? 存在计算问题,即 ...
- lesson - 2 笔记 yum /single /rescue /
一. yum 作用: yum 命令是在Fedora 和RedHat 以及SUSE 中基于rpm 的软件包管理器,它可以使系统管理人员交互和自动化地更新与管理R ...
- node基础篇二:模块、路由、全局变量课堂(持续)
今天继续更新node基础篇,今天主要内容是模块.路由和全局变量. 模块这个概念,在很多语言中都有,现在模块开发已经成为了一种潮流,它能够帮助我们节省很多的时间,当然咱们的node自然也不能缺少,看下例 ...
- JAVA 用数组实现 ArrayList
我们知道 ArrayList 是一个集合,它能存放各种不同类型的数据,而且其容量是自动增长的.那么它是怎么实现的呢? 其实 ArrayList 的底层是用 数组实现的.我们查看 JDK 源码也可以发现 ...
- php使用websocket示例详解
一.php 中处理 websocket WebSocket 连接是由客户端主动发起的,所以一切要从客户端出发.第一步是要解析拿到客户端发过来的 Sec-WebSocket-Key 字符串. 复制代码代 ...
- 【转】java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax/persistence/Index;
最近在配置最新的ssh(struts2.3.16.3+hibernate4.3.7+spring4.1.2)的时候遇到的这个错误提示,后来在网上找了半天都不能解决,虽然有个说法是model对象用这样@ ...