springcloud-eureka简单实现
请参考 spring+cloud为服务实战 第三章
一。创建Eureka服务
1.使用Idea创建一个项目
结构如下:
2.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>com.cloudlearn</groupId> <artifactId>core</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>core</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.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> <spring-cloud.version>Finchley.SR1</spring-cloud.version> </properties> <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> </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>
3.启动类配置
/*通过@EnableEurekaServer注解启动一个服务注册中心,提供给其他应用进行对话 * 默认情况下服务注册中心也会将自己作为客户端尝试注册自己,所以我们应该禁止,只需在配置文件中修改即可(application.properties有详细说明): * eureka.client.register-with-eureka=false * eureka.client.fetch-registry=false * */ @EnableEurekaServer @SpringBootApplication public class CoreApplication { public static void main(String[] args) { SpringApplication.run(CoreApplication.class, args); } }
4.application.properties配置
server.port=1111 eureka.instance.hostname=localhost #由于该应用未注册中心,所以设置为false,代表不向注册中心注册自己 eureka.client.register-with-eureka=false #由于注册中心的职责就是维护实例,所以它并不需要去检索服务,所以设置成false eureka.client.fetch-registry=false eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka
5.运行一下我们的项目 并且访问 http://localhost:1111/ 结果如下:
因为以上只是开启了服务注册中心,所以还没有服务,下面我们在创建一个注册服务
二。注册服务
1.创建过程与上面类似只是选择服务那里有些许不同(web项目)
2.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>com.cloudlearn</groupId> <artifactId>zhuce</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>zhuce</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.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> <spring-cloud.version>Finchley.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <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> </dependency> <!-- https://mvnrepository.com/artifact/log4j/log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </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>
3.启动类配置
@RestController public class HelloController { private final static org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(HelloController.class); @Autowired private DiscoveryClient discoveryClient; @RequestMapping("/hello") public String Hello(){ discoveryClient.getServices().forEach(id -> { discoveryClient.getInstances(id).forEach(instance -> { log.info("/hello, host:" + instance.getHost() + ", service_id:" + instance.getServiceId()); }); }); return "Hello World"; } }
//通过添加注解EnableDiscoveryClient,激活Eureka中的DiscoveryClient实现(自动化配置,创建DiscoveryClient接口针对Eureka客户端的EurekaDiscoveryClient实例)
@EnableDiscoveryClient @SpringBootApplication public class ZhuceApplication { public static void main(String[] args) { SpringApplication.run(ZhuceApplication.class, args); } }
4.application.properties配置:
#服务的名称
spring.application.name=hello-service
#指定服务中心的地址(我们上一个服务中心的地址) eureka.client.service-url.defaultZone=http://localhost:1111/eureka/
5.启动我们的项目,结果如下代表启动成功
然后在运行 http://localhost:1111/ 结果如下:
运行http://localhost:8080/hello,结果控制台日志打印: