spring|从零开始搭建spring cloud (一)创建eureka server注册中心
sring cloud简介
spring cloud 为开发人员提供了快速构建分布式系统中的一些通用模式(例如配置管理,服务发现,断路器,只能路由,微代理,控制总线,一次性令牌,全局锁,领导选举,分布式会话,群集状态)。分布式系统的协调导致了锅炉板模式,并且使用spring cloud开发人员可以快速的站起来实现这些模式的服务和应用程序。他们在任何分布式环境中正常工作。
高深的内容我也不说太多,这里我只说下springcloud中常用的组件
- 服务发现——Netflix Eureka
- 客服端负载均衡——Netflix Ribbon
- 断路器——Netflix Hystrix
- 服务网关——Netflix Zuul
- 分布式配置——Spring Cloud Config
创建eureka server注册中心
1.创建一个空的gradle项目,作为父项目,在gradle文件中引入依赖,spring boot版本为2.0.4.RELEASE,spring cloud版本为Finchley.RELEASE。这gradle文件作为其他模块的父文件,其他的模块依赖继承次依赖,代码如下
buildscript {
ext {
springBootVersion = '2.0.4.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}group 'spring-cloud'
version '1.0-SNAPSHOT'ext {
springCloudVersion = 'Finchley.SR1'
}allprojects {
repositories {
mavenLocal()
mavenCentral()
jcenter()
maven { url 'http://repo.spring.io/milestone' }
maven { url 'http://repo.spring.io/snapshot' }
maven { url 'https://repository.jboss.org/nexus/content/repositories/releases' }
maven { url 'https://oss.sonatype.org/content/repositories/releases' }
maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
maven { url 'http://repo.maven.apache.org/maven2' }
}}subprojects {
apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'war'
sourceCompatibility = 1.8
targetCompatibility = 1.8configurations {
providedRuntime
}dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
testCompile('org.springframework.boot:spring-boot-starter-test')
}dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}}
}
2.创建一个model工程,作为服务注册中心,即eureka server。
右键工程-->new-->moudle
文章图片
next-->选择项目类型为gradle项目
文章图片
next->选择cloud Discovery->Eureka Server,然后再下一步就好了
文章图片
3.对创建的工程进行修改,将注解@EnableEurekaServer加到springboot工程启动的application类上
文章图片
在父项目添加gradle管理,因为本项目采用的是将所有的依赖交给父项目进行管理,所以在模块中不需要配置gradle依赖,删除掉build.gradle的内容,在父项目中创建gradle管理文件夹,新建此模块的gradle
文章图片
在eureka中添加其独有的依赖
project(':maoj-eureka-registry'){
dependencies {
compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-server')
}
}
在父build.gradle文件中,使用apply from添加模块依赖的引用,在setting.gradle 添加 include('maoj-eureka-registry')
文章图片
文章图片
更改配置文件,将application.properties该为application.yml
server:
port: 8761eureka:
instance:
hostname: localhostclient:
register-with-eureka: false
fetch-registry: false
service-url:
default-zone: http://${eureka.instance.hostname}:${server.port}/eureka/spring:
application:
name: maoj-eureka-registry
在默认情况下eureka server本身也是一个eureka client,将register-with-eureka设置为false可以禁止将本身注册到注册中心中,fetch-registry设置为false来禁止从注册中心获取信息。
【spring|从零开始搭建spring cloud (一)创建eureka server注册中心】启动项目访问http://localhost:8761/即可出现spring eureka的注册中心界面。
推荐阅读
- Docker应用:容器间通信与Mariadb数据库主从复制
- 一个人的碎碎念
- 我从来不做坏事
- Activiti(一)SpringBoot2集成Activiti6
- 从蓦然回首到花开在眼前,都是为了更好的明天。
- 日志打卡
- 西湖游
- SpringBoot调用公共模块的自定义注解失效的解决
- 解决SpringBoot引用别的模块无法注入的问题
- 改变自己,先从自我反思开始