注意依赖的版本问题
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.0.5.RELEASEversion>
<relativePath/>
parent>
<groupId>com.boogroupId>
<artifactId>eureka-serverartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>eureka-servername>
<description>eureka server for Spring clouddescription>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
<spring-cloud.version>Finchley.SR1spring-cloud.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${spring-cloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<configuration>
configuration>
plugin>
plugins>
build>
project>
spring:
application:
name: eureka-server
server:
port: 8761
servlet:
# 应用的访问路径
context-path: /
eureka:
instance:
hostname: localhost
client:
#是否将自己注册到eureka server,构建集群环境时,需将自己注册到集群中,默认为true
registerWithEureka: false
#是否从eureka server获取注册信息,构建集群环境时,需同步其他eureka server节点,默认为true
fetchRegistry: false
serviceUrl:
defaultZone: http://127.0.0.1:8761/eureka/
@EnableEurekaServer
注解package com.boo.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
spring-boot-starter-parent | spring-cloud-dependencies |
---|
版本号 | 发布日期 | 版本号 | 发布日期 |
---|---|---|---|
1.5.2.RELEASE | 2017 年 3 月 稳定版 | Dalston.RC1 | 2017 年未知月 |
1.5.9.RELEASE | 2017 年 11 月 稳定版 | Edgware.RELEASE | 稳定版 |
1.5.16.RELEASE | |||
1.5.20.RELEASE | Edgware.SR5 | ||
2.0.2.RELEASE | 2018 年 5 月 | Finchley.BUILD-SNAPSHOT | 2018 年 |
2.0.6.RELEASE | Finchley.SR2 | ||
2.1.4.RELEASE | GreenWith.SR1 | ||
2.1.7.RELEASE | Aug, 2019 | Greenwich.RELEASE | Jan 23,2019 |
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.0.5.RELEASEversion>
<relativePath/>
parent>
<groupId>com.boogroupId>
<artifactId>eureka-providerartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>eureka-providername>
<description>eureka客户端(提供者) for Spring Clouddescription>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
<spring-cloud.version>Finchley.SR1spring-cloud.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${spring-cloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
server:
port: 8081
servlet:
context-path: /
spring:
application:
name: eureka-provider
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
@EnableEurekaClient
注解package com.boo.eurekaprovider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient // Eureka 客户端
public class EurekaProviderApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaProviderApplication.class, args);
}
}
@RestController
@RequestMapping("/car")
public class CarController {
@RequestMapping("/detail/{carId}")
public BizCar detail(@PathVariable("carId") Long carId){
BizCar bizCar = new BizCar();
bizCar.setCarId(carId);
bizCar.setCarName("HV H6");
return bizCar;
}
}
提供者已经创建完毕,为了验证我们是否可以调用来自提供者的服务,我们接着创建一个简单的maven项目eureka-consumer,在pom.xml中加入依赖,由于都是作为Eureka客户端,所以eureka-consumer的依赖和eureka-provider的依赖相同,只不过调用者作为服务的请求一方,并不关心具体是怎么拿到服务的,而服务提供者有可能不止一个–可能是集群。
<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>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.0.5.RELEASEversion>
<relativePath/>
parent>
<groupId>com.boogroupId>
<artifactId>eureka-consumerartifactId>
<version>1.0-SNAPSHOTversion>
<description>eureka客户端(提供者) for Spring Clouddescription>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
<spring-cloud.version>Finchley.SR1spring-cloud.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-configartifactId>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${spring-cloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
server:
port: 8082
servlet:
context-path: /
spring:
application:
name: eureka-consumer
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
@EnableEurekaClient
注解@SpringBootApplication
@EnableEurekaClient // Eureka 客户端
public class EurekaConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaConsumerApplication.class, args);
}
}
RestTemplate
是Spring框架里面提供的,它可以自动配置去使用Ribbon。spring-cloud-starter-netflix-eureka-xxx
包中,无需重复引入:package com.boo.web.controller;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
/**
* description: ConsumerController
* date: 2021/8/26 15:29
* author: Boo
* version: 1.0
*/
@Controller
@Configuration
public class ConsumerController {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
@GetMapping(value = "/router")
@ResponseBody
public String router(){
RestTemplate temp = getRestTemplate();
return temp.getForObject("http://eureka-provider/car/detail/1", String.class);
}
}