您的位置:首页 > 其它

eureka做服务注册与发现

2018-02-24 19:20 337 查看
关于eureka的介绍什么的就不写了哈,网上有很多。。

首先搭建eureka服务端

可以通过Spring Initializr创建



pom文件:

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>

<!--引入spring security做登录验证-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>


依赖引入好了之后就开始application.properties配置了

server.port=8666
spring.application.name=eureka-server
#服务注册中心实例的主机名
eureka.instance.hostname=127.0.0.1
#留存的服务示例低于多少比例进入保护模式
eureka.server.renewal-percent-threshold=0.5
#是否开启保护模式
eureka.server.enable-self-preservation=true
#是否向服务注册中心注册自己
eureka.client.register-with-eureka=false
#是否启用获取服务注册信息,因为这是一个单点的Eureka Server,不需要同步其他的Eureka Server节点的数据,故而设为false
eureka.client.fetch-registry=false
#注册和查询都需要依赖该地址,多个以逗号分隔
#这里有登录验证,把用户名密码写在这地址上,不然client连接的时候会报com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
eureka.client.serviceUrl.defaultZone=http://admin:123456@127.0.0.1:8666/eureka/

#这里使用spring security对注册中心做一个用户名密码登录
#先做个基础的,后面再来完善安全性问题
security.basic.enabled=true
security.user.name=admin
security.user.password=123456


配置好了之后,在主入口中

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

}


打上@EnableEurekaServer这一注解就可以了

然后访问localhost:8666



就会弹出身份验证



输入用户名密码登录上去就进入eureka页面了

然后注册一个client

新建一个project,首先还是需要添加依赖包

<!--如果要健康检查,就需要这个依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>


然后配置

server.port=8081
spring.application.name=user-service

eureka.client.service-url.defaultZone=http://admin:123456@127.0.0.1:8666/eureka/
eureka.instance.lease-renewal-interval-in-seconds=5
eureka.instance.lease-expiration-duration-in-seconds=10
eureka.client.healthcheck.enable=true




服务就成功注册上了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: