您可以捐助,支持我们的公益事业。

1元 10元 50元





认证码:  验证码,看不清楚?请点击刷新验证码 必填



  求知 文章 文库 Lib 视频 iPerson 课程 认证 咨询 工具 讲座 Model Center 汽车系统工程   模型库  
会员   
   
OCSMP认证课程:OCSMP-MU
4月9-10日 线上
基于模型的数据治理与数据中台
5月19-20日 北京+线上
网络安全原理与实践
5月21-22日 北京+线上
     
   
 订阅
  捐助
Eureka的基本功能和用法
 
  2305  次浏览      34
 2019-6-26
 
编辑推荐:
本文来自于博客园,本文主要介绍了Eureka的基本功能和用法以及搭建服务注册中心,希望对您的学习有所帮助。

1.基础架构

eueka按逻辑上可以划分为3个模块,eureka-server,service-provider,service-consumer

eureka-server:服务端,提供服务注册和发现

eureka-client-service-provider:服务端,服务提供者,通过http rest告知服务端注册,更新,取消服务

eureka-client-service-consumer:客户端,服务消费者,通过http rest从服务端获取需要服务的地址列表,然后配合一些负载均衡策略(ribbon)来调用服务端服务。

值得注意的一点,不同于其他服务注册与发现(zookeeper需要单独以中间件的形式部署集群server),以上3个角色都是逻辑角色,甚至可以在相同的jvm进程上

2.构建服务中心

2.1搭建服务注册中心

①新建Spring Boot工程,在pom.xml 中新增如下依赖

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository

-->
</parent>
<dependencies>
<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>Dalston.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

②通过@EnableEurekaServer注解启动一个服务注册中心

例如:

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.

class, args);
}
}

③添加application.yml配置(属性文件配置也行)

server:
port: 8761
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}

/eureka/

注意:在该默认配置下,注册中心也会将自己作为客户端尝试注册自己,因此要禁用客户端注册行为

④启动应用,并访问http://localhost:8761/,可以看到如下图所示的Eureka信息面板,其中Instances currently registered with Eureka栏为空,说明该注册中心还没有注册服务。

2.2 高可用注册中心(集群)

在Eureka的服务治理设计中,所有的节点既是服务提供方,也是服务消费方,服务注册中心也不例外,Eurekad的高可用实际就是将自己作为服务向其他服务注册中心注册自己,这样形成一组互相注册的服务注册中心,以实现服务清单的互相同步,达到高可用的效果

例如:构建一个双节点的注册中心集群

application1.yml配置如下:

server:
port: 8761
eureka:
instance:
hostname: 10.22.0.130
client:
register-with-eureka: false
fetch-registry: false
serviceUrl:
defaultZone: http://10.22.0.131:8762/eureka/

application2.yml配置如下:

server:
port: 8762
eureka:
instance:
hostname: 10.22.0.131
client:
register-with-eureka: false
fetch-registry: false
serviceUrl:
defaultZone: http://10.22.0.130:8761/eureka/

依次启动两个进程

在10.22.0.130上:java -jar Eureka-Server-0.0.1-SNAPSHOT.jar

在10.22.0.131上:java -jar Eureka-Server-0.0.1-SNAPSHOT.jar

然后访问http://10.22.0.130:8761/ 可以看到如下图所示:

我们可以看到,registered-replicas(可用分片)中已经有10.22.0.131节点的eureka-server了,同样访问http://10.22.0.131:8762/ 中也可以看到registered-replicas中已经有10.22.0.130节点的eureka-server了,至此服务集群搭建完成

注意:在搭建了注册中心集群后,服务提供方也要做一定的配置调整才能将服务注册到Eureka server集群中,修改配置eureka.client.serviceUrl.defaultZone=http : //10.22.0.130:8761/eureka,http: // 10.22.0.131:8762 / eureka /

3.服务注册

服务注册中心搭建好了,之后我们需要注册服务到注册中心中,新建一个Spring Boot工程,在pom.xml中添加如下依赖:

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

然后新建配置文件application.yml,配置如下:

eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka
server:
port: 8763
spring:
application:
name: service-hello

新建启动类:HelloApplication.java

@RestController
@EnableEurekaClient
@SpringBootApplication
public class HelloApplication {

public static void main(String[] args) {
SpringApplication.run(HelloApplication.class, args);
}

@Value("${server.port}")
String port;

@RequestMapping("/hi")
public String sayHi(String name){
return "hi" + port + ";name"+name;
}
}

启动注册中心,然后运行该应用,访问注册中心,会看到注册中心中多了个服务实例,如图所示:

我们还可以直接通过浏览器调用该服务,在浏览器中访问http://localhost:8763/hi?name=123

4.服务发现与消费

服务发现由eureka客户端完成,而服务的消费由ribbon完成。ribbon是客户端的一个负载均衡器,用于对消费的服务实例进行负载均衡。

上面已经介绍了通过浏览器消费服务,现在是通过接口调用的方式消费服务,具体步骤如下:

首先向注册中心注册两个服务,如图所示:

然后再构建一个eureka客户端,消费者两个服务

新建RibbonApplication.java

代码如下:

@EnableDiscoveryClient
@SpringBootApplication
public class RibbonApplication {

public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
}

@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
}

新建服务接口:HelloService.java

代码如下:

@Service
public class HelloService {

@Autowired
RestTemplate restTemplate;

public String hiService(String name){
return restTemplate.getForObject("http://SERVICE-HELLO/hi?name=" + name , String.class);
}
}

新建控制器:HelloControler.java

代码如下:

@RestController
public class HelloControler {

@Autowired
HelloService helloService;

@RequestMapping(value = "/hi")
public String hi(@RequestParam String name){
return helloService.hiService(name);
}
}

新增配置文件:application.yml

eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8764
spring:
application:
name: service-ribbon

然后访问http://localhost:8764/hi?name=1,会轮询调用两个服务,如下图所示:

 

   
2305 次浏览       34
相关文章

企业架构、TOGAF与ArchiMate概览
架构师之路-如何做好业务建模?
大型网站电商网站架构案例和技术架构的示例
完整的Archimate视点指南(包括示例)
相关文档

数据中台技术架构方法论与实践
适用ArchiMate、EA 和 iSpace进行企业架构建模
Zachman企业架构框架简介
企业架构让SOA落地
相关课程

云平台与微服务架构设计
中台战略、中台建设与数字商业
亿级用户高并发、高可用系统架构
高可用分布式架构设计与实践