1、什么是注册中心

(1)就是首先有一个eureka server,服务的注册与发现的中心
(2)你如果写好了一个服务,就可以将其注册到eureka server上去
(3)然后别人的服务如果要调用你的服务,就可以从eureka server上查找你的服务所在的地址,然后调用

2、Eureka基本原理

(1)服务都会注册到eureka的注册表
(2)eureka有心跳机制,自动检测服务,故障时自动从注册表中摘除
(3)每个服务也会缓存euraka的注册表,即使eureka server挂掉,每个服务也可以基于本地注册表缓存,与其他服务进行通信
(4)只不过是如果eureka server挂掉了,那么无法发布新的服务了

实验步骤

(1)启动和发布一个eureka server,注册中心,web界面可以看到所有注册的服务
(2)写一个hello world服务,注册到eureka server上去

3、eureka server

(1)pom.xml

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<!--eureka server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency> <!-- spring boot test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> <repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories> (2)Application @EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication { public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
} (3)application.yml server:
port: 8761 eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ (4)访问8761端口 4、eureka client (1)pom.xml <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> <repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories> (2)Application @SpringBootApplication
@EnableEurekaClient
@RestController
public class SayHelloServiceApplication { public static void main(String[] args) {
SpringApplication.run(SayHelloServiceApplication.class, args);
} @Value("${server.port}")
String port;
@RequestMapping("/sayHello")
public String home(@RequestParam String name) {
return "hello, " + name + " from port:" +port;
} } (3)application.yml eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8762
spring:
application:
name: service-say-hello (4)查看eureka server界面 (5)http://localhost:8762/sayHello?name=leo

  

通过ribbon+rest,RestTemplate调用rest服务接口,ribbon多个服务实例的负载均衡

2、创建一个新的工程,叫做greeting-service

(1)pom.xml

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> <repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories> (2)application.yml eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8764
spring:
application:
name: greeting-service (3)Application // @EnableDiscoveryClient,向eureka注册自己作为一个服务的调用client
// 之前的服务,EnableEurekaClient,代表的是向eureka注册自己,将自己作为一个服务
@SpringBootApplication
@EnableDiscoveryClient
public class GreetingServiceApplication { public static void main(String[] args) {
SpringApplication.run(GreetingServiceApplication.class, args);
} // 在spring容器中注入一个bean,RestTemplate,作为rest服务接口调用的客户端
// @LoadBalanced标注,代表对服务多个实例调用时开启负载均衡
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
} } (4)调用say-hello-service // 写一个服务,注入RestTemplate服务调用客户端
@Service
public class GreetingService { @Autowired
private RestTemplate restTemplate; // 用SAY-HELLO-SERVICE这个服务名替代实际的ip地址
// ribbon负载在多个服务实例之间负载均衡,每次调用随机挑选一个实例,然后替换服务名
public String greeting(String name) {
return restTemplate.getForObject("http://SAY-HELLO-SERVICE/sayHello?name="+name, String.class);
} } (5)controller @RestController
public class GreetingControler { @Autowired
private GreetingService greetingService; @RequestMapping(value = "/greeting")
public String greeting(@RequestParam String name){
return greetingService.greeting(name);
} } (6)多次访问http://localhost:8764/greeting?name=leo,发现每次访问的端口都不一样,在多个服务实例之间负载均衡了

  

hystrix去做资源隔离,限流,熔断,降级

1、让greeting-service支持hystrix

(1)pom.xml

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency> (2)application.xml feign.hystrix.enabled=true (3)SayHelloService @FeignClient(value = "say-hello-service", fallback = SayHelloServiceFallback.class)
public interface SayHelloService { @RequestMapping(value = "/sayHello", method = RequestMethod.GET)
public String sayHello(@RequestParam(value = "name") String name); } (4)SayHelloServiceFallback @Component
public class SayHelloServiceFallback implements SayHelloService { @Override
public String sayHello(String name) {
return "error, " + name;
} } (5)先保持SayHelloService启动,可以访问; 关闭SayHelloSerivce,再访问,调用失败直接走降级 包括限流,自动熔断,调用失败,都会走降级 2、hystrix dashboard (1)pom.xml <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency> (2)Application @EnableHystrixDashboard
@EnableCircuitBreaker (3)http://localhost:8764/hystrix 输入http://localhost:8764/hystrix.stream和title 访问接口,会在hystrix dashboard看到访问请求 3、改造say-hello-service支持hystrix 将一个服务多个实例的指标聚合起来看,改造say-hello-service (1)pom.xml <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency> (2)Application @SpringBootApplication
@EnableEurekaClient
@RestController
@EnableHystrix
@EnableHystrixDashboard
@EnableCircuitBreaker
public class SayHelloServiceApplication { public static void main(String[] args) {
SpringApplication.run(SayHelloServiceApplication.class, args);
} @Value("${server.port}")
private String port; @RequestMapping("/sayHello")
@HystrixCommand(fallbackMethod = "sayHelloFallback")
public String sayHello(String name) {
return "hello, " + name + " from port: " + port;
} public String sayHelloFallback(String name) {
return "error, " + name
} } (3)locahost:8762/hystrix 输入locahost:8762/hystrix.stream,2000,title 访问这个接口 4、创建turbin工程,hystrix-turbine-server (1)pom.xml <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.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> (2)Application @SpringBootApplication
@EnableTurbine
public class HystrixTurbineServer { public static void main(String[] args) {
new SpringApplicationBuilder(HystrixTurbineServer.class).web(true).run(args);
} } (3)application.yml spring:
application.name: hystrix-terbine-server
server:
port: 8765
security.basic.enabled: false
turbine:
aggregator:
clusterConfig: default
appConfig: say-hello-service
clusterNameExpression: new String("default")
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/ (4)对say-hello-service每个服务实例都访问几次 http://localhost:8762/hystrix stream输入:http://localhost:8765/turbine.stream 在dashboard可以看到两个服务实例聚合起来的指标

  

