服务注册中心,用来对服务统一的管理和调用,这点很像spring的ioc
Euerka服务类
Euerka7001
- applcaition
server:
port: 7001
spring:
application:
name: Eureka7001
eureka:
instance:
hostname: localhost
client:
# false 表示不向注册中心注册自己
register-with-eureka: false
# false 表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
defaultZone: http://localhost:7001/eureka/
###BUG
- 启动server报错,版本问题,不能只在setting中修改jdk,还要再project structure中修改为jdk1.8
- 找不到@EnableEurekaClient
mvn -U idea:idea
mvn clean
mvn install
实现负载均衡
- 修改order的请求地址,设置为服务名
这样通过eureka来提供具体地址
public static final String PAYMENT_URL = "http://PAYMENT-SERVICE/payment/";
- 设置负载均衡
信息完善
eureka:
instance:
instance-id: payment8002 # 实例名
appname: payment-service # 服务名
prefer-ip-address: true #访问路径可以显示ip地址
服务发现
查询服务或者服务下实例的详细信息
- 添加控制器
@GetMapping("payment/discovery")
public Object discovery(){
List<String> services = discoveryClient.getServices();//获取服务名称
services.forEach(System.out::println);
List<ServiceInstance> instances = discoveryClient.getInstances("COMMONS");
List<ServiceInstance> instances1 = discoveryClient.getInstances("PAYMENT-SERVICE");
for (ServiceInstance instance : instances) {
System.out.println(instance.getInstanceId());
System.out.println(instance.getHost());
System.out.println(instance.getPort());
}
System.out.println("wdnmd");
for (ServiceInstance instance : instances1) {
System.out.println(instance.getInstanceId());
System.out.println(instance.getHost());
System.out.println(instance.getPort());
}
return this.discoveryClient;
}
- 添加标签
eureka自我保护
在服务超过一段时间没有发送心跳后,eureka不会立即将它从注册表中删除,默认开启
最后
以上就是潇洒玫瑰最近收集整理的关于SpringCloud Euerka的全部内容,更多相关SpringCloud内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复