栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java springcloud搭建

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java springcloud搭建

父pom


    4.0.0

    org.example
    mypro
    1.0-SNAPSHOT
    
        Test
        eureka
        config
        client
    
    pom

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

    
        8
        8
    

    


    

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

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

eureka服务端 application.yml
server:
  port: 8080
eureka:
  instance:
    hostname: localhost
  client:
    #实例是否在eureka服务器上注册自己的信息以提供其他服务发现,默认为true
    register-with-eureka: false
    #此客户端是否获取eureka服务器注册表上的注册信息,默认为true
    fetch-registry: false
    #eureka客户端需要多长时间发送心跳给eureka服务器,表明他仍然或者,默认30秒
    #lease-renewal-interval-in-seconds: 5
    #eureka服务器在接受到实力的最后一次发出的心跳后,需要等待多久才可以将此实例删除
    #lease-expiration-duration-in-seconds: 10

  server:
    #自我保护模式
    enable-self-preservation: false
    #清理无效节点,默认60*1000毫秒,即60秒,配合client中lease-renewal-interval-in-seconds,lease-expiration-duration-in-seconds
    eviction-interval-timer-in-ms: 1000

启动类
package ws;

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

@SpringBootApplication
@EnableEurekaServer
public class eureka {
    public static void main(String[] args) {
        SpringApplication.run(eureka.class,args);
    }
}

config配置中心 application.yml
server:
  port: 8083

eureka:
  instance:
    instance-id: config-1

  client:
    service-url:
      defaultZone: http://localhost:8080/eureka/
    #eureka客户端需要多长时间发送心跳给eureka服务器,表明他仍然或者,默认30秒
    lease-renewal-interval-in-seconds: 5
    #eureka服务器在接受到实力的最后一次发出的心跳后,需要等待多久才可以将此实例删除
    lease-expiration-duration-in-seconds: 10

spring:
  application:
    name: config

  #本地存储
  profiles:
    active: native

  #本地存储路径
  cloud:
    config:
      server:
        native:
          search-locations: classpath:/config

在resources下新建config目录存储配置文件a.yml
server:
port: 8085

启动类
package ws;

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

@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
//@EnableDiscoveryClient:和EnableEurekaClient功能一样,服务注册发现
//是springclound标准注解,EnableEurekaClient只是eureka的
public class config {
    public static void main(String[] args) {
        SpringApplication.run(config.class,args);
    }
}


客户端 bootstrap.yml

客户端要读取远程配置文件,所以要优先加载bootstrap

#bootstrap.yml最先加载
eureka:
  instance:
    instance-id: client-1

  client:
    service-url:
      defaultZone: http://localhost:8080/eureka/
    #eureka客户端需要多长时间发送心跳给eureka服务器,表明他仍然或者,默认30秒
    lease-renewal-interval-in-seconds: 5
    #eureka服务器在接受到实力的最后一次发出的心跳后,需要等待多久才可以将此实例删除
    lease-expiration-duration-in-seconds: 10

spring:
  application:
    name: client

  #配置文件客户端配置,只要有配置和依赖无需其它注解配置就能生效
  cloud:
    config:
      name: a #获取配置文件的名称
      #profile: dev #获取配置文件的环境dev,prd等
      #label: dev # 远程分支名默认为master,由于是本地模式 label无效
      discovery:
        enabled: true # 开启配置服务发现,表示从eureka注册中心发现服务
        service-id: config #指定配置中心的服务名
启动类
package ws;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@SpringBootApplication
@EnableEurekaClient
//@EnableDiscoveryClient:和EnableEurekaClient功能一样,服务注册发现
//是springclound标准注解,EnableEurekaClient只是eureka的
public class client {
    public static void main(String[] args) {
        SpringApplication.run(client.class,args);
    }


}

@Controller
class c{
    @RequestMapping("/a")
    @ResponseBody
    public String a(){
        return "client1";
    }
}




end
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/340145.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号