1, Eureka
是Netflix开发的基于Rest服务的服务注册与服务发现的组件,包括EurekaServer用于监控注册的服务和接收请求发送给服务的提供者,EurekaClient为注册者
复制代码
2, Dubbo 是一个微服务整体架构的框架,提供的功能包括服务注册发现,远程调用,监控等待 对标于SpringCloud,而Dubbo的服务发现模块是基于zookeeper的
二,单机部署
1, 部署服务器
复制代码
server:
port: 7002
eureka:
server:
#开启(默认)自我保护机制
enable-self-preservation: true
#心跳包发送间隔,服务端收到心跳包后2秒后没有收到就会剔除服务
#eviction-interval-timer-in-ms: 2000
instance:
hostname: eureka7002
client:
#不向注册机注册自己
register-with-eureka: false
#在注册中心获取其他服务
fetch-registry: false
service-url:
#服务监控地址
defaultZone: http://eureka7001:7001/eureka/
复制代码
启动器选择服务
@EnableEurekaServer
@SpringBootApplication
@EnableDiscoveryClient
public class Eureka7001Application {
public static void main(String[] args) {
SpringApplication.run(Eureka7001Application.class,args);
}
}
复制代码
2, 注册服务者
复制代码
eureka:
instance:
instance-id: payment8002
prefer-ip-address: true
client:
register-with-eureka: true
fetch-registry: true
service-url:
#注册到注册中心
defaultZone: http://eureka7001:7001/eureka/
复制代码
对外发布服务
@EnableEurekaClient
@SpringBootApplication
@EnableDiscoveryClient
public class Payment8002Application {
public static void main(String[] args) {
SpringApplication.run(Payment8002Application.class, args);
}
}
获取服务信息
@Autowired
private DiscoveryClient discoveryClient;
@GetMapping("/payment/discovery")
public Object discovery() {
Map<Object,Object> map = new HashMap<>();
List<String> services = discoveryClient.getServices();
for (String s : services) {
List<ServiceInstance> instances = discoveryClient.getInstances(s);
map.put(s,instances);
}
map.put("discoveryClient",discoveryClient);
return map;
}
复制代码
3, 注册消费者
复制代码
server:
port: 80
eureka:
instance:
instance-id: consumer80
#暴露IP地址
prefer-ip-address: true
client:
#将自己注册到注册中心
register-with-eureka: true
#去注册中心获取其他服务地址
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka/
复制代码
/**
* RestTemplate:
* 提供了多种方式访问远程HTTP服务,是一种简单便捷访问restful服务模板类,
* 是Spring提供用于访问Rest服务的客户端模板工具集
*/
@Autowired
private RestTemplate template;
@GetMapping(value = "/consumer/payment/create")
public CommonResult<Payment> create(Payment payment) {
return template.postForObject(PAYMENT_URL + "payment/create", payment, CommonResult.class);
}
复制代码
三,集群部署
1, 部署服务器
复制代码
server:
port: 7001
eureka:
instance:
hostname: eureka7001
client:
#不向注册机注册自己
register-with-eureka: false
#去注册中心获取其他服务的地址
fetch-registry: false
service-url:
#两个服务器相互注册
defaultZone: http://eureka7002:7002/eureka/
--------------------------------------------------------
server:
port: 7002
eureka:
server:
#开启(默认)自我保护机制
enable-self-preservation: true
#心跳包发送
# eviction-interval-timer-in-ms: 2000
instance:
hostname: eureka7002
client:
#不向注册机注册自己
register-with-eureka: false
fetch-registry: false
service-url:
#两个服务器相互注册
defaultZone: http://eureka7001:7001/eureka/
复制代码
2, 注册提供者
复制代码
eureka:
instance:
instance-id: payment8001
prefer-ip-address: true
client:
register-with-eureka: true
fetch-registry: true
service-url:
#同时注册两个服务器
defaultZone: http://eureka7001:7001/eureka/,http://eureka7002:7002/eureka/
-----------------------------------------------------------
eureka:
instance:
instance-id: payment8002
prefer-ip-address: true
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://eureka7001:7001/eureka/,http://eureka7002:7002/eureka/
复制代码
3, 注册消费者
复制代码
server:
port: 80
eureka:
instance:
instance-id: consumer80
prefer-ip-address: true
client:
#将自己注册到注册中心
register-with-eureka: true
#去注册中心获取其他服务地址
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka/,http://localhost:7002/eureka/
复制代码
https://juejin.im/post/5e8825c3f265da47cd3562b4