此篇主要实操Eureka 服务端的服务注册,以及服务发现,并需要认证才能访问控制中心。
分五个部分说明:
一. 认识 Eureka
二. Eureka 服务端开发
三. Eureka 客户端开发
四. 多客户端情况查看
五. 下节预告(多个服务提供后,怎么消费)
(一) 认识 Eureka
1. 怎么读
Eureka英文读音[juə'ri:kə], 中文读音:尤里卡,古希腊词语,词性为感叹词,意思是“我找到了!我发现了!”
2. what is Eureka?
尤里卡是一个基于REST服务,主要是用于做服务注册与服务发现的。
3. Eureka 架构
图分析下: 有2个角色,Eureka Server和Eureka Client。可以理解成,服务都注册在哪里,哪里就可称为Eureka Server,注册中心的意思 。
注册在注册中心可以是服务的提供者(Applicaton Service)或者服务的消费者(Application Client),这两类都被称为Eureka client。
每个区域有一个Eureka集群,并且每个区域至少有一个eureka服务器可以处理区域故障,以防服务器瘫痪。
Eureka Client向Eureka Server注册,并将自己的一些客户端信息发送Eureka Server。
然后,Eureka Client(application service)通过向Eureka Serve发送心跳(每30秒)来续约服务的。
如果客户端持续不能续约,那么,它将在大约90秒内从服务器注册表中删除。 注册信息和续订被复制到集群中的Eureka Serve所有节点。
来自任何区域的Eureka Client(Application Client)都可以查找注册表信息(每30秒发生一次)。根据这些注册表信息,Application Client可以远程调用Applicaton Service来消费服务。
二. Eureka 服务端开发
1. 服务端代码结构
2. gradle 构建源码:
import java.text.SimpleDateFormat
group 'com.angus'
version '1.0-SNAPSHOT'
buildscript {
ext {
springBootVersion = '1.5.2.RELEASE'
}
repositories {
jcenter {url 'http://jcenter.bintray.com/'}
// maven{ url 'http://maven.aliyun.com/nexus/content/groups/public'}
mavenLocal()
mavenCentral()
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion"
classpath 'org.springframework:springloaded:1.2.4.RELEASE'
classpath 'org.hidetake:gradle-ssh-plugin:2.0.0'
classpath "io.spring.gradle:dependency-management-plugin:0.5.6.RELEASE"
}
}
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'
apply plugin: 'application'
apply plugin: 'org.hidetake.ssh'
apply plugin: "io.spring.dependency-management"
def env = System.getProperty("env") ?: "prd"
sourceSets {
main {
resources {
srcDirs = ["src/main/resources","src/main/profile/$env"]
}
}
}
def date = new SimpleDateFormat("yyyy-MM-dd").format(new Date())
jar {
baseName = "eureka-$env"
version = '1.0-RELEASE'
}
war {
baseName = "eureka-$env"
version = '1.0-RELEASE'
}
repositories {
maven{ url 'http://maven.aliyun.com/nexus/content/groups/public'}
mavenLocal()
mavenCentral()
}
distZip {
archiveName "$baseName-$version-$date-$env-RELEASE.zip"
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:Camden.SR5"
}
}
dependencies {
compile "org.springframework.boot:spring-boot-starter-web:$springBootVersion"
compile 'org.springframework.cloud:spring-cloud-starter-eureka-server'
compile 'org.springframework.boot:spring-boot-starter-security'
}
springBoot {
mainClass = "com.angus.eureka.BaseApplication"
}
3. Eureka 配置说明(application.properties)
server.port = 8001
server.sessionTimeout=15
server.tomcat.max-threads = 800
server.tomcat.uri-encoding = UTF-8
#服务Eureka Server的身份验证
security.basic.enabled=true
security.user.name=root
security.user.password=angus
#是否将eureka自身作为应用注册到eureka注册中心
eureka.client.registerWithEureka=false
#为true时,可以启动,但报异常:Cannot execute request on any known server
eureka.client.fetchRegistry=false
eureka.client.serviceUrl.defaultZone=http://root:angus@localhost:8001/eureka/
package com.angus.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* Created by Angus on 2017-5-31.
*/
@EnableEurekaServer
@SpringBootApplication
public class BaseApplication {
public static void main(String[] args) {
SpringApplication.run(BaseApplication.class, args);
}
}
启动:bootRun -Denv=test
5. 启动效果:
打开浏览器: http://localhost:8001
三. Eureka 客户端开发
1. Eureka Client(Application Server)代码结构
2. gradle 构建源码:
eureka client:
import java.text.SimpleDateFormat
group 'com.angus'
version '1.0-SNAPSHOT'
buildscript {
ext {
springBootVersion = '1.5.2.RELEASE'
}
repositories {
jcenter {url 'http://jcenter.bintray.com/'}
// maven{ url 'http://maven.aliyun.com/nexus/content/groups/public'}
mavenLocal()
mavenCentral()
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion"
classpath 'org.springframework:springloaded:1.2.4.RELEASE'
classpath 'org.hidetake:gradle-ssh-plugin:2.0.0'
classpath "io.spring.gradle:dependency-management-plugin:0.5.6.RELEASE"
}
}
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'
apply plugin: 'application'
apply plugin: 'org.hidetake.ssh'
apply plugin: "io.spring.dependency-management"
def env = System.getProperty("env") ?: "prd"
sourceSets {
main {
resources {
srcDirs = ["src/main/resources","src/main/profile/$env"]
}
}
}
def date = new SimpleDateFormat("yyyy-MM-dd").format(new Date())
jar {
baseName = "eureka-client-$env"
version = '1.0-RELEASE'
}
war {
baseName = "eureka-client-$env"
version = '1.0-RELEASE'
}
repositories {
// maven{ url 'http://maven.aliyun.com/nexus/content/groups/public'}
mavenLocal()
mavenCentral()
}
distZip {
archiveName "$baseName-$version-$date-$env-RELEASE.zip"
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:Camden.SR5"
}
}
dependencies {
compile "org.springframework.boot:spring-boot-starter-web:$springBootVersion"
compile 'org.springframework.cloud:spring-cloud-starter-eureka'
compile 'org.springframework.boot:spring-boot-starter-actuator'
}
springBoot {
mainClass = "com.angus.BaseApplication"
}
3. Eureka client 配置说明
server.port = 8888
server.sessionTimeout=15
server.tomcat.max-threads = 800
server.tomcat.uri-encoding = UTF-8
spring.application.name=sayHelloServer
#为true时,可以启动,但报异常:Cannot execute request on any known server
eureka.client.fetchRegistry=false
# 指定服务注册中心的地址
eureka.client.serviceUrl.defaultZone=http://root:angus@localhost:8001/eureka/
package com.angus;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* Created by Angus on 2017-5-31.
*/
@EnableEurekaClient
@SpringBootApplication
public class BaseApplication {
public static void main(String[] args) {
SpringApplication.run(BaseApplication.class, args);
}
}
SpringCloud中的“Discovery Service”有多种实现,比如:eureka, consul, zookeeper。@EnableDiscoveryClient
注解是基于spring-cloud-commons
依赖,并且在classpath中实现; @EnableEurekaClient
注解是基于spring-cloud-netflix
依赖,只能为eureka作用;
5. 服务提供代码:
package com.angus.web;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created with eureka-client.
* User: anguszhu
* Date: Jun,22 2017
* Time: 16:25
* description:
*/
@RestController
public class HelloController {
@RequestMapping("/name/{who}")
public String sayHello(@PathVariable String who){
return "Hey "+who+", what's up man!";
}
}
6. 启动:
修改application.properties 的server.port 为8889 ,再启动一个,然后到控制台查看。
7. 启动一个服务,启动两个服务后,效果分别是:
两个服务如下:
四. 多客户端情况查看
上图可以看到,同一个应用,有两个服务提供者,分别是8889 端口与8888 端口
五. 下节预告(多个服务提供后,怎么消费)
这节实操了Eureka 服务注册,但是没有描述怎么玩服务消费,下节再写吧,通过Spring Cloud Ribbon 负载调动不同的服务提供者。