您的位置:首页 > 编程语言 > Java开发

SPRINGCLOUD(EUREKA+RIBBON+FEIGN+HYSTRIX)

2017-08-18 17:58 891 查看
这周人身体不适也没去运动就研究了下SPRINGCLOUD
看了半本周立大神的《Spring Cloud与Docker微服务实战》 给个赞!
跟着写了点helloWorld,对微服务稍微有了点概念,直接进入主题!


项目结构




springcloud eureka做服务发现 ~~ (接触过dubbo的同学应该比较熟悉这个词)


@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}


<?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>beacon</artifactId>
<groupId>com.panchen</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>beacon-eureka-node1</artifactId>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
</project>


server:
port: 8761

eureka:
instance:
hostname: localhost #Eureka实例的主机名
client:
register-with-eureka: false  #在默认设置下,该服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为
fetch-registry: false
service-url:
defaultZone: http://localhost:${server.port}/eureka


注册中心服务搭完后,是注册服务
springcloud 同样有 provider customer的概念


@EnableDiscoveryClient            //注册服务
@SpringBootApplication
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}

}


@RestController
public class HelloWorldController {
//注入发现客户端
@Autowired
private DiscoveryClient discoveryClient;

/**
* 注:@GetMapping("/{id}")是spring 4.3的新注解等价于:
* @RequestMapping(value = "/id", method = RequestMethod.GET)
* 类似的注解还有@PostMapping等等
* @param id
* @return user信息
*/
@GetMapping("/{id}")
public String findById(@PathVariable Long id) {
return "HelloWorld"+id;
}
}


server:
port: 7070
spring:
application:
name: beacon-provider-test    #命名应用

eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka   # 指定注册中心的地址
instance:
preferIpAddress: true


<?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>beacon</artifactId>
<groupId>com.panchen</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>beacon-provider-test</artifactId>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
</project>


接下来是消费端


/**
* RIBBON-NETFLIX
* 提高客户端的软件负载均衡算法
* 1.先选择Eureka Server,它优先选择在同一个Zone且负载较少的Server
* 2.根据用户指定的策略,在从Server取到的服务注册列表中选择一个地址(策略:轮询,随机,根据响应时间加权等,
* 与大底的负载均衡中间件相同)
*/

/**
* FEIGN
* 声明式的webService客户端
*/

/**
* HYSTRIX
* 熔断器--实现快速失败 侦测到某个远程服务器多次调用失败后,不再访问服务器,从而防止应用服务器浪费CPU资源IO资源
* 个人理解:监听异常的代理中间件
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableCircuitBreaker
public class ConsumerApplication {

public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}

// 实例化RestTemplate,开启均衡负载能力
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}


@RestController
public class HelloWorldController {

private static final org.slf4j.Logger log= LoggerFactory.getLogger(HelloWorldController.class);
@Autowired
private HelloWorldClient helloWorldClient;
@Autowired
private RestTemplate restTemplate;

@GetMapping("/{id}")
public String helloWorld(@PathVariable Long id){
return helloWorldClient.helloWorld(id);
}

@GetMapping("/again/{id}")
public String helloWorldAgain(@PathVariable Long id){
return helloWorldClient.helloWorld(id);
}
}


//匹配provider的spring application name
//hystrix fallback指定fallback类
@FeignClient(name = "beacon-provider-test",fallback = HelloWorldClient.HelloWorldFallBack.class )
public interface HelloWorldClient {

@RequestMapping("/{id}")
public String helloWorld(@RequestParam("id") Long id);

@Component
static class HelloWorldFallBack implements  HelloWorldClient{

@Override
public String helloWorld(Long id) {
return "fallback!";
}
}
}


server:
port: 8000
spring:
application:
name:beacon-customer-test
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka   # 指定注册中心的地址
instance:
prefer-ip-address: true

ribbon:
eureka:
enabled:true  #开启ribbon从eureka中获取服务列表


<?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>beacon</artifactId>
<groupId>com.panchen</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>beacon-customer-test</artifactId>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
</dependencies>
</project>


hystrix-dashboard --hystrix的可视化监控页面


/**
* 测试步骤:
* 1. 访问http://localhost:8030/hystrix.stream 可以查看Dashboard
* 2. 在上面的输入框填入: http://想监控的服务:端口/hystrix.stream进行测试 * 注意:首先要先调用一下想监控的服务的API,否则将会显示一个空的图表.
*/
@SpringBootApplication
@EnableHystrixDashboard
public class HystrixDashBoardApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(HystrixDashBoardApplication.class).web(true).run(args);
}
}


<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>


配完以上模块后启动测试


eureka可视化页面




调用服务




fallback




hystrix监控页面




之前也使用过dubbo 感觉两者配置复杂度55开把 但开发调用的时候springcloud更好理清思路!完!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: