Eureka学习

Spring Cloud下有很多工程:

  • Spring Cloud Config:依靠git仓库实现的中心化配置管理。配置资源可以映射到Spring的不同开发环境中,但是也可以使用在非Spring应用中。
  • Spring Cloud Netflix:不同的Netflix OSS组件的集合:Eureka、Hystrix、Zuul、Archaius等。
  • Spring Cloud Bus:事件总线,利用分布式消息将多个服务连接起来。非常适合在集群中传播状态的改变事件(例如:配置变更事件)
  • Spring Cloud Consul:服务发现和配置管理,由Hashicorp团队开发。我决定先从Spring Cloud Netflix看起,它提供了如下的功能特性:
  • 服务发现:Eureka-server实例作为服务提供者,可以注册到服务注册中心,Eureka客户端可以通过Spring管理的bean发现实例;
  • 服务发现:嵌套式的Eureka服务可以通过声明式的Java配置文件创建;
  • 断路器:利用注解,可以创建一个简单的Hystrix客户端;
  • 断路器:通过Java配置文件可以创建内嵌的Hystrix控制面板;
  • 声明式REST客户端:使用Feign可以创建声明式、模板化的HTTP客户端;
  • 客户端负载均衡器:Ribbon
  • 路由器和过滤器:Zuul可以在微服务架构中提供路由功能、身份验证、服务迁移、金丝雀发布等功能。

1. 服务注册中心

在IDEA中创建一个Spring Cloud工程,引入Eureka-Server包,pom文件整体如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.sl</groupId>

<artifactId>RegistryCenter</artifactId>

<version>0.0.1-SNAPSHOT</version>

<packaging>jar</packaging>

<name>RegistryCenter</name>

<url>http://maven.apache.org</url>

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

</properties>

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>1.3.5.RELEASE</version>

<relativePath/>

</parent>

<dependencies>

<!-- 用于注册中心访问账号认证 -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-security</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>

<!-- 注册中心所需的包 -->

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-eureka-server</artifactId>

</dependency>

</dependencies>

<dependencyManagement>

<dependencies>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-dependencies</artifactId>

<version>Brixton.RELEASE</version>

<type>pom</type>

<scope>import</scope>

</dependency>

</dependencies>

</dependencyManagement>

<!-- spring boot的maven打包插件 -->

<build>

<defaultGoal>compile</defaultGoal>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

</plugins>

</build>

</project>

创建APP类,并用@EnableEurekaServer和@SpringBootApplication两个注解修饰,后者是Spring Boot应用都需要用的,这里不作过多解释;@EnableEurekaServer注解的作用是触发Spring Boot的自动配置机制,由于我们之前在pom文件中导入了eureka-server,spring boot会在容器中创建对应的bean。Eureka的代码如下:

package com.sl.RegistryCenter;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.builder.SpringApplicationBuilder;

import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**

* Hello world!

*

*/

@EnableEurekaServer

@SpringBootApplication

public class App {

public static void main(String[] args) {

new SpringApplicationBuilder(App.class).web(true).run(args);

}

}

添加配置文件application.properties,并且添加如下参数,才能创建一个真正可以使用的服务注册中心。

######eureka服务端######

spring.application.name=eureka-server

#驱逐下线的服务,间隔,5秒,默认是60,建议开发和测试环境配置

eureka.server.evictionIntervalTimerInMs=10000

server.port=8761

#是否需要注册到注册中心,因为该项目本身作为服务注册中心,所以为false

eureka.client.register-with-eureka=false

#是否需要从注册中心获取服务列表,原因同上,为false

eureka.client.fetch-registry=false

security.basic.enabled=true

security.user.name=admin

security.user.password=123

#注册服务器的地址:服务提供者和服务消费者都要依赖这个地址

eureka.client.serviceUrl.defaultZone=http://admin:123@localhost:8761/eureka

###Eureka自我保护机制,为true表示开,false表示关,默认为开####

eureka.server.enable-self-preservation=true

启动注册服务,并访问:http://localhost:8761,就可以看到如下界面。

2. 服务提供者

