转自 https://blog.csdn.net/u012702547/article/details/77823434
这个系列我感觉真的太好了,可以一步一步的了解spring cloud 的搭建以及更深层次的东西,对想学这门技术的朋友真的入门特别的快,感谢这位大哥的分享,我也会持续的更新过来。
先新建一个最简单的Spring Boot项目,命名为eureka-server,普通到什么程度呢?就是一个starter都不需要添加,创建成功之后就只引用了一个父starter。
启动一个服务注册中心的方式很简单,就是在Spring Boot的入口类上添加一个@EnableEurekaServer注解,如下:
@EnableEurekaServer @SpringBootApplication public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }最后我们再做一点简单的配置就可以了,配置就写在Spring Boot的配置文件application.properties中,写法如下:
server.port=1111 eureka.instance.hostname=localhost eureka.client.register-with-eureka=false eureka.client.fetch-registry=false eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/OK,做完这一切之后,我们就可以启动这一个Spring Boot 服务,服务启动成功之后,在浏览器中输入:http://localhost:1111就能够看到如下页面:
OK,看到上面这个页面之后,表示你的服务注册中心已经搭建好了。
OK,那么现在服务注册中心有了之后,我们可以考虑向这个服务注册中心注册一个服务提供者了。
还是创建一个Spring Boot工程,这次创建比之前创建多一个步骤,在创建的时候选中web的starter,我们来创建一个web工程,在IntelliJ IDEA中创建的时候选中web,如下:
在创建好的工程中,我们需要添加Eureka依赖,添加方式如下:
<?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>org.sang</groupId> <artifactId>provider</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>provider</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.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> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.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>
在Spring Boot的入口函数处,通过添加@EnableDiscoveryClient注解来激活Eureka中的DiscoveryClient实现(因为我们在HelloController中注入了DiscoveryClient)。
@EnableDiscoveryClient @SpringBootApplication public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); } }最后的最后,我们在application.properties文件中配置一下服务名和注册中心地址即可,如下:
spring.application.name=hello-service eureka.client.service-url.defaultZone=http://localhost:1111/eureka做完这一切之后,我们就可以来测试了,直接运行这个Spring Boot工程,运行成功之后,我们刷新刚才的http://localhost:1111,就可以看到有一个服务已经注册成功了。如下:
亲测成功!还没有入门的朋友可以跟着这个,方便进一步理解spring cloud。