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,页面访问效果
--记录点滴