服务提供者:提供服务被人调用
消费者:调用被人服务 五、服务的注册与发现(Eureka)在这里,我们需要用的的组件上SpringCloudNetflix的Eureka,eureka是一个服务注册和发现模块。
4.1服务注册
4.1.1创建eurekaserver项目
4.1.2引入maven依赖
以上都是概念。image.png
以上是我的springcloud项目,eureka是注册中心,zuul是注册网关,ribbon和feign都是cloud的rpc远程调用。
zuul主要是用来配置网关 <parent> <groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.2.RELEASE</version><relativePath/><!--lookupparentfromrepository--></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><!--eurekaserver--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka-server</artifactId></dependency><!--springboottest--><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>SpringMilestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository></repositories>4.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.4启动EurekaServer
@SpringBootApplication @EnableEurekaServer publicclassEurekaServerApplication{publicstaticvoidmain(String[]args){SpringApplication.run(EurekaServerApplication.class,args);}}1.eureka.client.registerWithEureka=true#是否将自身注册
2.eureka.client.fetchRegistry=false#如果为true,启动时报警.
4.5打开eurekaserver界面的image.png
4.2服务提供者
创建一个服务提供者(eurekaclient),当client向server注册时,它会提供一些元数据,例如主机和端口,URL,主页等。Eurekaserver从每个client实例接收心跳消息。如果心跳超时,则通常将该实例从注册server中删除。4.2.1创建项目eurekaclient
4.2.2引入maven依赖 <parent> <groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.2.RELEASE</version><relativePath/><!--lookupparentfromrepository--></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>SpringMilestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository></repositories>4.2.3application.yml配置
eureka: client: serviceUrl: defaultZone:http://localhost:8761/eureka/ server: port:8762 spring: application: name:service-hi4.2.4发布服务
通过注解@EnableEurekaClient表明自己是一个eurekaclient. @SpringBootApplication @EnableEurekaClient @RestController publicclassServiceHiApplication{publicstaticvoidmain(String[]args){SpringApplication.run(ServiceHiApplication.class,args);}@Value("${server.port}")Stringport;@RequestMapping("/hi")publicStringhome(@RequestParamStringname){return"hi"+name+",iamfromport:"+port;}}需要指明spring.application.name,这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name。启动工程,打开http://localhost:8761,即eurekaserver的网址:
image.png
你会发现一个服务已经注册在服务中了,服务名为SERVICE-HI,端口为7862
这时打开http://localhost:8762/hi?name=forezp,你会在浏览器上看到:
hiforezp,iamfromport:8762application.yml配置
在工程的配置文件application.yml文件,指定程序名为service-feign,端口号为8765,服务注册地址为http://localhost:8761/eureka/,代码如下: eureka: client: serviceUrl: defaultZone:http://localhost:8761/eureka/ server: port:8765 spring: application: name:service-feign定义一个feign接口
@FeignClient(value="service-hi") publicinterfaceSchedualServiceHi{@RequestMapping(value="/hi",method=RequestMethod.GET)StringsayHiFromClientOne(@RequestParam(value="name")Stringname);}一个”/hi”的API接口
@RestController publicclassHiController{@AutowiredSchedualServiceHischedualServiceHi;@RequestMapping(value="/hi",method=RequestMethod.GET)publicStringsayHi(@RequestParamStringname){returnschedualServiceHi.sayHiFromClientOne(name);}}启动方式
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients publicclassSericeFeign{publicstaticvoidmain(String[]args){SpringApplication.run(SericeFeign.class,args);}}演示效果
启动程序,多次访问http://localhost:8765/hi?name=forezp(http://localhost:8765/hi?name=forezp),浏览器交替显示: hiforezp,iamfromport:8762 hiforezp,iamfromport:8763在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC),在SpringCloud可以用RestTemplate+Ribbon和Feign来调用。为了保证其高可用,单个服务通常会集群部署。由于网络原因或者自身的原因,服务并不能保证100%可用,如果单个服务出现问题,调用这个服务就会出现线程阻塞,此时若有大量的请求涌入,Servlet容器的线程资源会被消耗完毕,导致服务瘫痪。服务与服务之间的依赖性,故障会传播,会对整个微服务系统造成灾难性的严重后果,这就是服务故障的“雪崩”效应。
为了解决这个问题,业界提出了断路器模型。
7.1什么是Hystrix
Netflix开源了Hystrix组件,实现了断路器模式,SpringCloud对这一组件进行了整合。在微服务架构中,一个请求需要调用多个服务是非常常见的,如下图:image.png
较底层的服务如果出现故障,会导致连锁故障。当对特定的服务的调用的不可用达到一个阀值(Hystric是5秒20次)断路器将会被打开。
image.png
断路打开后,可用避免连锁故障,fallback方法可以直接返回一个固定值。
这篇文章基于上一篇文章的工程,首先启动上一篇文章的工程,启动eureka-server工程;启动service-hi工程,它的端口为8762。
在ribbon使用断路器
改造serice-ribbon工程的代码,首先在pox.xml文件中加入spring-cloud-starter-hystrix的起步依赖: <dependency> <groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-hystrix</artifactId></dependency>改造service
改造HelloService类,在hiService方法上加上@HystrixCommand注解。该注解对该方法创建了熔断器的功能,并指定了fallbackMethod熔断方法,熔断方法直接返回了一个字符串,字符串为”hi,”+name+”,sorry,error!”,代码如下: @Service publicclassHelloService{@AutowiredRestTemplaterestTemplate;@HystrixCommand(fallbackMethod="hiError")publicStringhiService(Stringname){returnrestTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);}publicStringhiError(Stringname){return"hi,"+name+",sorry,error!";}}在启动类上加入
@SpringBootApplication @EnableEurekaClient @EnableHystrix//断路器 publicclassApp{publicstaticvoidmain(String[]args){SpringApplication.run(App.class,args);}@Bean@LoadBalancedRestTemplaterestTemplate(){returnnewRestTemplate();}}本文内容总结:一、微服务架构,二、微服务架构,三、SpringCloud,4.2.5演示效果,一、服务消费者(Feign),Hystrix断路器,准备工作,演示效果,
原文链接:https://www.cnblogs.com/laojiao/p/9506158.html