配置服务在eureka中的显示名称,ip以及从服务获取注册信息 以及自我保护的关闭

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
#      defaultZone: http://localhost:7001/eureka
      defaultZone: http://eureka2.com:7002/eureka,http://eureka.com:7001/eureka
  instance:
    instance-id: pay321
    prefer-ip-address: true

配置最后的instance-id 以及prefer-ip-address
rueka显示服务名,鼠标移动左下角ip
只需要这样一个配置即可

获取注册信息 先注入DiscoverCliennt 是springcloud的 在8001的controler中写这样一个方法 即可获取服务的名称,主机,访问路径。等
在 主启动类上加这样一个注解就ok了。步加注解也是能获取到的。

@EnableDiscoveryClient
public class Pay8001 {
    @Autowired
    DiscoveryClient discoveryClient;
@GetMapping(value = "pay/info")
    public Object getInfo(){
    List services = discoveryClient.getServices();
    for (String service : services) {
        System.out.println("service = " + service);
        List instances = discoveryClient.getInstances(service);
        for (ServiceInstance instance : instances) {
            System.out.println(instance.getServiceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri()+"\t");
        }

    }
    return discoveryClient;

    }

8001服务中的yml配置

server:
  port: 8001
spring:
  application:
    name: pay-service

#    datasource.type=com.alibaba.druid.pool.DruidDataSource
#    spring.datasource.url = jdbc:mysql://127.0.0.1:3306/demo
#    spring.datasource.username = root
#    spring.datasource.password = 123456
#    spring.datasource.driverClassName = com.mysql.jdbc.Driver
#  datasource:
#    type:
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
#    单机模式
      defaultZone: http://localhost:7001/eureka
#集群模式
#      defaultZone: http://eureka2.com:7002/eureka,http://eureka.com:7001/eureka
  instance:
    instance-id: pay321
    prefer-ip-address: true
#    这个是为了和服务端沟通的频率
    lease-renewal-interval-in-seconds: 1
#    这个是服务端的耐心
    lease-expiration-duration-in-seconds: 2
#eureka.instance.lease-renewal-interval-in-seconds	# 续约更新时间间隔(默认30秒)
#eureka.instance.lease-expiration-duration-in-seconds # 续约到期时间(默认90秒)

添加了最后的两行
7001 eureka服务的配置

server:
  port: 7001
spring:
  application:
    name: eureka01
eureka:
  instance:
    hostname: eureka.com
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
#    单机模式
      defaultZone: http://eureka.com:7001/eureka
#      关闭自我保护
  server:
    enable-self-preservation: false
#     清理间隔(单位毫秒,默认是60*1000)
    eviction-interval-timer-in-ms: 2000
#      集群模式
#      defaultZone: http://eureka2.com:7002/eureka
#debug: true


#    datasource.type=com.alibaba.druid.pool.DruidDataSource
#    spring.datasource.url = jdbc:mysql://127.0.0.1:3306/demo
#    spring.datasource.username = root
#    spring.datasource.password = 123456
#    spring.datasource.driverClassName = com.mysql.jdbc.Driver
#  datasource:
#    type:

也是最后两行,一个是关闭自我保护机制,一个是清理,跟gc很像。定时清理。

eureka2.0 官网已经宣布停更了。以前的项目可能用的还是,新项目建议使用别的,后期介绍

你可能感兴趣的:(cloud)