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. 在小程序中实现 Mixins 方案

    摘要: 小程序开发技巧 作者:jrainlau 原文:在小程序中实现 Mixins 方案 Fundebug经授权转载,版权归原作者所有. 在原生开发小程序的过程中,发现有多个页面都使用了几乎完全一样的 ...

  2. Centos7 安装配置 Rabbitmq Cluster

    Rabbitmq介绍 RabbitMQ是由 LShift 提供的一个 Advanced Message Queuing Protocol (AMQP) 的开源实现,由以高性能.健壮以及可伸缩性出名的 ...

  3. mysql查询表大小

    工作中常用命令参考,收集如下: 查询表大小:select table_name, data_length from information_schema.tables where table_sche ...

  4. Linux C/C++编译过程中的各种not declared in this scope

    Linux C/C++编译时经常会"XXX was not declared in this scope" 原因可能是以下几种: 变量名或函数名写错了; 忘记定义了 没有成功链接到 ...

  5. ubuntu安装vbox虚拟机

    ubuntu安装vbox虚拟机 一.安装准备 1.查看主机配置 二.下载安装包    (建议将安装包下载并保存) a.下载virtualbox安装包 下载链接https://www.virtualbo ...

  6. cocoapods升级

    1.更新gem:sudo gem update --system 先要查看下源,如果源被墙了就换地址:https://gems.ruby-china.com 1.1.删除gem源:gem source ...

  7. vue2.0 实现导航守卫(路由守卫)---登录验证

    路由跳转前做一些验证,比如登录验证,是网站中的普遍需求. 对此,vue-route 提供的 beforeRouteUpdate 可以方便地实现导航守卫(navigation-guards). 导航守卫 ...

  8. java JSONObject

    JSONObject.has("key")方法首先判断是否含有该key字段,如果不存在该字段,返回false;如果存在此字段,还判断了该字段的value值是否为null,如果val ...

  9. phpcms网站迁移无法更新内容提示Table 'led_com.lz_' doesn't exist的解决方法

    新接的一位客户说要把旧phpcms网站迁移到新的服务器并更换新域名,这对ytkah是小菜一碟,但往往事与愿违,忽略了一些细节会很惨的.进入新站后台怎么都无法生成内容,提示Table 'led_com. ...

  10. wordpress nginx详细环境配置安装命令和相关问题解决

    很详细的有关WordPress和nginx的环境配置安装操作步骤 指南,适合新手一步步按照命令操作安装WordPress并运行在生产环境中. 操作步骤转载自: Heap Stack blog(ping ...