一、构建Eureka Server
1、导入eureka-server的pom依赖
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.2.4.RELEASE</version>
</dependency>
2、在application.yaml中进行相关配置:
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、主启动类,在上面添加@EnableEurekaServer注解
package com.xingyu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer // 服务端启动类,可以接受其他服务注册进来
public class EurekaServer_7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaServer_7001.class, args);
}
}
4、运行后访问http://localhost:7001/,可以访问到Eureka的初始页面:
注意:此时的Instances currently registered with Eureke 为空,因为还没有服务提供者注册服务
二、配置服务提供者
springcloud-provider-dept-8001
1、导入pom依赖:
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
2、在application.yaml中进行相关配置:
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka/
instance:
instance-id: springcloud-provider-dept8001
3、在主启动类上添加@EnableEurekaClient注解:
package com.xingyu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient //在服务启动后,自动注册到eureka中
public class DeptProvider_8001 {
public static void main(String[] args) {
SpringApplication.run(DeptProvider_8001.class, args);
}
}
4、启动运行后,再次访问http://localhost:7001/,可以发现此时的Instances currently registered with Eureke 已经显示出来该实例:
三、其他操作
1、修改显示的实例id:
eureka:
instance:
instance-id: springcloud-provider-dept8001
2、配置点击实例链接后显示的内容(若不配置,显示的是404):
<!-- 完善监控信息 -->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.3.4.RELEASE</version>
</dependency>
info:
app.name: xingyu-springcloud
company.name: xingyu-XJTU
3、团队合作时,在服务中心可以通过 Discovery 实现服务发现机制:
启动类上增加注解:@EnableDiscoveryClient //服务发现
package com.xingyu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient //在服务启动后,自动注册到eureka中
@EnableDiscoveryClient //服务发现
public class DeptProvider_8001 {
public static void main(String[] args) {
SpringApplication.run(DeptProvider_8001.class, args);
}
}
编写对应controller进行测试:
...
import org.springframework.cloud.client.discovery.DiscoveryClient;
@Autowired
private DiscoveryClient client;
...
@ApiOperation("注册进来的微服务,获取一些消息")
@GetMapping("/dept/discovery")
public Object discovery(){
//获取微服务列表的清单
List<String> services = client.getServices();
System.out.println(services);
//得到一个具体的微服务信息,通过具体的微服务id获取(application)
List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-DEPT");
for (ServiceInstance inctance:instances) {
System.out.println(
inctance.getHost()+"\t"
+inctance.getPort()+"\t"
+inctance.getUri()+"\t"
+inctance.getInstanceId());
}
return client;
}
输出:
[springcloud-provider-dept]
DESKTOP-JSLIG7K 8001 http://DESKTOP-JSLIG7K:8001 springcloud-provider-dept8001