`
拓子轩
  • 浏览: 204941 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Eureka Server微服务注册中心搭建

阅读更多

一、Eureka Server注册中心初步搭建

1. 在pom.xml文件中添加依赖

 

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

 

 2. 编写启动类,并在启动类上添加@EnableEurekaServer注解

 

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

 

 

3. 在resources目录下创建application.yml配置文件,并添加配置信息

 

server:

  port: 8761

 

eureka:

  client:

    register-with-eureka: false

    fetch-registry: false

    service-url:

      default-zone: http://localhost:8761/eureka

 

eureka.client.registerWithEureka:表示是否将自己注册到Eureka Server,默认为true. 当前应用即为Eureka Server,故设为false。

eureka.client.fetch-registry:表示是否从Eureka Server获取注册信息,默认为true. 因为这是一个单点的Eureka Server,不需要同步其他的Eureka Server节点的数据,故设为false。

eureka.client.serviceUrl.defaultZone:设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。多个地址可使用,(逗号)分割

 

二、创建服务提供者并进行注册

1. 创建maven工程spring-cloud-eureka-client,并在pom.xml文件中添加依赖

 

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

 

 2. 编写启动类,并在启动类上添加@EnableEurekaClient或@EnableDiscoveryClient注解

 

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

 

 

 

3. 在resources目录下创建application.yml配置文件,并添加配置信息

server:

  port: 8080

  

spring:

  application:

    name: spring-cloud-eureka-client

    

eureka:

  client: 

    service-url:

      default-zone: http://localhost:8761/eureka

  instance: 

    prefer-ip-address: true

 

spring.application.name:用于指定注册到Eureka Server上的应用名称。

eureka.instance.prefer-ip-address=true:表示将自己的IP注册到Eureka Server。如不配置该属性或将其设为false,则表示注册微服务所在操作系统的hostname到Eureka Server。

 

4. 访问http://localhost:8761/eureka

 

三、为Eureka Server添加用户认证

 

1. 在pom文件中添加依赖

  	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-security</artifactId>
	</dependency>

 

 2. 在application.yml中添加配置

security:

  basic:

    enabled: true # 开启基于HTTP basic的认证

  user:

    name: tuozixuan  # 配置登录的账户是user

    password: 123456  # 配置登录的密码是password

 

注意:如果不设置用户的账户及密码,账户默认是user,密码是一个随机值,该值会在启动时打印出来。

 

3. 微服务注册到需要认证的Eureka Server

只需将eureka.client.service-url.default-zone配置为http://user:password@EUREKA_HOST:EUREKA_PORT/eureka/的形式

如:http://tuozixuan:123456@localhost:8761/eureka

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics