Eureka服务,类似于一个zookpeer的注册中心,起到服务发现的作用
接口提供者,通过远程调用在invoker服务中调用该项目中的接口
服务调用者,可以远程调用provider提供的接口
选择maven→选择SDK版本→点击next(记住不能选择crete from archeyype)
在空的maven项目上→右键→new→modul
选择spring initializr→输入项目名→输入group→输入artiface→点击next
选择web-spring web →选择 spring cloud discover - Eureka Server→点击finish完成
等待下载依赖,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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springcloud-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
<spring-cloud.version>2020.0.2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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>
修改配置文件,增加如下内容:(需要在application.properties文件同目录下创建一个applicaiton.yaml文件即可)
server:
port: 8761
eureka:
instance:
hostname: localhost
lease-renewal-interval-in-seconds: 5 # 设置心跳的时间间隔(默认是30秒)
lease-expiration-duration-in-seconds: 20 # 如果现在超过了20秒的间隔(默认是90秒)则服务无效了
client:
registerWithEureka: false
fetchRegistry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
server:
waitTimeInMsWhenSyncEmpty: 0
eviction-interval-timer-in-ms: 30000 # eureka清理无效服务的间隔时间
enable-self-preservation: false # 设置为false表示关闭保护模式--某一个微服务不可用了,eureka清理掉
在启动类增加Eureka启动配置:@EnableEurekaServer
启动服务,页面访问http://localhost:8761 ,可以看到Eureka服务页面,当前没有服务加入进来,下面的Application部分没有结果
重复创建springcloud-server前面的步骤,直到选择依赖部分
选择web - spring web → 选择Spring Cloud Discovery - Eureka Discovery Client
等待下载依赖,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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.iking</groupId>
<artifactId>springcloud-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud-provider</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
<spring-cloud.version>2020.0.2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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>
application.yaml文件添加配置信息:
server:
port: 8501
spring:
application:
name: service-provider
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/ # eureka server 设置的地址
启动类添加@EnableEurekaClient注解
注意:此处是client,上面server对应的是server
编写测试接口
创建controller.TestController.java 插入下面的代码:
@RestController
public class TestController {
@RequestMapping(value = "/person/{name}",method = RequestMethod.GET)
public String findName(@PathVariable("name") String name){
System.out.println("============:"+name);
return name;
}
@RequestMapping("/")
public String home(){
return "hello world --- springcloud-provider";
}
}
项目结构如下:
启动项目,访问localhost:8501,localhost:8501/person/test可以正常获取到接口信息
访问http://localhost:8761/ 可以看到,服务已经加入进来了
重复创建springcloud-provider模块部分步骤,直到项目创建完成
等待下载依赖,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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.iking</groupId>
<artifactId>springcloud-invoker</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud-invoker</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
<spring-cloud.version>2020.0.2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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>
在application.yaml中修改配置信息:
server:
port: 8502
spring:
application:
name: service-invoker
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
启动类添加@EnableEurekaClient注解
注意:此处是client,上面server对应的是server
编写测试接口,通过RestTemplate实现远程调用,只是一开始提供的方式,开发基本不会用这种方式,只是用来实现测试,完成demo。
新建controller.InvokerController.java,代码内容如下:
@RestController
@Configuration
public class InvokerController {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
@RequestMapping(value = "/router",method = RequestMethod.GET)
public String router(){
RestTemplate restTemplate = getRestTemplate();
String name = restTemplate.getForObject("http://service-provider/person/chonor",String.class);
return name;
}
}
项目结构如下:
启动项目,可以在Eureka看到对应服务
访问http://localhost:8502/router 实现远程调用springcloud-provider下的接口