SpringCloud学习(一):eureka注册中心和客户端

菜鸟学渣接触spring cloud 系列...

公司也上微服务了,再不学习下就凉了,所以来踩坑吧...

版本:

  spring-boot:  2.0

  spring-cloud: Finchley.SR1

SpringCloud注册中心可以用Eureka、consul、zookeeper之类的,这里用eureka。

能上图绝不BB

  

一、Server端  [eureka-server]

  引入依赖  spring-cloud-starter-netflix-eureka-server

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5 
 6     <groupId>com.renzku</groupId>
 7     <artifactId>eureka-server</artifactId>
 8     <version>0.0.1-SNAPSHOT</version>
 9     <packaging>jar</packaging>
10 
11     <name>eureka-server</name>
12     <description>Demo project for Spring Boot</description>
13 
14     <parent>
15         <groupId>org.springframework.boot</groupId>
16         <artifactId>spring-boot-starter-parent</artifactId>
17         <version>2.0.4.RELEASE</version>
18         <relativePath/> <!-- lookup parent from repository -->
19     </parent>
20 
21     <properties>
22         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24         <java.version>1.8</java.version>
25         <spring-cloud.version>Finchley.SR1</spring-cloud.version>
26     </properties>
27 
28     <dependencies>
29         <dependency>
30             <groupId>org.springframework.cloud</groupId>
31             <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
32         </dependency>
33 
34         <dependency>
35             <groupId>org.springframework.boot</groupId>
36             <artifactId>spring-boot-starter-test</artifactId>
37             <scope>test</scope>
38         </dependency>
39     </dependencies>
40 
41     <dependencyManagement>
42         <dependencies>
43             <dependency>
44                 <groupId>org.springframework.cloud</groupId>
45                 <artifactId>spring-cloud-dependencies</artifactId>
46                 <version>${spring-cloud.version}</version>
47                 <type>pom</type>
48                 <scope>import</scope>
49             </dependency>
50         </dependencies>
51     </dependencyManagement>
52 
53     <build>
54         <plugins>
55             <plugin>
56                 <groupId>org.springframework.boot</groupId>
57                 <artifactId>spring-boot-maven-plugin</artifactId>
58             </plugin>
59         </plugins>
60     </build>
61 
62 
63 </project>
pom.xml

  配置文件 application.yml

server:
  port: 8761


eureka:
  instance:
      hostname: localhost
      lease-renewal-interval-in-seconds: 5  # 设置心跳的时间间隔(默认是30秒)
      lease-expiration-duration-in-seconds: 20 # 如果现在超过了20秒的间隔(默认是90秒)则服务无效了
  client:
    registerWithEureka: false
    fetchRegistry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
  server:
    waitTimeInMsWhenSyncEmpty: 0
    eviction-interval-timer-in-ms: 30000  # eureka清理无效服务的间隔时间
    enable-self-preservation: false  # 设置为false表示关闭保护模式--某一个微服务不可用了,eureka清理掉

  启动类  EurekaServerApplication.java

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

  目录结构

  

  一个简单的Server端完成,访问http://localhost:8761

  

 

二、Client端  [eureka-client-one]

  引入依赖  spring-cloud-starter-netflix-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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.renzku</groupId>
    <artifactId>eureka-client-one</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>eureka-client-one</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</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.SR1</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.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </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>
pom.xml

  配置文件 application.yml

server:
  port: 8501

spring:
  application:
    name: eureka-client-one

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/  # eureka server 设置的地址

  启动类  EurekaClientOneApplication.java

@Configuration
@ComponentScan
@EnableAutoConfiguration
@EnableEurekaClient
public class EurekaClientOneApplication {

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

  Rest服务  HelloWorld.java

@RestController
public class HelloWorld {

    @RequestMapping("/")
    public String home(){
        return "hello world";
    }
}

  目录结构

  

 

  一个简单的Client端完成,访问http://localhost:8501

  

 

三、结果

  查看注册中心,再次访问 http://localhost:8761,eureka-client-one 已经展示到已注册的实例列表中了:

  

 

其中[eureka-client-one]为serviceId,其他注册到eureka-server的实例可以通过serviceId相互调用,还自带负载均衡...

posted on 2018-09-09 22:16  江湖吹风  阅读(686)  评论(0编辑  收藏  举报

renzku