文章目录
cloud Discovery
====>Eureka Server
<dependencies>
<!-- eureka的服务端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
server:
port: 7001
eureka:
instance:
hostname: localhost ## 实例名称
client:
fetch-registry: false # 注册中心不向自己注册自己
register-with-eureka: false # 表示是否将自己注册到Eureka Server,不去检索服务
service-url:
# 设置与Eureka Server的地址,查询服务和注册服务都需要依赖这个地址.默认是http://localhost:7001/eureka/;多个地址可使用','风格.
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
@EnableEurekaServer
这个注解即可启动http://localhost:7001/
,如果出现下图,那么表示启动成功:cloud Discovery
==>Eureka Discovery
<dependencies>
<!--eureka客户端的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
server:
port: 8003
eureka:
client:
serviceUrl:
defaultZone: http://localhost:7001/eureka # eureka的暴露地址,直接注册
spring:
application:
name: eureka-client #应用的名称,在同一个eureka中必须不重复
http://localhost:7001
将会看到如下界面:192.168.1.1:eureka-client:8003
,现在我们需要改变名称,让开发者能够一目了然的知道这个服务是干什么的,配置如下:eureka.instance.instance-id=eureka-client:8003 #改变微服务的名称
eureka.instance.prefer-ip-address=true ## 显示IP地址
<!--actuator依赖,不需要填写version,由springBoot父类控制 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<!-- 读取以$符号开头的和$符号结尾的,都能够读取 -->
<delimiters>
<delimiter>$</delimiter>
</delimiters>
</configuration>
</plugin>
</plugins>
</build>
src/main/resuorces/application.yml
文件中添加信息即可,格式是键值对的形式,如下:# 说明info的信息,key-value的形式
info:
app.name: eureka-client
company.name: http://chenjiabing666.github.io
groupId: $project.groupId$
artifactId: $project.artifactId$
version: $project.version$
EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.
eureka:
server:
enable-self-preservation: false # 禁用自我保护模式
@RestController
public class DeptController {
@Resource
private DiscoveryClient discoveryClient; //服务发现的client
/**
* 服务发现
* @return
*/
@GetMapping("/dept/discovery")
public Object discovery(){
List<String> list=discoveryClient.getServices(); //获取所有的微服务实例
System.out.println(list);
//根据为微服务的名称获取实例
List<ServiceInstance> serviceList=discoveryClient.getInstances("EUREKA-PROVIDER");
for (ServiceInstance element : serviceList) {
System.out.println(element.getServiceId()+"\t"+element.getHost()+"\t"+element.getUri());
}
return this.discoveryClient;
}
}
@EnableDiscoveryClient
@SpringBootApplication
@EnableEurekaClient //eureka服务客户端
@EnableDiscoveryClient //eureka服务发现
public class EurekaProvider8001Application {
public static void main(String[] args) {
SpringApplication.run(EurekaProvider8001Application.class, args);
}
}
http://localhost:8001/eureka-provider/dept/discovery
控制台就会打印如下信息:EUREKA-PROVIDER 192.168.1.102 http://192.168.1.102:8001
eureka3.png
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
127.0.0.1 eureka7003.com
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com # 配置主机的名称,之前是需要在hosts文件中做映射的
client:
fetch-registry: false # 注册中心不向自己注册自己
register-with-eureka: false # 表示是否将自己注册到Eureka Server,默认为true.由于当前应用就是Eureka Server,故而设置为false.
service-url:
# 设置与Eureka Server的地址,查询服务和注册服务都需要依赖这个地址,多个地址可使用','风格,配置集群必须指向除自己之外的其他的eureka服务的地址
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
server:
port: 7002
eureka:
instance:
hostname: eureka7002.com # 配置主机的名称,之前是需要在hosts文件中做映射的
client:
fetch-registry: false # 注册中心不向自己注册自己
register-with-eureka: false # 表示是否将自己注册到Eureka Server,默认为true.由于当前应用就是Eureka Server,故而设置为false.
service-url:
# 设置与Eureka Server的地址,查询服务和注册服务都需要依赖这个地址,多个地址可使用','风格,配置集群必须指向除自己之外的其他的eureka服务的地址
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/
server:
port: 7003
eureka:
instance:
hostname: eureka7003.com
client:
fetch-registry: false # 注册中心不向自己注册自己
register-with-eureka: false # 表示是否将自己注册到Eureka Server,默认为true.由于当前应用就是Eureka Server,故而设置为false.
service-url:
# 设置与Eureka Server的地址
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7001.com:7001/eureka/
http://eureka7001.com:7001/
http://eureka7002.com:7002/
http://eureka7003.com:7003/
eureka.client.serviceUrl.defaultZone
的值为eureka集群中的值,配置如下:server:
port: 8003
eureka:
client:
serviceUrl:
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/,http://eureka7001.com:7001/eureka/ # eureka的暴露地址,指向集群中每一个eureka,多个用都好分隔
instance:
instance-id: eureka-client:8003 ## 改变eureka中显示微服务的名称
prefer-ip-address: true ## 访问路径可以显示服务主机的IP地址
spring:
application:
name: eureka-client #应用的名称,在同一个eureka中必须不重复
# 说明info的信息,key-value的形式
info:
app.name: eureka-client
company.name: http://chenjiabing666.github.io
server:
port: 7001
eureka:
# server:
# enable-self-preservation: false ##禁用自我保护机制,但是生产环境中不推荐
instance:
hostname: localhost
client:
fetch-registry: false # 注册中心不向自己注册自己
register-with-eureka: false # 表示是否将自己注册到Eureka Server,默认为true.由于当前应用就是Eureka Server,故而设置为false.
service-url:
# 设置与Eureka Server的地址,查询服务和注册服务都需要依赖这个地址.默认是http://localhost:8761/eureka/;多个地址可使用','风格.
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
server:
port: 8003
eureka:
client:
serviceUrl:
defaultZone: http://localhost:7001/eureka/ # eureka的暴露地址,直接注册
instance:
instance-id: eureka-client:8003 ## 改变eureka中显示微服务的名称
prefer-ip-address: true ## 访问路径可以显示服务主机的IP地址
spring:
application:
name: eureka-client #应用的名称,在同一个eureka中必须不重复