IDEA 中构建注册和服务发现中心(一)
2017-08-11 16:36
537 查看
IDEA 中构建Spring Cloud注册和服务发现中心(一)
本文主要是利用IDEA构建Spring Cloud注册和服务发现中心:IDEA 中构建Spring Cloud注册和服务发现中心一
本文中涉及的Spring Cloud架构节点图
创建主工程cloud
创建服务注册发现组件
创建路由网关组件
创建一个服务提供者
启动顺序
输入测试地址
本文中涉及的Spring Cloud架构节点图
服务注册中心详解
创建主工程cloud
1,IDEA 中new 一个普通的Maven 工程,包名com.spring,模块命名为cloud2,在项目的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.spring</groupId> <artifactId>cloud</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>discovery</module> <module>gateway</module> <module>service</module> </modules> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.5.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR6</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</group 4000 Id> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix-eureka-server</artifactId> <version>1.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
创建服务注册发现组件
1,在cloud项目上右击,新建new一个Module,普通的Maven,ArtifactId 为:discovery,点击next ,Content root : discovery2,在src.main.java下创建一个文件夹discovery,并创建一个服务启动类DiscoveryApplicaion.java
package discovery; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class DiscoveryApplicaion { public static void main(String[] args) { SpringApplication.run(DiscoveryApplicaion.class, args); } }
3, 在resource文件夹下创建配置文件:application.yml
server: port: 8081 eureka: instance: hostname: localhost client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/[/code]创建路由网关组件
1,在cloud项目上右击,新建new一个Module,普通的Maven,ArtifactId 为:geteway,点击next ,Content root : geteway
2,在src.main.java下创建一个文件夹geteway,并创建一个服务启动类GatewayApplication.javapackage geteway; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @SpringBootApplication @EnableZuulProxy @EnableEurekaClient public class GatewayApplication { public static void main(String[] args) { SpringApplication.run(GatewayApplication.class, args); } }
3,在resource文件夹下创建配置文件:application.ymleureka: client: serviceUrl: defaultZone: http://localhost:8081/eureka/ spring: application: name: gateway server: port: 8084 zuul: routes: service1: /service1/**创建一个服务提供者
1,在cloud项目上右击,新建new一个Module,普通的Maven,ArtifactId 为:service1,点击next ,Content root : service1
2,在src.main.java下创建一个文件夹service1,并创建一个服务启动Service1Application.javapackage service1; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @EnableDiscoveryClient @RestController @RequestMapping("service1") public class Service1Application { @GetMapping("/test") public String service(){ System.out.println("asdfafdas"); return "service1"; } @GetMapping("/hi") public String hi(@RequestParam(value = "id") String id){ System.out.println("asdfafdas"); return "id === " + id; } public static void main(String[] args) { SpringApplication.run(ServiceApplication.class, args); } }
3,在resource文件夹下创建配置文件:application.ymlspring: application: name: service1 eureka: client: serviceUrl: defaultZone: http://localhost:8081/eureka/ server: port: 8082启动顺序
1,首先启动 discovery服务,后面依次启动gateway,service1服务
2,启动方式,进入配置主类,运行main方法即可。输入测试地址
http://localhost:8081
http://localhost:8082/service1/test
http://localhost:8084/service1/service1/test
相关文章推荐
- Spring Cloud构建微服务架构(六)高可用服务注册中心
- SpringCloud构建微服务入门架构(二)服务注册与发现Eureka集群搭建
- Spring Cloud构建微服务架构服务注册与发现
- Spring Cloud构建微服务架构(六)高可用服务注册中心
- Spring Cloud构建微服务架构—创建“服务注册中心”
- Spring Cloud构建微服务架构(一)服务注册与发现
- Spring Cloud构建微服务架构(一)服务注册与发现
- Spring Cloud构建微服务架构(一)服务注册与发现
- Spring Cloud构建微服务架构(一)服务注册与发现
- Spring Cloud构建微服务架构(一)服务注册与发现
- Spring Cloud构建微服务架构:服务注册与发现(Eureka、Consul)【Dalston版】
- Spring Cloud构建微服务架构:服务注册与发现(Eureka、Consul)
- springcloud-1.使用Eureka -robbon构建自动服务发现注册集群和消费者
- Spring Cloud构建微服务架构:服务注册与发现(Eureka、Consul)【Dalston版】
- Spring Cloud构建微服务架构(一)服务注册与发现
- Spring Cloud Consul使用——服务注册与发现(注册中心)
- SpringCloud服务注册与发现(Eureka 注册中心)
- Spring Cloud构建微服务架构(六)高可用服务注册中心
- Spring Cloud构建微服务架构(六)高可用服务注册中心
- Spring Cloud构建微服务架构(六)高可用服务注册中心