Eureka服务治理

Eureka治理概念

spring cloud Eureka 实现了netflix Eureka 的标准, spring 对其进行了封装形成了spring cloud Eureka.
Eureka服务端: 服务的注册中心
Eureka客户端: 客户端向服务端注册自身提供的服务并且周期性的发起心跳来更新服务租约,与此同时, 该服务也从服务端查询
当前注册的服务的信息,并且把他们缓存到本地并且周期性的刷新服务的状态。


所以当服务A --> 服务B  的时候 , 如果发生了服务B宕机了, 那么服务端会擦除服务B在服务端的注册信息, 当服务A周期性的
发起心跳获取其他服务信息的时候就会发现服务B 不可用了, 那么就会使用到Hystrix 短路器(后期再研究)。
Hystrix 是对服务调用时候出现的一种异常情况的补救的一种措施,  以防止服务的链路调用形成服务的雪崩效应。

Eureka服务端的搭建

pom添加:
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
application.yml
server:
  port: 8761
spring:
  application:
    name: eurka-server
eureka:
  server:
    enable-self-preservation: false
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
application代码如下:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

Eureka客户端的搭建

pom 添加:
<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-netflix-hystrix</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
yaml 配置
server:
  port: 8763
spring:
  application:
    name: eurka-client
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
management:
  endpoints:
    web:
      exposure:
        include: '*'
      cors:
        allowed-origins: '*'
        allowed-methods: '*'
application 代码如下:
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@RestController
@EnableHystrix
@EnableHystrixDashboard
@EnableCircuitBreaker
public class EurekaClientApplication {

    /**
     * 访问地址 http://localhost:8762/actuator/hystrix.stream
     *
     * @param args
     */

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


    @Value("${server.port}")
    String port;

    @RequestMapping("/hi")
    @HystrixCommand(fallbackMethod = "hiError")
    public String home(@RequestParam(value = "name", defaultValue = "ninuxGithub") String name) {
        return "hi " + name +  " i am from port: " + port;
    }

    public String hiError(String name) {
        return "hi," + name + ",sorry,error!";
    }

}

访问:http://localhost:8762/hi?name=springCloud 查看客户端的调用测试
访问:http://localhost:8761/ 查看注册的服务信息