Eureka入门
2024-09-08 04:11:40
一个Eureka中分为eureka server和eureka client。其中eureka server是作为服务的注册与发现中心。eureka client既可以作为服务的生产者,又可以作为服务的消费者。
用三个模块简单演示一下:Provider ,Consumer , Eureka Server
Provider(提供者)和Consumer(消费者)都需要向 Eureka Server 注册到注册中心里。消费者通过一个动态方式获取到提供者的访问路径,然后去远程调用提供者的接口,并且这里用的是Restfull风格。
父工程pom文件: spring cloud是基于spring boot进行的开发,因此我们需要创建一个spring boot项目,我这里使用了一个父工程。
<modules>
<!--下面是三个子模块-->
<module>xzeureka-provider</module>
<module>xzeureka-consumer</module>
<module>xteureka-server</module>
</modules>
<!--spring boot环境-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.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 版本-->
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
</properties>
<!--引入spring cloud 依赖-->
<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>
一:eureka server实现
添加eureka server包。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
添加完成后,在我们的启动类中添加注解@EnableEurekaServer
@SpringBootApplication
// 启用EurekaServer
@EnableEurekaServer
public class EurekaApp {
public static void main(String[] args) {
SpringApplication.run(EurekaApp.class,args);
}
}
下面是配置文件 我这里用的是application.yml
server:
port: 8761
# eureka 配置
# eureka 一共有4部分 配置
# 1. dashboard:eureka的web控制台配置
# 2. server:eureka的服务端配置
# 3. client:eureka的客户端配置
# 4. instance:eureka的实例配置
eureka:
instance:
hostname: localhost # 主机名
client:
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka # eureka服务端地址,将来客户端使用该地址和eureka进行通信
register-with-eureka: false # 是否将自己的路径 注册到eureka上。eureka server 不需要的,eureka provider client 需要
fetch-registry: false # 是否需要从eureka中抓取路径。eureka server 不需要的,eureka consumer client 需要
更新使用properties文件配置
server.port=8761
eureka.instance.hostname=localhost
eureka.client.service-url.defalultZone: http://${eureka.instance.hostname}:${server.port}/eureka
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
演示:启动后访问8761端口
eureka client服务提供者注册 Provider
添加依赖
<dependencies>
<!--spring boot web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
然后在启动类中添加注解@EnableEurekaClient。
@SpringBootApplication
@EnableEurekaClient //该注解在新版本中可以省略
public class ProviderApp {
public static void main(String[] args) {
SpringApplication.run(ProviderApp.class,args);
}
}
Controller控制器的接受与返回
@RestController
@RequestMapping("/goods")
public class GoodsController {
@Autowired //查询数据库信息的service
private GoodsService goodsService;
@GetMapping("/findOne/{id}")
public Goods findOne(@PathVariable("id") Integer id){
Goods goods = goodsService.finOne(id);
return goods;
}
}
配置文件
server:
port: 8000
eureka:
instance:
hostname: localhost # 主机名
client:
service-url:
defaultZone: http://localhost:8761/eureka # eureka服务端地址,将来客户端使用该地址和eureka进行通信
spring:
application:
name: eureka-provider # 设置当前应用的名称。将来会在eureka中Application显示。将来需要使用该名称来获取路径
properties配置
server.port=8000
eureka.instance.hostname=localhost
eureka.client.service-url.defaultZone: http://localhost:8761/eureka
spring.application.name=eureka-provider
启动演示
eureka client服务消费者 Consumer
上面我们创建了一个注册中心和一个注册的服务,下面我们再通过eureka client来调用所注册的服务。
添加依赖
<dependencies>
<!--spring boot web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
在启动类添加两个注解
@EnableDiscoveryClient // 激活DiscoveryClient
@EnableEurekaClient
@SpringBootApplication
public class ConsumerApp {
public static void main(String[] args) {
SpringApplication.run(ConsumerApp.class,args);
}
}
在eureka中,实际上是不区分服务的消费者和服务生产者的,一个服务的消费者,同样也可以是一个服务的生产者。因此我们首先要做的就是再创建一个eureka client。作为消费者需要通过一个API来进行远程调用:创建一个配置类,创建RestTemplate来进行服务间的连接
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
配置文件
server:
port: 9000
eureka:
instance:
hostname: localhost # 主机名
client:
service-url:
defaultZone: http://localhost:8761/eureka # eureka服务端地址,将来客户端使用该地址和eureka进行通信
spring:
application:
name: eureka-consumer # 设置当前应用的名称。将来会在eureka中Application显示。将来需要使用该名称来获取路径
Controller的编写调用
/**
* 服务的调用方
*/
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
@GetMapping("/goods/{id}")
public Goods findGoodsById(@PathVariable("id") int id){
System.out.println("findGoodsById..."+id);
/*
//远程调用Goods服务中的findOne接口 实体类和sql查询
使用RestTemplate
1. 定义Bean restTemplate
2. 注入Bean
3. 调用方法
*/
/*
动态从Eureka Server 中获取 provider 的 ip 和端口
1. 注入 DiscoveryClient 对象.激活
2. 调用方法
*/
//演示discoveryClient 使用
List<ServiceInstance> instances = discoveryClient.getInstances("EUREKA-PROVIDER");
//判断集合是否有数据
if(instances == null || instances.size() == 0){
//集合没有数据
return null;
}
ServiceInstance instance = instances.get(0);
String host = instance.getHost();//获取ip
int port = instance.getPort();//获取端口
System.out.println(host);
System.out.println(port);
String url = "http://"+host+":"+port+"/goods/findOne/"+id;
// 3. 调用方法 路径和返回类型
Goods goods = restTemplate.getForObject(url, Goods.class);
return goods;
}
}
同Provider一样,启动后会被注册到Eureka Server里
在Controller里 注入了
@Autowired
private RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
通过他们 一个获取路径一个负责远程调用. 在Provider里 写好了一个Service 的查询和返回数据到Controller 最后返回给远程调用的消费者
实体类
上面图是提供者 地址 下面是消费者地址 地址不同 参数一样
再接再厉~继续完善更新
最新文章
- MSSQL 死锁查询
- 报告一个IE很奇葩的滚动条问题——百分比计算宽度为浮点数时的滚动条显示异常
- linux 添加新硬盘的方法
- Socket编程基本流程实践
- UNIX/Linux下C语言的学习路线
- 2016 ACM/ICPC Asia Regional Qingdao Online HDU5879
- Linux系统监视资源与进程管理
- error RC1205: invalid code page
- Android 4.4前后版本读取图库图片和拍照完美解决方案
- python3.4.3如何获取文件的路径
- [Windwos Phone] 实作地图缩放 MapAnimationKind 属性效果
- NFS文件系统配置 和 GLIBC更新
- IP数据报格式 及路由转发算法
- Tomcat简介
- 最长上升子序列 nlogn
- word2vec_文本相似度
- null类型
- UVa 815 洪水!
- 团队项目之UML图设计
- feignClient中修改ribbon的配置