您的位置:首页 > 编程语言 > Java开发

高效使用Eclipse开发微服务-注册中心(二)

2017-07-26 15:37 603 查看
上篇高效使用Eclipse开发微服务-环境准备(一)已经讲述了如何搭建开发环境,这篇文章将讲述如何开发微服务的注册核心

第一章:

      微服务最重要也是最需要解决的一个问题就是服务治理问题,那么注册中心由此而生,注册中心顾名思义为集中管理系统,当然注册中心是非必需的,除非你能在脑海中或者通过其它方式完全记住每个服务实例相关信息并且提供发现服务也是可以的,如图:



上图讲述的就是多个服务实例注册到注册中心,并且注册中心提供服务管理和发现服务,话不多说,大家了解了这个注册中心的含义就行了,有兴趣的可以去了解一下源码,笔者虽然看了源码,但是每个人的理解会不一样,所以在这里不在阐述其原理。

      之前我们的环境已经搭建完成,第一步我们通过Eclipse创建一个Gradle工程,如图:

创建完毕后初始化的目录结构是这样的:

接下来我们打开build.gradle文件,笔者默认的文件是这样的:

apply plugin: 'java'
apply plugin: 'eclipse'

sourceCompatibility = 1.5
version = '1.0'
jar {
manifest {
attributes 'Implementation-Title': 'Gradle Quickstart',
'Implementation-Version': version
}
}

repositories {
mavenCentral()
}

dependencies {
compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
testCompile group: 'junit', name: 'junit', version: '4.+'
}

test {
systemProperties 'property': 'value'
}

uploadArchives {
repositories {
flatDir {
dirs 'repos'
}
}
}

我们需要修改几个地方达到我们需要的引入以及相关环境定义:

1、首先构建插件变成了:

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

2、第一行插入spring boot版本定义:

buildscript {
ext {
springBootVersion = '1.5.4.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}

3、构建版本定义变成了:

sourceCompatibility = 1.8
version = '1.0.1-SNAPSHOT'

4、增加资源文件目录定义(非必须,可以使用默认):

sourceSets {
main {
resources {
srcDir 'src/main/config'
}
}
}

5、增加spring cloud依赖引入:

dependencies {
compile('org.springframework.boot:spring-boot-starter-parent:+')
compile('org.springframework.boot:spring-boot-starter-test:+')
compile('org.springframework.cloud:spring-cloud-starter-eureka-server:+')
compile('org.springframework.cloud:spring-cloud-starter-config:+')
compile('org.springframework.cloud:spring-cloud-dependencies:Brixton.SR7')
compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
testCompile group: 'junit', name: 'junit', version: '4.+'
}

至此,一个可用的注册中心构建依赖配置完毕,笔者这边完整的配置如下:

buildscript {
ext {
springBootVersion = '1.5.4.RELEASE'
}
repositories {
maven {
url 'http://192.168.18.99:48081/nexus/content/groups/public/'
}
ivy {
url 'http://192.168.18.99:48081/nexus/content/repositories/thirdparty/'
}
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}

apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'org.springframework.boot'

sourceCompatibility = 1.8 version = '1.0.1-SNAPSHOT'
jar {
manifest {
attributes 'Implementation-Title': 'Gradle Quickstart',
'Implementation-Version': version
}
}

repositories {
maven {
url 'http://192.168.18.99:48081/nexus/content/groups/public/'
}
ivy {
url 'http://192.168.18.99:48081/nexus/content/repositories/thirdparty/'
}
}

sourceSets { main { resources { srcDir 'src/main/config' } } }

dependencies { compile('org.springframework.boot:spring-boot-starter-parent:+') compile('org.springframework.boot:spring-boot-starter-test:+') compile('org.springframework.cloud:spring-cloud-starter-eureka-server:+') compile('org.springframework.cloud:spring-cloud-starter-config:+') compile('org.springframework.cloud:spring-cloud-dependencies:Brixton.SR7') compile group: 'commons-collections', name: 'commons-collections', version: '3.2' testCompile group: 'junit', name: 'junit', version: '4.+' }

test {
systemProperties 'property': 'value'
}

uploadArchives {
repositories {
flatDir {
dirs 'repos'
}
}
}

保存好gradle构建文件后,我们选择工程根目录右键刷新整个工程,如图所示:



刷新过程中会自动去maven中央仓库下载对应jar引入,等待过程根据大家网速来定,接下来我们需要开始开发和配置注册中心了。

第二章:

      我们知道spring初始化的过程中需要定义一些初始化参数,那么我们这些参数的定义就包含在了application.yml

文件当中,我们需要定义服务实例名,端口,以及作用域,那么我们定义注册中心需要哪些呢,非常简单,只需要定义如下参数即可:

server:
port: 1111
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://localhost:1111/eureka/[/code]eureka的默认端口为8761,笔者这里改为了1111,为了方便管理和记忆。eureka有很多的参数,一般只需要定义笔者所示参数即可,大概解释一下这几个参数的含义: 
register-with-eureka
表示是否注册自身到eureka服务器,因为当前这个应用就是eureka服务器,没必要注册自身,所以这里是false

fetch-registry
表示是否从eureka服务器获取注册信息,同上,这里不需要

defaultZone
就比较重要了,是设置eureka服务器所在的地址,查询服务和注册服务都需要依赖这个地址

配置定义好了以后,我要开发启动类了,先给大家看下笔者这边的目录结构:

图中EurekaApplication就是我们的启动类,启动类也非常简单,总共的代码就如下一点点:

@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {

/**
* Title: main<br/>
* Description: <br/>
* @author hejun
* @date 2017年6月30日下午4:28:57
*
* @param args
*/
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}

我们来启动我们刚刚写的注册中心,因为我们的spring boot已经内嵌了tomcat作为服务器,所以启动方式如图:



启动完毕后,我们在浏览器中输入:http://localhost:1111,即可看到如图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 微服务