Eureka注册中心的搭建与使用

一.单机版eureka的搭建

  1. 使用restTemple进行消息的传递
  2. 搭建负载均衡(进行轮训测试)

1.服务端的构建

1.1导入依赖
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        
        <!--图形化界面所需要的依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
1.2编写application.yml文件

------单机版eureka注册的地址是自己

server:
  port: 7001

eureka:
  instance:
    hostname: localhost
  client:
    #不向eureka注册自己
    register-with-eureka: false
    #自己是服务端,不需要被检测
    fetch-registry: false
    service-url:
      defaultZone: http://localhost:7001/eureka/  单机版
      #defaultZone: http://localhost:7002/eureka/  #集群配置
  server:
    #eureka的自我保护机制
    enable-self-preservation: true

#服务名称
spring:
  application:
    name: eureka-7001
1.3主启动类添加注解

添加eurekaserver服务端的标识
在这里插入图片描述

2.服务的提供者

2.1导入依赖

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.example</groupId>
            <artifactId>cloud-commons</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.3.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>2.3.0.RELEASE</version>
        </dependency>

2.2编写application.yml

server:
  port: 8001


#注册到eureka
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      #defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka
      defaultZone: http://localhost:7001/eureka  单机版

2.3配置主启动类

在这里插入图片描述

2.4浏览器访问 localhost:7001

会出现服务注册进去的信息
在这里插入图片描述

2.Eureka集群的搭建

2.1 eurekaServer端的修改

修改yml文件
  • 构建相同的eurekaServer服务端
  • 两个服务相互注册
server:
  port: 7002


eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false  #不注册自己
    fetch-registry: false   #自己是服务,不被检索
    service-url:
      defaultZone: http://localhost:7001/eureka/
spring:
  application:
    name: eureka-7002

2.2 服务的提供者端的修改

yml文件的修改

注册到两台eureka中

server:
  port: 8001


#注册到eureka
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka
      #defaultZone: http://localhost:7001/eureka  单机版

3.eureka注册中心的使用案例(实现如下模式)

在这里插入图片描述

1.搭建提供者集群(具体业务代码就是一个简单的crud)

重新构建一个一样的服务提供者就行,端口修改一下就行
@RestController
@RequestMapping("/payment")
@Slf4j
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @Resource
    private DiscoveryClient discoveryClient;

    @PostMapping("/create")
    public CommentResult creat(@RequestBody Payment payment){
        int creat = paymentService.creat(payment);
        log.info("*******插入结果"+creat+"笑脸");
        if (creat>0){
            return new CommentResult(200,"插入成功"+serverPort,creat);
        }
        return new CommentResult(404,"插入失败",null);
    }

    @GetMapping("/get/{id}")
    public CommentResult getById(@PathVariable("id") Long id){
        Payment paymentById = paymentService.getPaymentById(id);
        log.info("查询成功:"+paymentById);
        if (paymentById !=null){
            return new CommentResult(200,"查询成功"+serverPort,paymentById);
        }
        return new CommentResult(404,"查询失败",null);
    }
}

2.搭建消费者80

2.1导入依赖
<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.example</groupId>
        <artifactId>cloud-commons</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.3.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
        <version>2.3.0.RELEASE</version>
    </dependency>
2.2注册到eureka集群
server:
  port: 80

spring:
  application:
    name: cloud-consumer
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka
2.3构建restTemple进行跨服务访问

在这里插入图片描述

2.4 Controller层进行调用
@RestController
@RequestMapping("/consumer")
public class ConsumerController {


	#通过注册到eureka的服务名称进行访问
    private static final String URL="http://CLOUD-PAYMENT";

    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/payment/create")
    public CommentResult<Payment> create(Payment payment){

       return restTemplate.postForObject(URL+"/payment/create",payment, CommentResult.class);
    }

    @GetMapping("/payment/get/{id}")
    public CommentResult<Payment> get(@PathVariable("id") Long id){

        return restTemplate.getForObject(URL+"/payment/get/"+id, CommentResult.class);
    }
}

3.服务的提供者进行负载均衡搭建

只需要在消费者方开启负载均衡
进行测试只需要在服务的提供端显示各自的端口号就可以看见效果

在这里插入图片描述

@Value("${server.port}")
    private String serverPort;


    @PostMapping("/create")
    public CommentResult creat(@RequestBody Payment payment){
        int creat = paymentService.creat(payment);
        log.info("*******插入结果"+creat+"笑脸");
        if (creat>0){
							#添加服务的端口号进行负载均衡效果查看
            return new CommentResult(200,"插入成功"+serverPort,creat);
        }
        return new CommentResult(404,"插入失败",null);
    }

4.actuator图形化界面的修改

#注册到eureka
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka
      #defaultZone: http://localhost:7001/eureka  单机版

#修改注册到eureka服务上的服务名和ip地址的显示
  instance:
    instance-id: payment8081
    prefer-ip-address: true

在这里插入图片描述

8001端口服务的发现discovery 获取注册到eureka服务的信息
1.配置启动类注解

在这里插入图片描述

2.编写cotroller
//注入discovery
 @Resource
 private DiscoveryClient discoveryClient;



@GetMapping("/discovery")
    public Object getDiscovery(){
        //获取服务的名称
        List<String> services = discoveryClient.getServices();
        for (String service : services) {
            log.info("*******"+service);
        }

        //获取服务的主机,端口,地址
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT");
        for (ServiceInstance instance : instances) {
            String host = instance.getHost();
            int port = instance.getPort();
            URI uri = instance.getUri();
            log.info(host+"\t"+port+"\t"+uri);
        }

        return discoveryClient;
    }
3.得到的信息如下

在这里插入图片描述

5.eureka的自我保护机制如何关闭(不建议关闭)

eureka:
  instance:
    hostname: localhost
  client:
    #不向eureka注册自己
    register-with-eureka: false
    #自己是服务端,不需要被检测
    fetch-registry: false
    service-url:
      #defaultZone: http://localhost:7001/eureka/  单机版
      defaultZone: http://localhost:7002/eureka/  #集群配置
  server:
    #eureka的自我保护机制----修改为false就关闭了
    enable-self-preservation: false

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值