Spring Cloud第一章节(服务的注册)


服务的注册与发现Eureka(Finchley版本)

创建父项目section1

父项目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>
    <!-- 继承父配置,使用Spring boot Start -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <!-- 继承父配置,使用Spring boot End -->
    <!-- 本项目信息定义 Start -->
    <groupId>com.thirty</groupId>
    <artifactId>section1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>section1</name>
    <description>Demo project for Spring Boot</description>
    <!-- 本项目信息定义 End -->
    <!-- 定义配置项 Start -->
    <properties>
        <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>Finchley.RELEASE</spring-cloud.version>
    </properties>
    <!-- 定义配置项 End -->
    <!-- 插件列表 Start -->
    <dependencies>
        <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>
        </dependency>
    </dependencies>
    <!-- 插件列表 End -->
    <!-- 插件管理器 End -->
    <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>
    <!-- 插件管理器 Start-->
    <!-- 编译设置 Start-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    <!-- 编译设置 End-->

</project>

创建Eureka-Server

添加EureKa依赖

Eureka_Dependencies

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>
    <parent>
        <!-- 继承父pom文件-->
        <groupId>com.thirty</groupId>
        <artifactId>section1</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.thirty</groupId>
    <artifactId>eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>eureka-server</name>
    <description>Demo project for Spring Boot</description>

    <dependencies>
        <!-- 添加 eureka依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>
</project>

application.yml文件如下

server:
  port: 8100 # 端口
eureka:
  instance:
    hostname: localhost # 
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
spring:
  application:
    name: eurka-server

启动类需要添加@EnableEurekaServer注释

创建Eureka-Client

步骤同Eureka-Service

依赖加入Eureka Discovery

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>
    <parent>
        <groupId>com.thirty</groupId>
        <artifactId>section1</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.thirty</groupId>
    <artifactId>eureka-client1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>eureka-client1</name>
    <description>Demo project for Spring Boot</description>

    <dependencies>
        <dependency><!--客户端依赖,用来声明本module是一个client-->
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency><!--web依赖,用来访问测试-->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

application.yml文件如下

server:
  port: 8101 #端口

spring:
  application:
    name: client1 # 服务名
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8100/eureka/ #定义服务端链接地址

启动类文件EurekaClient1Application.java

@SpringBootApplication
@EnableEurekaClient
@RestController
public class EurekaClient1Application {
    public static void main(String[] args) {
        SpringApplication.run(EurekaClient1Application.class, args);
    }
    @RequestMapping("/hello")
    public String hello(@RequestParam(value = "name", defaultValue = "forezp") String name) {
        return "Hello World";
    }

}

启动项目

先启动Eureka-server,浏览器访问[http://localhost:8100/]:http://localhost:8100/ 可以看到可视化界面,但是并无client

再启动Eureka-Client,再次访问[http://localhost:8100/]:http://localhost:8100/ 可以看到client已注册

浏览器访问[http://localhost:8101/hello]:http://localhost:8101/hello 可以看到Hello World

阅读更多

没有更多推荐了,返回首页