使用IDEA创建Eureka服务提供者和消费者(手把手)

编程

1. 创建项目

在idea的File–New–Project中新创建一个Spring boot模板项目

点击next

点击next

点击next,如下:

修改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">

<modelVersion>4.0.0</modelVersion>

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>2.2.6.RELEASE</version>

<relativePath/> <!-- lookup parent from repository -->

</parent>

<groupId>com.eureka</groupId>

<artifactId>eurekaprovide</artifactId>

<version>0.0.1-SNAPSHOT</version>

<name>eurekaprovide</name>

<description>Demo project for Spring Boot</description>

<properties>

<maven.compiler.parameters>true</maven.compiler.parameters>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<java.version>1.8</java.version>

<spring-cloud.version>Hoxton.SR3</spring-cloud.version>

</properties>

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter</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-web</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-context</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>

</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.yml如下:

server:

port: 8811

servlet:

context-path: /eurekaprovide

#设置服务的名称

spring:

application:

name: eurekaprovide

eureka:

client:

service-url:

#Eureka 注册中心的地址,多个地址有逗号隔开

defaultZone: http://cluster1:7071/eureka,http://cluster2:8081/eureka,http://cluster3:9091/eureka

注册中心生产者如图:

访问地址:http://localhost:8811/eurekaprovide/hi?name=1231

2. 创建消费者项目

在idea的File–New–Project中新创建一个Spring boot模板项目

点击next,如下:

点击next,如下:

点击next,如下:

修改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.2.6.RELEASE</version>

<relativePath/> <!-- lookup parent from repository -->

</parent>

<groupId>com.eureka</groupId>

<artifactId>eurekaconsumer</artifactId>

<version>0.0.1-SNAPSHOT</version>

<name>eurekaconsumer</name>

<description>Demo project for Spring Boot</description>

<properties>

<maven.compiler.parameters>true</maven.compiler.parameters>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<java.version>1.8</java.version>

<spring-cloud.version>Hoxton.SR3</spring-cloud.version>

</properties>

<dependencies>

<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-web</artifactId>

</dependency>

<dependency>

<groupId>org.apache.httpcomponents</groupId>

<artifactId>httpclient</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-config</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>

</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>

修改yml配置文件如下:

server:

port: 8101

servlet:

context-path: /eurekaconsumer

#设置服务的名称

spring:

application:

name: eurekaconsumer

eureka:

client:

service-url:

#Eureka 注册中心的地址,多个地址有逗号隔开

defaultZone: http://cluster1:7071/eureka,http://cluster2:8081/eureka,http://cluster3:9091/eureka

instance:

instance-id: eurekaconsumer

prefer-ip-address: true

封装客户端:

@Configuration

public class ConfigBean {

@Bean

@LoadBalanced// Spring Cloud Ribbon 是基于 Netflix Ribbon 实现的一套客户端

public RestTemplate getRestTemplate(){

return new RestTemplate();

}

}

消费者调用接口:

@RestController

public class ConsumerController {

@Autowired

private RestTemplate restTemplate;

private static final String REST_URL_PREFIX = "http://eurekaprovide";

@GetMapping(value = "/test/{name}")

public String test(@PathVariable("name") String name){

return restTemplate.getForObject(REST_URL_PREFIX + "/eurekaprovide/hi?name=" + name, String.class);

}

}

启动类EurekaconsumerApplication

@SpringBootApplication

@EnableEurekaClient

public class EurekaconsumerApplication {

public static void main(String[] args) {

SpringApplication.run(EurekaconsumerApplication.class, args);

}

}

消费者启动:

注册中心展示:

调用接口:http://localhost:8101/eurekaconsumer/test/465

以上是 使用IDEA创建Eureka服务提供者和消费者(手把手) 的全部内容, 来源链接: utcz.com/z/515430.html

回到顶部