当前位置:   article > 正文

Eureka 入门_eureka-client-1.9.2

eureka-client-1.9.2

Spring Cloud 和 Spring boot 的版本对应关系:

Release TrainBoot Version
2020.0.x aka Ilford2.4.x
Hoxton2.2.x, 2.3.x (Starting with SR5)
Greenwich2.1.x
Finchley2.0.x
Edgware1.5.x
Dalston1.5.x

为什么需要Eureka

1. url地址管理

很简单, 当一个项目拥有多个微服务的时候, 互相调用时, 消费者(Consumer) 需要记住 提供者(provoide) 的url地址。

如果有多个测试环境, 那么记录和管理这些url地址就会什么困难。

Eureka相当于1个地址本

相同环境下, 所有微服务启动时都往同1个Eureka 注册, 然后消费者只需要主机提供者的名字(service name) 就可以访问对应的提供者API
在配置文件里只需要记录Eureka注册中心的地址。

2. 负载均衡

如果一个微服务在两台服务器or 端口启动, 而且都用同1个service name 往 Eureka注册。 那么Eureka 就认为这个微服务启动两个实例
在这里插入图片描述

Eureka利用内部Ribbon
当其他微服务通过Eureka查找 提供者地址时,Eureka会发送不同的提供者实例地址给消费者, 就可以方便地实现负载均衡

![在这里插入图片描述](https://img-blog.csdnimg.cn/7606676ac41e49669e12940cf754aa24.png

remote call
Register/Renew/Cancel
Get Registry
Service Consumer
Service Provider
Eureka Server
3. 微服务状态感知

有了负载均衡后, 消费者 可以在多个 提供者中选择一个, 但是怕选择了1个坏的节点。

对于这点, Eureka有个心跳续约机制, 各个注册的微服务都必须每30秒发送1个请求给Eureka证明自己存在, 否则就会被踢出注册中心, 就避免消费者找到坏的提供者

搭建Eureka中心

1. POM 文件引入如下依赖

spring-cloud-dependencies 注意spring boot 和spring cloud的版本对应(本文开头)
spring-cloud-starter-netflix-eureka-server

2. 在Spingboot 启动类加上@EnableEurekaServer 注解

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
3. 配置文件加上下面的配置
server:
  port: 8761

spring:
  application:
    name: eurekaserver  # the name of eureka service


# eureka.client.fetch-registry=false
# fetch-registry=false
# Eureka server also act as a client and it try to register with other Eureka servers. You are getting this error as there is only one Eureka server running and trying to register with other.
#
# If you are planing to have single instance in production or working under development environment, you can disable their registry process( disable client like behaviour)
# if false, you could not see enurka register itself

eureka:
  client:
    service-url:
      defaultZone: http://0.0.0.0:8761/eureka # eureka service is also a micro service, it can register itself to Eureka
      # eureka support cluster, in that case, we need to put multiple url here
      # main page is http://127.0.0.1:8761 is not http://127.0.0.1:8761/eureka

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

把这个springboot项目打包部署, Eureka 就启动了

然后我们可以通过
http://ipaddress:port 打开管理页面,(不要加上/eureka)

注册demo-order-service 和 demo-user-service 服务

其中order是消费者, usersevice 是提供者, order service 必须call user service 得到user的detail 信息

1.pom.xml

在相应的springboot 项目中, 引用如Eureka相同的
springboot 和 spring cloud 版本

而且要引入下面这个依赖
spring-cloud-starter-netflix-eureka-client

2. 在配置文件中加上 Eureka 中心的地址
spring:
  application:
    name: demo-cloud-order-service # 不能用下划线_,  这个是注册在Eureka的service name

eureka:
  client:
    service-url:
      defaultZone: http://43.138.xxx.xxx:3366/eureka #url of eureka service
  instance:
    prefer-ip-address: true  # 用ip而不是hostname去注册, docker微服务必须

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

部署启动后就可以在管理页面见到这个服务被注册

服务发现

这是 在order service里, 就不必hardcode user serivce的地址, 直接用user service的service name当做hostname来访问

@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private RestTemplate restTemplate;

    private String user_service = "demo-cloud-user-service";

    public Order getOrderById(Long orderId) {
        // 1.get order object
        Order order = orderMapper.getOrderById(orderId);

        //2 use user service to get the user details
        String url = "http://" + user_service.toUpperCase(Locale.ROOT) + "/user/" + order.getUserId();
        System.out.println("url:" + url);

        User user = restTemplate.getForObject(url, User.class); // User.class means return format
        order.setUser(user);

        return order;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

负载均衡

我们只需要在对应的RestTemplate 这个Bean里加上注解就可以实现, 如果有多个提供者。

@Configurable
public class SpringConfiguration {

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/818125
推荐阅读
相关标签
  

闽ICP备14008679号