Eureka學習java
例子學習地址:https://github.com/MyCreazy/EurekaStudygit
這裏有參考http://blog.csdn.net/nero__a/article/details/65631367進行總結操做github
Spring Cloud下有不少工程:web
1. 服務註冊中心spring
在IDEA中建立一個Spring Cloud工程,引入Eureka-Server包,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>app
<groupId>com.sl</groupId>負載均衡
<artifactId>RegistryCenter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>RegistryCenter</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<!-- 用於註冊中心訪問帳號認證 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 註冊中心所需的包 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- spring boot的maven打包插件 -->
<build>
<defaultGoal>compile</defaultGoal>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
建立APP類,並用@EnableEurekaServer和@SpringBootApplication兩個註解修飾,後者是Spring Boot應用都須要用的,這裏不做過多解釋;@EnableEurekaServer註解的做用是觸發Spring Boot的自動配置機制,因爲咱們以前在pom文件中導入了eureka-server,spring boot會在容器中建立對應的bean。Eureka的代碼以下:
package com.sl.RegistryCenter;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* Hello world!
*
*/
@EnableEurekaServer
@SpringBootApplication
public class App {
public static void main(String[] args) {
new SpringApplicationBuilder(App.class).web(true).run(args);
}
}
添加配置文件application.properties,而且添加以下參數,才能建立一個真正可使用的服務註冊中心。
######eureka服務端######
spring.application.name=eureka-server
#驅逐下線的服務,間隔,5秒,默認是60,建議開發和測試環境配置
eureka.server.evictionIntervalTimerInMs=10000
server.port=8761
#是否須要註冊到註冊中心,由於該項目自己做爲服務註冊中心,因此爲false
eureka.client.register-with-eureka=false
#是否須要從註冊中心獲取服務列表,緣由同上,爲false
eureka.client.fetch-registry=false
security.basic.enabled=true
security.user.name=admin
security.user.password=123
#註冊服務器的地址:服務提供者和服務消費者都要依賴這個地址
eureka.client.serviceUrl.defaultZone=http://admin:123@localhost:8761/eureka
###Eureka自我保護機制,爲true表示開,false表示關,默認爲開####
eureka.server.enable-self-preservation=true
啓動註冊服務,並訪問:http://localhost:8761,就能夠看到以下界面。
2. 服務提供者
建立一個Spring Boot工程,表明服務提供者,該服務提供者會暴露一個當前請求產生的時間字符串。
工程的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>
<groupId>com.sl</groupId>
<artifactId>TestMicroService</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>TestMicroService</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- 用於註冊中心訪問帳號認證 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<defaultGoal>compile</defaultGoal>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
其中的關鍵在於spring-cloud-starter-eureka這個Jar包,其中包含了eureka的客戶端實現。
在src/main/java/com.sl.TestMicroService下建立工程的主類App,使用@EnableDiscoveryClient註解修飾,該註解在服務啓動的時候,能夠觸發服務註冊的過程,向配置文件中指定的服務註冊中心(Eureka-Server)的地址註冊本身提供的服務。App的源碼以下:
package com.sl.TestMicroService;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* Hello world!
*
*/
@EnableDiscoveryClient
@SpringBootApplication
public class App {
public static void main(String[] args) {
new SpringApplicationBuilder(App.class).web(true).run(args);
}
}
application.properties配置文件的內容以下:
server.port=8111
#設置應用的名稱
spring.application.name=microservice-provider-user
#服務註冊的Eureka Server地址
eureka.client.serviceUrl.defaultZone=http://admin:123@localhost:8761/eureka
#設置註冊ip
eureka.instance.prefer-ip-address=true
#自定義應用實例id
#健康檢查
eureka.client.healthcheck.enabled=true
服務提供者的基本框架搭好後,須要實現服務的具體內容,在ComputeController類中實現,它的具體代碼以下:
package com.sl.TestMicroService;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.log4j.Logger;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ComputeController {
private final Logger logger = Logger.getLogger(getClass());
@Autowired
private DiscoveryClient client;
private SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String test() {
ServiceInstance instance = client.getLocalServiceInstance();
String temp = "當前時間【" + df.format(new Date()) + "】/add, host:" + instance.getHost() + ", service_id:"
+ instance.getServiceId();
logger.info(temp);
return temp;
}
}
先啓動服務註冊中心的工程,而後再啓動服務提供者,在訪問:localhost:8761,以下圖所示,服務提供者已經註冊到服務註冊中心啦,下圖能夠查看
在Spring Cloud Netflix中,使用Ribbon實現客戶端負載均衡,使用Feign實現聲明式HTTP客戶端調用——即寫得像本地函數調用同樣。
3. 服務消費者-Feign
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>
<groupId>com.sl</groupId>
<artifactId>ServiceConsumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>ServiceConsumer</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<defaultGoal>compile</defaultGoal>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependencies>
<!-- Feign實現聲明式HTTP客戶端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<!-- eureka客戶端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- spring boot實現Java Web服務-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
首先建立應用程序啓動類:ConsumerApp,代碼以下:
package com.sl.ServiceConsumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
/**
* Hello world!
*
*/
@EnableDiscoveryClient // 用於啓動服務發現功能
@EnableFeignClients // 用於啓動Fegin功能
@SpringBootApplication
public class ConsumerApp {
/**
* main函數入口
*
* @param args
*/
public static void main(String[] args) {
SpringApplication.run(ConsumerApp.class);
}
}
而後建立ComputeClient接口,使用@FeignClient("microservice-provider-user")註解修飾,microservice-provider-user就是服務提供者的名稱,而後定義要使用的服務,代碼以下:
package com.sl.ServiceConsumer;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient("microservice-provider-user")
public interface ConsumerClient {
// @RequestMapping(method = RequestMethod.GET, value = "/add")
// Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value =
// "b") Integer b);
@RequestMapping(method = RequestMethod.GET, value = "/test")
String test();
}
在ConsumerController中,像引入普通的spring bean同樣引入ComputeClient對象,其餘的和Ribbon的相似。
package com.sl.ServiceConsumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConsumerController {
@Autowired
private ConsumerClient computeClient;
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String test() {
return computeClient.test();
}
}
application.properties的內容以下:
#應用名稱
spring.application.name=fegin-consumer
#端口號
server.port=9000
#註冊中心的地址
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
啓動fegin消費者,訪問localhost:9000/add,也能夠看到服務提供者已經收到了消費者發來的請求。
能夠看到兩個服務輪流被調用
注意事項
1.關於版本對應問題
2.直接打包
打包時記得引用下面的包
<build>
<defaultGoal>compile</defaultGoal>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>