第二篇:SpringCloud 服务的注册与发现Eureka(Dalston.SR5版本)

一.创建父工程

1 首先创建一个maven父工程。

<?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>com.zhihua</groupId>
    <artifactId>spring_cloud_pro</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

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

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.13.RELEASE</version>
    </parent>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR5</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>

这里写图片描述

二 . 创建Eureka Server

2 然后创建2个子工程工程: 一个model工程作为服务注册中心,即Eureka Server,另一个作为Eureka Client。

下面以创建Eureka Server为例子,详细说明创建过程:

右键工程->创建model-> 选择spring initialir 如下图:

这里写图片描述

这里写图片描述

创建完后的工程,其pom.xml继承了父pom文件,同时父工程加入相应的model,引入依赖,代码如下

<?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>com.zhihua</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>

    <parent>
        <groupId>com.zhihua</groupId>
        <artifactId>spring_cloud_pro</artifactId>
        <version>0.0.1-SNAPSHOT</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>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>**重点内容**
</project>

3 启动一个服务注册中心

    只需要一个注解@EnableEurekaServer,这个注解需要在springboot工程的启动application类上加:
@SpringBootApplication
@EnableEurekaServer    //eureka服务
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

4 配置文件appication.yml

spring:
  application:
    name: eureka-server      #服务名称
server:
 port: 8761                     #端口号
eureka:
 client:
  register-with-eureka: false   #eureka服务自身也是个客户端  此处因为是单机版关闭
  fetch-registry: false          #同上
  service-url:
   defaultZone: http://localhost:8761/eureka       #服务端默认地址

5 eureka server 启动工程,打开浏览器访问:
http://localhost:8761 ,界面如下:

这里写图片描述

三、创建一个服务提供者 (eureka client)

当client向server注册时,它会提供一些元数据,例如主机和端口,URL,主页等。Eureka server 从每个client实例接收心跳消息。 如果心跳超时,则通常将该实例从注册server中删除。
此处同样是通过jpa查询数据库,具体代码参照上一章节
userControlle类:

package com.zhihua.eureka_client.controller;



import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.zhihua.eureka_client.entity.User;
import com.zhihua.eureka_client.jpa.UserJpa;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

@RestController
public class userController {

    @Autowired
    private UserJpa userJpa;

    @Autowired
    private EurekaClient eurekaClient;

    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/simple/{id}")
    public User findById(@PathVariable Long id){
        User user = userJpa.getOne(id);
         return user;
    }


    @PostMapping(value = "/user")
    public User postUser(@RequestBody User user){
        return user;
    }

    @GetMapping(value = "/getUser")
    public User getUser(User user){
        return user;
    }


    @GetMapping("/eureka-instance")
    public String serviceUrl() {
        InstanceInfo instance = this.eurekaClient.getNextServerFromEureka("EUREKA-SERVER", false);
        return instance.getHomePageUrl();
    }

    @GetMapping("/instance-info")
    public ServiceInstance showInfo() {
        ServiceInstance localServiceInstance = this.discoveryClient.getLocalServiceInstance();
        return localServiceInstance;
    }

}

创建过程同server类似,创建完pom.xml如下:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>

    </dependencies>

通过注解@EnableEurekaClient 表明自己是一个eurekaclient.

@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaClientApplication.class, args);
    }
}

配置文件appication.yml

server:
  port: 9060
spring:
  application:
      name: eureka-client
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost/test?characterEncoding=utf8
    username: root
    password: 123456
  jpa:
    properties:
      hibernate:
        show_sql: true
        format_sql: true
eureka:
  instance:
    hostname: localhost
    prefer-ip-address: true
  client:
    healthcheck:
          enabled: true
    service-url:
      defaultZone: http://${eureka.instance.hostname}:8761/eureka/

启动工程,打开http://localhost:8761 ,即eureka server 的网址

这里写图片描述

你会发现一个服务已经注册在服务中了,服务名为SERVICE-HI ,端口为9060

此时的项目:

这里写图片描述

猜你喜欢

转载自blog.csdn.net/chen1092248901/article/details/81077509
今日推荐