分布式-SpringCloud-Eureka注册中心-SpringBoot

分布式-SpringCloud-Eureka注册中心-SpringBoot

前言:随着互联网的发展,网站规模不断扩大,所以单体服务无法满足日常所需,可以采用将各个任务模块分到不同的服务器中,但是例如用户模块需要使用订单模块,位于不同服务器的就涉及到RPC,所以就需要注册中心,将两个模块都注册在注册中心中,用户模块则可以在注册中心中找到想要的。下面就涉及到用户买票需要调用到卖票的模块的一些服务。

·新建项目

  1. 新建项目,建一个空白的项目,分别使用spring初始化器在里面放入三个module。此处三个模块名为eureka-server、consumer-user、provider-ticket。这边SpringBoot的版本为2.2.11。

    (1). 创建注册中心服务模块:
    eureka-server项目模块

    (2).创建消费者模块consumer-user:provider-ticket也如下所示,因为他们都需要在注册中心注册,所以需要使用spring初始化向导加入如下组件。因为此处通过web进行测试,所以还需要加入web组件。

在这里插入图片描述

·编写注册中心eureka-server服务

  1. 编写注册中心eureka-server服务:

(1)配置Eureka信息,在主配置文件中配置,如下

server:
  port: 8761
eureka:
  instance:
    hostname: eureka-server #eureka实例的主机名
  client:
    register-with-eureka: false #不把本身注册到eureka
    fetch-registry: false #不从服务上来获取服务的注册信息
    service-url:
      defaultZone: http://localhost:8761/eureka/  #指定eureka注册中心地址

defaultZone系统会有一个默认值,但我们这边通过自己指定。

(2)在主配置类中添加@EnableEurekaServer表示开启注册中心Eureka服务

package com.atguigu.eurekaserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

//注册中心
@EnableEurekaServer//启用注册中心Eureka服务
@SpringBootApplication
public class EurekaServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(EurekaServerApplication.class, args);
	}

}

	启动此模块的服务,在浏览器上访问localhost:,出现如下编写完成,

此服务需要一直开着

在这里插入图片描述

·编写售票中心provider-ticket子模块

  1. 编写售票中心provider-ticket子模块:
    (1)编写配置文件,主要指定端口、注册中心地址、应用名称,以及在注册服务的时候可以通过ip注册服务。
server:
  port: 8002

spring:
  application:
    name: provider-ticket

eureka:
  instance:
    prefer-ip-address: true #注册服务的时候使用ip注册
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/  #指定eureka注册中心地址

(2)编写其server端:

package com.atguigu.providerticket.service;

import org.springframework.stereotype.Service;

@Service
public class TicketService {

    public String getTicket(){
        System.out.println("8002");
        return "《我和我的祖国》";
    }

}

(3)编写controller控制器:

package com.atguigu.providerticket.controller;

import com.atguigu.providerticket.service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TicketController {

    @Autowired
    TicketService ticketService;

    @GetMapping("/ticket")
    public String getTicket(){
        String ticket = ticketService.getTicket();
        return ticket;
    }

}

启动此模块后,就会将其对应的信息注册到注册中心中,如下:

在这里插入图片描述

·编写用户模块**consumer-user

  1. 编写用户模块consumer-user

(1)编写配置文件,主要指定端口、注册中心地址、应用名称,以及在注册服务的时候可以通过ip注册服务。

spring:
  application:
    name: consumer-user

server:
  port: 8200

eureka:
  instance:
    prefer-ip-address: true #注册服务的时候使用ip注册
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/  #指定eureka注册中心地址

(2)为了简单直接创建controller控制器,测试能否实现通过注册中心请求获取到服务,因为ticket服务器返回的是String类型,所以传入String的字节码文件,代码如下:

package com.atguigu.consumeruser.controller;

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 UserController {

    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/buy")
    public String buyTicket(String name){
        String s = restTemplate.getForObject("http://PROVIDER-TICKET/ticket", String.class);
        return name+"买了"+s;
    }

}

(2)因为当前应用需要在注册中心中发现服务,所以在主类上添加开启发现服务,而RestTemplate是帮我们调用其他服务的,他是帮我们发送http请求的,在发http请求的时候可以发起负载均衡机制,如下注解@LoadBalanced:

package com.atguigu.consumeruser;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@EnableDiscoveryClient//开启发现服务
@SpringBootApplication
public class ConsumerUserApplication {

	public static void main(String[] args) {
		SpringApplication.run(ConsumerUserApplication.class, args);
	}

	@LoadBalanced //开启负载均衡机制
	@Bean
	public RestTemplate restTemplate(){
		return new RestTemplate();
	}

}

开启负载均衡机制后,能使两个ticket服务端相平衡,也就是发送请求的时候请求的次数是在两个服务端平衡。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值