SpringCloud系列使用Eureka进行服务治理

1. 什么是微服务?
在这里插入图片描述
如果您不能看懂英文文档,可以跳转到搜简体中文的文档
在这里插入图片描述
这是国人翻译的文档,可以学习参考:
在这里插入图片描述

引用官方文档解释:

简单来说,微服务架构风格[1]是一种将一个单一应用程序开发为一组小型服务的方法,每个服务运行在自己的进程中,服务间通信采用轻量级通信机制(通常用HTTP资源API)。这些服务围绕业务能力构建并且可通过全自动部署机制独立部署。这些服务共用一个最小型的集中式的管理,服务可用不同的语言开发,使用不同的数据存储技术。

2. 什么是Spring Cloud?

  • Spring Cloud是一个分布式的整体解决方案的框架。基于Spring Boot开发。Spring Cloud 为开发者提供了在分布式系统(配置管理,服务发现,负载,网关,消息总线,集群管理,安全管理,分布式锁,分布式事务等等)中快速构建的工具,使用Spring Cloud的开发者可以快速的启动服务或构建应用、同时能够快速和云平台资源进行对接。

3. 什么是Spring Cloud Eureka?

  • Spring Cloud Eureka 是 Spring Cloud Netflix 微服务套件的一部分,基于 Netflix Eureka 做了二次封装,主要负责实现微服务架构中的服务治理功能。

  • Spring Cloud Eureka 是一个基于 REST 的服务,并且提供了基于 Java 的客户端组件,能够非常方便地将服务注册到 Spring Cloud Eureka 中进行统一管理。

4. Eureka服务注册中心

Eureka server:创建服务注册中心

环境准备:

  • JDK 1.8
  • SpringBoot2.2.1
  • SpringCloud(Hoxton.SR6)
  • Maven 3.2+
  • 开发工具
    • IntelliJ IDEA
    • smartGit

创建一个SpringBoot Initialize项目,详情可以参考我之前博客:SpringBoot系列之快速创建项目教程

在这里插入图片描述
pom,加上spring-cloud-starter-netflix-eureka-server

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

@EnableEurekaServer配置Eureka服务端:

package com.example.springcloud.server;

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

@SpringBootApplication
@EnableEurekaServer
public class SpringcloudEurekaServerApplication {

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

}

eureka服务端配置:

spring:
  application:
    name: eurka-server
server:
  port: 8761
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://localhost:8761/eureka/

在这里插入图片描述
启动项目,访问:http://localhost:8761

在这里插入图片描述

5. Eureka服务提供者

在Eureka中,服务提供者和服务消费者是Eureka client提供的,使用注解@EnableEurekaClient标明

新建SpringBoot Initializer项目
在这里插入图片描述

server:
  port: 8081
spring:
  application:
    name: eureka-service-provider
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
    register-with-eureka: true
    fetch-registry: true
    healthcheck:
      enabled: false
  instance:
    status-page-url-path: http://localhost:8761/actuator/info
    health-check-url-path: http://localhost:8761/actuator//health
    prefer-ip-address: true
    instance-id: eureka-service-provider8081

在这里插入图片描述
写个例子,以github用户为例:

package com.example.springcloud.provider.bean;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;

import java.io.Serializable;

/**
 * <pre>
 *  Github User
 * </pre>
 *
 * <pre>
 * @author mazq
 * 修改记录
 *    修改后版本:     修改人:  修改日期: 2020/07/27 17:38  修改内容:
 * </pre>
 */
@JsonIgnoreProperties(ignoreUnknown = true)
@Data
public class User implements Serializable {

    private String name;
    private String blog;

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", blog='" + blog + '\'' +
                '}';
    }
}

读取github用户信息:

package com.example.springcloud.provider.service;

import www.lanboyulezc.cn com.example.springcloud.provider.bean.User;
import www.fengminpt.cn lombok.extern.slf4j.Slf4j;
import www.shentuylgw.cn  org.springframework.boot.web.client.RestTemplateBuilder;
import www.xingyunylpt.com org.springframework.stereotype.Service;
import www.lecaixuanzc.cn org.springframework.web.client.RestTemplate;

/**
 * <pre>
 *  UserService
 * </pre>
 *
 * <pre>
 * @author mazq
 * 修改记录
 *    修改后版本:     修改人:  修改日期: 2020/07/27 17:42  修改内容:
 * </pre>
 */
@Service
@Slf4j
public class UserService {

    private final RestTemplate restTemplate;

    public UserService(RestTemplateBuilder restTemplateBuilder) {
        this.restTemplate = restTemplateBuilder.build(www.jucaiyle.cn);
    }


    public User findUser(String user) throws InterruptedException {
        log.info("username[{www.tyyleapp.com}]" , user);
        String url =www.shangdu2zc.cn String.format("https://api.github.com/users/%s", user);
        User results = restTemplate.getForObject(url, User.class);
        return results;
    }
}

@EnableEurekaClient指定eureka client:

package com.example.springcloud.provider;

import www.yongshiyule178.com com.example.springcloud.provider.bean.User;
import com.example.springcloud.provider.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import www.chuancenpt.com org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import www.jintianxuesha.com org.springframework.web.bind.annotation.GetMapping;
import www.tengyao3zc.cn org.springframework.web.bind.annotation.PathVariable;
import www.feihongyul.cn  org.springframework.web.bind.annotation.ResponseBody;
import www.jujinyule.com  org.springframework.web.bind.annotation.RestController;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值