Eureka提供基于REST的服务,在集群中主要用于服务管理。Eureka提供了基于Java语言的客户端组件,客户端组件实现了负载均衡的功能,为业务组件的集群部署创造了条件。使用该框架,能够将业务组件注册到Eureka容器中,这些业务组件可进行集群部署,Eureka主要维护这些服务的列表并自动检查它们的状态。git
maven依赖github
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies>
更改spring boot 启动端口 在application.ymlspring
server: port: 8761
开启Eureka服务注解 @EnableEurekaServer浏览器
@EnableEurekaServer @SpringBootApplication public class EKServerApplication { public static void main(String[] args) { new SpringApplicationBuilder(EKServerApplication.class).run(args); } }
启动springbootspringboot
[Thread-11] o.s.c.n.e.server.EurekaServerBootstrap: Initialized server context [main] s.b.c.e.t.TomcatEmbeddedServletContainer: Tomcat started on port(s): 8761 (http) [main] .s.c.n.e.s.EurekaAutoServiceRegistration: Updating port to 8761 [main] c.b.firstEkServer.EKServerApplication: Started EKServerApplication in 8.594 seconds (JVM running for 9.523)
启动期间会出现一个没法链接到服务器的异常 这个是因为Eureka在启动的时候会把本身看成一个客户端去服务器抓取注册信息服务器
com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
增长以下配置启动时便不会再出现该异常app
eureka: client: registerWithEureka: false fetchRegistry: false
在浏览器中访问 http://localhost:8761 查看Eureka控制台 负载均衡
依赖框架
<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-ribbon</artifactId> </dependency>
在 application.yml 中配置端口、Eureka实例名称和Eureka服务地址maven
server: port: 8080 spring: application: name: ek-provider eureka: instance: hostname: localhost client: serviceUrl: defaultZone: http://localhost:8761/eureka/
建立一个 REST 服务
@RestController public class HelloController { @RequestMapping("/hello") public String hello(HttpServletRequest request) { return "hello:" + request.getRequestURL(); } }
开启Eureka客户端注解 @EnableEurekaServer
@EnableEurekaClient @SpringBootApplication public class EkProviderApplication { public static void main(String[] args) { new SpringApplicationBuilder(EkProviderApplication.class).run(args); } }
启动以后在 Eureka 控制台能够看到服务提供者已经在 Eureka 中注册
依赖
<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-ribbon</artifactId> </dependency>
在 application.yml 中配置端口、Eureka实例名称和Eureka服务地址
server: port: 9000 spring: application: name: ek-invoke eureka: instance: hostname: localhost client: serviceUrl: defaultZone: http://localhost:8761/eureka/
编写一个 REST 服务 调用服务提供者的 “/hello”
@RestController @Configuration public class InvokeController { @Bean @LoadBalanced public RestTemplate getRestTemplate() { return new RestTemplate(); } @RequestMapping("/invoke") public String invoke() { RestTemplate restTemplate = getRestTemplate(); return restTemplate.getForObject("http://ek-provider/hello", String.class); } }
在传统模式中,咱们一般会用Apache中的Httpclient来调用 REST 服务,在这里咱们使用 Spring 提供调用 REST 服务的组件 RestTemplate。 RestTemplate 自己并不具有调用分布式服务的能力,可是RestTemplate的bean被@LoadBalanced注解修饰后,这个RestTemplate实例就具备访问分布式服务的能力,这得益于 Spring 为其提供的各类拦截器 详情
开启Eureka客户端注解 @EnableEurekaServer
@EnableEurekaClient @SpringBootApplication public class EkInvokeApplication { public static void main(String[] args) { new SpringApplicationBuilder(EkInvokeApplication.class).run(args); } }
启动以后在 Eureka 控制台能够看到服务调用者已经在 Eureka 中注册
以后在浏览器访问服务调用者的 “invoke” 接口 返回以下
Eureka 服务器 经过心跳连接来维护最新的注册信息,这些注册信息都保存在内存中。
Eureka 服务提供者 主要进行:
Eureka 服务调用者 主要进行: