SpringCloud-Finchley.SR1-Eureka学习(Hello World)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012430402/article/details/88924377

一、源码地址

https://github.com/devilyang123/spring-cloud-learn

二、创建spring-cloud-parent父工程,目录结构如下

父工程会包含三个模块,分别为eureka-server,cloud-producer(生产者),cloud-consumer(消费者)。

父工程的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>com.xiao</groupId>
    <artifactId>spring-cloud-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>cloud-producer</module>
        <module>cloud-consumer</module>
        <module>eureka-server</module>
    </modules>
    <packaging>pom</packaging>

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

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <mapper.starter.version>2.0.3</mapper.starter.version>
        <mysql.version>5.1.32</mysql.version>
        <spring-cloud.version>Finchley.SR1</spring-cloud.version>
    </properties>
    
    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <type>pom</type>
                <scope>import</scope>
                <version>${spring-cloud.version}</version>
            </dependency>

            <!-- 通用mapper -->
            <dependency>
                <groupId>tk.mybatis</groupId>
                <artifactId>mapper-spring-boot-starter</artifactId>
                <version>${mapper.starter.version}</version>
            </dependency>

            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

三、Eureka注册中心

目录结构如下

在pom文件中添加eureka-server的依赖,如下

<?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>spring-cloud-parent</artifactId>
        <groupId>com.xiao</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>eureka-server</artifactId>
    <packaging>jar</packaging>


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

</project>

在yml配置文件中添加配置,如下

server:
  port: 8083
spring:
  application:
    name: eureka-server # 应用名称,会在Eureka中显示
eureka:
  client:
    register-with-eureka: false # 是否注册自己的信息到EurekaServer,默认是true
    fetch-registry: false # 是否拉取其它服务的信息,默认是true
    service-url: # EurekaServer的地址,现在是自己的地址,如果是集群,需要加上其它Server的地址。
      defaultZone: http://127.0.0.1:${server.port}/eureka

创建启动类

package com.xiao;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
 * @Description
 * @Auther: 笑笑
 * @Date: 17:30 2019/3/30
 */
@EnableEurekaServer //使用注解启用EurekaServer
@SpringBootApplication
public class EurekaServerEntry {

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

启动eureka-server,浏览器访问http://localhost:8083测, 如下

出现以上界面,表示eureka注册中心服务启动成功!

四、服务提供者cloud-producer

目录结构如下

在pom文件中添加依赖,服务提供者需要查询数据库返回数据,所以需要mysql的依赖和通用mapper的依赖(关于通用mapper如何使用在此不做描述),以及eureka-client(客户端),如下

<?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>spring-cloud-parent</artifactId>
        <groupId>com.xiao</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>cloud-producer</artifactId>


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

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
        </dependency>

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

</project>

修改配置yml文件,数据库连接信息换成自己的,如下

server:
  port: 8081
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/cloud_db?characterEncoding=utf-8&useSSL=false
    username: root
    password: 123456
  application:
    name: cloud-producer # 应用名称,会在Eureka中显示
eureka:
  client:
    service-url: # EurekaServer的地址
      defaultZone: http://127.0.0.1:8083/eureka/
mybatis:
  type-aliases-package: com.xiao.pojo

service和dao层只是查了个数据返回,没有其他的业务逻辑,在此不做过多的展示,仅看一下controller即可,如下

package com.xiao.comtroller;

import com.xiao.pojo.User;
import com.xiao.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description
 * @Auther: 笑笑
 * @Date: 15:58 2019/3/30
 */
@RestController
public class ProducerUserController {

    @Autowired
    private IUserService userService;


    @RequestMapping(value = "/selectAll")
    @ResponseBody
    public List<User> selectAll(){
        return userService.selectAll();
    }
}

对应数据表中的数据如下

启动类

package com.xiao;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import tk.mybatis.spring.annotation.MapperScan;

/**
 * @Description
 * @Auther: 笑笑
 * @Date: 15:45 2019/3/30
 */
@EnableDiscoveryClient //使用注解开启eureka客户端
@MapperScan("com.xiao.mapper")
@SpringBootApplication
public class ProducerApplicationEntry {

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

}

启动工程,浏览器访问http://localhost:8081/selectAll测试,如下

可以看到数据已全部成功返回。

然后我们回到Eureka注册中心的管理界面,刷新后可以看到,服务的提供方已经注册,如下

五、服务消费者cloud-consumer

目录结构如下

在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>spring-cloud-parent</artifactId>
        <groupId>com.xiao</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-consumer</artifactId>


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

</project>

修改yml配置文件,如下

server:
  port: 8082
spring:
  application:
    name: cloud-consumer # 应用名称,会在Eureka中显示
eureka:
  client:
    service-url: # EurekaServer的地址
      defaultZone: http://127.0.0.1:8083/eureka/

启动类,如下

package com.xiao;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

/**
 * @Description
 * @Auther: 笑笑
 * @Date: 16:04 2019/3/30
 */
@EnableDiscoveryClient
@SpringBootApplication
public class ConsumerApplicationEntry {

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

    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

这里解释一下,为什么还用@EnableDiscoveryClient注解, 因为服务的消费者既可以消费服务,也可以作为服务的提供者,所以消费者也要作为eureka注册中心的客户端。

controller如下

package com.xiao.controller;


import com.xiao.pojo.User;
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.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.Arrays;
import java.util.List;

/**
 * @Description
 * @Auther: 笑笑
 * @Date: 16:06 2019/3/30
 */
@RestController
public class ConsumerUserController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;


    @GetMapping(value = "/selectAll")
    @ResponseBody
    public List<User> selectAll(){
        //根据服务ID获取实例, 可能为多个
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PRODUCER");
        //通过实例获取ID 和端口(自己测试,确定为一个)
        String host = instances.get(0).getHost();
        int port = instances.get(0).getPort();
        User[] userArray = restTemplate.getForObject("http://" + host + ":" + port + "/selectAll", User[].class);
        if(userArray != null){
          return Arrays.asList(userArray);
        }
        return null;
    }
}

启动项目,访问http://localhost:8082/selectAll测试,如下

可以看到,虽然我访问的是服务的消费者,但数据是从服务的提供者处返回,然后我们再回到Eureka注册中心的管理界面,刷新,可以看到cloud-consumer服务也成功注册,如下

 到此,Eureka学习demo搭建成功!

 

展开阅读全文

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