Eureka之服务发现
本文介绍微服务作为Eureka Client如何从Eureka Server发现/拉取服务。
1 创建项目,导入依赖
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yiidian</groupId>
<artifactId>user_consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath />
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<!-- SpringCloud版本,F系列 -->
<spring-cloud.version>Finchley.RC1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 添加OkHttp支持 -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.9.0</version>
</dependency>
<!-- Eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<!-- SpringCloud依赖,一定要放到dependencyManagement中,起到管理版本的作用即可 -->
<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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
2 配置Eureka Client
建立application.yml,内容如下:
# Eureka服务拉取配置
eureka:
client:
register-with-eureka: false
fetch-registry: true # 服务消费方必须从Eureka服务器拉取注册信息,该值必须为true
service-url:
defaultZone: http://127.0.0.1:10086/eureka
3 编写启动类
package com.yiidian;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* 一点教程网 - http://www.yiidian.com
*
*/
@SpringBootApplication
@EnableEurekaClient // 开启Eureka客户端
public class UserConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(UserConsumerApplication.class, args);
}
}
4 使用RestTemplate拉取服务
1)在启动类实例化RestTemplate对象
package com.yiidian;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* 一点教程网 - http://www.yiidian.com
*
*/
@SpringBootApplication
@EnableEurekaClient // 开启Eureka客户端
public class UserConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(UserConsumerApplication.class, args);
}
/**
* 创建RestTemplate
*/
@Bean // 把RestTemplate放入IOC容器
public RestTemplate createRestTemplate() {
return new RestTemplate();
}
}
2)编写代码获取Eureka服务
@Autowired
private DiscoveryClient discoveryClient;
/**
* 使用RestTemplate从Eureka Server获取服务
*/
@RequestMapping("/{id}")
public User findById(@PathVariable("id")Integer id) {
//1.从Eureka获取服务ID
List<ServiceInstance> instances = discoveryClient.getInstances("user-service");
//2.获取第一个即可
ServiceInstance serviceInstance = instances.get(0);
String url = "http://"+serviceInstance.getHost()+":"+serviceInstance.getPort()+"/user/"+id;
System.out.println(url);
User user = restTemplate.getForObject(url, User.class);
return user;
}
热门文章
优秀文章