spring cloud应用的更多相关文章

  1. spring/spring boot/spring cloud开发总结

    背景        针对RPC远程调用,都在使用dubbo.dubbox等,我们也是如此.由于社区暂停维护.应对未来发展,我们准备尝试新技术(或许这时候也不算什么新技术了吧),选择使用了spring ...

  2. 转 Netflix OSS、Spring Cloud还是Kubernetes? 都要吧!

    Netflix OSS.Spring Cloud还是Kubernetes? 都要吧! http://www.infoq.com/cn/articles/netflix-oss-spring-cloud ...

  3. spring cloud 学习研究- spring-cloud-microservice-example

    spring cloud + docker 微服务架构 http://www.open-open.com/lib/view/open1437363835818.html 实例项目 https://gi ...

  4. Spring Cloud集成相关优质项目推荐

    Spring Cloud Config 配置管理工具包,让你可以把配置放到远程服务器,集中化管理集群配置,目前支持本地存储.Git以及Subversion. Spring Cloud Bus 事件.消 ...

  5. spring boot分布式技术,spring cloud,负载均衡,配置管理器

    spring boot分布式的实现,使用spring cloud技术. 下边是我理解的spring cloud的核心技术: 1.配置服务器 2.注册发现服务器eureka(spring boot默认使 ...

  6. Spring Cloud 配置服务

    Spring Cloud 配置服务 1. 配置服务简介 产生背景: 传统开发中,我们通常是将系统的业务无关配置(数据库,缓存服务器)在properties中配置,在这个文件中不会经常改变,但随着系统规 ...

  7. Microservices Reference Architecture - with Spring Boot, Spring Cloud and Netflix OSS--转

    原文地址:https://www.linkedin.com/pulse/microservices-reference-architecture-spring-boot-cloud-anil-alle ...

  8. 综合使用spring cloud技术实现微服务应用

    在之前的章节,我们已经实现了配置服务器.注册服务器.微服务服务端,实现了服务注册与发现.这一章将实现微服务的客户端,以及联调.实现整个spring cloud框架核心应用. 本文属于<7天学会s ...

  9. Spring cloud实现服务注册及发现

    服务注册与发现对于微服务系统来说非常重要.有了服务发现与注册,你就不需要整天改服务调用的配置文件了,你只需要使用服务的标识符,就可以访问到服务. 本文属于<7天学会spring cloud系列& ...

  10. 使用spring cloud实现分布式配置管理

    <7天学会spring cloud系列>之创建配置管理服务器及实现分布式配置管理应用. 本文涉及到的项目: 开源项目:http://git.oschina.net/zhou666/spri ...

随机推荐

  1. windows10删除多出的oem分区

    某次windows升级后,磁盘管理里新出现一个500多M的OEM分区 其实系统里本来就有一个OEM分区是第一个分区,大小499M,可能因为这个分区太小,系统就又新建一个 因为在windows10分区后 ...

  2. nginx的 ngx.var ngx.ctx ngx.req

    ngx.var 是获取 Nginx 的变量,需要经历字符串 hash.hash 表查找等过程. ngx.ctx 仅仅是一个 Lua table 而已,它的引用存放在 ngx_lua 的模块上下文(ct ...

  3. Nginx Rewrite相关功能-rewrite指令

    Nginx Rewrite相关功能-rewrite指令 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.

  4. yum lockfile is held by another process

    使用yum安装软件报错 yum lockfile is held by another process 解决方法 rm -f /var/run/yum.pid

  5. react.js知识汇总

    首先ract的基本结构 var Input = React.createClass({ getInitialState: function() { return {value: 'Hello!'}; ...

  6. 不刷新网页修改url链接:history.pushState()和history.replaceState()新增、修改历史记录用法介绍

    最近遇到了在不刷新页面的情况下修改浏览器url链接的需求,考虑到可以通过history.pushState()解决.现在将我理解的一些内容分享一下,不对的地方欢迎大家指出. 在使用方法前首先需要了解它 ...

  7. 项目中常用的js方法封装---自留

    1.输入一个值,返回其数据类型 type = para => { return Object.prototype.toString.call(para).slice(8,-1) } 2.冒泡排序 ...

  8. 原生 JS 实现最简单的图片懒加载

    懒加载 什么是懒加载 懒加载其实就是延迟加载,是一种对网页性能优化的方式,比如当访问一个页面的时候,优先显示可视区域的图片而不一次性加载所有图片,当需要显示的时候再发送图片请求,避免打开网页时加载过多 ...

  9. Spring Boot 《一》开发一个“HelloWorld”的 web 应用

    一,Spring Boot 介绍 Spring Boot不是一个新的框架,默认配置了多种框架使用方式,使用SpringBoot很容易创建一个独立运行(运行jar,内嵌Servlet).准生产级别的基于 ...

  10. [IOI 1994]数字三角形

    数字三角形 总时间限制: 1000ms 内存限制: 65536kB 描述 73 88 1 02 7 4 44 5 2 6 5 (图1) 图1给出了一个数字三角形.从三角形的顶部到底部有很多条不同的路径 ...