spring cloud微服务-eureka
言念君子、温其如玉
- spring cloud微服务-eureka
- eureka简单介绍
- 创建maven分布式项目
- 在maven项目中创建spring项目
- eureka整合security权限认证
- idea配置启动项
- eureka客户端访问
spring cloud微服务-eureka
eureka简单介绍
Eureka是Netflix开源的服务发现组件,本身是一个基于REST的服务,包含Server和Client两部分。Spring Cloud Eureka 是 基于 Netflix Eureka 做了二次封装,主要负责完成微服务架构中的服务治理功能,Spring Cloud将它集成在子项目Spring Cloud Netflix中。常见的注册中心有eureka,Consul,Etcd,Zookeeper等。Dubbo中的注册中心有是基于redis的,有基于Zookeeper的,最常用的还是基于Zookeeper的。
创建maven分布式项目
在maven父项目中pom文件中确定spring boot的版本2.0.6.RELEASE,和一些公共的依赖引用,完整的pom文件中代码如下:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.6.RELEASE</version> <relativePath/> <!– lookup parent from repository –> </parent> <groupId>com.learning.wen</groupId> <artifactId>learning-item</artifactId> <version>1.0.0</version> <name>learning-item</name> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!-- mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.4</version> </dependency> <!--热部署依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> <!-- 阿里fastJson--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency> <!-- apache组件 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.3.2</version> </dependency> <!-- 添加swagger依赖 swagger start--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> <exclusions> <exclusion> <groupId>io.swagger</groupId> <artifactId>swagger-annotations</artifactId> </exclusion> <exclusion> <groupId>io.swagger</groupId> <artifactId>swagger-models</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-annotations</artifactId> <version>1.5.21</version> </dependency> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-models</artifactId> <version>1.5.21</version> </dependency> <!--swagger增强,界面更友好--> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>swagger-bootstrap-ui</artifactId> <version>1.8.9</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!--热部署插件,CTRL+f9--> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> </configuration> </plugin> </plugins> </build>在maven项目中创建spring项目
现在的项目使用spring boot开发,除了基本的项目中的业务代码,开始的部分要做的就是:
加依赖、加注解、写配置
对于依赖的相关版本查找:链接地址
加依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>加注解
在项目启动类上添加以下注解:
@EnableEurekaServer写配置
注意yml的格式问题、application的编码问题
server: port: 10000 eureka: client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://localhost:10000/eureka/注:eureka的集群配置方法
配置改为
--- server: port: 10000 spring: application: name: eureka-server profiles: peer1 #将peer1注册到peer2,peer3 eureka: instance: hostname: peer1 client: service-url: defaultZone: http://peer2:10001/eureka/,http://peer3:10002/eureka/ --- server: port: 10001 spring: application: name: eureka-server profiles: peer2 #将peer2注册到peer1,peer3 eureka: client: service-url: defaultZone: http://peer1:10000/eureka/,http://peer3:10002/eureka/ instance: hostname: peer2 --- server: port: 10002 spring: application: name: eureka-server profiles: peer3 #将peer3注册到peer1,peer2 eureka: client: service-url: defaultZone: http://peer1:10000/eureka/,http://peer2:10001/eureka/ instance: hostname: peer3并找到自己的hosts文件 ,在C:\Windows\System32\drivers\etc目录下,使用文本方式打开,在文本的末尾阶段加上:
#eureka 127.0.0.1 peer1 127.0.0.1 peer2 127.0.0.1 peer3 #eureka endeureka整合security权限认证
加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>加注解
写配置
peer1、peer2、peer3都要改
server: port: 10000 spring: application: name: eureka-server profiles: peer1 security: user: name: wen password: qiyu # 将peer1注册到peer2,peer3 eureka: instance: hostname: peer1 client: service-url: defaultZone: http://wen:qiyu@peer2:10001/eureka/,http://wen:qiyu@peer3:10002/eureka/idea配置启动项
项目
peer1节点启动项:
peer2节点启动项:
peer3节点启动项:
eureka客户端访问
peer1节点访问路径:http://localhost:10000
peer2、peer3端口换10001、10002
