Spring Cloud Eureka

服务发现

  • 谈到服务发现,阿里得Dubbo经典架构图如下:

  • 服务治理,发现Spring Cloud Eureka
  • 1 首先在pom.xml中引入对应依赖
  • 2 在主入口Application类中加入@EnableEurekaServer注解
  • 3 在配置文件中对服务中心进行配置
  • 4 运行程序体验Eureka服务发现

  • 创建spring-cloud-01-eureka-a项目 pom中引入依赖

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
  • 在主入口配置 @EnableEurekaServer注解
@EnableEurekaServer //启用服务器的配置中心
@SpringBootApplication
public class EurekaApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }
}
  • 在application.properties中配置
server.port=1000
eureka.client.service-url.defaultZone=http://eureka2:1000/eureka/
eureka.instance.hostname=eureka1
#客户端向服务器(注册中心发送心跳得时间间隔)
eureka.instance.lease-renewal-interval-in-seconds=10  
#服务器(注册中心)租期到期的时间, 也就是说服务器在收到最后一次心跳的时间上线
eureka.instance.lease-expiration-duration-in-seconds=120
  • 访问地址 localhost:1000 如图

Eureka.png

服务提供者 Provider

  • 实现了服务发现注册中心,接下来需要把自己得应用服务注册到SpringCloud Eureka注册中心上
    1. 对pom.xml进行配置
    1. 在主入口Application类中加入@EnableDiscoveryClient注解
  • 3 在配置文件中对服务中心进行配置
  • 4 最后先运行Eureka服务,在运行Provider服务提供者。然后观察Eureka信息

  • 创建spring-cloud-01-provider项目 pom中引入依赖
<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
</dependencies>
  • 在主入口配置 @EnableDiscoveryClient注解
@EnableDiscoveryClient    
@SpringBootApplication
public class ProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class, args);
    }
}
  • 在application.properties中配置
spring.application.name=provider-service
server.context-path=/provider
server.port=2001
eureka.client.service-url.defaultZone=http://eureka1:1000/eureka/
  • 创建IndexController类给consumer端调用
@RestController
public class IndexController {


    @RequestMapping(value = "/hello",method = {RequestMethod.GET})
    public String hello(){
        return "hello world";
    }
}

服务消费者 consumer

  • consumer和Provider 配置一样
  • 将consumer端也注册到服务中心
  • 主入口
@EnableDiscoveryClient
@SpringBootApplication
public class ConsumerApplication {

    @Bean
    @LoadBalanced //自动负载均衡 机制是通过 application name 去寻找服务发现 然后去做负载均衡
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

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

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping(value = "/getAppNameUrl", method = {RequestMethod.GET})
    public String getApplicationNameUrl(){
         ResponseEntity<String> response  = restTemplate.getForEntity("http://provider-service/provider/hello",String.class);
        System.err.println("body:" + response.getBody());
        return "调用成功";
    }

    @RequestMapping(value = "/getUrl", method = {RequestMethod.GET})
    public String getUrl() {
        RestTemplate rest = new RestTemplate();
        ResponseEntity<String> response = rest.getForEntity("http://localhost:2001/provider/hello", String.class);
        System.err.println("body:" + response.getBody());
        return "调用成功";
    }
}
  • 启动consumer端
 http://localhost:2000/consumer/getUrl 调用P端服务是通过路径调用得
 http://localhost:2000/consumer/getAppNameUrl 是通过P 端得application name调用得

高可用Eureka服务注册中心

 在微服务架构环境中,一个非常关键部分就是高可用性,需要充分考虑发生故障得路径,必须对
 各个组件进行高可用,只需要建立两个Eureka服务,把两个注册中心地址相互配置到对方地址即可。
 在P、C端加上eureka.client.service-url.defaultZone=http://eureka1:1000/eureka/,http://eureka2:1001/eureka/
 当一个服务中心宕掉,还可以访问

服务发现注册机制

  • 服务注册:服务提供者在启动时会通过发送REST请求得方式将自己注册到Eureka服务器上,同时带上了自身服务得一些元素信息。Eureka收到这个Rest请求后,将数据信息存储在一个双层结构Map中,其中一层得Key是服务名,第二层得Key是具体服务实例名。
  • 服务同步: 如果所示:这两个服务提供者分别注册到了两个不同得注册中心上,他们得信息分别被两个服务注册中心所维护,此时由于服务注册中心之间因为互相注册服务得关系,当服务提供者发送注册请求到一个服务注册中心得时候,它会将该请求转发给集群得其他注册中心,从而实现注册中心之间得服务同步过程。通过服务同步,两个服务提供者得服务信息就可以通过这两台服务注册中心得任意一台获取到。
  • 服务续约: 服务注册完成后,服务提供者会维护一个心跳来持续告诉注册中心“我还活者”防止注册中心“剔除该服务”也就是将服务列表排除出去。
  • 服务调用:消费者获得清单后,通过服务名可以获得具体提供服务得实例名和改实例得元数据信息,因为有这些服务实例得详细信息,所以客户端可以根据自己得需要决定调用哪个实例,在Ribbon中会默认采用轮询得方式进行服务调用,从而实现负载均衡。
  • 服务下线:在系统运行中必然会出现关闭或重启某个实例得情况,在关闭服务期间,我们自然不希望调用到已经下线得服务实例,所以客户端程序中,当服务实例正常关闭操作时,它会出发一个服务下线得Rest请求给Eureka,通知其下线,当然注册中心收到该请求后,会出发一个服务下先得Rest请求给Eureka,通知其下线,当然注册中心收到该请求后把其服务列表状态改为下线,并把该时间传播给齐群其他节点。当出现非正常下线时,注册中心可能并没有收到正常得下线通知请求,而这种情况,我们得Eureka自己会有一个内部得定时任务,每隔一段时间定时检查超时得清单进行剔除。

  • 最后Eureka其实没有Provider Consumer 之分,可以当P端 也可以当C端。两者可以互相调用。

  • GitHub地址
下节Ribbon
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值