Spring Cloud 入门教程1、服务注册与发现(Eureka)

一、前言

1、什么是Eureka?

Eureka是Netflix开源的服务注册与发现框架,Eureka由两个组件组成:Eureka服务器和Eureka客户端。
Eureka Server提供服务注册服务,各个节点启动后,会在Eureka Server中进行注册,这样EurekaServer中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观的看到。
Eureka客户端是一个Java客户端,用来简化与服务器的交互、作为轮询负载均衡器,并提供服务的故障切换支持。
2、Eureka概览

行为
ken.io 的说明

  1. Registry
    服务启动时,Eureka客户端会将Service(ServiceName、IP、Port)注册到Eureka Server
  2. Query
    应用通过Eureka客户端调用注册在Eureka Server的服务时,会通过ServiceName查询到服务实例列表
  3. Call
    根据拿到的服务实例信息,通过负载均衡或其他策略访问指定服务实例

3、本篇环境信息

框架
版本
Spring Boot
2.0.0.RELEASE
Spring Cloud
Finchley.BUILD-SNAPSHOT
JDK
1.8.x

二、搭建Eureka Server

1、创建Eureka Server项目

创建一个Spring Boot工程
使用maven-archtype-quickstart模板创建项目


说明
GroupId
io.ken.springcloud.eurekaserver
ArtifactId
eurekaserver

在pom.xml加入相关依赖


4.0.0

io.ken.springcloud.eurekaserver
eurekaserver
1.0-SNAPSHOT

eurekaserver


<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>


org.springframework.boot
spring-boot-starter-parent
2.0.0.RELEASE




org.springframework.cloud
spring-cloud-dependencies
Finchley.BUILD-SNAPSHOT
pom
import



org.springframework.cloud
spring-cloud-starter-netflix-eureka-server


junit
junit
4.11
test



spring-snapshots
Spring Snapshots
https://repo.spring.io/libs-snapshot

true



eurekaserver

2、配置EurekaServer启动类

修改\src\main\java\io\ken\springcloud\eurekaserver\App.java 基于Spring Boot创建启动类,添加上 @EnableEurekaServer 注解
package io.ken.springcloud.eurekaserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class App {

public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
3、配置Eureka Server

在\src\main下创建文件夹resources文件夹并设置为Resources Root
在resources文件夹下创建application.yml文件并配置Eureka Server
server:
port: 8800

spring:
application:
name: EurekaServer

eureka:
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://localhost😒{server.port}/eureka/
Eureka默认会以客户端来注册自己,所以registerWithEureka、fetchRegistry要配置为false。
4、Eureka Server信息预览

启动Eureka Server项目后,访问:http://localhost:8800/ ,可以看到下面的页面,其中还没有发现任何服务。

三、搭建服务并注册(Eureka Client)

1、创建Eureka Client项目

创建一个Spring Boot工程
使用maven-archtype-quickstart模板创建项目


说明
GroupId
io.ken.springcloud.helloservice
ArtifactId
helloservice

在pom.xml加入相关依赖


4.0.0

io.ken.springcloud.helloservice
helloservice
1.0-SNAPSHOT

helloservice
http://ken.io


<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>


org.springframework.boot
spring-boot-starter-parent
2.0.0.RELEASE




org.springframework.cloud
spring-cloud-dependencies
Finchley.BUILD-SNAPSHOT
pom
import



org.springframework.cloud
spring-cloud-starter-netflix-eureka-client


org.springframework.boot
spring-boot-starter-web


junit
junit
4.11
test



spring-snapshots
Spring Snapshots
https://repo.spring.io/libs-snapshot

true



helloservice

2、配置Eureka Client启动类

修改\src\main\java\io\ken\springcloud\helloservice\App.java 基于Spring Boot创建启动类,添加上 @EnableDiscoveryClient 注解
package io.ken.springcloud.helloservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class App {

public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
3、创建测试服务

在\src\main\java\io\ken\springcloud\helloservice创建package:controller 然后创建HelloController.java
package io.ken.springcloud.helloservice.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

@Autowired
DiscoveryClient discoveryClient;

@RequestMapping("/")
public Object index() {
return "hello service";
}

@RequestMapping("/info")
public Object info() {
return discoveryClient.getServices();
}
}
4、配置Eureka Client

在\src\main下创建文件夹resources文件夹并设置为Resources Root
在resources文件夹下创建application.yml文件并配置Eureka Client
server:
port: 8601

spring:
application:
name: helloservice

eureka:
client:
serviceUrl:
defaultZone: http://localhost:8800/eureka/
5、服务注册&访问测试

HelloService项目成功启动后,访问 http://localhost:8601 ,或者 http://localhost:8601/info 查看是否正常启动
访问 http://localhost:8800 ,会发现服务已注册

搞定!
四、备注

本篇示例代码
https://github.com/ken-io/springcloud-course/tree/master/chapter-01

posted @ 2020-10-23 14:56  miss520net  阅读(164)  评论(0)    收藏  举报