IDEA 中构建注册和服务发现中心(一)

IDEA 中构建Spring Cloud注册和服务发现中心(一)

本文主要是利用IDEA构建Spring 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</groupId>
            <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/

创建路由网关组件

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


  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值