服务治理是微服务架构体系中,不可或缺的重要的一部分。SpringCloud 中对 Netflix Eureka 进行了再封装,将其做为默认的推荐的服务注册中心。Spring Cloud Eureka 是一个基于 Rest 的服务,提供了基于 Java 的客户端,很是方便快捷的使用。java
注册中心的存在,能够很好的隔绝生产者和消费者。当你须要调用某一个服务的时候,你不须要知道服务的具体实现细节,也不须要知道具体的是有多少的服务提供方在工做。你只须要去 Eureka 中去哪去服务列表,查看其中是否有你须要的服务,若是在其中,这获取服务地址、端口等信息,而后进行调用。web
下面的配置使用过程当中,均须要一些共同的 jar 依赖,故建立一个 maven 父工程,取名 nativecloud,具体pom配置以下:spring
<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>com.cfang</groupId> <artifactId>nativecloud</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> <!-- 设置查找顺序:relativePath元素中的地址–本地仓库–远程仓库。 若是为空,则始终从仓库获取,不获取本地路径--> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Finchley.RELEASE</spring-cloud.version> <fastjson.version>1.2.58</fastjson.version> <druid.version>1.1.18</druid.version> </properties> <modules> <module>nativeEureka</module> <module>nativefshService</module> <module>nativefshsubService</module> </modules> <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> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>${fastjson.version}</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>28.0-jre</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
一、建立 maven 子工程 nativeEureka ,工程的 pom 配置以下:apache
<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> <parent> <groupId>com.cfang</groupId> <artifactId>nativecloud</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>nativeEureka</artifactId> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> <version>1.4.7.RELEASE</version> </dependency> </dependencies> </project>
二、新建启动类 NativeEurekaApplicationjson
package com.cfang; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class NativeEurekaApplication { public static void main(String[] args) { SpringApplication.run(NativeEurekaApplication.class, args); } }
三、新建配置文件 application.properties浏览器
在 src/main/resources 目录下,新建 application.properties 配置文件:安全
server.port=8081 spring.application.name=nativeEureka eureka.instance.hostname= server1 #是否将本身做为客户端注册到注册中心:false-不注册 eureka.client.register-with-eureka=false #是否须要从注册中心检索获取服务的注册信息。默认值为true #单机版的可设置成false,集群版的因为须要同步其余节点的服务注册数据,故设成true。 eureka.client.fetch-registry=false #是否开启自我保护模式,默认值true #eureka server默认在运行期间会去统计心跳失败比例在 15 分钟以内是否低于 85%,若是低于 85%,Eureka Server 会将这些实例保护起来,让这些实例不会过时, #可是在保护期内若是服务恰好这个服务提供者非正常下线了,此时服务消费者就会拿到一个无效的服务实例,此时会调用失败 eureka.server.enable-self-preservation=false #扫描失效服务的间隔时间,单位毫秒,默认值 60 * 1000 eureka.server.eviction-interval-timer-in-ms=10000 #服务地址 eureka.client.service-url.defaultZone= http://server1:8081/eureka/
其中,server1 是修改的本地 hosts 文件。架构
四、启动app
直接运行 NativeEurekaApplication 就能够了启动注册中心服务了。因为在配置文件 application.properties 中配置的端口号为 8081,故能够直接在浏览器中输入 http://localhost:8081/ 进行Eureka控制绕的查看:maven
编写个服务提供者,并注册到上面启动好的 Eureka 注册中心中去,并暴露一个 Rest 接口。
一、建立 maven 子工程 nativefshService ,工程 pom 配置以下:
<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> <parent> <groupId>com.cfang</groupId> <artifactId>nativecloud</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>nativefshService</artifactId> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
二、src/main/resources 目录下,新建 application.properties 配置文件:
server.port=8701 spring.application.name=nativefshService #是否将本身做为客户端注册到注册中心:false-不注册 eureka.client.register-with-eureka=true #服务地址 eureka.client.service-url.defaultZone= http://server1:8081/eureka/
三、提供 REST 接口
package com.cfang.facade; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/user") public class UserController { @GetMapping("/hello") public String hello() { return "hello world!"; } }
四、新建启动类 NativefshServiceApplication
package com.cfang; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) @EnableEurekaClient public class NativefshServiceApplication { public static void main(String[] args) { SpringApplication.run(NativefshServiceApplication.class, args); } }
编写好启动类,直接运行。查看控制台输出:
或者在 Eureka 注册中心页面上查看
说明服务已正常启动并注册到 Eureka 中。
五、验证 REST 接口可用性
浏览器测试访问 http://localhost:8701/user/hello , 打印出 hello world! 字符串,说明服务接口正常启用。
建立个服务的消费者,调用上面的 /user/hello 服务。
一、建立 maven 子工程 nativefshsubService,工程 pom 配置以下
<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> <parent> <groupId>com.cfang</groupId> <artifactId>nativecloud</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>nativefshsubService</artifactId> <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> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> </dependencies> </project>
二、配置文件 application.properties
server.port=8702 spring.application.name=nativefshsubService #是否将本身做为客户端注册到注册中心:false-不注册 eureka.client.register-with-eureka=true #服务地址 eureka.client.service-url.defaultZone= http://server1:8081/eureka/
三、RestTemplate 配置
package com.cfang.config; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class CommonConfiguration { @Bean // @LoadBalanced public RestTemplate getRestTemplate() { return new RestTemplate(); } }
四、调用
package com.cfang.Controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController @RequestMapping("/test") public class TestController { @Autowired private RestTemplate restTemplate; /** * 直接调用 */ @GetMapping("callHello") public String callHello() { return restTemplate.getForObject("http://server1:8701/user/hello", String.class); } /** * 经过 Eureka 调用 */ @GetMapping("callHello2") public String callHello2() { return restTemplate.getForObject("http://nativefshService/user/hello", String.class); } }
五、建立启动类 NativefshsubServiceApplication
package com.cfang; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) @EnableDiscoveryClient public class NativefshsubServiceApplication { public static void main(String[] args) { SpringApplication.run(NativefshsubServiceApplication.class, args); } }
直接运行 main 方法启动。浏览器中输入 http://localhost:8702/test/callHello ,显示 hello world! 字符串则说明正常调用接口。
六、经过 Eureka 调用
上面的第 3 步配置 RestTemplate 中,去除注解 @LoadBalanced 的行注释。重启应用,浏览器中经过访问 http://localhost:8702/test/callHello2,显示 hello world!字符串,接口正常调用。
关于注解 @LoadBalanced ,后续文章中还会有说明使用。
主要的介绍使用单机版的 Eureka 注册中心、服务的提供注册、服务的消费注册以及经过 Eureka 来管理注册的服务接口信息。后续中,将介绍 Eureka 的高可用搭建、Eureka 的安全认证以及经常使用的 Eureka 的配置项信息。