spring cloud 构建微服务一(eureka)服务注册与
2018-02-08 本文已影响72人
阿波罗程序猿
spring cloud
的由来就不说了,可以查看官网。昨天刚刚上手spring cloud
这里记录下我的学习过程,算是做一个知识储备,等以后业务增长为拆分微服务做准备。
spring cloud 版本
- 到现在
spring cloud
已经发展了有5个版本Angel、Brixton、Camden、Dalston、Edgware、Finchley
顺序就是ABCDEF
。其中Angel、Brixton
这两个版本已经不维护了。最新GA
的是Edgware SR1
。所以本例就根据这个版本作为开发。
目前官方维护的版本
spring boot 与 spring cloud 的关系
- 可以说
spring cloud
是建立在spring boot
基础之上的。spring cloud
脱离spring boot
是玩不转的。但是spring boot
脱离spring cloud
依然可以玩得转。
创建 Eureka 服务发现
Eureka
是属于Netflix OSS components
。Netflix
开源软件组件中的一种,专门用来进行服务发现的组件。使用Eureka
可以实现实例的注册,并且客户端可以通过spring
管理的beans
发现该实例。
- 先看一下配置表,
eureka-customer
不在这次范围之内。
应用名称 | eureka-server | eureka-client | eureka-customer |
---|---|---|---|
端口 | 9000-9099 | 9100-9199 | 9200-9299 |
说明 | 提供注册服务 | 服务本身 | 消费服务 |
创建eureka-server
项目。使用SPRING INITIALIZR
初始化项目。
初始化 eureka-server 项目
-
@EnableEurekaServer
激活eureka
服务。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @author 张博
* @since 2018/2/7 下午3:59
*/
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
- 配置
application.properties
。 默认情况下eureka-server
会将自己作为客户端注册在eureka-server
上。所以我们这里禁用此默认。
# 服务注册中心
spring.application.name=eureka-server
server.port=9000
eureka.instance.hostname=localhost
# 指示此实例是否向该服务注册,并被其它 eureka server 发现
eureka.client.register-with-eureka=false
# 指示此客户端是否从 eureka server 上获取 eureka 注册表信息
eureka.client.fetch-registry=false
# 设为false,关闭自我保护,默认为true
eureka.server.enable-self-preservation=true
# 清理间隔(单位毫秒,默认是0 不清除)
eureka.server.eviction-interval-timer-in-ms=1000
#eureka.client.serviceUrl.defaultZone=http://127.0.0.1:9001/eureka/
#EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.
- 启动该服务,由于还没有服务注册到上面所以看到
Instances currently registered with Eureka
这行是空的。
启动该服务
创建eureka-client
项目。
- 创建
controller
。DiscoveryClient
可以发现可用的服务。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* @author 张博
*/
@RestController
public class UserController {
@Autowired
private DiscoveryClient discoveryClient;
@RequestMapping(value = "/getServices/{port}", method = RequestMethod.GET)
public String getServices(@PathVariable String port) {
String str = "" + discoveryClient.getServices();
return str.concat(": ").concat(port);
}
}
-
@EnableDiscoveryClient
与@EnableEurekaClient
,它们都能启用eureka
发现。@EnableDiscoveryClient
在 spring-cloud-commons 中,@EnableEurekaClient
在 spring-cloud-netflix 中。如果是使用eureka
的话这两个注解使用哪个都行。但spring cloud
服务发现不止有eureka
还有Consul、zookeeper
。所以建议使用 spring-cloud-commons 中的@EnableDiscoveryClient
。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
- 配置
application.properties
。spring.application.name
这个名字很重要,起相同的名字,以后配置集群时,可以把多个ip绑定到这个名字下,做负载均衡。
# 服务提供者
spring.application.name=eureka-client
server.port= 9100
# 把服务注册到 eureka-server 中
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:9000/eureka/
- 启动应用,查看
eureka server
发现Instances currently registered with Eureka
多了一项。到此服务注册与发现搞定。
eureka server - 在浏览器输入
http://192.168.0.109:9100/getServices/9100
,得到响应结果。
[eureka-client]: 9100
复制刚才创建eureka-client
项目。
- 只修改
server.port
的端口号,之后启动并刷新,http://127.0.0.1:9000/
# 服务提供者
spring.application.name=eureka-client
server.port=9101
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:9000/eureka/
可以看到,相同的应用名下,挂着2个服务
下节我们来看看如何实现客户端负载均衡的操作。