您的位置:首页 > 编程语言 > Java开发

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,模块命名为cloud

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>

<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 : discovery

2,在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.java

package 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.yml

eureka:
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.java

package 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.yml

spring:
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 idea spring