SpringCloud将微服务注册到Eureka Server

官档:https://cloud.spring.io/spring-cloud-static/Finchley.SR2/single/spring-cloud.html#netflix-eureka-server-starter

pom.xml 老版本方式:


    org.springframework.cloud
    spring-cloud-starter-eureka

 MicroserviceSimpleProviderUserApplication.java 

老版本方式:

@EnableEurekaClient (其实也是@EnableDiscoveryClient)

@EnableDiscoveryClient

package com.itmuch.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class MicroserviceSimpleProviderUserApplication {

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

 

 application.yml

 instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}

 实现如下staus格式:

 Eureka控制台加密码如下:SpringCloud将微服务注册到Eureka Server_第1张图片

 上面是官档的操作方法,但是可能不生效,如下解决方案:

application.yml

security:
  basic:
    enabled: true
  user:
    name: user
    password: password123
server:
  port: 8761
eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://user:password123@localhost:8761/eureka

pom.xml加入:


	org.springframework.boot
	spring-boot-starter-security

状态页和健康指示器 

SpringCloud将微服务注册到Eureka Server_第2张图片

pom.xml


	org.springframework.boot
	spring-boot-starter-actuator

http://localhost:7900/env     environment  可查看对应信息

http://localhost:7900/health  可查看对应信息 

官档上述路径一般不修改

SpringCloud将微服务注册到Eureka Server_第3张图片

 pom.xml

eureka:
  client:
    healthcheck:
      enabled: true
    serviceUrl:
      defaultZone: http://user:password123@localhost:8761/eureka

 

SpringCloud将微服务注册到Eureka Server_第4张图片

com.itmuch.cloud.controller.UserController

@GetMapping("/eureka-instance")
  public String serviceUrl() {
    InstanceInfo instance = this.eurekaClient.getNextServerFromEureka("MICROSERVICE-PROVIDER-USER", false);
    return instance.getHomePageUrl();
  }

 运行结果:

 com.itmuch.cloud.controller.UserController

@GetMapping("/instance-info")
  public ServiceInstance showInfo() {
    ServiceInstance localServiceInstance = this.discoveryClient.getLocalServiceInstance();
    return localServiceInstance;
  }

 运行结果:

 application.yml    Eureka服务注册配置如下:

eureka:
  client:
    healthcheck:
      enabled: true
    serviceUrl:
      defaultZone: http://user:password123@localhost:8761/eureka
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
    metadata-map:
      zone: ABC      # eureka可以理解的元数据
      lilizhou: BBC  # 不会影响客户端行为
    lease-renewal-interval-in-seconds: 5

prefer-ip-address: true 默认主机名称访问 可以置为true 变成支持IP访问

你可能感兴趣的:(springCloud)