一、将springboot拆分为springcloud的简单介绍
springcloud 主要组成部分有 eureka、hystrix、ribbon、feign、zuul、config.
当然还有其他很多组件,以上只是基础中的基础。
eureka作为服务注册中心
hystrix是项目的熔断机制,提升服务的健壮性
ribbon和feign都是用来做服务的负载均衡
zuul是网关组件,用于分发请求及过滤消息的功能
config作为整个springcloud的总配置服务
二、创建eureka-server服务
2.1 开始创建项目
2.2 修改配置文件
spring.application.name=eureka-server
# 应用服务 WEB 访问端口
server.port=8761
eureka.instance.hostname=localhost
#eureka注册服务地址
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
#不向注册中心注册自己
eureka.client.register-with-eureka=false
#不去检索服务信息
eureka.client.fetch-registry=false
2.3 服务启动类加上eureka-server的注解 @EnableEurekaServer
package com.example.eurekaserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
2.4运行测试 http://localhost:8761/
三、创建eureka-client项目
3.1 创建eureka-client
3.2 修改配置文件
spring.application.name=eureka-client
# 应用服务 WEB 访问端口
server.port=8762
#eureka注册服务地址
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
3.3 在启动类上添加注解 @EnableEurekaClient
package com.example.eurekaclient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
3.4 刷新eureka监控中心
四、将上一章的代码拷被到client中试运行
五、eureka集群搭建
https://mp.youkuaiyun.com/console/editor/html/88191285