创建一个Spring Boot工程,代表服务提供者,该服务提供者会暴露一个当前请求产生的时间字符串。

工程的pom文件内容如下:

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.sl</groupId>

<artifactId>TestMicroService</artifactId>

<version>0.0.1-SNAPSHOT</version>

<packaging>jar</packaging>

<name>TestMicroService</name>

<url>http://maven.apache.org</url>

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

</properties>

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>1.3.5.RELEASE</version>

<relativePath /> <!-- lookup parent from repository -->

</parent>

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-eureka</artifactId>

</dependency>

<!-- 用于注册中心访问账号认证 -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-actuator</artifactId>

</dependency>

</dependencies>

<dependencyManagement>

<dependencies>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-dependencies</artifactId>

<version>Brixton.RELEASE</version>

<type>pom</type>

<scope>import</scope>

</dependency>

</dependencies>

</dependencyManagement>

<build>

<defaultGoal>compile</defaultGoal>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

</plugins>

</build>

</project>

其中的关键在于spring-cloud-starter-eureka这个Jar包,其中包含了eureka的客户端实现。

在src/main/java/com.sl.TestMicroService下创建工程的主类App,使用@EnableDiscoveryClient注解修饰,该注解在服务启动的时候,可以触发服务注册的过程,向配置文件中指定的服务注册中心(Eureka-Server)的地址注册自己提供的服务。App的源码如下:

package com.sl.TestMicroService;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.builder.SpringApplicationBuilder;

import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**

* Hello world!

*

*/

@EnableDiscoveryClient

@SpringBootApplication

public class
App {

public static void main(String[] args) {

new
SpringApplicationBuilder(App.class).web(true).run(args);

}

}

配置文件的内容如下:

server.port=8111

#设置应用的名称

spring.application.name=microservice-provider-user

#服务注册的Eureka Server地址

eureka.client.serviceUrl.defaultZone=http://admin:123@localhost:8761/eureka

#设置注册ip

eureka.instance.prefer-ip-address=true

#自定义应用实例id

#健康检查

eureka.client.healthcheck.enabled=true

服务提供者的基本框架搭好后,需要实现服务的具体内容,在ComputeController类中实现,它的具体代码如下:

package com.sl.TestMicroService;

import java.text.SimpleDateFormat;

import java.util.Date;

import org.apache.log4j.Logger;

import
org.springframework.beans.factory.annotation.Autowired;

import org.springframework.cloud.client.ServiceInstance;

import
org.springframework.cloud.client.discovery.DiscoveryClient;

import org.springframework.web.bind.annotation.RequestMapping;

import
org.springframework.web.bind.annotation.RequestMethod;

import
org.springframework.web.bind.annotation.RestController;

@RestController

public class ComputeController {

private
final Logger logger = Logger.getLogger(getClass());

@Autowired

private
DiscoveryClient client;

private
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

@RequestMapping(value
= "/test", method = RequestMethod.GET)

public
String test() {

ServiceInstance
instance = client.getLocalServiceInstance();

String
temp = "当前时间【" + df.format(new Date()) +
"】/add, host:" + instance.getHost() + ",
service_id:"

+
instance.getServiceId();

logger.info(temp);

return
temp;

}

}

先启动服务注册中心的工程,然后再启动服务提供者,在访问:localhost:8761,如下图所示,服务提供者已经注册到服务注册中心啦,下图可以查看

在Spring Cloud Netflix中,使用Ribbon实现客户端负载均衡,使用Feign实现声明式HTTP客户端调用——即写得像本地函数调用一样。

3. 服务消费者-Feign

pom配置如下:

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"
>

<modelVersion>4.0.0</modelVersion>

<groupId>com.sl</groupId>

<artifactId>ServiceConsumer</artifactId>

<version>0.0.1-SNAPSHOT</version>

<packaging>jar</packaging>

<name>ServiceConsumer</name>

<url>http://maven.apache.org</url>

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>1.3.5.RELEASE</version>

<relativePath /> <!-- lookup parent from repository -->

</parent>

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

</properties>

