1,建立一個maven項目
2,src文件無用,可刪除
3,pom.xml的依賴(包括了一些後期配置)
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<!--子工程的引用-->
<modules>
<module>eu_server</module>
<module>eu_client</module>
</modules>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>mycloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mycloud</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.0.4</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<!--日誌包@Slf4j註解形式-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<!--Spring Cloud 服務註冊組件-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4,建立module(服務端和客戶端方式一樣)
1和2可以修改
5,對於server和client的pom進行修改,對project的pom修改
6,生產者與消費者的application的配置
#生產者
server.port=8801
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
spring.application.name=eu_server #別名
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
#消費者
server.port=8761
# 別名
spring.application.name=eu_client
# 服務註冊地址
eureka.client.serviceUrl.defaultZone=http://localhost:8801/eureka/
7,寫註解
@EnableEurekaServer--生產者的啓動類
@EnableEurekaClient--消費者啓動類
8,相關代碼片段
消費者啓動類
@SpringBootApplication
@EnableEurekaClient
public class EuClientApplication {
@Bean //相當於xml中的bean標籤,主要是用於調用當前方法獲取指定對象
public RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(EuClientApplication.class, args);
}
}
消費者控制類
@RestController
public class OrderController {
@Autowired
private RestTemplate restTemplate;//spring 提供的一個用於訪問rest接口的模板對象
private String server_Url="http://localhost:8801/user/";
@GetMapping("/order/{id}")
public User showOrder(@PathVariable("id") String id){
//通過訪問rest,獲取json對象,轉換爲user對象
User user = restTemplate.getForObject(server_Url+id, User.class);
return user;
}
}
生產者控制類
@RestController
public class UserController {
@GetMapping("/user/{id}")
public User showUser(@PathVariable("id") String id){
return new User(id,"李四","小牛",new Date());
}
}
生產者啓動類
@EnableEurekaServer
public class EuServerApplication {
public static void main(String[] args) {
SpringApplication.run(EuServerApplication.class, args);
}
}
9,頁面訪問效果
--記錄點滴