02【微服务爱情故事之暑假风波】Eurake服务注册与发现
暑假到了,小情侣李雷和韩梅梅挥泪告别之后,决定暑假经常打电话联系。在上文中写到,李雷通过打电话表白了韩梅梅并且愉快的。。。在夕阳下一起奔跑。现在,出现了一些问题,韩梅梅的手机号可能欠费停机,这样总会有可能打通的电话对不对。但是新的问题又出现了,韩梅梅有多个手机号码,并且有的手机号码会更换或停机,那么李雷要怎么才能拨打正确号码呢。
这个时候,一个狂拽酷炫吊炸天的宝宝Eurake出现了,它能够和李雷和韩梅梅以一种特殊的方式取得联系,他会记住韩梅梅所有的可用联系电话,韩梅梅的手机号码信息变更停机等,他也能够及时的得到通知。他和李雷在30秒之内都会发生一次联系,并且告诉李雷,让李雷判断要不要更新这些信息,李雷会记住Eurake通知自己的信息,并且自己判断要不要重新记忆。于是问题得以解决,小两口可以在暑假里肆无忌惮的煲电话粥了!
让我们愉快的生成一个Eurake宝宝吧:
项目结构如图所示:
①pom.xml与application.yml文件配置如下:
<?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>spring.cloud.love.eurake</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>1.5.7.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>Dalston.SR3</spring-cloud.version> </properties> <dependencies> <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> <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>
注意其中:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency>
看到这个配置了么,这在上篇博客里面已经埋下了伏笔,原来,李雷和韩梅梅在上篇博客里就已经和Eurake相识了。
application.yml的配置非常简单:
server: port: 8100 eureka: client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://localhost:8100/eurekaBaby
②创建EurakeBabyApplication 用于EurakeBaby出生
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class EurakeBabyApplication { public static void main(String[] args) { SpringApplication.run(EurakeBabyApplication.class, args); } }
通过
EurakeBabyApplication
启动,使EurakeBaby出生。
使用浏览器访问:
EurakeBaby就这样诞生了。
让我们来看一看李雷和韩梅梅是怎么通过Eurake宝宝来打电话的吧。
来看一下此时的Eurake宝宝吧:
①pom.xml与application.yml文件配置如下:
<?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>spring.cloud.love.eurake</groupId> <artifactId>spring-cloud-love-eurake</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-cloud-love-eurake</name> <description>I'm EurakeBaby</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.7.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>Dalston.SR3</spring-cloud.version> </properties> <dependencies> <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> <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>
server: port: 8100 eureka: client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://localhost:8100/eureka
②EurakeBabyApplication
package spring.cloud.love.eurake; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class EurakeBabyApplication { public static void main(String[] args) { SpringApplication.run(EurakeBabyApplication.class, args); } }
通过
EurakeBabyApplication
启动
与Eurake宝宝有过约定之后的韩梅梅:
①pom.xml与application.yml文件配置如下:
<?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>spring.cloud.love.hanmeimei</groupId> <artifactId>spring-cloud-love-hanmeimei</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-cloud-love-hanmeimei</name> <description>I'm HanMeiMei</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.7.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>Dalston.SR3</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</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-actuator</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> </project>
启动两个韩梅梅的电话接听方式:
server: port: 8002 #8001 使用这两个端口分别启动应用 spring: application: name: spring-cloud-love-hanmeimei eureka: client: healthcheck: enabled: true service-url: defaultZone: http://localhost:8100/eureka #eurake的地址 instance: prefer-ip-address: true instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
②HanmeimeiApplication
package spring.cloud.love.hanmeimei; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class HanmeimeiApplication { public static void main(String[] args) { SpringApplication.run(HanmeimeiApplication.class, args); } }
③HanRespondController
package spring.cloud.love.hanmeimei.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class HanRespondController { @Value("${server.port}") private String port; @GetMapping("/respond/{message}") public String respond(@PathVariable String message) { System.out.println("***李雷发微信说:"+message); if(message.equals("I love u")){ return "I miss u"; }else { return "我要睡觉了,回聊"; } } @GetMapping("/call/{message}") public String call(@PathVariable String message) { System.out.println("***李雷打电话说:"+message); return port+":"+"I love u"; } }
此时两个端口的服务都在Euraka宝宝那里通知成功,此时,李雷无论选择哪一个都可以与韩梅梅正常联系。
与Eurake约定之后的李雷:
①pom.xml与application.yml文件配置如下:
<?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>spring.cloud.love.lilei</groupId> <artifactId>spring-cloud-love-lilei</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-cloud-love-lilei</name> <description>I'm LiLei</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.7.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>Dalston.SR3</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</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-actuator</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> </project>
server: port: 8000 spring: application: name: spring-cloud-love-lilei eureka: client: healthcheck: enabled: true service-url: defaultZone: http://localhost:8100/eureka instance: prefer-ip-address: true
②LileiApplication
package spring.cloud.love.lilei; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableEurekaClient public class LileiApplication { /** * 创建restTemplate用于依赖入注 * 注意此处多个一个 @LoadBalanced 注解 * @return */ @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(LileiApplication.class, args); } }
③CallHanController
package spring.cloud.love.lilei.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class CallHanController { @Autowired private RestTemplate restTemplate; @GetMapping("/showLove/{message}") public String showLove(@PathVariable String message) { return this.restTemplate.getForObject("http://localhost:8001/respond/" + message, String.class); } @GetMapping("/callHan/{message}") public String callHan(@PathVariable String message) { return this.restTemplate.getForObject("http://spring-cloud-love-hanmeimei/call/" + message, String.class); } }
启动李雷给韩梅梅联系:
可以清楚的看到,虽然联系的号码不同,但总是可以和韩梅梅正常联系。
此时,韩梅梅8001号码的手机停机了:
那么,当韩梅梅通知到Eurake宝宝之后,Eurake宝宝会告知李雷韩梅梅依旧使用的号码,那么每次李雷依旧可以和韩梅梅正常联系。
Eurake宝宝的神器作用,在这里就告一段落了。请关注下篇博客:傲娇的Eurake宝宝。
转载于:https://my.oschina.net/ruyu/blog/1545033