<build>

<defaultGoal>compile</defaultGoal>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

</plugins>

</build>

<dependencies>

<!-- Feign实现声明式HTTP客户端 -->

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-feign</artifactId>

</dependency>

<!-- eureka客户端 -->

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-eureka</artifactId>

</dependency>

<!-- spring boot实现Java Web服务-->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>

</dependencies>

<dependencyManagement>

<dependencies>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-dependencies</artifactId>

<version>Brixton.RELEASE</version>

<type>pom</type>

<scope>import</scope>

</dependency>

</dependencies>

</dependencyManagement>

</project>

首先创建应用程序启动类:ConsumerApp,代码如下:

package com.sl.ServiceConsumer;

import org.springframework.boot.SpringApplication;

import
org.springframework.boot.autoconfigure.SpringBootApplication;

import
org.springframework.cloud.client.discovery.EnableDiscoveryClient;

import
org.springframework.cloud.netflix.feign.EnableFeignClients;

/**

* Hello world!

*

*/

@EnableDiscoveryClient // 用于启动服务发现功能

@EnableFeignClients // 用于启动Fegin功能

@SpringBootApplication

public class ConsumerApp {

/**

* main函数入口

*

* @param args

*/

public
static void main(String[] args) {

SpringApplication.run(ConsumerApp.class);

}

}

然后创建ComputeClient接口,使用@FeignClient("microservice-provider-user")注解修饰,microservice-provider-user就是服务提供者的名称,然后定义要使用的服务,代码如下:

package com.sl.ServiceConsumer;

import
org.springframework.cloud.netflix.feign.FeignClient;

import
org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient("microservice-provider-user")

public interface ConsumerClient {

//
@RequestMapping(method = RequestMethod.GET, value = "/add")

// Integer
add(@RequestParam(value = "a") Integer a, @RequestParam(value =

//
"b") Integer b);

@RequestMapping(method
= RequestMethod.GET, value = "/test")

String
test();

}

在ConsumerController中,像引入普通的spring
bean一样引入ComputeClient对象,其他的和Ribbon的类似。

package com.sl.ServiceConsumer;

import
org.springframework.beans.factory.annotation.Autowired;

import
org.springframework.web.bind.annotation.RequestMapping;

import
org.springframework.web.bind.annotation.RequestMethod;

import
org.springframework.web.bind.annotation.RestController;

@RestController

public class ConsumerController {

@Autowired

private
ConsumerClient computeClient;

@RequestMapping(value
= "/test", method = RequestMethod.GET)

public
String test() {

return
computeClient.test();

}

}

application.properties的内容如下:

#应用名称

spring.application.name=fegin-consumer

#端口号

server.port=9000

#注册中心的地址

eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

启动fegin消费者,访问localhost:9000/add,也可以看到服务提供者已经收到了消费者发来的请求。

可以看到两个服务轮流被调用

注意事项

1.关于版本对应问题

  • Angel版本对应Spring Boot 1.2.x
  • Brixton版本对应Spring Boot 1.3.x
  • Camden版本对应Spring Boot 1.4.x

2.直接打包

打包时记得引用下面的包

<build>

<defaultGoal>compile</defaultGoal>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

</plugins>

</build>

