基于spring-cloud实现eureka注册服务小案例
2016-07-30 00:53
1106 查看
本案例来自于spring官网:http://spring.io/guides/gs/service-registration-and-discovery/
JDK要求1.8或以上版本。
首先创建两个项目,eureka-service和eureka-client。
eureka-server作为eureka的服务端,提供注册服务,eureka-client作为eureka的客户端,属于一个应用,注册到eureka注册中心。
eureka-service的配置文件pom.xml如下:
eureka-service项目中创建包,自定,本例为:com.spring
在该包下创建类用于启动服务EurekaServiceApplication.java
关于eureka-service的配置文件 applicaiton.yml 或者使用application.properties,两者格式不同。
application.yml
简单的eureka-service就写好了,运行下EurekaServiceApplication.java
访问:http://localhost:8761/
关于eureks-client 的pom.xml文件如下,因为不提供其他服务,这里基本与eureka-service一样:
同样创建入口类EurekaClientApplication.java
关于eureka-client的配置文件bootstrap.yml和application.yml,
bootstrap.yml先于application.yml加载,一般不变的东西卸载bootstrap里,使用一样:
application.yml
好了,现在启动eureka-client,再次访问http://localhost:8761,可能需要等一会
访问 http://localhost:7070/service-instances/cloud-client/ ,这里我把应用名改了
会出现关于应用的信息:
这里因为两个都部署在同一台机器上,所以可以直接发现服务,并注册,如果在不同的机器上不部署,application.yml配置中需要加上eureka的地址如,多个eureka服务地址,可以用,号隔开:
JDK要求1.8或以上版本。
首先创建两个项目,eureka-service和eureka-client。
eureka-server作为eureka的服务端,提供注册服务,eureka-client作为eureka的客户端,属于一个应用,注册到eureka注册中心。
eureka-service的配置文件pom.xml如下:
<?xml version="1.0" encoding="UTF-8"?> <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.example</groupId> <artifactId>eureka-service</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.5.RELEASE</version> <!--配合spring cloud版本--> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <!--设置字符编码及java版本--> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <!--增加eureka-server的依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <!--用于测试的,本例可省略--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <!--依赖管理,用于管理spring-cloud的依赖--> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-parent</artifactId> <version>Brixton.SR3</version> <!--官网为Angel.SR4版本,但是我使用的时候总是报错--> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <!--使用该插件打包--> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
eureka-service项目中创建包,自定,本例为:com.spring
在该包下创建类用于启动服务EurekaServiceApplication.java
package com.spring; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer //开启eureka服务 @SpringBootApplication //springBoot注解,spring在springBoot基础之上来构建项目 public class EurekaServiceApplication { //spirng boot的标准入口 public static void main(String[] args) { SpringApplication.run(EurekaServiceApplication.class, args); } }
关于eureka-service的配置文件 applicaiton.yml 或者使用application.properties,两者格式不同。
application.yml
server: port: 8761 #指定服务端口 eureka: client: registerWithEureka: false #是否将eureka自身作为应用注册到eureka注册中心 fetchRegistry: false #为true时,可以启动,但报异常:Cannot execute request on any known server
简单的eureka-service就写好了,运行下EurekaServiceApplication.java
访问:http://localhost:8761/
关于eureks-client 的pom.xml文件如下,因为不提供其他服务,这里基本与eureka-service一样:
<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.huawei.it</groupId> <artifactId>eureka-client</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.5.RELEASE</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-parent</artifactId> <version>Brixton.SR3</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!--此处是差异所在--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
同样创建入口类EurekaClientApplication.java
package com.spring; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author Administrator * */ @EnableDiscoveryClient //通过该注解,实现服务发现,注册 @SpringBootApplicati a317 on public class EurekaClientApplication { public static void main(String[] args) { SpringApplication.run(EurekaClientApplication.class, args); } } @RestController class ServiceInstanceRestController { @Autowired private DiscoveryClient discoveryClient; @RequestMapping("/service-instances/{applicationName}") public List<ServiceInstance> serviceInstancesByApplicationName( @PathVariable String applicationName) { return this.discoveryClient.getInstances(applicationName); } @RequestMapping("/") public String sayhello() { return "hello"; } }
关于eureka-client的配置文件bootstrap.yml和application.yml,
bootstrap.yml先于application.yml加载,一般不变的东西卸载bootstrap里,使用一样:
application.yml
server: port: 7070 spring: application: name: cloud-client #为你的应用起个名字,该名字将注册到eureka注册中心
好了,现在启动eureka-client,再次访问http://localhost:8761,可能需要等一会
访问 http://localhost:7070/service-instances/cloud-client/ ,这里我把应用名改了
会出现关于应用的信息:
这里因为两个都部署在同一台机器上,所以可以直接发现服务,并注册,如果在不同的机器上不部署,application.yml配置中需要加上eureka的地址如,多个eureka服务地址,可以用,号隔开:
eureka: client: serviceUrl: defaultZone: http://${eureka-serviceIP}:8761/eureka/[/code]
相关文章推荐
- centos6.5上安装docker
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- Spring和ThreadLocal
- Spring Boot 开发微服务
- Spring AOP动态代理-切面
- Spring整合Quartz(JobDetailBean方式)
- Spring整合Quartz(JobDetailBean方式)
- 模拟Spring的简单实现
- Spring整合WebSocket应用示例(上)
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- 监听器获取Spring配置文件的方法
- Spring Boot 定制URL匹配规则的方法
- Java利用Sping框架编写RPC远程过程调用服务的教程
- springmvc 发送ajax出现中文乱码的解决方法汇总
- Spring MVC中Ajax实现二级联动的简单实例