文章目录
Eureka简介
- 主要用于服务注册和发现
- 由服务器和客户端组成,主要用于服务的注册和发现
- 它的客户端是一个java客户端
1 创建 Eureka-server服务注册中心
1.1 创建Springboot项目
<!-- 引入的Eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
1.2 开启注解配置
在启动类上加 @EnableEurekaServer
1.3 添加application.yml配置
server:
port: 8011
eureka:
instance:
hostname: 127.0.0.1 #服务注册中心ip地址
client:
register-with-eureka: false # 是否将自身注册
fetch-registry: false #是否检索服务
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #服务注册中心的配置内容,指定服务注册中心的位置。注意此处/eureka是默认的路径后缀
1.4 启动测试
访问 http://127.0.0.1:8011/,出现下图
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0YjzQ7LT-1589351980748)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\1589341164235.png)]
目前只有服务,还没有实例注册
2 创建Eureka服务提供者
2.1 创建Springboot项目,添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
2.2 开启注解
@EnableEurekaClient
2.3 修改application.yml
server:
port: 8012
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8011/eureka/
spring:
application:
name: eureka-provider
2.4 创建controller
此处创建的目的主要为后续测消费者用,提供一个rest接口
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@GetMapping("/getUser")
public String getUser(){
return "写这么多,就为测个这";
}
}
2.5 启动测试
访问: http://127.0.0.1:8011,会发现已经注册上去了服务提供者
3 创建消费者
3.1 创建springboot项目,添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
3.2 开启注解,添加配置
下面代码干了这几个事,开启eureka注解,实例化RestTemple
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@EnableEurekaClient
@Configuration
public class Config {
@LoadBalanced
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
3.3 修改application.yml
server:
port: 8013
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8011/eureka/
spring:
application:
name: eureka-counmer
3.4 调用生产者,进行消费
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class TestController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/test")
public String getUser() throws Exception{
return restTemplate.getForObject("http://eureka-provider/getUser",String.class);
}
}
3.5 启动测试
启动后访问http://localhost:8013/test. 输出了服务提供者接口的返回值
也就是在使用的过程中,可以不用关注服务所在的节点信息,只需知道服务名称就可以了
4 负载均衡
把第二步创建的服务提供者,复制一份代码出来,然后改下端口号启动。服务名保持相同,就搞定基础的负载均衡了