SpringCloud(一)——Eureka架构

Eureka架构图

image

PS:

所有的服务提供者和服务调用者都会在注册中心进行注册,服务提供者在注册中心上提供服务,服务调用者在注册中心中获取服务列表。

构建第一个应用

1. 创建一个Eureka服务

导入相关依赖,pom文件如下



    4.0.0

    com.zgy
    first-114
    0.0.1-SNAPSHOT
    jar

    first-114
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.13.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Dalston.SR5
                pom
                import
            
        
    

    
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.cloud
            spring-cloud-starter-eureka-server
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

将注解@EnableEurekaServer加在启动类上

package com.zgy.first114;

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

/**
 * @EnableEurekaServer:说明该应用是一个Eureka服务器
 */
@SpringBootApplication
@EnableEurekaServer
public class First114Application {

    public static void main(String[] args) {
        new SpringApplicationBuilder(First114Application.class).web(true).run(args);
    }
}

配置文件

server:
  port: 8761 #设置服务器端口号为:8761
eureka:
  client:
    register-with-eureka: false #不注册到其他Eureka服务器上
    fetch-registry: false #不在其他Eureka服务器上读取注册信息

启动应用

image

2. 创建服务提供者

导入依赖,pom文件如下



    4.0.0

    com.zgy
    first-police
    0.0.1-SNAPSHOT
    jar

    first-police
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.13.RELEASE
         
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Dalston.SR5
                pom
                import
            
        
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            org.springframework.cloud
            spring-cloud-starter-eureka
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

将注解@EnableEurekaClient加在启动类上

package com.zgy.firstpolice;

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

/**
 * @EnableEurekaClient:说明该应用是一个客户端
 */
@SpringBootApplication
@EnableEurekaClient
public class FirstPoliceApplication {

    public static void main(String[] args) {
        SpringApplication.run(FirstPoliceApplication.class, args);
    }
}
package com.zgy.firstpolice;

public class Police {

    private Integer id;
    private String name;
    private String message;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
package com.zgy.firstpolice;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PoliceController {

    @GetMapping("/call/{id}")
    public Police call(@PathVariable Integer id){
        Police police = new Police();
        police.setId(id);
        police.setName("张三");
        police.setMessage("AAA");

        return police;
    }
}

配置文件

spring:
  application:
    name: first-police #定义项目的名称
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/ #将该应用注册到Eureka服务器上

启动应用,然后查看eureka服务器

image

3. 创建服务调用者

引入依赖,pom文件如下



    4.0.0

    com.zgy
    first-person
    0.0.1-SNAPSHOT
    jar

    first-person
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.13.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Dalston.SR5
                pom
                import
            
        
    

    
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            org.springframework.cloud
            spring-cloud-starter-eureka
        

        
            org.springframework.cloud
            spring-cloud-starter-ribbon
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

将注解@EnableEurekaClient加在启动类上

package com.zgy.firstperson;

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

@SpringBootApplication
@EnableEurekaClient
public class FirstPersonApplication {

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

编写Controller

package com.zgy.firstperson;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@Configuration
public class TestController {

    /*
    @LoadBalanced:该RestTemplate具有负载均衡的功能
     */
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }

    @GetMapping("/router")
    public String router(){
        RestTemplate restTemplate = getRestTemplate();
        String json = restTemplate.getForObject("http://first-police/call/100", String.class);
        return json;
    }
}

配置文件

server:
  port: 8081
spring:
  application:
    name: first-person
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

启动应用,查看eureka服务器

image

访问Controller

image

第一个应用就已经完成,以下是本次程序的结构图

image

你可能感兴趣的:(SpringCloud(一)——Eureka架构)