微服务学习笔记二:Eureka服务注册发现

Eureka服务注册发现

服务发现:云端负载均衡,一个基于 REST 的服务,用于定位服务,以实现云端的负载均衡和中间层服务器的故障转移。

1. Service Discovery: Eureka Server

Spring Cloud Netflix - Service Discovery: Eureka Server

Eureka服务端,实现服务注册中心。

1.1 Eureka 注册中心(注册表)实现

1. 添加依赖

<!-- 注册中心 -->
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
		
<!-- 用于注册中心访问账号认证 -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-security</artifactId>
</dependency>

 

2.application.yml配置

server:
  port: 8761
security:
  basic:
    enabled: true #开启认证
  user:
    name: user
    password: 123456
eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://user:password@localhost:8761/eureka

 

3.主程序入口

@SpringBootApplication
@EnableEurekaServer//开启Eureka Server 
public class MicroserviceDiscoveryEurekaApplication {

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

4.测试,浏览器访问:http://localhost:8761/

 

2. Service Discovery: Eureka Clients  

Spring Cloud Netflix - Service Discovery: Eureka Clients

Eureka客户端,提供服务,进行服务注册。

1.引入依赖  

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- 用于注册中心访问账号认证 -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

 

2.application.yml配置

server:
  port: 8081 #8181
spring:
  application:
    name: microservice-provider-user
eureka:
  client:
    serviceUrl:
      defaultZone: http://user:123456@localhost:8761/eureka  #注册 中心已经开启认证
  instance:
    prefer-ip-address: true
    instanceId: ${spring.application.name}:${spring.application.instance_id:${server.port}}

3.主程序入口

@SpringBootApplication
@EnableEurekaClient //启动EnableEureka客户端
@RestController
public class MicroserviceProviderUserApplication {

	@GetMapping("/hello/{name}")
	public String hello(@PathVariable String name){
		System.out.println(name+" welcome . My is microservice provider user");
		return name+" welcome . My is microservice provider user";
	}
	public static void main(String[] args) {
		SpringApplication.run(MicroserviceProviderUserApplication.class, args);
	}
}

 

 

spring.cloud-Eureka服务注册发现.zip

posted @ 2017-01-18 22:20  陈小兵  阅读(47895)  评论(1编辑  收藏  举报