Eureka2.0已经闭源了,但也是注册中心的热门组件,咱们了解他的使用以及原理。web
首先是parent的pom配置,这里使用的是Hoxton.SR9版本。spring
<properties> <spring.cloud-version>Hoxton.SR9</spring.cloud-version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring.cloud-version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
而后是服务端的pom配置segmentfault
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies>
服务端代码,除了@SpringBootApplication注解,还须要@EnableEurekaServer注解。app
@SpringBootApplication @EnableEurekaServer public class EurekaServer8888Application { public static void main(String[] args){ SpringApplication.run(EurekaServer8888Application.class, args); } }
yml文件配置:ide
server: port: 8888 eureka: client: service-url: defaultZone: http://localhost:8888/eureka/ # 单个注册中心服务的时候不注册本身 register-with-eureka: false # 单个注册中心服务的时候不拉取数据 fetch-registry: false spring: application: name: eureka-server
上面配置完后,运行EurekaServer8888Application的main方法,访问http://127.0.0.1:8888,出现如下界面,Eureka服务端配置完成。spring-boot
pom的配置:fetch
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies>
yml配置:url
server: port: 7000 eureka: client: service-url: defaultZone: http://localhost:8888/eureka/ spring: application: name: eureka-provider
提供方Application代码:spa
@SpringBootApplication public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); } }
Controller代码:3d
@RestController public class ProviderController { @RequestMapping("/getInfo") public String getInfo(String name) { return "provider-" + name; } }
启动ProviderApplication后,查看服务端的地址,能够看到已经注册到注册中心了。
此时的状态是这样的:
yml配置
server: port: 7500 eureka: client: service-url: defaultZone: http://localhost:8888/eureka/ spring: application: name: eureka-consumer
消费方Application代码,注意这边有一个@LoadBalanced注解的RestTemplate。
@SpringBootApplication public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } }
Controller代码,咱们这边访问的地址是eureka-provider,并无访问到具体的ip和端口。
@RestController public class ConsumerController { @Autowired RestTemplate restTemplate; @RequestMapping("/getInfo") public String getInfo(String name) { ResponseEntity<String> forEntity = restTemplate.getForEntity("http://eureka-provider/getInfo?name=" + name, String.class); return forEntity.getBody(); } }
启动ConsumerApplication后,查看服务端的地址,能够看到已经注册到注册中心了。
此时的状态是这样的:
经过地址调用消费方,能够看到调用成功:
由于Consumer在调用Provider以前,会经过服务发现拿到注册中心的注册表信息,而后经过经过注册表信息去找到Provider的IP和端口,再进行调用。
在生成过程当中,为了高可用,咱们会搭建多个Euraka Server,这里演示两个,再加一个9999端口的Server。
首先是8888的yml修改,注释掉register-with-eureka和fetch-registry,这样使用默认值true,另外defaultZone的地址为另一个Server的地址。
server: port: 8888 eureka: client: service-url: defaultZone: http://localhost:9999/eureka/ # 单个注册中心服务的时候不注册本身 #register-with-eureka: false # 单个注册中心服务的时候不拉取数据 #fetch-registry: false spring: application: name: eureka-server
9999的配置以下,跟上面差很少:
server: port: 9999 eureka: client: service-url: defaultZone: http://localhost:8888/eureka/ spring: application: name: eureka-server
Consumer和Provider的yml的defaultZone都修改成:
eureka: client: service-url: defaultZone: http://localhost:8888/eureka/,http://localhost:9999/eureka/
四个Application启动后,无论访问的是8888端口仍是9999端口,均可以看到如下信息:
此时的状态是这样的:Eureka的简单示例就到这边,后面咱们看看他的源码是怎么实现这些功能的。