1.Eureka简介
Eureka是Netflix开源的一款提供服务注册和发现的产品,它提供了完整的Service Registry和Service Discovery实现。也是springcloud体系中最重要最核心的组件之一。
Eureka是一个服务注册和发现模块,采用了 C-S 的设计架构。Eureka Server 作为服务注册功能的服务器,它是服务注册中心。而系统中的其他微服务,使用 Eureka 的客户端连接到 Eureka Server,并维持心跳连接。这样系统的维护人员就可以通过 Eureka Server 来监控系统中各个微服务是否正常运行。Spring Cloud 的一些其他模块(比如Zuul)就可以通过 Eureka Server 来发现系统中的其他微服务,并执行相关的逻辑。
Eureka由三个角色组成:
1)Eureka Server
- 提供服务注册和发现
2)Service Provider
- 服务提供方
- 将自身服务注册到Eureka,从而使服务消费方能够找到
3)Service Consumer
- 服务消费方
- 从Eureka获取注册服务列表,从而能够消费服务
2.创建注册中心 Eureka Server
1)可以通过浏览器打开SpringBoot项目初始化工具新建SpringBoot项目,也可以通过IDEA创建,本文通过网址创建
通过浏览器打开https://start.spring.io/,选择编开发语言、SpringBoot版本、项目名称等信息。
填好之后点击 Generate Project alt+,生成项目压缩包,解压缩之后通过eclipse导入项目。
2)导入项目之后在pom.xml中添加依赖Eureka,如果提示缺失版本号,再添加SpringCloud版本号,最终如下:
<?xml version="1.0" encoding="UTF-8"?>
<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.su</groupId>
<artifactId>sbc-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>sbc-server</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</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>Camden.SR6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3)配置application.properties
spring.application.name=register-server
server.port=9999
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
4)启动代码中添加@EnableEurekaServer注解
@EnableEurekaServer
@SpringBootApplication
public class SbcServerApplication {
public static void main(String[] args) {
SpringApplication.run(SbcServerApplication.class, args);
System.out.println("注册中心启动成功-------------------------");
}
}
3.创建服务提供方
1)创建SpringBoot项目demo1,创建过程以及pom.xml文件与注册中心类似
2)配置application.properties,指明注册中心地址端口号等
spring.application.name=demo1
server.port=8091
eureka.instance.hostname=localhost
eureka.client.serviceUrl.defaultZone=http://localhost:9999/eureka/
3)启动代码中添加@EnableEurekaServer注解
@SpringBootApplication
@EnableEurekaServer
public class DemoNameApplication {
public static void main(String[] args) {
SpringApplication.run(DemoNameApplication.class, args);
System.out.println("demo1启动成功----------------------");
}
}
4)提供服务
@RestController
public class HelloWordController {
@RequestMapping(value="/hello",method=RequestMethod.GET)
public String sayHello(@RequestParam String name){
return "hello "+name+",welcome to demo1";
}
}
4.创建服务消费方
1)创建SpringBoot项目demo2,创建过程以及pom.xml文件与注册中心类似,需要添加feign依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
2)配置application.properties,指明注册中心地址端口号等
spring.application.name=demo2
server.port=8092
eureka.instance.hostname=localhost
eureka.client.serviceUrl.defaultZone=http://localhost:9999/eureka/
3)启动类中添加@EnableEurekaServer @EnableFeignClients注解
@SpringBootApplication
@EnableEurekaServer
@EnableFeignClients
public class Demo2Application {
public static void main(String[] args) {
SpringApplication.run(Demo2Application.class, args);
System.out.println("demo2启动成功----------------------");
}
}
4)提供消费入口
@RestController
public class ConsumerController {
@Autowired
HelloRemote HelloRemote;
@RequestMapping("/hello/{name}")
public String sayHello(@PathVariable("name") String name) {
return HelloRemote.sayHello(name);
}
}
5)提供消费接口,@FeignClient注解定义了该接口是一个Feign客户端,name指定了注册到Eureka上的服务名,demo1为上面服务提供方注册的服务名。通过@ FeignClient(“服务名”),来指定调用哪个服务。比如在代码中调用了demo1服务的“/hello”接口,代码如下
@FeignClient(name= "demo1")
public interface HelloRemote {
@RequestMapping(value="/hello",method=RequestMethod.GET)
public String sayHello(@RequestParam(value="name") String name);
}
5.测试
浏览器中输入http://localhost:8092/hello/su,页面显示 hello su,welcome to demo1