1. 開發Eureka服務注冊中心
這里使用的spring cloud版本是Edgware.SR3
使用maven新建一個quickstart工程,pom文件主要內容如下:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
新建啟動類
@SpringBootApplication
@EnableEurekaServer
public class EurekaRegisterCenter {
public static void main(String[] args) {
SpringApplication.run(EurekaRegisterCenter.class, args);
}
}
這里的@EnableEurekaServer 注解表示啟動EurekaServer,即啟動服務注冊中心。
新建配置文件 src/main/resources/application.yml
server:
port: 8761
eureka:
client:
registerWithEureka: false
fetchRegistry: false
- 這里設置了端口是8761端口
- eureka.client.registerWithEureka 表明不將自己作用服務注冊到別的注冊中心上,需要注意的是,eureka server同時也可以是一個eureka client,也可以注冊到別的注冊中心,這一塊我們到后面的高可用集群再說。
- eureka.client.fetchRegistry,表示此服務不抓取注冊中心信息。
打開瀏覽器,輸入http://localhost:8761/,就可以看到Eureka的控制臺
2.開發服務提供者
同樣新建一個工程,pom文件主要內容如下:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
新建配置文件 src/main/resources/application.yml
server:
port: 8080
spring:
application:
name: eureka-demo-provider
eureka:
instance:
hostname: localhost
client:
serverUrl:
defaultZone: http://localhost:8761/eureka
配置了端口,服務名稱,主機地址,還有服務注冊中心的地址
新建一個用于接收服務消費者調用的rest接口
@RestController
public class SayHelloController {
@RequestMapping(value = "/sayHello/{name}", method = RequestMethod.GET)
public String sayHello(@PathVariable("name") String name) {
return "hello," + name;
}
}
應用啟動類
@SpringBootApplication
@EnableEurekaClient
public class EurekaDemoProvider {
public static void main(String[] args) {
SpringApplication.run(EurekaDemoProvider.class, args);
}
}
@EnableEurekaClient,表明這是個Eureka客戶端應用,需要向Eureka服務端注冊自己為一個服務。
此時看一下Eureka控制臺,就會發現一個名為eureka-demo-provider的服務出現。
3.開發一個服務調用者
新建工程,pom文件主要內容
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</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>
調用者需要引入spring-cloud-starter-ribbon依賴,因為我們要使用ribbon來進行客戶端負載均衡調用。
新建配置文件 src/main/resources/application.yml
server:
port: 8081
spring:
application:
name: eureka-demo-consumer
eureka:
instance:
hostname: localhost
client:
serverUrl:
defaultZone: http://localhost:8761/eureka
配置了eureka地址,就可以從eureka抓取到所有注冊的服務了
新建一個配置類,定義RestTemplate,結合Ribbon可以實現客戶端負載均衡,Ribbon我后面的博客也會寫。這里使用@Bean注解,表示注冊一個bean到spring上下文中。
@Configuration
public class EurekaDemoConfiguration {
/**
* LoadBalanced 基于ribbon進行負載均衡
* @return
*/
@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
新建一個給用戶訪問的rest接口
@RestController
public class GreetingController {
@Autowired
RestTemplate restTemplate;
@RequestMapping(value = "/greeting/{name}", method = RequestMethod.GET)
public String greeting(@PathVariable("name") String name) {
//使用restTemplate發送http請求
return restTemplate.getForObject("http://eureka-demo-provider/sayHello/" + name, String.class);
}
}
新建啟動類
@SpringBootApplication
@EnableDiscoveryClient
@EnableEurekaClient
public class EurekaDemoConsumer {
public static void main(String[] args) {
SpringApplication.run(EurekaDemoConsumer.class, args);
}
}
然后在瀏覽器里訪問,http://localhost:8081/greeting/animal。