赞
踩
Release Train | Boot Version |
---|---|
2020.0.x aka Ilford | 2.4.x |
Hoxton | 2.2.x, 2.3.x (Starting with SR5) |
Greenwich | 2.1.x |
Finchley | 2.0.x |
Edgware | 1.5.x |
Dalston | 1.5.x |
很简单, 当一个项目拥有多个微服务的时候, 互相调用时, 消费者(Consumer) 需要记住 提供者(provoide) 的url地址。
如果有多个测试环境, 那么记录和管理这些url地址就会什么困难。
Eureka相当于1个地址本
相同环境下, 所有微服务启动时都往同1个Eureka 注册, 然后消费者只需要主机提供者的名字(service name) 就可以访问对应的提供者API
在配置文件里只需要记录Eureka注册中心的地址。
如果一个微服务在两台服务器or 端口启动, 而且都用同1个service name 往 Eureka注册。 那么Eureka 就认为这个微服务启动两个实例
Eureka利用内部Ribbon
当其他微服务通过Eureka查找 提供者地址时,Eureka会发送不同的提供者实例地址给消费者, 就可以方便地实现负载均衡

spring-cloud-starter-netflix-eureka-server
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
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
把这个springboot项目打包部署, Eureka 就启动了
然后我们可以通过
http://ipaddress:port 打开管理页面,(不要加上/eureka)
其中order是消费者, usersevice 是提供者, order service 必须call user service 得到user的detail 信息
在相应的springboot 项目中, 引用如Eureka相同的
springboot 和 spring cloud 版本
而且要引入下面这个依赖
spring-cloud-starter-netflix-eureka-client
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微服务必须
部署启动后就可以在管理页面见到这个服务被注册
这是 在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; } }
我们只需要在对应的RestTemplate 这个Bean里加上注解就可以实现, 如果有多个提供者。
@Configurable
public class SpringConfiguration {
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。