【Spring Cloud】--SpringCloud的服务注册与发现【Eureka】
2017-12-19 11:18
1051 查看
spring cloud 提供了类似于zookeeper的服务注册中与管理中心Eureka。
spring cloud的服务与注册中心与其他的注册中心结构类似:
1,注册中心:
不同于zookeeper的安装形式,spring cloud 的注册中心需要写一个单独的工程来实现:
项目结构:
实现注册中心只需要三个文件即可:
RegApplication.java
application.properties
pom.xml
首先引入需要的包:
RegApplication.java
注册中心配置:application.properties
验证:访问http://localhost:1111/ 出现Eureka配置中心界面【看见你就知道】
服务提供者:
服务的提供者需要四个文件,项目结构如下:
引入包的pom.xml
HelloController.java
ServerApplication.java
配置application.properties
验证:访问:http://localhost:8080/hello 返回 Hello World Server!
服务的消费者:
项目结构:
服务的消费者同样需要四个文件即可:
引入包:pom.xml
ConsumerController.java
ConsumerApplication
注册中心配置: application.properties
spring cloud的服务与注册中心与其他的注册中心结构类似:
1,注册中心:
不同于zookeeper的安装形式,spring cloud 的注册中心需要写一个单独的工程来实现:
项目结构:
实现注册中心只需要三个文件即可:
RegApplication.java
application.properties
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>spring-cloud-register</groupId> <artifactId>spring-cloud-register</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.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</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> <!-- 加入监控开始 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- 加入监控结束 --> <!-- 增加Eureka开始 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <!-- 增加Eureka结束 --> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <dependencyManagement> 4000 <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR6</version> <!-- Dalston.SR1 --> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project>
RegApplication.java
import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; /** * Created by wangpengzhi1 on 2017/12/19. */ @EnableEurekaServer @SpringBootApplication public class RegApplication { public static void main(String[] args){ new SpringApplicationBuilder(RegApplication.class).web(true).run(args); } }
注册中心配置: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/
验证:访问http://localhost:1111/ 出现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>spring-eureka-server</groupId> <artifactId>spring-eureka-server</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.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</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> <!-- 加入监控开始 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- 加入监控结束 --> <!-- 增加Eureka开始 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <!-- 增加Eureka结束 --> </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>Camden.SR6</version> <!-- Dalston.SR1 --> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project>
HelloController.java
package www.jd.com; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * Created by wangpengzhi1 on 2017/12/19. */ @RestController public class HelloController { private final Logger logger = Logger.getLogger(HelloController.class); @Autowired private DiscoveryClient client; @RequestMapping public String index(){ ServiceInstance instance = client.getLocalServiceInstance(); logger.info("host:"+instance.getHost()+instance.getServiceId()+":"+instance.getPort()); return "Hello world server!"; } }
ServerApplication.java
package www.jd.com; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; /** * Created by wangpengzhi1 on 2017/12/19. */ @EnableDiscoveryClient @SpringBootApplication public class ServerApplication { public static void main(String[] args){ SpringApplication.run(ServerApplication.class, args); } }
配置application.properties
spring.application.name=hello-service eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
验证:访问:http://localhost:8080/hello 返回 Hello World Server!
服务的消费者:
项目结构:
服务的消费者同样需要四个文件即可:
引入包: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>spring-eureka-client</groupId> <artifactId>spring-eureka-client</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.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</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> <!-- 加入监控开始 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- 加入监控结束 --> <!-- 增加Eureka开始 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <!-- 增加Eureka结束 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</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>Camden.SR6</version> <!-- Dalston.SR1 --> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project>
ConsumerController.java
package www.jd.com; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; /** * Created by wangpengzhi1 on 2017/12/19. */ @RestController public class ConsumerController { @Autowired RestTemplate restTemplate; @RequestMapping(value = "/consumer", method = RequestMethod.GET) public String helloConsumer(){ return restTemplate.getForEntity("http://HELLO-SERVICE/hello",String.class).getBody(); } }
ConsumerApplication
package www.jd.com; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; /** * Created by wangpengzhi1 on 2017/12/19. */ @RestController public class ConsumerController { @Autowired RestTemplate restTemplate; @RequestMapping(value = "/consumer", method = RequestMethod.GET) public String helloConsumer(){ return restTemplate.getForEntity("http://HELLO-SERVICE/hello",String.class).getBody(); } }
注册中心配置: application.properties
spring.application.name=ribbon-consumer server.port=9000 eureka.client.serviceUrl.defaultZone = http://localhost:1111/eureka/[/code]
验证:访问:http://localhost:9000/consumer 返回 Hello World Server!
相关文章推荐
- springcloud(第一篇)springcloud eureka 服务注册与发现
- springcloud(第三篇)springcloud eureka 服务注册与发现 *****
- springcloud(第三篇)springcloud eureka 服务注册与发现
- 《Spring Cloud Netflix》 -- 服务注册和服务发现-Eureka 的使用
- Spring Cloud Spring Boot mybatis 企业分布式微服务云(一)服务注册与发现(Eureka)【Dalston版】
- SpringCloud--Eureka服务注册和发现
- Spring Cloud Eureka服务注册与发现
- Spring Cloud Eureka 服务注册与发现中心(一)
- SpringCloud(3)---Eureka服务注册与发现
- Spring-Cloud-Eureka服务注册发现中心server+client案列模拟说明
- 微服务架构 SpringCloud(二)Eureka(服务注册和服务发现基础篇)
- Spring Cloud:服务注册与发现(Eureka)【Dalston版】
- springcloud eureka服务注册与发现
- SpringCloud : 服务的注册与发现(Eureka)
- 每天学点SpringCloud(二):服务注册与发现Eureka
- Spring cloud Eureka服务注册及发现(一)创建Eurake服务器
- Spring Cloud构建微服务架构:Eureka服务注册与发现
- 《Spring Cloud Netflix》 -- 服务注册和服务发现-Eureka的常用配置
- 3. 服务注册与发现之server springcloud-eureka-server
- SpringCloud服务的注册与发现(Eureka)