Spring Cloud Netflix提供了一系列搭建微服务基础架构的功能组件
本小节将讲述其中一个较为重要的服务管理框架:Eureka[juˈri:kə]
Eureka服务器(114电话查询平台)
Eureka客户端服务提供者(医院、警察局电话放置114电话查询平台)
Eureka客户端服务调用者(查询电话的我们)
我们去114平台查询某处电话号码(服务查找),得到电话号码后(获取注册信息),拨打电话(服务调用)
对于注册到服务器的服务组件,Eureka服务器并没有提供后台的存储,这些注册的服务实例被保存在内存的服务注册中心,它们通过心跳来保持其最新状态,这些操作都可以在内存中完成。
客户端存在相同的机制,同样在内存中保存了注册表信息,这样的机制提升了Eureka组件性能,每次服务的请求都不用经过服务器端的注册中心。
作为Eureka客户端存在的服务提供者,主要进行以下工作:
第一:向服务器注册服务
第二:发送心跳给服务器
第三:向服务器端获取注册列表
当客户端注册到服务器时,它将会提供一些关于它自己的信息给服务器端,例如:IP、Port、健康链接等
对于发布到Eureka服务器的服务,使用调用者可对其进行服务查找和调用,服务调用者也是作为客户端存在,但是其职责主要是发现和调用服务。
在实际情况中,有可能出现本身既是服务提供者,又是服务调用者的情况,例如:传统的企业应用三层架构中,服务层(service)会调用数据访问层(dao)的接口进行数据操作,它本身也会提供服务给控制层(controller)使用。
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.atm.cloudgroupId>
<artifactId>atm_enreka_serverartifactId>
<packaging>warpackaging>
<version>0.0.1-SNAPSHOTversion>
<name>atm_enreka_server Maven Webappname>
<url>http://maven.apache.orgurl>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>Dalston.SR1version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>3.8.1version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-eureka-serverartifactId>
dependency>
dependencies>
<build>
<finalName>atm_enreka_serverfinalName>
build>
project>
package com.atm.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class MyApplicationServer {
public static void main(String[] args) {
SpringApplication.run(MyApplicationServer.class, args);
}
}
server:
port: 8761
server:
port: 8761
eureka:
client:
registerWithEureka: false
fetchRegistry: false
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.atm.cloudgroupId>
<artifactId>atm_enreka_providerartifactId>
<packaging>warpackaging>
<version>0.0.1-SNAPSHOTversion>
<name>atm_enreka_provider Maven Webappname>
<url>http://maven.apache.orgurl>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>Dalston.SR1version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>3.8.1version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-configartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-eurekaartifactId>
dependency>
dependencies>
<build>
<finalName>atm_enreka_providerfinalName>
build>
project>
server:
port: 8080
spring:
application:
name: first-service-provider
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
hostname: localhost
package com.atm.cloud;
public class Person {
private Integer id;
private String name;
private Integer age;
//...省略setter/getter...
}
package com.atm.cloud;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
@RequestMapping(value = "/info", method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public Person findPerson(){
Person person=new Person();
person.setId(1);
person.setAge(18);
person.setName("atm");
return person;
}
@RequestMapping(value = "/person/{personId}", method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public Person findPerson(@PathVariable("personId")Integer personId){
Person person=new Person();
person.setId(personId);
person.setAge(18);
person.setName("atm");
return person;
}
}
package com.atm.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class MyApplicationProvider {
public static void main(String[] args) {
SpringApplication.run(MyApplicationProvider.class, args);
}
}
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.atm.cloudgroupId>
<artifactId>atm_enreka_invokerartifactId>
<packaging>warpackaging>
<version>0.0.1-SNAPSHOTversion>
<name>atm_enreka_invoker Maven Webappname>
<url>http://maven.apache.orgurl>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>Dalston.SR1version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>3.8.1version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-configartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-eurekaartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-ribbonartifactId>
dependency>
dependencies>
<build>
<finalName>atm_enreka_invokerfinalName>
build>
project>
server:
port: 9000
spring:
application:
name: first-service-invoker
eureka:
instance:
hostname: localhost
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
package com.atm.cloud;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@Configuration
public class InvokerController {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
@RequestMapping(value="/router",method=RequestMethod.GET,
produces=MediaType.APPLICATION_JSON_VALUE)
public String router() {
RestTemplate restTemplate = getRestTemplate();
// 根据应用名称调用服务
String json = restTemplate.getForObject(
"http://first-service-provider/person/1", String.class);
return json;
}
}
package com.atm.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class MyApplicationInvoker {
public static void main(String[] args) {
SpringApplication.run(MyApplicationInvoker.class, args);
}
}