一、认识服务治理
服务治理: 主要用来实现各个微服务实例的自动化注册与发现。
例如画几个简图来帮忙理解
项目A调用项目B,正常调用项目A请求项目B
有了服务中心之后,任何一个服务都不能直接去掉用,都需要通过服务中心来调用
项目A调用项目B,项目B在调用项目C
这时候调用的步骤就会为两步:第一步,项目A首先从服务中心请求项目B服务器,然后项目B在从服务中心请求项目C服务。
二、认识Eureka
按照官方介绍:
Eureka is a REST (Representational State Transfer) based service that is primarily used in the AWS cloud for locating services for the purpose of load balancing and failover of middle-tier servers.
Eureka 是一个基于 REST 的服务,主要在 AWS 云中使用, 定位服务来进行中间层服务器的负载均衡和故障转移。
Eureka由两个组件组成:Eureka服务器
和Eureka客户端
。
Eureka服务器:用作服务注册服务器。
Eureka客户端:是一个java客户端,用来简化与服务器的交互、作为轮询负载均衡器,并提供服务的故障切换支持。
Netflix在其生产环境中使用的是另外的客户端,它提供基于流量、资源利用率以及出错状态的加权负载均衡。
上图简要描述了Eureka的基本架构,由3个角色组成:
1、Eureka Server
- 提供服务注册和发现
2、Service Provider
- 服务提供方将自身服务注册到Eureka,从而使服务消费方能够找到
3、Service Consumer
- 服务消费方从Eureka获取注册服务列表,从而能够消费服务
三、搭建服务注册中心
1.首先,创建一个基础的Spring Boot
工程,创建一个Eureka注册中心,命名为eureka-server
, 并在pom.xml
中引入必要的依赖内容, 代码如下:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
2.配置application.yml文件:
server:
port: 8081
eureka:
instance:
hostname: eurka-server
client:
#表示是否将自己注册到Eureka Server,默认为true。
register-with-eureka: false
#表示是否从Eureka Server获取注册信息,默认为true。
fetch-registry: false
#设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址
service-url:
defaultZone: http://localhost:8081/eureka/
3.在启动类添加@EnableEurekaServer
注解,如下:
@EnableEurekaServer
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
4.启动工程后,访问:http://localhost:8081/, 可以看到下面的页面,其中还没有发现任何服务
四、创建服务提供者
1.pom.xml配置如下:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
2.配置application.xml,
server:
port: 8082
eureka:
instance:
hostname: eurka-server2
client:
service-url:
defaultZone: http://localhost:8081/eureka/
3.启动类添加如下注解:
@SpringBootApplication
@EnableEurekaClient
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
4.最后,将两个项目打成jar包,通过cmd命令启动如下:
5.查看服务中心注册页面,便可看到相对应的服务已经注册到注册中心了。
五、创建服务消费者
1.pom.xml配置和服务提供者一样
2.application.xml
server:
port: 8083
spring:
application:
name: consumer-user
eureka:
instance:
hostname: eurka-server3
client:
service-url:
defaultZone: http://localhost:8081/eureka/
3.启动类添加@EnableDiscoveryClient
注解
@EnableDiscoveryClient
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
4.注册中心可以查看服务消费者: