1. 创建子项目springcloud-eureka-7001并引入依赖
<!--eureka服务端~-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
2. application.yml
server:
port: 7001
#Eureka配置
eureka:
instance:
hostname: localhost #Eureka服务端的实例名称
client:
register-with-eureka: false # 表示是否向eureka注册中心注册自己
fetch-registry: false #fetch-registry如果为false,则表示自己为注册中心
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
3. 开启Eureka
package com.jx.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
//启动之后,访问 http://localhost:7001/
@SpringBootApplication
@EnableEurekaServer //EnableEurekaServer 服务端的启动类,可以接受别人注册进来~
public class EurekaServer_7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaServer_7001.class, args);
}
}
4. springcloud-provider-dept-8001中添加依赖(不带server)
<!--eureka客户~-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
5. 注册地址
#Eureka的配置,服务注册到哪里
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka/
instance:
instance-id: springcloud-provider-dept8001 # 修改eureka上的默认描述信息!
prefer-ip-address: true # true,可以显示服务的IP地址 ~
6. 启动类开启注册功能
@EnableEurekaClient
<!--actuator完善监控信息-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
#info配置
info:
app.name: jx-springcloud
company.name: jx.com
7. 获取服务列表
//获取一些配置的信息,得到具体的微服务!
@Autowired
private DiscoveryClient client;
//注册进来的微服务~,获取一些消息~
@GetMapping("/dept/discovery")
public Object discovery() {
//获取微服务列表的清单
List<String> services = client.getServices();
System.out.println("discovery=>services:" + services);
//得到一个具体的微服务信息,通过具体的微服务id,applicaioinName;
List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-DEPT");
for (ServiceInstance instance : instances) {
System.out.println(
instance.getHost() + "\t" +
instance.getPort() + "\t" +
instance.getUri() + "\t" +
instance.getServiceId()
);
}
return this.client;
}
@EnableDiscoveryClient//开启服务发现
http://localhost:8001/dept/discovery
8. Eureka集群
以同样方式创建springcloud-eureka-7002、springcloud-eureka-7003项目并修改注册中心的hostname,同时将其他两个注册中心进行关联
#springcloud-eureka-7001
hostname: eureka7001.com
#springcloud-eureka-7001
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
打开C:\Windows\System32\drivers\etc\hosts文件对localhost进行模拟映射
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
127.0.0.1 eureka7003.com
最后修改springcloud-provider-dept-8001的注册地址
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
分别访问http://eureka7001.com:7001、http://eureka7002.com:7002、http://eureka7003.com:7003进行测试