微服务:实现服务发现与服务注册
2017-05-06 03:24
609 查看
一.服务发现的方式:
1.客户端发现:Eureka、ZooKeeper(存在缺陷)原因:http://blog.csdn.net/whereismatrix/article/details/53305045
2.服务端发现:consul+nginx
描述:Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的。SpringCloud将它集成在其子项目spring-cloud-netflix中,一实现SpringCloud的服务发现功能
二.服务注册的功能
1.服务注册表:服务注册是一个记录当前可用的微服务实例的网络信息数据库,是服务发现机制的主要核心,服务注册表查询api、管理api,使用查询api获得可用服务的实例,使用管理api实现注册、注销
2.服务注册:节点启动后,会在Eureka Server中进行注册,这样EurekaServer中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观的看到。
三.实现一个Eurka Serve【microservice-discovery-eureka】
1.添加Eureka的maven依赖
2.启动类:使用@EnableEurekaServer注解
3.application.properties的配置项
4.启动【microservice-discovery-eureka】
四.实现一个【microservice-provider-user】
1.添加Eureka的maven依赖
2.启动类:使用@EnableEurekaClient注解
3.application.properties的配置项
#添加身份验证
4.启动【microservice-provider-user】
描述:先启动服务发现,再启动服务注册类,刷新服务发现管理页面
五.对Eureka服务的身份验证(Authenticating with the Eureka Server)
1.修改应用maven依赖(服务注册、服务发现)
2.添加身份验证
服务发现application.properties的配置项
3.重新启动,访问服务发现管理页面
4.服务注册应用也需要用到身份验证,否则注册不了
服务注册application.properties的配置项
1.客户端发现:Eureka、ZooKeeper(存在缺陷)原因:http://blog.csdn.net/whereismatrix/article/details/53305045
2.服务端发现:consul+nginx
描述:Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的。SpringCloud将它集成在其子项目spring-cloud-netflix中,一实现SpringCloud的服务发现功能
二.服务注册的功能
1.服务注册表:服务注册是一个记录当前可用的微服务实例的网络信息数据库,是服务发现机制的主要核心,服务注册表查询api、管理api,使用查询api获得可用服务的实例,使用管理api实现注册、注销
2.服务注册:节点启动后,会在Eureka Server中进行注册,这样EurekaServer中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观的看到。
三.实现一个Eurka Serve【microservice-discovery-eureka】
1.添加Eureka的maven依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>microservice-discovery-eureka</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>microservice-discovery-eureka</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Dalston.RELEASE</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 身份验证 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </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> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.启动类:使用@EnableEurekaServer注解
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer//注册成一个服务注册中心,给其他微服务应用进行提供会话 @SpringBootApplication public class MicroserviceDiscoveryEurekaApplication { public static void main(String[] args) { SpringApplication.run(MicroserviceDiscoveryEurekaApplication.class, args); } }
3.application.properties的配置项
security.basic.enabled=true security.user.name=user security.user.password=test123456 server.port=8761 eureka.instance.hostname=localhost eureka.client.registerWithEureka=false eureka.client.fetchRegistry=false #添加身份验证
eureka.client.serviceUrl.defaultZone=http://${security.user.name}:${security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/ #默认
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/ spring.application.name=cloud-eureka-server
4.启动【microservice-discovery-eureka】
四.实现一个【microservice-provider-user】
1.添加Eureka的maven依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>microservice-provider-user</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>microservice-provider-user</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Dalston.RELEASE</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </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> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.启动类:使用@EnableEurekaClient注解
package com.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableAutoConfiguration @EnableEurekaClient//注册一个服务注册应用 public class MicroserviceProviderUserApplication { public static void main(String[] args) { SpringApplication.run(MicroserviceProviderUserApplication.class, args); } }
3.application.properties的配置项
server.port=9090 #服务注册表中描述应用的名称
spring.application.name=microservice-provider-user security.user.name=user security.user.password=test123456 #eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka-server-1.4.6/eureka/
#添加身份验证
eureka.client.serviceUrl.defaultZone=http://${security.user.name}:${security.user.password}@localhost:8761/eureka/ eureka.instance.appname=microservice.client.user #eureka.client.registerWithEureka=true #eureka.client.fetchRegistry=true
4.启动【microservice-provider-user】
描述:先启动服务发现,再启动服务注册类,刷新服务发现管理页面
五.对Eureka服务的身份验证(Authenticating with the Eureka Server)
1.修改应用maven依赖(服务注册、服务发现)
<!-- 身份验证--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
2.添加身份验证
服务发现application.properties的配置项
security.basic.enabled=true security.user.name=user security.user.password=test123456 server.port=8761 eureka.instance.hostname=localhost eureka.client.registerWithEureka=false eureka.client.fetchRegistry=false eureka.client.serviceUrl.defaultZone=http://${security.user.name}:${security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/ spring.application.name=cloud-eureka-server
3.重新启动,访问服务发现管理页面
4.服务注册应用也需要用到身份验证,否则注册不了
服务注册application.properties的配置项
server.port=9090
spring.application.name=microservice-provider-user
security.user.name=user
security.user.password=test123456
eureka.client.serviceUrl.defaultZone=http://${security.user.name}:${security.user.password}@localhost:8761/eureka/ eureka.instance.appname=microservice.client.user #eureka.client.registerWithEureka=true #eureka.client.fetchRegistry=true
相关文章推荐
- 使用Spring Cloud Consul实现服务的注册和发现
- 使用Spring Cloud Zookeeper实现服务的注册和发现
- (二)SpringBoot+SpringCloud —— 使用Eureka实现服务注册与发现
- 微服务~Eureka实现的服务注册与发现及服务之间的调用
- 使用 Eureka 实现服务注册与发现
- 使用consul实现服务的注册和发现
- spring cloud搭建微服务second-fiberhome(二):结合consul实现服务注册与服务发现
- Eureka+ribbon 实现服务注册与发现和负载均衡
- 服务注册发现consul之二:使用Spring Cloud Consul实现服务的注册和发现
- Spring Cloud Eureka 实现服务注册与发现
- 使用consul实现服务的注册和发现
- Spring cloud 实现服务注册及发现
- Spring cloud实现服务注册及发现
- spring boot(实现服务注册及发现)
- 【Zookeeper】JAVA通过ZK实现服务注册和服务发现
- Spring cloud实现服务注册及发现
- 使用Spring Cloud Consul实现服务的注册和发现
- 使用consul实现分布式服务注册和发现
- 使用 Eureka 实现服务注册与发现
- Spring cloud实现服务注册及发现