SpringCloud 第二篇: 服务提供者 (eureka client) 浅浅的花香味﹌ 2022-05-24 06:34 175阅读 0赞 在上一篇中我创建了一个注册中心,现在我就可以给他注册服务。当client向server注册时,它会提供一些元数据,例如主机和端口,URL,主页等。Eureka server 从每个client实例接收心跳消息。 如果心跳超时,则通常将该实例从注册server中删除。 ## 1.创建服务 ## 创建服务的过程和创建注册中心的步骤一样,可以参考[SpringCloud 第一篇: 服务注册中心(Eureka)][SpringCloud _ _Eureka]。 创建完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.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.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.BUILD-SNAPSHOT</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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </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> <repositories> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project> ## 2.添加服务注解 ## 通过注解@EnableEurekaClient 表明自己是一个eurekaclient。 package com.example.client; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @EnableEurekaClient @RestController public class DemoClientApplication { public static void main(String[] args) { SpringApplication.run(DemoClientApplication.class, args); } @Value("${server.port}") String port; @RequestMapping("/hello") public String home(@RequestParam String name) { return "hello 来自 port:" +port +"的," + name; } } ## 3.配置文件 ## 在配置文件中配置服务注册中心的地址,applicaton.yml配置如下: eureka: client: serviceUrl: #注册中心地址 defaultZone: http://localhost:10000/eureka/ server: port: 10001 spring: application: #服务名用于将来服务之间的调用(注意:服务名中含有下划线将无法使用Feign进行跨服务调用) name: service-demo ## 4.发现服务 ## 启动工程,打开[http://localhost:10000/][http_localhost_10000],即eureka server 的网址,可以看到已经注册了一个服务,服务名为:service-demo。 ![70][] ## 5.调用服务 ## 这时打开 [http://localhost:10001/hello?name=yanling][http_localhost_10001_hello_name_yanling] ,你会在浏览器上看到 : ![70 1][] [SpringCloud _ _Eureka]: https://blog.csdn.net/qq_34996727/article/details/80405053 [http_localhost_10000]: http://localhost:10000/ [70]: /images/20220524/c02db6d46dea46c58e6ae8a48ebb579a.png [http_localhost_10001_hello_name_yanling]: http://localhost:10001/hello?name=yanling [70 1]: /images/20220524/1b18dfa690604d71ba3a7c09edeaf8e8.png
还没有评论,来说两句吧...