SpringBoot整合Eureka搭建微服务
Posted 缘故为何
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot整合Eureka搭建微服务相关的知识,希望对你有一定的参考价值。
1.创建一个services项目,添加三个子模块client(客户端)、service(服务端)、registry(注册中心)
1.1 创建一个services项目
1.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> <packaging>pom</packaging> <modules> <module>client</module> <module>service</module> <module>registry</module> </modules> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.micro</groupId> <artifactId>services</artifactId> <version>0.0.1-SNAPSHOT</version> <name>services</name> <description>services</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</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-netflix-eureka-server</artifactId> <version>1.4.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project>
1.3 添加子模块client(客户端)
1.4 添加子模块service(服务端)
1.5 添加子模块registry(注册中心)
2.搭建registry(注册中心)
2.1 application.yml 配置
server: port: 8080 eureka: instance: hostname: localhost client: registerWithEureka: false fetchRegistry: false
2.2 编写启动程序
package com.services.registry; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class RegistryApplication { public static void main(String[] args) { SpringApplication.run(RegistryApplication.class); } }
3.搭建service(服务端)
3.1 application.yml 配置
server: port: 8081 spring: application: name: service eureka: client: serviceUrl: defaultZone: http://localhost:8080/eureka/
3.2 编写启动程序
package com.services.service; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class ServiceApplication { public static void main(String[] args) { SpringApplication.run(ServiceApplication.class, args); } }
3.3 编写服务
package com.services.service; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloWorldController { @RequestMapping("hello/{name}") public String hello(@PathVariable String name) { return name + " say hello"; } }
4.搭建client(客户端)
4.1 application.yml 配置
server: port: 8082 spring: application: name: client eureka: client: serviceUrl: defaultZone: http://localhost:8080/eureka/ app: service-url: http://localhost:8081/
4.2 编写启动程序
package com.services.client; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableDiscoveryClient public class ClientApplication { public static void main(String[] args) { SpringApplication.run(ClientApplication.class, args); } @Bean RestTemplate restTemplate() { return new RestTemplate(); } }
4.3 编写service
package com.services.client; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; @Service public class CallHelloService { @Value("${app.service-url}") private String appServiceUrl; @Autowired private RestTemplate restTemplate; public String callHello(String name) { // 是一个http client ResponseEntity result = restTemplate.postForEntity(appServiceUrl + "hello/" + name, null, String.class); return result.getBody().toString(); } }
4.4 编写controller
package com.services.client; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class CallHelloController { @Autowired private CallHelloService callHelloService; @GetMapping("hello") public String hello(String name) { String result = callHelloService.callHello(name); return result; } }
5.启动registry-service-client模块
5.1 registry
http://localhost:8080/
5.2 service
刷新网页 http://localhost:8080/
5.3 client
刷新网页 http://localhost:8080/
client 访问 http://localhost:8082/hello?name=tao
源码地址: https://github.com/80905949/SpringCloud.git
以上是关于SpringBoot整合Eureka搭建微服务的主要内容,如果未能解决你的问题,请参考以下文章
SpringCloud从入门到进阶——使用SpringBoot搭建微服务
微服务之SpringCloud实战:SpringCloud Eureka服务治理
Java之 Spring Cloud 微服务 Eureka (第一个阶段)SpringBoot项目实现商品服务器端是调用
Java之 Spring Cloud 微服务搭建 Consul(第一个阶段)SpringBoot项目实现商品服务器端是调用