SpringCloud微服务项目快速搭建(一)(SpringCloud Netflix,H版-SR3)

一、前言

        Spring Cloud Netflix是Spring Cloud生态系统中的重要组成部分,也是使用Spring Cloud进行微服务架构开发的主要方向之一。Spring Cloud Netflix主要目标是为开发者提供构建分布式系统所需的组件,包括服务注册中心、负载均衡、断路器、转发器、过滤器等。二

二、创建SpringCloud Eureka

1.导入父项目依赖 父项目只用于管理项目不做代码处理



  4.0.0

  com.lzc
  springcloud-netflix
  1.0-SNAPSHOT
  pom
  
    pojo
    pojo/user-pojo
    server
  


  
    8
    8
    UTF-8
  

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


  
  
    
      
      
        org.springframework.cloud
        spring-cloud-dependencies
        Hoxton.SR3
        pom
        import
      
    
  

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

  


 2.导入Eureka服务端组件依赖


    4.0.0
    
        com.lzc
        springcloud-netflix
        1.0-SNAPSHOT
    

    eureka-server
    jar

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

        
    

 2.1配置启动类

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

/**
 * Hello world!
 */
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApp {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApp.class, args);
    }
}

 2.2配置yml

server:
  port: 8761 # 标识当前Eureka服务端的端口号

eureka:
  instance:
    hostname: localhost # 当前Eurek的实例名称
  client:
    registerWithEureka: false # 在客户端依赖中默认为true,所以服务端需要关闭
    fetchRegistry: false # 在客户端依赖中默认为true,所以服务端需要关闭
    serviceUrl: # http://localhost:8761/eureka/
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  server:
    enable-self-preservation: false # 关闭Eureka自我保护机制

三、SpringCloud-Eureka客户端组件

1.导入依赖


    4.0.0
    
        com.lzc
        server
        1.0-SNAPSHOT
    

    user-server
    jar

    
        UTF-8
    

    

        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        
   
    

 2.启动类

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

/**
 * Hello world!
 */
@SpringBootApplication
@EnableEurekaClient
public class UserServerApp {
    public static void main(String[] args) {
        SpringApplication.run(UserServerApp.class, args);
    }
}

3.配置yml

server:
  port: 1020 # 标识当前user服务端的端口号

eureka:
  client:
    serviceUrl: # http://localhost:8761/eureka/
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true # 开启使用IP地址进行注册
    instance-id: ${spring.application.name}:${server.port} # 修改实例Id
spring:
  application: # 指定此服务的应用名称
    name: user-server

4.负载均衡

    @Bean
    @LoadBalanced// 开启负载均衡
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

// 交给spring管理就能开启随机算法 这里直接@bean就行了
//    @Bean
//    public RandomRule randomRule(){
//        return new RandomRule();
//    }

5.集群

SpringCloud微服务项目快速搭建(一)(SpringCloud Netflix,H版-SR3)_第1张图片

-Dserver.port=1021 -Deureka.instance.instance-id=user-server:1021 

四、引入OpenFeign

1.概述

        OpenFeign是一个声明式的Web服务客户端,是SpringCloud生态系统中负责服务间调用的一个服务调用工具。通过使用OpenFeign,我们可以方便地定义和调用RESTful服务,使得服务间调用变得简单、优雅,而且容易维护。它基于Netflix Feign库进行了扩展和改进,支持了更多的特性和功能。

2.导入依赖

     
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        

3.配置yml

4.启动类加注解@EnableFeignClients

5.interface类

package com.lzc.feign;

import com.lzc.domain.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

// contextId FeignClient的唯一标识,推荐类名小驼峰
// path 映射路径 也可以requestMapping写在类上
// value 服务名称
@FeignClient(contextId = "userFeignClient",path = "user",value = "user-server")
public interface UserFeignClient {
    @GetMapping
    User getUser();
}

6.接口类自动注入直接调用

package com.lzc.controller;

import com.lzc.domain.User;
import com.lzc.feign.UserFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/pay")
public class PayController {
    @Autowired
    private UserFeignClient userFeignClient;
    @GetMapping
    public User getUser(){
      return userFeignClient.getUser();
    }
}

你可能感兴趣的:(spring,cloud,微服务,java,后端,maven)