在微服务多项目交互中,经常用到服务注册服务组件,可以方便通过FeignClient 和RestTemplate 客户端发起跨微服务的调度。在基于Spring 生态的注册组件中, Console 和Eureka 流行度比较高。以Eureka 为例,如何实现基于Spring Cloud 微服务注册。
Eureka 服务安装
Eureka server 有多种安装方式,基于docker 、基于Spring frame work 启动。为了选择更好的兼容 Spring Cloud 微服务的Eureka ,我们手动构建一个基于Spring 的包,然后封装为docker 镜像。 步骤如下。
构建Spring frame Eureka框架
我们先通过 https://start.spring.io/构建一个Eureka的微服务。
WX20200419-124400@2x.png下载demo.zip ,解压用编辑器打开。需要两步配置。
1.在启动类上开启@EnableEurekaServer注释。
@EnableEurekaServer
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
2.修改application.yaml
文件,当然,本地配置是application.properties
可以改成yml
的文件,方便配置。 写入以下配置内容:
server:
port: 8761
eureka:
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://localhost:8761/eureka/
启动application应用。 通过浏览器 [http://localhost:8761/](http://localhost:8761/)
可以访问 服务页面。如下。
封装Eureka 服务
为了方便的启动和管理服务,也是为了开发和测试需要,我们想把Eureka 改造成docker 镜像方便启动。当然每次用IDE的启动方式也能满足需求,IDE的开发环境每次加载大量内存到我们的开发机。我们封装为docker镜像,启动更轻量。
顺便,看看 Spring Eureka 项目都加载了哪些依赖。 其实除了,正常的spring-boot-starter-web 外,还单独依赖了spring-cloud-starter-netflix-eureka-server核心的依赖。
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
把刚才Spring Eureka的服务用meven package 打包。
找到构建的好的 jar 文件的目录。
/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/bin/java "-Dmaven.multiModuleProjectDirectory=/Users/baidu/Downloads/demo 2" "-Dmaven.home=/Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven3" "-Dclassworlds.conf=/Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven3/bin/m2.conf" "-javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=52238:/Applications/IntelliJ IDEA.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath "/Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven3/boot/plexus-classworlds-2.5.2.jar" org.codehaus.classworlds.Launcher -Didea.version=2018.3.1 package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building demo 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/baidu/Downloads/demo 2/target/classes
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/baidu/Downloads/demo 2/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/baidu/Downloads/demo 2/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ demo ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.example.demo.DemoApplicationTests
13:16:12.297 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
......
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.6.RELEASE)
2020-04-19 13:16:14.517 INFO 95862 --- [ main] com.example.demo.DemoApplicationTests : No active profile set, falling back to default profiles: default
.....
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] --- maven-jar-plugin:3.1.2:jar (default-jar) @ demo ---
[INFO] Building jar: /Users/dashuai/Downloads/demo/target/demo-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.2.6.RELEASE:repackage (repackage) @ demo ---
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 17.624 s
[INFO] Finished at: 2020-04-19T13:16:24+08:00
[INFO] Final Memory: 52M/543M
[INFO] ------------------------------------------------------------------------
Process finished with exit code 0
进入构建的jar 目录, 构建Dockerfile。
FROM openjdk:8
COPY target/demo-0.0.1-SNAPSHOT.jar.jar app.jar //本地的jar的路径
EXPOSE 8761
CMD java -jar app.jar
编译一个 Eurake Server 镜像。
$docker build -f Dockerfile -t spring.eurakeserver:v0.1 .
编译完后运行
$docker run -ti -d sspring.eurakeserver:v0.1
通过访问本地 [http://localhost:8761/](http://localhost:8761/)
也可以成功访问Eurake Server 端。
微服务注册
要把微服务注册到 Eurake Server 端 。我们要启动一个微服务框架。
微服务需要在pom.xml 里依赖一个Eurake客户端的类。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
在微服务启动类里开启@EnableEurekaServer依赖。
@SpringBootApplication
@EnableDiscoveryClient
public class ApitestApplication {
public static void main(String[] args) {
SpringApplication.run(ApitestApplication.class, args);
}
}
修改配置文件application.yaml
文件,配置两部分,第一部分是Eurake Server 地址。第二部分是本地服务在Eureka里的服务名称。
server:
port: 8991
spring:
application:
name: EVENT-CENTER-SERVICE // 本地服务名称
eureka:
client:
registerWithEureka: true
fetchRegistry: false
serviceUrl:
defaultZone: http://localhost:8761/eureka/ // Eurake服务地址
8761.png
查看服务已经注册成功了,可以进行远程调用测试了。
网友评论