SpringCloud 系列 --1.Eureka

本贴最后更新于 1848 天前,其中的信息可能已经时过境迁

1.搭建 Eureka 服务端

pom.xml 中添加引用

<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>org.crazyit.cloud</groupId> <artifactId>first-ek-server</artifactId> <version>0.0.1-SNAPSHOT</version> <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> </project>

spring-cloud-starter-eureka-server 会把 spring-boot-starter-web 依赖自动引入,所以同时也支持 web 能力
image.png

启动类添加注解 @EnableEurekaServer

package org.crazyit.cloud; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class FirstServer { public static void main(String[] args) { new SpringApplicationBuilder(FirstServer.class).run(args); } }

在 resource 目录下面的 applicaiton.yml 文件中指定端口等属性
registerWithEureka: false
是否将自己的信息注册到 Eureka 服务器,默认值 true

fetchRegistry: false
是否到 Eureka 服务器中抓取注册信息,默认值是 true

server: port: 8761 eureka: client: registerWithEureka: false fetchRegistry: false logging: level: com.netflix: INFO

启动服务浏览器访问如下:
image.png

2.服务提供者

a.pom.xml 中引入依赖

<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>

完整代码如下:

<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>org.crazyit.cloud</groupId> <artifactId>first-ek-service-provider</artifactId> <version>0.0.1-SNAPSHOT</version> <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-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> </dependencies> </project>

b. application.yml 中配置

spring: application: name: first-service-provider eureka: instance: hostname: localhost client: serviceUrl: defaultZone: http://localhost:8761/eureka/ logging: level: com.netflix: INFO

将应用名称配置为 first-server-provider,该服务将会被注册到端口为 8761 的 Eureka 服务器,
eureka.instance.hostname 配置该服务实例的主机名称.

在启动类上面使用 @EnableEurekaClient 注解,声明是一个 Eureka 客户端,

package org.crazyit.cloud; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class FirstServiceProvider { public static void main(String[] args) { new SpringApplicationBuilder(FirstServiceProvider.class).run(args); } }

编写一个简单的 controller

package org.crazyit.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 FirstController { @RequestMapping(value = "/person/{personId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public Person findPerson(@PathVariable("personId") Integer personId) { Person person = new Person(personId, "Crazyit", 30); return person; } }

对应的实体为:

package org.crazyit.cloud; public class Person { private Integer id; private String name; private Integer age; public Person() { super(); } public Person(Integer id, String name, Integer age) { super(); this.id = id; this.name = name; this.age = age; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }

运行后访问 eureka 可以看到我们的实例
image.png

3.服务调用者

pom.xml

<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>org.crazyit.cloud</groupId> <artifactId>first-ek-service-invoker</artifactId> <version>0.0.1-SNAPSHOT</version> <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-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> </dependencies> </project>

配置文件:

server: port: 9000 spring: application: name: first-service-invoker eureka: instance: hostname: localhost client: serviceUrl: defaultZone: http://localhost:8761/eureka/

在启动类加上 @EnableDiscoveryClient 注解
注意: @EnableEurekaClient 注解已经包含了 @EnableDiscoveryClient 的功能,也就说,一个 Eureka 客户端,本身就具有服务发现的能力。

package org.crazyit.cloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class FirstInvoker { public static void main(String[] args) { SpringApplication.run(FirstInvoker.class, args); } }

编写简单的 Controller

package org.crazyit.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 restTpl = getRestTemplate(); // 根据应用名称调用服务 String json = restTpl.getForObject( "http://first-service-provider/person/1", String.class); return json; } }

注意: RestTemplate 的 Bean 被 @LoadBalanced 注解修饰后,这个 RESTTemplate 实例就具有访问分布式服务的能力了。

启动服务 eureka 中有两个实例
image.png

并且访问 localhost:9000/router 结果如下:

image.png

  • Spring

    Spring 是一个开源框架,是于 2003 年兴起的一个轻量级的 Java 开发框架,由 Rod Johnson 在其著作《Expert One-On-One J2EE Development and Design》中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。框架的主要优势之一就是其分层架构,分层架构允许使用者选择使用哪一个组件,同时为 JavaEE 应用程序开发提供集成的框架。

    946 引用 • 1460 回帖
  • Eureka
    22 引用 • 3 回帖

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...