ToB企服应用市场:ToB评测及商务社交产业平台

标题: Spring Cloud项目搭建 [打印本页]

作者: 自由的羽毛    时间: 2024-8-18 21:20
标题: Spring Cloud项目搭建
一、Spring Cloud 简介
Spring cloud 为开发职员提供了快速构建分布式系统的一些工具,包括配置管理、服务发现、断路器、路由、微代理、事件总线、全局锁、决策竞选、分布式会话等。
它运行情况简朴,可以在开发职员电脑上运行。另外分析Spring cloud是基于springboot的。
二、创建服务注册中央
在这里我们需要用到的组件Spring cloud Netflix 的Eureka,eureka是一个服务注册和发现模块。
2.1、首先创建一个springboot的maven主工程
在pom文件中引入依赖,spring boot版本为 2.1.1.RELEASE ,spring cloud 版本为 Finchley.RELEASE ,该pom文件作为父pom文件,起到版本依赖控制的作用,其他module工程依赖该pom。具体pom.xml 文件如下:


    4.0.0
    com.lbh
    eurekademo
    0.0.1-SNAPSHOT
    pom
    EurekaDemo
    Demo project for Spring Boot
   
        org.springframework.boot
        spring-boot-starter-parent
        2.1.1.RELEASE
         
   
   
        eureka-server
        eureka-client
   
   
        UTF-8
        UTF-8
        1.8
        Finchley.RELEASE
   
   
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
   
   
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
   
   
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
   


2.2 创建两个module工程:eureka-server 作为服务注册中央, eureka-client 作为客户端
右键工程–new–Module – 选择 Spring initialir,与创建boot工程类似。
选择cloud discovery->eureka server。pom.xml 文件如下:


    4.0.0
   
        com.lbh
        eurekademo
        0.0.1-SNAPSHOT
         
   
    com.lbh
    eureka-server
    0.0.1-SNAPSHOT
    eureka-server
    Demo project for Spring Boot
   
        1.8
        Greenwich.RC2
   
   
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
   
   
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
   
   
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
   
   
        
            spring-milestones
            Spring Milestones
            https://repo.spring.io/milestone
        
   


2.3 启动一个服务注册中央 只需要一个注解@EnableEurekaServer,这个注解需要在springboot工程的启动application类上加:
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
        System.out.println("=======================eureka-server 启动乐成=======================");
    }
}

2.4、 eureka是一个高可用的组件,它没有后端缓存,每一个实例注册之后需要向注册中央发送心跳(因此可以在内存中完成),在默认情况下erureka server也是一个eureka client ,必须要指定一个 server。eureka server的配置文件appication.yml:
server:
  port: 8761
eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}{server.port}/eureka/
spring:
  application:
    name: eureka-server

通过eureka.client.registerWithEureka:false和fetchRegistry:false来表明自己是一个eureka server.
2.5、 eureka server 是有界面的,启动工程,打开浏览器访问: http://localhost:8761
 

 
三、创建一个服务提供者 (eureka-client)
当client向server注册时,它会提供一些元数据,例如主机和端口,URL,主页等。Eureka server 从每个client实例吸收心跳消息。 假如心跳超时,则通常将该实例从注册server中删除。
创建过程同server类似,创建完pom.xml如下:


    4.0.0
   
        com.lbh
        eurekademo
        0.0.1-SNAPSHOT
         
   
    com.lbh
    eureka-client
    0.0.1-SNAPSHOT
    eureka-client
    Demo project for Spring Boot
   
        1.8
        Greenwich.RC2
   
   
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
            2.0.0.RELEASE
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
   
   
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
   
   
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
   
   
        
            spring-milestones
            Spring Milestones
            https://repo.spring.io/milestone
        
   


通过注解@EnableEurekaClient 表明自己是一个eurekaclient.
@RestController
@EnableEurekaClient
@SpringBootApplication
public class EurekaClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaClientApplication.class, args);
        System.out.println("====================eureka-client 启动乐成=======================");
    }
    @Value("${server.port}")
    String port;
    @RequestMapping("/hi")
    public String home(@RequestParam(value = "name", defaultValue = "lbh") String name) {
        return "hi " + name + " ,i am from port:" + port;
    }
}

仅仅@EnableEurekaClient是不够的,还需要在配置文件中注明自己的服务注册中央的地点,application.yml配置文件如下:
server:
  port: 8762
spring:
  application:
    name: eureka-client
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

需要指明  spring.application.name,这个很紧张,这在以后的服务与服务之间相互调用一样平常都是根据这个name 。 启动工程,打开http://localhost:8761 ,即eureka server 的网址:
 

 
 
你会发现一个服务已经注册在服务中了,服务名为SERVICE-HI ,端口为7862这时打开 http://localhost:8762/hi?name=lbh, 你会在浏览器上看到 :
 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4