SpringCloud版本配置与环境搭建教程详解

1、maven父子工程搭建

项目使用maven工程搭建,下面是工程的结构图。SpringCloud2020是父工程,仅负责依赖的管理,eureka是注册中心的服务端,testclient是测试的客户端。

1.1 父工程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 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>

<artifactId>SpringCloud2020</artifactId>

<packaging>pom</packaging>

<version>1.0-SNAPSHOT</version>

<modules>

<module>eureka</module>

<module>testclient</module>

</modules>

<parent>

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

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

<version>2.4.1</version>

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

</parent>

<properties>

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

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

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

</properties>

<dependencyManagement>

<dependencies>

<dependency>

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

<artifactId>spring-cloud-dependencies</artifactId>

<!-- Provide the latest stable Spring Cloud release train version (e.g. 2020.0.0) -->

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

1.2 eureka子工程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 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<parent>

<artifactId>SpringCloud2020</artifactId>

<groupId>org.example</groupId>

<version>1.0-SNAPSHOT</version>

</parent>

<modelVersion>4.0.0</modelVersion>

<artifactId>eureka</artifactId>

<dependencies>

<dependency>

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

<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>

</dependency>

</dependencies>

</project>

1.3 testclient子工程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 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<parent>

<artifactId>SpringCloud2020</artifactId>

<groupId>org.example</groupId>

<version>1.0-SNAPSHOT</version>

</parent>

<modelVersion>4.0.0</modelVersion>

<artifactId>testclient</artifactId>

<dependencies>

<dependency>

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

<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

</dependency>

<!--引入WebStart-->

<dependency>

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

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

</dependency>

</dependencies>

</project>

2、配置application

2.1 eureka 配置

server:

port: 20001 #eureka运行的端口号

address: 127.0.0.1 #注册中心运行地址

servlet:

context-path: /server #eureka注册中心管理界面地址

eureka:

client:

register-with-eureka: false #是否加入eureka注册表

fetch-registry: false #还是向eureka请求注册信息表

service-url:

defaultZone: http://${server.address}:${server.port}/eureka #注册中心地址,其它服务需要注册到该地址

2.1 testclient 配置

server:

port: 20002

# Spring

spring:

application:

name: test_service

# Eureka

eureka:

client:

service-url:

defaultZone: http://127.0.0.1:20001/eureka #这里的port与eureka的端口对应

instance:

lease-renewal-interval-in-seconds: 5 # 每隔5秒发送一次心跳

lease-expiration-duration-in-seconds: 10 # 10秒不发送就过期

prefer-ip-address: true

instance-id: ${spring.application.name}:${server.port}

3、启动类

3.1 Eureka启动类EurekaApplication

package org.example.demo;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication

@EnableEurekaServer

public class DemoApplication {

public static void main(String[] args) {

SpringApplication.run(DemoApplication.class, args);

}

}

3.2 TestClient启动类TestClientApplication

package org.example.demo;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication

@EnableEurekaClient

public class TestClientApplication {

public static void main(String[] args) {

SpringApplication.run(TestClientApplication.class, args);

}

}

4、运行结果

如果没有意外,那么你将看到

如果启动testclient时报错

请检查testclient工程的依赖中是否存在下面的依赖项,如果没有,请添加。原因可能是eureka-client依赖spring-boot-starter-web

<dependency>

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

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

</dependency>

如果没有出现TEST_SERVICE,并且testclient出现以下报错

请检查testclient配置的defaultZone是否与eureka配置对应,并清空已经构建的内容,再重新启动eureka,testclient。

在testclient控制台看到以下日志信息,说明注册成功。

访问管理界面默认使用127.0.0.1:port,如果要改变它,请按照下面的提示配置

server:

port: 20001 #eureka运行的端口号

address: 127.0.0.1 #管理界面的地址

servlet:

context-path: /eureka-ui#管理界面的context-path

eureka:

client:

register-with-eureka: false #是否加入eureka注册表

fetch-registry: false #是否向eureka请求注册信息表

service-url:

defaultZone: http://127.0.0.1:${server.port}/eureka # 配置注册中心的地址,其它服务注册的时候使用。

到此这篇关于SpringCloud2020版本配置与环境搭建教程详解的文章就介绍到这了,更多相关SpringCloud2020版本配置内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

以上是 SpringCloud版本配置与环境搭建教程详解 的全部内容, 来源链接: utcz.com/z/345938.html

回到顶部