springcloud使用eureka进行服务注册发现和负载均衡

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 负载均衡

把第二步创建的服务提供者,复制一份代码出来,然后改下端口号启动。服务名保持相同,就搞定基础的负载均衡了
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值