引入步骤
搭建Eureka Server
创建一个module用于搭建EurekaServer
pom.xml文件中加入对Eureka的支持
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
- 配置application.yml
加入Eureka相关配置到resources -> application.yml
-
配置节点eureka
-
加入一个instance节点
-
设置eureka hostname地址
-
设置client相关:
-
register-with-eureka 表示是否将自己注册进注册中心,因为本身就是注册中心不需要注册,false
-
fetch-registry 表示是否从Eureka中获取注册信息,因为是注册中心,也不需要。 false
-
serveri-url表示暴露给Eureka Client的请求地址:
- defaultZone: 访问url
-
server:
port: 9000
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
配置启动类
配置一个springboot的application启动类
- 加EnableEurekaServer的注解表明激活使用EurekaServer
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args)
{
SpringApplication.run(EurekaServerApplication.class, args);
}
}
服务提供者注册服务到Eureka Server上
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
修改application.yml添加Eureka Server相关信息
server:
port: 8081
spring:
application:
name: service-product
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8
username: root
password: root
jpa:
database: MySQL
show-sql: true
open-in-view: true
eureka:
client:
service-url:
defaultZone: http://localhost:9000/eureka/
instance:
prefer-ip-address: true
修改启动类,添加服务发现的支持 (可选)
这里可以选择使用netflix的@EnableEurekaClient注解,或者spring提供的@EnableDiscoveryClient注解,
甚至不用去添加EurekaClient的注解。系统会默认选择导入的EurekaClient
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EntityScan("cn.itcast.product.entity")
@EnableEurekaClient
public class ProductApplication {
public static void main(String[] args)
{
SpringApplication.run(ProductApplication.class, args);
}
}
测试注册服务到Eureka server
首先启动Eureka Server模块,然后启动带有Eureka client的product-service模块。
访问http ://localhost:9000/ 就能看到Eureka server的界面。这时候就能看到product-service的服务已经被注册
服务消费者通过注册中心获取服务列表并调用
就是服务的主机名, IP地址或者检查信息,这些信息可以通过eurekaServer进行获取,用于服务间的调用。
步骤基本上同上一个单元。
- 服务消费者引入Eureka Client依赖
- 配置application.yml添加Eureka Server相关信息
- 修改启动类,添加服务发现的支持 (可选)
- 关键调用代码
在服务调用者的调用外部服务的地方做代码修改。
注入一个spring包下面的DiscoveryClient,该对象用于获取元数据的工具类
可以调用里面的方法获取Eureka服务的元数据信息。并对外部服务进行访问
- 注入DiscoveryClient
- 调用处通过DiscoveryClient.getInstance获取到元数据实例的List
- 获取到需要的服务实例 (当前只有一个所以直接get(0)取得)
- 使用当前实例中的元数据拼装实际API的url调用http接口
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
public RestTemplate restTemplate;
@Autowired
public DiscoveryClient discoveryClient;
@RequestMapping(value = "/buy/{id}", method = RequestMethod.GET)
public Product findById(@PathVariable Long id) {
List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
ServiceInstance instance = instances.get(0);
Product product = null;
product = restTemplate.getForObject(instance.getUri().toString() + "/product/" + id,Product.class);
return product;
}
}