1.基礎架構

eueka按邏輯上可以划分為3個模塊,eureka-server,service-provider,service-consumer
eureka-server:服務端,提供服務注冊和發現
eureka-client-service-provider:服務端,服務提供者,通過http rest告知服務端注冊,更新,取消服務
eureka-client-service-consumer:客戶端,服務消費者,通過http rest從服務端獲取需要服務的地址列表,然后配合一些負載均衡策略(ribbon)來調用服務端服務。
值得注意的一點,不同於其他服務注冊與發現(zookeeper需要單獨以中間件的形式部署集群server),以上3個角色都是邏輯角色,甚至可以在相同的jvm進程上
2.構建服務中心
2.1搭建服務注冊中心
①新建Spring Boot工程,在pom.xml 中新增如下依賴
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<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>Dalston.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
②通過@EnableEurekaServer注解啟動一個服務注冊中心
例如:
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
③添加application.yml配置(屬性文件配置也行)
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
注意:在該默認配置下,注冊中心也會將自己作為客戶端嘗試注冊自己,因此要禁用客戶端注冊行為
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
④啟動應用,並訪問http://localhost:8761/,可以看到如下圖所示的Eureka信息面板,其中Instances currently registered with Eureka欄為空,說明該注冊中心還沒有注冊服務。
2.2 高可用注冊中心(集群)
在Eureka的服務治理設計中,所有的節點既是服務提供方,也是服務消費方,服務注冊中心也不例外,Eurekad的高可用實際就是將自己作為服務向其他服務注冊中心注冊自己,這樣形成一組互相注冊的服務注冊中心,以實現服務清單的互相同步,達到高可用的效果
例如:構建一個雙節點的注冊中心集群
application1.yml配置如下:
server:
port: 8761
eureka:
instance:
hostname: 10.22.0.130
client:
register-with-eureka: false
fetch-registry: false
serviceUrl:
defaultZone: http://10.22.0.131:8762/eureka/
application2.yml配置如下:
server:
port: 8762
eureka:
instance:
hostname: 10.22.0.131
client:
register-with-eureka: false
fetch-registry: false
serviceUrl:
defaultZone: http://10.22.0.130:8761/eureka/
依次啟動兩個進程
在10.22.0.130上:java -jar Eureka-Server-0.0.1-SNAPSHOT.jar
在10.22.0.131上:java -jar Eureka-Server-0.0.1-SNAPSHOT.jar
然后訪問http://10.22.0.130:8761/ 可以看到如下圖所示:
我們可以看到,registered-replicas(可用分片)中已經有10.22.0.131節點的eureka-server了,同樣訪問http://10.22.0.131:8762/ 中也可以看到registered-replicas中已經有10.22.0.130節點的eureka-server了,至此服務集群搭建完成
注意:在搭建了注冊中心集群后,服務提供方也要做一定的配置調整才能將服務注冊到Eureka server集群中,修改配置eureka.client.serviceUrl.defaultZone=http://10.22.0.130:8761/eureka,http://10.22.0.131:8762/eureka/
3.服務注冊
服務注冊中心搭建好了,之后我們需要注冊服務到注冊中心中,新建一個Spring Boot工程,在pom.xml中添加如下依賴:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
然后新建配置文件application.yml,配置如下:
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka
server:
port: 8763
spring:
application:
name: service-hello
新建啟動類:HelloApplication.java
@RestController
@EnableEurekaClient
@SpringBootApplication
public class HelloApplication {
public static void main(String[] args) {
SpringApplication.run(HelloApplication.class, args);
}
@Value("${server.port}")
String port;
@RequestMapping("/hi")
public String sayHi(String name){
return "hi" + port + ";name"+name;
}
}
啟動注冊中心,然后運行該應用,訪問注冊中心,會看到注冊中心中多了個服務實例,如圖所示:
我們還可以直接通過瀏覽器調用該服務,在瀏覽器中訪問http://localhost:8763/hi?name=123

4.服務發現與消費
服務發現由eureka客戶端完成,而服務的消費由ribbon完成。ribbon是客戶端的一個負載均衡器,用於對消費的服務實例進行負載均衡。
上面已經介紹了通過瀏覽器消費服務,現在是通過接口調用的方式消費服務,具體步驟如下:
首先向注冊中心注冊兩個服務,如圖所示:
然后再構建一個eureka客戶端,消費者兩個服務
新建RibbonApplication.java
代碼如下:
@EnableDiscoveryClient
@SpringBootApplication
public class RibbonApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
}
新建服務接口:HelloService.java
代碼如下:
@Service
public class HelloService {
@Autowired
RestTemplate restTemplate;
public String hiService(String name){
return restTemplate.getForObject("http://SERVICE-HELLO/hi?name=" + name , String.class);
}
}
新建控制器:HelloControler.java
代碼如下:
@RestController
public class HelloControler {
@Autowired
HelloService helloService;
@RequestMapping(value = "/hi")
public String hi(@RequestParam String name){
return helloService.hiService(name);
}
}
新增配置文件:application.yml
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8764
spring:
application:
name: service-ribbon
然后訪問http://localhost:8764/hi?name=1,會輪詢調用兩個服務,如下圖所示:

