1.介绍:Eureka是一个服务管理器,其他业务组件(服务)通过rest注册到Eureka服务器,由服务器对服务进行监控,管理。
2.原理:
Eureka服务器:统一管理服务
Eureka客户端(服务提供者):将服务注册到Eureka服务器,获取注册列表
Eureka客户端(服务调用者):同样将服务注册到Eureka服务器,并且获取注册列表,再去调用具体的某个服务。
如果还不理解,可以举个例子:
小明现在想报警,又不知道报警电话是多少,但是知道咨询平台,于是小明先向咨询平台咨询,知道了报警电话之后再去拨打报警电话。。在这里,我们可以把咨询平台当成Eureka服务器,警察局当成服务提供者,小明当成服务调用者。首先,警察局会先把报警电话提供给到咨询平台,当小明去咨询平台查询时会把警局电话给小明,小明再去拨打电话。
3.第一个Eureka程序(下面我们模拟上面报警的例子,写一个Eureka程序)
- Eureka服务器(建立项目consultingPlatform_server):
1).pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
2).application.properties
server.port=6001
#不把自己作为客户端注册
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
#eureka服务器路径
eureka.client.service-url.defaultZone=http://localhost:${server.port}/eureka/
3).Application.java(启动类)
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
- 服务提供者(创建项目police_client)
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
2 ).application.properties
server.port=6002
#服务名称
spring.application.name=police
#注册路径
eureka.client.service-url.defaultZone=http://localhost:6001/eureka/
3 ).Application.java
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @author: lqh
* @description:
* @program: police_client
* @create: 2018-06-06 11:50
**/
@SpringBootApplication
@EnableEurekaClient
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
4 ).PoliceController.java
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author: lqh
* @description:
* @program: police_client
* @create: 2018-06-06 11:59
**/
@RestController
public class PoliceController {
@RequestMapping("call")
public String call(){
return "call the police";
}
}
- 服务调用者(创建项目people_caller):
- 1).pom.xml
2.application.properties<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.13.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Edgware.SR3</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> </dependencies>
3.Applicationserver.port=6003 #服务名称 spring.application.name=people #注册路径 eureka.client.service-url.defaultZone=http://localhost:6001/eureka/
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class Application { public static void main(String[] args) { new SpringApplicationBuilder(Application.class).web(true).run(args); } }
4.PeopleControllerimport org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @Configuration @RestController public class PeopleController { @Bean @LoadBalanced RestTemplate getRestTemplate() { return new RestTemplate(); } @RequestMapping("call") public String call() { return getRestTemplate().getForObject("http://police/call",String.class); } }
OK,跑起来。。