Eureka是Netflix开发的服务发现组件,本身是一个基于REST的服务。Spring Cloud将它集成在其子项目spring-cloud-netflix中,以实现Spring Cloud的服务发现功能。目前Eureka 项目相当活跃,代码更新相当频繁,目前最新的版本是1.5.5。Eureka 2.0也在紧锣密鼓地开发中,2.0将会带来更强的功能和更好的扩展性,但是由于还没有Release,故而不作讨论
1、新建一个项目hjduan.spring.cloud.eureka继承自父工程,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.0modelVersion>
<parent>
<groupId>com.hjduan.spring.cloudgroupId>
<artifactId>hjduan.spring.cloudartifactId>
<version>0.0.1-SNAPSHOTversion>
parent>
<artifactId>hjduan.spring.cloud.eurekaartifactId>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-securityartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-eureka-serverartifactId>
dependency>
dependencies>
<build>
<resources>
<resource>
<directory>src/main/resourcesdirectory>
<includes>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
<include>**/*.ymlinclude>
includes>
<filtering>truefiltering>
resource>
resources>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
plugin>
plugins>
build>
project>
2、application.yml配置
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://admin:admin@${eureka.instance.hostname}:${server.port}/eureka/
security:
basic:
enabled: true
user:
name: admin #登陆用户名
password: admin#登陆密码
3、启动类
package com.hjduan.spring.cloud.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* 启动一个服务注册中心
*/
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplicationConfiguration {
public static void main (String[]args){
new SpringApplication(EurekaApplicationConfiguration.class).run(args);
}
}
1、修改hjduan.spring.cloud.provider 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.0modelVersion>
<parent>
<groupId>com.hjduan.spring.cloudgroupId>
<artifactId>hjduan.spring.cloudartifactId>
<version>0.0.1-SNAPSHOTversion>
parent>
<artifactId>hjduan.spring.cloud.providerartifactId>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-eurekaartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
dependencies>
<build>
<resources>
<resource>
<directory>src/main/resourcesdirectory>
<includes>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
<include>**/*.ymlinclude>
includes>
<filtering>truefiltering>
resource>
resources>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
plugin>
plugins>
build>
project>
2、application.yml配置
server:
port: 8762
eureka:
client:
serviceUrl:
defaultZone: http://admin:admin@localhost:8761/eureka/
instance:
prefer-ip-address: true
instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}
spring:
application:
name: hjduan-spring-cloud-provider
3、控制器
package com.hjduan.spring.cloud.provider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
@RestController
public class UserController {
@Autowired
private EurekaClient eurekaClient;
@Autowired
private DiscoveryClient discoveryClient;
@GetMapping(value = "eureka-instance")
public String serviceUrl() {
InstanceInfo instance = eurekaClient.getNextServerFromEureka("HJDUAN-SPRING-CLOUD-CONSUMER", false);
return instance.getHomePageUrl();
}
@SuppressWarnings("deprecation")
@GetMapping(value = "instance-info")
public ServiceInstance showInfo() {
ServiceInstance localServiceInstance = this.discoveryClient.getLocalServiceInstance();
return localServiceInstance;
}
/*@Autowired
RestTemplate restTemplate;
@GetMapping(value = "/hi/{name}")
public String ribbonHelloWordService(@PathVariable String name) {
return restTemplate.getForObject("http://HJDUAN-SPRING-CLOUD-CONSUMER/hi/{name}", String.class, name);
}*/
}
4、启动类
package com.hjduan.spring.cloud.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.client.RestTemplate;
/**
* 注册提供者
*
* 作者: 段浩杰 2017年7月25日
*/
@EnableEurekaClient
@SpringBootApplication
@Controller
public class ApplicationConfiguration {
public static void main(String[] args) {
new SpringApplication(ApplicationConfiguration.class).run(args);
}
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
5、修改hjduan.spring.cloud.consumer 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.0modelVersion>
<parent>
<groupId>com.hjduan.spring.cloudgroupId>
<artifactId>hjduan.spring.cloudartifactId>
<version>0.0.1-SNAPSHOTversion>
parent>
<artifactId>hjduan.spring.cloud.consumerartifactId>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-eurekaartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
dependencies>
<build>
<resources>
<resource>
<directory>src/main/resourcesdirectory>
<includes>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
<include>**/*.ymlinclude>
includes>
<filtering>truefiltering>
resource>
resources>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
plugin>
plugins>
build>
project>
6、application.yml配置
server:
port: 8763
eureka:
client:
serviceUrl:
defaultZone: http://admin:admin@localhost:8761/eureka/
instance:
prefer-ip-address: true
instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}
spring:
application:
name: hjduan-spring-cloud-consumer
7、控制器
package com.hjduan.spring.cloud.consumer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
/**
* 暴露的接口
* 作者: 段浩杰 2017年7月25日
*/
@RestController
public class UserController {
@Value("${server.port}")
private String port;
@GetMapping(value = "/hi/{name}")
public String showInfo(@PathVariable String name) {
return "hi" + name+port;
}
}
8、启动类
package com.hjduan.spring.cloud.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* 注册提供者
* 作者: 段浩杰 2017年7月25日
*/
@EnableEurekaClient
@SpringBootApplication
public class ApplicationConfiguration {
public static void main(String[] args) {
new SpringApplication(ApplicationConfiguration.class).run(args);
}
}
9、然后分别启动hjduan.spring.cloud.eureka、hjduan.spring.cloud.provider、hjduan.spring.cloud.consumer这三个项目
10、访问http://localhost:8761,输入用户名密码,进入到eureka中心,来监控和管理服务。