微服务做"完一个项目现在总结一下。
服务治理:主要实现各个微服务实例的自动化注册和发现。
搭建步骤:
1、新建maven 工程 并引入springboot 等 jar.
例如:
服务注册中心 spring Cloud Eureka
在Maven中引入springboot、springCloud jar
1、pom.xml 中引入 服务注册中心配置文件 |
<?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>eureka-server</groupId> <artifactId>eureka-server</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <!--基于Springboot--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.7.RELEASE</version> <relativePath/> </parent> <!--设置字符编码及java版本--> <!-- <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> --> <dependencies> <!--增加eureka-server的依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <!--用于测试的,本例可省略--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <!--依赖管理,用于管理spring-cloud的依赖,其中Camden.SR3是版本号--> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.SR5</version> <!-- <version>Camden.SR3</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、在application.properties 文件引入一下参数 |
#当前应用名称 spring.application.name=eureka-server #当前服务端口 server.port=1111 #本地服务名称 eureka.instance.hostname=eurka-server-1 #表示不想注册中心注册自己 eureka.client.register-with-eureka=false #防止自己注册自己: 维护服务实例,此标签表示不需要去检索服务 eureka.client.fetch-registry=false #本工程服务URL eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/ |
3、新建启动类 |
package com.eureka.server; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; /** * Created by Administrator on 2017/5/14. */ @EnableEurekaServer @SpringBootApplication public class Application { public static void main(String[] args) { // SpringApplication.run(EurekaServerApplication.class, args); new SpringApplicationBuilder(Application.class).web(true).run(args); } } |
@EnableEurekaServer: 注册中心标签 源码自己点入查看。 |
4、启动现象 |
启动端口 属性文件配置 |
注意: spring-boot-maven-plugin maven 集成插件。如果无,命令操作会有异常。
5、 如果注册高可用 注册中心 |
配置多个.properties 文件
|
.properties 文件 文件内容:
application-peer1.properties:
spring.application.name=eureka-server
#当前服务端口
server.port=1111
#本地服务名称
eureka.instance.hostname=eurka-server-1
#表示不想注册中心注册自己
eureka.client.register-with-eureka=false
#防止自己注册自己: 维护服务实例,此标签表示不需要去检索服务
eureka.client.fetch-registry=false
#本工程服务URL
eureka.client.service-url.defaultZone=http://eurka-server-2:1112/eureka/
application-peer2.properties:
spring.application.name=eureka-server
#当前服务端口
server.port=1112
#本地服务名称
eureka.instance.hostname=eurka-server-2
#表示不想注册中心注册自己
eureka.client.register-with-eureka=false
#防止自己注册自己: 维护服务实例,此标签表示不需要去检索服务
eureka.client.fetch-registry=false
#本工程服务URL
eureka.client.service-url.defaultZone=http://eurka-server-1:1111/eureka/
注意: peer1、peer2 URL是相互指向
6、高可用启动方式 |
a、本地配置 ip 地址 |
使用maven 打包命令将工程打成jar 并执行以下命令运行 java -jar eureka-server-1.0-SNAPSHOT.jar --spring.profiles.active=peer1 java -jar eureka-server-1.0-SNAPSHOT.jar --spring.profiles.active=peer2
|
以上就是服务单注册中心和高可用注册中心工程。
注意: # 当完成 服务注册后,服务提供者会维护一个心跳来持续高速注册中心此服务有效。 #以下两个参数: 维护服务心跳时间 30 90 为默认时间 eureka.instance.lease-expiration-duration-in-seconds=30 eureka.instance.lease-renewal-interval-in-seconds=90