赞
踩
1.Eureka-server就是一个注册中心,其他的Eureka-Client都是一个个的微服务,彼此间相互调用。
2.服务的提供仅仅需要引用spring-cloud-starter-netflix-eureka-client
3.服务的消费方式
1):feign,一个通过本地接口的形式来进行调用服务的,其中Feign中默认引入了Ribbon,以接口的形式进行调用服务,看起来简洁,而且Feign中还可以增加熔断器,来进行服务的熔断和降级,防止服务调用中的服务的雪崩
需要引用spring-cloud-starter-netflix-eureka-client和spring-cloud-starter-openfeign,
spring-cloud-starter-netflix-eureka-client讲此模块注册到Eureka-server
spring-cloud-starter-openfeign,调用远程的服务
2):ribbon,一个基于Http端的负载均衡,通过在Configuration中配置RestTemplate来进行调用,可以自定义负载均衡的方式,需要在配置Bean:RestTemplate时加上@LoadBalanced
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>spring-clould-Official</artifactId> <groupId>com.wx</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>Eureka-Client</artifactId> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</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-actuator</artifactId> </dependency> </dependencies> </project>
功效备注:
spring-cloud-starter-netflix-eureka-client
声明这是一个eureka客户端,要注册到eureka的服务之一
spring-boot-starter-web
使用@RestController等等注解,搭建MVC系统
spring-boot-starter-actuator
服务健康检查
eureka: client: serviceUrl: defaultZone: http://192.168.88.1:8761/eureka/ healthcheck: enabled: true # By default, the EurekaClient bean is refreshable, meaning the Eureka client properties can be changed and refreshed. When a refresh occurs clients will be unregistered from the Eureka server and there might be a brief moment of time where all instance of a given service are not available. One way to eliminate this from happening is to disable the ability to refresh Eureka clients. To do this set eureka.client.refresh.enable=false. refresh: enable: false instance: instanceId: ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}} leaseRenewalIntervalInSeconds: 30 server: port: 9001 spring: application: name: client01
package com.wx.client01;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
@SpringBootApplication
//EnableEurekaClient可省略
//@EnableEurekaClient
public class EurekaCliApplication01 {
public static void main(String[] args) {
new SpringApplicationBuilder(EurekaCliApplication01.class).web(true).run(args);
}
}
package com.wx.client01.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/hello") public class HelloController { @RequestMapping("/sayhi") public String sayHi(@RequestParam(value = "name" ,defaultValue = "Lin") String name){ return "Hi,"+name; } }
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>spring-clould-Official</artifactId> <groupId>com.wx</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>Eureka-Client-02</artifactId> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</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-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies> </project>
server: port: 9002 eureka: client: serviceUrl: defaultZone: http://192.168.88.1:8761/eureka/ healthcheck: enabled: true # By default, the EurekaClient bean is refreshable, meaning the Eureka client properties can be changed and refreshed. When a refresh occurs clients will be unregistered from the Eureka server and there might be a brief moment of time where all instance of a given service are not available. One way to eliminate this from happening is to disable the ability to refresh Eureka clients. To do this set eureka.client.refresh.enable=false. refresh: enable: false instance: instanceId: ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}} leaseRenewalIntervalInSeconds: 30 spring: application: name: client01
package com.wx.client01; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication //EnableEurekaClient可省略 //@EnableEurekaClient @EnableFeignClients public class EurekaCliApplication02 { public static void main(String[] args) { new SpringApplicationBuilder(EurekaCliApplication02.class).web(true).run(args); } }
package com.wx.client01.controller; import com.wx.client01.service.HelloService; import org.springframework.beans.factory.annotation.Autowired; //import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/hello") public class HelloController { @Autowired private HelloService helloService; @RequestMapping("/sayhi") public String sayHi(@RequestParam(value = "name" ,defaultValue = "opopopop") String name){ return helloService.sayHi(name); } }
package com.wx.client01.service; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; @Service @FeignClient(value = "client01",name = "client01") public interface HelloService { @RequestMapping(value = "/hello/sayhi") public String sayHi(@RequestParam("name") String name); }
package com.wx.client02.config; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class RestTemplateConfig { @Bean @LoadBalanced public RestTemplate restTemplate(){ return new RestTemplate(); } }
package com.wx.client02.controller; import com.wx.client02.service.HelloService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController @RequestMapping("/hello") public class HelloController { @Autowired private RestTemplate restTemplate; @RequestMapping("/sayRest/{name}") public String sayRest(@PathVariable("name") String name){ String serviceRest = restTemplate.getForObject("http://client01/hello/sayhi?name=" + name, String.class); return serviceRest; } }
- RoundRobinRule:轮询。
- RandomRule:随机。
- AvailabilityFilteringRule:先过滤掉由于多次访问故障而处于断路器跳闸状态的服务,以及并发连接数超过阈值的服务,剩下的服务,使用轮询策略。
- WeightedResponseTimeRule:根据平均响应时间计算所有服务的权重,响应越快的服务权重越高,越容易被选中。一开始启动时,统计信息不足的情况下,使用轮询。
- RetryRule:先轮询,如果获取失败则在指定时间内重试,重新轮询可用的服务。
- BestAvailableRule:先过滤掉由于多次访问故障而处于断路器跳闸状态的服务,然后选择一个并发量最小的服务。
- ZoneAvoidanceRule:复合判断 server 所在区域的性能和 server 的可用性选择服务器
package com.wx.client02.config; import com.netflix.loadbalancer.RandomRule; import com.netflix.loadbalancer.RoundRobinRule; import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.SmartInitializingSingleton; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.client.loadbalancer.LoadBalancerAutoConfiguration; import org.springframework.cloud.client.loadbalancer.RestTemplateCustomizer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; import java.util.List; @Configuration public class RestTemplateConfig { @Bean @LoadBalanced public RestTemplate restTemplate(){ return new RestTemplate(); } @Bean public RoundRobinRule randomRule(){ return new RoundRobinRule(); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。