eureka搭建

server端:
1)添加pom依赖
<!-- eureka 注册中心组件 -->
           <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka-server</artifactId>
           </dependency>

 

2)配置文件-application.properties
#系统标识名称
spring.application.name=tp-eureka
#配置网关映射端口
server.port=18700
 
#Eureka server的主机名
eureka.instance.hostname=localhost
#关闭eureka-server的保护机制
#eureka.server.enableSelfPreservation=false
#是否将自己注册到eureka server  默认true
eureka.client.registerWithEureka= false
#是否从eureka server取消息  默认true
eureka.client.fetchRegistry=false
#注册中心地址
eureka.client.serviceUrl.defaultZone= http://${eureka.instance.hostname}:${server.port}/eureka/
 
3)启动类添加注解@EnableEurekaServer
package com.kanshu;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
 
@EnableEurekaServer
@SpringBootApplication
public class StartApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(StartApplication.class, args);
    }
 
}

  

Client客户端:
1)添加pom依赖
<!-- eureka 注册中心组件 -->
           <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
           </dependency>
 
2)配置文件-application.properties
#系统标识名称(注册中心显示的名字)
spring.application.name=tp-cust
#配置网关映射端口
server.port=18702
#是否显示主机ip
eureka.instance.preferIpAddress=true
#指定eureka服务端地址
eureka.client.serviceUrl.defaultZone=http://localhost:18700/eureka/
#leaseRenewalIntervalInSeconds,表示eureka client发送心跳给server端的频率
eureka.instance.leaseRenewalIntervalInSeconds=50
#leaseExpirationDurationInSeconds,表示eureka server至上一次收到client的心跳之后,等待下一次心跳的超时时间,在这个时间内若没收到下一次心跳,则将移除该instance。
#默认为90秒
#如果该值太大,则很可能将流量转发过去的时候,该instance已经不存活了。
#如果该值设置太小了,则instance则很可能因为临时的网络抖动而被摘除掉。
#该值至少应该大于leaseRenewalIntervalInSeconds
eureka.instance.leaseExpirationDurationInSeconds=30
#表示eureka client间隔多久去拉取服务注册信息,默认为30秒,对于api-gateway,如果要迅速获取服务注册状态,可以缩小该值,比如5秒
eureka.client.registryFetchIntervalSeconds=50
 
3)启动类添加@EnableEurekaClient注解
package com.threepicture;
 
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
 
/**
* 启动加载,必须放在项目根目录
* @author 杏仁拌饭
*
*/
@SpringBootApplication
//eureka客户端注册(此注解属于组合注解包含@EnableDiscoveryClient)
@EnableEurekaClient
//开启hystrix断路功能-需要在contrller中配置HystrixCommand
@EnableCircuitBreaker
//Feign负载
@EnableFeignClients
//定义扫描的路径从中找出标识了需要装配的类自动装配到spring的bean容器中
//@ComponentScan( basePackages = "com.threepicture.frame.*")
//将项目中对应的mapper类的路径加进来就可以了
@MapperScan("com.threepicture.dao.mapper")
public class StartApplication {
 
    private static Logger logger = LoggerFactory.getLogger(StartApplication.class);
    
    public static void main(String[] args) {
        
        logger.info("进入cust....");
        
        SpringApplication.run(StartApplication.class, args);
    }
 
}
 
至此eureka配置完成
posted @ 2019-03-24 19:37  杏仁拌饭  阅读(76)  评论(0)    收藏  举报