eureka搭建 EurekaServer,服务注册,服务发现

eureka 原理
搭建 EurekaServer
服务注册
服务发现

eureka原理

Eureka架构中,微服务角色有两类:

        一:EurekaServer:服务端,注册中心

1.记录服务信息
2.心跳监控
 二: EurekaClient :客户端
1.Provider :服务提供者
(1)注册自己的信息到 EurekaServer
(2)每隔 30 秒向 EurekaServer 发送心跳
2.consumer :服务消费者
(1)根据服务名称从 EurekaServer 拉取服务列表
(2)基于服务列表做负载均衡,选中一个微服务后发起远程调用

搭建EurekaServer 

搭建EurekaServer分三步:

1.创建项目,引入spring-cloud-starter-netflix-eureka-server的依赖 

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

2.编写启动类,添加@EnableEurekaServer注解。

启动类:

package cn.itcase.eureka;

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

@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
    public static void main(String[] args){
        SpringApplication.run(EurekaApplication.class,args);
    }
}

3.创建application.yml文件,编写如下的配置:

server:
  port: 10010 # 服务端口
spring:
  application:
    name: eurekaserver # eureka的服务名称
eureka:
  client:
    service-url:  # eureka的地址信息
      defaultZone: http://127.0.0.1:10010/eureka

 这样我们就搭建好了EurekaServer,我们启动就可以看到eureka吧自己注册了上去,搭建成功了。

 服务注册

将模块user-service服务注册到EurekaServer步骤分两步,如下:

1.user-service项目的pom中引入spring-cloud-starter-netflix-eureka-client客户端的依赖。

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

2.application.yml文件中配置:

spring:
  application:
    name: userservice # eureka的服务名称
eureka:
  client:
    service-url:  # eureka的地址信息
      defaultZone: http://127.0.0.1:10010/eureka

        根据如上将其他模块orderserver也进行注册。注册完成后启动可以看到已经注册成功了。

 

1. 服务注册
        引入eureka-client依赖
        在application.yml中配置 eureka 地址
2. 无论是消费者还是提供者,引入 eureka-client 依赖、知道 eureka 地址后,都可以完成服务注册

服务发现

在模块orderserver中完成服务拉取userservice;服务拉取是基于服务名称获取服务列表,然后再对服务列表做负载均衡

1.修改OrderService的代码,修改访问的url路径,用服务名代替ip、端口改成如下:

package cn.itcast.order.service;

import cn.itcast.order.mapper.OrderMapper;
import cn.itcast.order.pojo.Order;
import cn.itcast.order.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private RestTemplate restTemplate;

    public Order queryOrderById(Long orderId) {
        // 1.查询订单
        Order order = orderMapper.findById(orderId);
        // 2.利用RestTemplate发起http请求,查询用户
        // 2.1 url路径
        String url = "http://userservice/user/" + order.getUserId();
        // 2.2 发送http请求,实现远程调用
        User user = restTemplate.getForObject(url, User.class);
        // 3.封装user到Order
        order.setUser(user);
        // 4.返回
        return order;
    }
}

2.order-service项目的启动类OrderApplication中的RestTemplate添加负载均衡注解:

package cn.itcast.order;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@MapperScan("cn.itcast.order.mapper")
@SpringBootApplication
public class OrderServiceApplication {

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

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序小白学编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值