1. 什么是微服务?

“微服务”一词来自国外的一篇博文,网站:https://martinfowler.com/articles/microservices.html



如果您不能看懂英文文档,可以跳转到搜简体中文的文档



这是国人翻译的文档,可以学习参考:

引用官方文档解释:

简单来说,微服务架构风格[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 com.example.springcloud.provider.bean.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.stereotype.Service;
import 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();
} public User findUser(String user) throws InterruptedException {
log.info("username[{}]" , user);
String url = 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 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 org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController; @SpringBootApplication
@EnableEurekaClient
@RestController
public class SpringcloudServiceProviderApplication { @Autowired
UserService userService; public static void main(String[] args) {
SpringApplication.run(SpringcloudServiceProviderApplication.class, args);
} @GetMapping({"/api/users/{username}"})
@ResponseBody
public User findUser(@PathVariable("username")String username) throws InterruptedException{
User user= userService.findUser(username);
return user;
}
}

部署后,注册信息发布到eureka server:



服务注册信息打印到控制台



访问接口:http://localhost:8081/api/users/mojombo,能访问就是注册成功

提示:EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.

SpringCloud警告(Eureka):EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.

警告!Eureka可能存在维护了错误的实例列表(当它们没有启动的时候,Eureka却把它当成启动的了);Renews值小于Threshold值,因此剩下未过期的都是安全的。

方法:参考博客https://www.cnblogs.com/gudi/p/8645370.html,可以关了eureka的自我保护模式eureka.server.enableSelfPreservation=false

eureka也引用SpringBoot actuator监控管理,SpringBoot actuator可以参考我之前博客: SpringBoot系列之actuator监控管理极速入门与实践



具体可以进行配置,配置成eureka server的ip,加上actuator

eureka:
instance:
status-page-url-path: http://localhost:8761/actuator/info
health-check-url-path: http://localhost:8761/actuator/health

显示ip和实例id配置:



6. Eureka服务消费者

server:
port: 8082
spring:
application:
name: eureka-service-consumer
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
fetch-registry: true
register-with-eureka: false
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-consumer8082



关键点,使用SpringCloud的@LoadBalanced,才能调http://EUREKA-SERVICE-PROVIDER/api/users/? 接口的数据,浏览器是不能直接调的

package com.example.springcloud.consumer;

import com.example.springcloud.consumer.bean.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; @SpringBootApplication
@EnableEurekaClient
@RestController
public class SpringcloudServiceConsumerApplication { @Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(SpringcloudServiceConsumerApplication.class, args);
} @GetMapping("/findUser/{username}")
public User index(@PathVariable("username")String username){
return restTemplate().getForObject("http://EUREKA-SERVICE-PROVIDER/api/users/"+username,User.class);
}
}

附录:

ok,本博客参考官方教程进行实践,仅仅作为入门的学习参考资料,详情可以参考Spring Cloud官方文档https://cloud.spring.io/spring-cloud-netflix/reference/html/#registering-with-eureka

代码例子下载:code download

优质学习资料参考:

SpringCloud系列使用Eureka进行服务治理的更多相关文章

  1. 《springCloud系列》——Eureka 进行服务治理

    整理一下: @EnableEurekaServer 注册中心 @EnableDiscoveryClient 提供服务 @EnableFeignClients 消费者(Feign特有的,而且他自带断路器 ...

  2. spring cloud 入门系列二:使用Eureka 进行服务治理

    服务治理可以说是微服务架构中最为核心和基础的模块,它主要用来实现各个微服务实例的自动化注册和发现. Spring Cloud Eureka是Spring Cloud Netflix 微服务套件的一部分 ...

  3. 基于Eureka的服务治理

    代码地址如下:http://www.demodashi.com/demo/11927.html 一.服务的注册与发现 关系调用说明: 服务生产者启动时,向服务注册中心注册自己提供的服务 服务消费者启动 ...

  4. Spring Cloud 系列之 Eureka 实现服务注册与发现

    如果你对 Spring Cloud 体系还不是很了解,可以先读一下 Spring Cloud 都有哪些模块 Eureka 是 Netflix 开源的服务注册发现组件,服务发现可以说是微服务架构的核心功 ...

  5. springcloud系列11 整合微服务网关zuul

    这个模块是一个独立的模块所以需要建立一个模块, 首先引入: 依赖pom.xml <?xml version="1.0" encoding="UTF-8"? ...

  6. SpringCloud系列一:微服务理解

    1. 单体架构 一个归档包(例如war格式)包含所有功能的应用程序,通常称为单体应用. > 复杂性高:模块多,模块的边界模糊,依赖关系不清楚,代码质量参差不齐. > 技术债务:随着时间推移 ...

  7. Spring Cloud Netflix Eureka【服务治理】

    一.简介 二.使用 一.源码分析

  8. 【SpringCloud微服务实战学习系列】服务治理Spring Cloud Eureka

    Spring Cloud Eureka是Spring Cloud Netflix微服务中的一部分,它基于NetFlix Sureka做了二次封装,主要负责完成微服务架构中的服务治理功能. 一.服务治理 ...

  9. SpringCloud开发学习总结(三)—— 服务治理Eureka

    在最初开始构建微服务系统的时候可能服务并不多,我们可以通过做一些静态配置来完成服务的调用.比如,有两个服务A和B,其中服务A需要调用服务B来完成一个业务操作时,为了实现服务B的高可用,不论采用服务端负 ...

随机推荐

  1. Java BigDecimal和double BigDecimal类

    BigDecimal类 对于不需要任何准确计算精度的数字可以直接使用float或double,但是如果需要精确计算的结果,则必须使用BigDecimal类,而且使用BigDecimal类也可以进行大数 ...

  2. tomcat配置项目虚拟路径

    tomcat版本:apache-tomcat-7.0.42 参考:http://blog.csdn.net/pangdingshan/article/details/7214786 一.虚拟根目录 1 ...

  3. Flutter轮播图

    前端开发当中最有意思的就是实现动画特效,Flutter提供的各种动画组件可以方便实现各种动画效果.Flutter中的动画组件主要分为两类: 隐式动画控件:只需设置组件开始值,结束值,执行时间,比如An ...

  4. 飞越面试官(三)--JVM

    大家好!我是本公众号唯一官方指定没头屑的小便--怕屁林.   JVM,全称Java Virtual Machine,作为执行Java程序的容器,几乎代理了Java内存与服务器内存的交互,可以说是程序拥 ...

  5. P2607[ZJOI2008] 骑士 题解

    题目 Z 国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬. 最近发生了一件可怕的事情,邪恶的 Y 国发动了一场针对 Z 国的侵略战争.战火绵延五 ...

  6. Java基础笔记01-02-03-04

    一.今日内容介绍 1.Java开发环境搭建 2.HelloWorld案例 3.注释.关键字.标识符 4.数据(数据类型.常量) 01java语言概述 * A: java语言概述 * a: Java是s ...

  7. 【博客搭建】Typecho个人博客搭建,快速安装,超小白(很简单的)

    使用Typecho框架一个月又十二天了,就目前感觉来说,整体还不错,很多方面都支持个性化,二次开发,但是目前MD编辑器有一丢丢问题,不能同步滚动条滚动,就是编辑器区域滚动,预览区域没有动静,需要两边都 ...

  8. Nmap常见扫描方式流量分析

    环境说明 扫描者:manjaro linux , IP地址:192.168.31.160 被扫描者:centos 7,IP地址:192.168.31.175 分析工具:wireshark nmap 版 ...

  9. element-ui自定义table表头,修改标题样式、添加tooltip及 :render-header使用简介

    修改列标题样式1.在列标题后面加一个图标. 以element-ui官方文档一个table表格为例,我们在地址的后面加一个定位标志的图标,代码如下: <template> <el-ta ...

  10. JavaScript学习 Ⅶ

    十四. DOM(文档对象模型) 节点:Node--构成HTML文档最基本的单元 文档节点:整个HTML文档 元素节点:HTMl文档中的HTML标签 属性节点:元素的属性 文本节点:HTML标签中的文本 ...