Eureka学习例子的更多相关文章

  1. javascript闭包学习例子

    javascript中的闭包个很让人头疼的概念.总结一下 闭包是指有权访问一个函数作用域中的变量的函数.创建闭包最常见的方式,是在一个函数内部创建另一个函数,用return返回出去. 使用闭包可能造成 ...

  2. Spring Cloud Eureka Server例子程序

    Spring-Cloud-Eureka-Server 及Client 例子程序 参考源代码:https://github.com/spring-cloud-samples/eureka 可以启动成功, ...

  3. or1200下Raw-OS学习(例子篇)

    没有图我说个~毛(J)线(B)~对吧??? 直接上一个以前做过的项目来说说怎么去从一个前后台的程序过度到利用操作系统去管理的你代码吧~以前想过直接用事件驱动的框架去编写代码的,无奈这方面的资料实在太少 ...

  4. springcloud Eureka学习笔记

    最近在学习springcloud,抽空记录下学习笔记;主要记录Eureka的实现过程和高可用性的实现 Eureka是一个服务治理框架,它提供了Eureka Server和Eureka Client两个 ...

  5. vuex的学习例子

    最近在学习vuejs,一直有听说vuex,用来实现多组件共享的一种状态管理模式,但是网上都说,不要为了用vuex而用vuex,大概意思就是尽量少用vuex,一些小项目可以用bus来实现组件之间的传值问 ...

  6. Spring-cloud微服务 Eureka学习教程-分布式搭建EurekaServer、EurekaClient+Ribbon(中级)

    我们这里只有一台服务器,所以我们先仿集群搭建. 完整demo项目代码:https://github.com/wades2/EurekaDemo2 在这之前我们先分析分析Eureka相比其他注册中心的好 ...

  7. Spring-cloud微服务 Eureka学习教程-分布式搭建EurekaServer、EurekaClient(中级)

    我们这里只有一台服务器,所以我们先仿集群搭建. 完整demo项目代码:https://github.com/wades2/EurekaDemo2 在这之前我们先分析分析Eureka相比其他注册中心的好 ...

  8. Spring AOP 学习例子

    http://outofmemory.cn/code-snippet/3762/Spring-AOP-learn-example     工作忙,时间紧,不过事情再多,学习是必须的.记得以前的部门老大 ...

  9. eureka 学习

    Eureka is a REST (Representational State Transfer) based service that is primarily used in the AWS c ...

随机推荐

  1. html 自定义标签使用实现方法

    通过指定html命名空间的名字来定义自定义标签:默认的一些标签p div等都在html默认的命名空间下.而自定义的标签可以放在自定义的命名空间下,可通过xmlns:命名空间名 来指定,而自定义标签需要 ...

  2. 统一addEventListener与attachEvent中this指向问题

    1.this指向问题 使用addEventListener注册的事件,事件处理函数中 this指向目标元素: 使用attachEvent注册的事件,事件处理函数中 this指向window对象 要想将 ...

  3. python是如何进行内存管理的

    Python引入了一个机制:引用计数. python内部使用引用计数,来保持追踪内存中的对象,Python内部记录了对象有多少个引用,即引用计数,当对象被创建时就创建了一个引用计数,当对象不再需要时, ...

  4. linux分析日志的一些常用方法

    head -n 2016_05_23_access_log |grep "/859" 显示前10000行中包含 /859 的记录 增加 |wc -l  则改为输出记录数 cat 2 ...

  5. wordpress搬家到 linode 步骤简析

    1. 购买并安装系统 购买就不说了哈,英文不好的自己搜教程. 然后是安装系统 linode 系统安装: 购买完成后登录,进入找到购买的vps ,点击 Dashboard (控制面板) 进入后点击 面板 ...

  6. 【ThinkPHP框架学习 】(2) --- 后台管理系统如何用iframe点击左边右边局部刷新

    如题:         在写后台管理系统时,需要实现后台界面的局部动态刷新.         左边的导航栏使用a标签进行设置,通过href和target属性的配合,就可以将iframe中的子页实现动态 ...

  7. Python3学习笔记 - 准备环境

    前言 最近乘着项目不忙想赶一波时髦学习一下Python3.由于正好学习了Docker,并深深迷上了Docker,所以必须趁热打铁的用它来创建我们的Python3的开发测试环境.Python3的中文教程 ...

  8. ListView中点击Item没有任何响应

    不多说,上代码:如下图 红色方框的东西, android:descendantFocusability=”blocksDescendants” 如果不添加的话,在android的8.0手机上点击没有响 ...

  9. nodejs a和b文件相互引用

    //取自于node中文网 http://nodejs.cn/api/modules.html 当循环调用 require() 时,一个模块可能在未完成执行时被返回. 例如以下情况: a.js: con ...

  10. Git问题集锦

    1.初始新建git,出现No refs in common and none specified; doing nothing 解决方案:Perhaps you should specify a br ...