Eureka架構(gòu)圖
image
PS:
所有的服務(wù)提供者和服務(wù)調(diào)用者都會在注冊中心進行注冊,服務(wù)提供者在注冊中心上提供服務(wù),服務(wù)調(diào)用者在注冊中心中獲取服務(wù)列表。
構(gòu)建第一個應(yīng)用
1. 創(chuàng)建一個Eureka服務(wù)
導(dǎo)入相關(guān)依賴,pom文件如下
<?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.zgy</groupId>
<artifactId>first-114</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>first-114</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
將注解@EnableEurekaServer加在啟動類上
package com.zgy.first114;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @EnableEurekaServer:說明該應(yīng)用是一個Eureka服務(wù)器
*/
@SpringBootApplication
@EnableEurekaServer
public class First114Application {
public static void main(String[] args) {
new SpringApplicationBuilder(First114Application.class).web(true).run(args);
}
}
配置文件
server:
port: 8761 #設(shè)置服務(wù)器端口號為:8761
eureka:
client:
register-with-eureka: false #不注冊到其他Eureka服務(wù)器上
fetch-registry: false #不在其他Eureka服務(wù)器上讀取注冊信息
啟動應(yīng)用
image
2. 創(chuàng)建服務(wù)提供者
導(dǎo)入依賴,pom文件如下
<?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.zgy</groupId>
<artifactId>first-police</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>first-police</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
將注解@EnableEurekaClient加在啟動類上
package com.zgy.firstpolice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @EnableEurekaClient:說明該應(yīng)用是一個客戶端
*/
@SpringBootApplication
@EnableEurekaClient
public class FirstPoliceApplication {
public static void main(String[] args) {
SpringApplication.run(FirstPoliceApplication.class, args);
}
}
package com.zgy.firstpolice;
public class Police {
private Integer id;
private String name;
private String message;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
package com.zgy.firstpolice;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PoliceController {
@GetMapping("/call/{id}")
public Police call(@PathVariable Integer id){
Police police = new Police();
police.setId(id);
police.setName("張三");
police.setMessage("AAA");
return police;
}
}
配置文件
spring:
application:
name: first-police #定義項目的名稱
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/ #將該應(yīng)用注冊到Eureka服務(wù)器上
啟動應(yīng)用胶坠,然后查看eureka服務(wù)器
image
3. 創(chuàng)建服務(wù)調(diào)用者
引入依賴,pom文件如下
<?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.zgy</groupId>
<artifactId>first-person</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>first-person</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
將注解@EnableEurekaClient加在啟動類上
package com.zgy.firstperson;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class FirstPersonApplication {
public static void main(String[] args) {
SpringApplication.run(FirstPersonApplication.class, args);
}
}
編寫Controller
package com.zgy.firstperson;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@Configuration
public class TestController {
/*
@LoadBalanced:該RestTemplate具有負載均衡的功能
*/
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
@GetMapping("/router")
public String router(){
RestTemplate restTemplate = getRestTemplate();
String json = restTemplate.getForObject("http://first-police/call/100", String.class);
return json;
}
}
配置文件
server:
port: 8081
spring:
application:
name: first-person
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
啟動應(yīng)用繁堡,查看eureka服務(wù)器
image
訪問Controller
image
第一個應(yīng)用就已經(jīng)完成沈善,以下是本次程序的結(jié)構(gòu)圖
image