Spring cloud 学习(一)

参考网址:

http://blog.csdn.net/lc0817/article/details/53266212

环境:JDK1.8

工具:Eclipse

1.新建Maven Project

image

 

image

 

image

 

pom.xml:

 

<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>springcloudtest</groupId>
  <artifactId>springcloudtest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
  <modules>
      <module>discovery</module>
      <module>service0</module>
      <module>service1</module>
      <module>gateway</module>
  </modules>
 
   <!--以下dependency来自官方--> 
    <parent> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-starter-parent</artifactId> 
        <version>1.4.0.RELEASE</version> 
    </parent> 
 
    <dependencyManagement> 
        <dependencies> 
            <dependency> 
                <groupId>org.springframework.cloud</groupId> 
                <artifactId>spring-cloud-dependencies</artifactId> 
                <version>Camden.SR2</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-starter-eureka-server</artifactId>
        </dependency>
        <dependency> 
            <groupId>org.springframework.boot</groupId> 
            <artifactId>spring-boot-devtools</artifactId> 
            <optional>true</optional> 
        </dependency> 
       
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zuul</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies> 
</project>

 

2.新建Maven Module discovery

image

image

 

image

 

新建一个包:

cn.demo.discovery

 

在包里面新建一个Class:Discovery,代码如下:

package cn.demo.discovery;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class Discovery {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SpringApplication.run(Discovery.class, args);

    }

}

 

image

 

在resources目录下新建一个配置文件application.yml(注意yml格式与properties文件不同):

image

 

配置信息如下(注意排版):

 

server:
           port: 8080 #注册中心占用8080端口,冒号后面必须要有空格
      eureka:
          instance:
              hostname: localhost #冒号后面必须要有空格
          client:
              registerWithEureka: false #冒号后面必须要有空格
              fetchRegistry: false #冒号后面必须要有空格
              serviceUrl:
                 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/  #其实就是 http://localhost:8080/eureka/,冒号后面必须要有空格

 

image

 

运行项目:

image

 

控制台输出信息:

 

image

 

浏览网页,http://localhost:8080/

 

image

 

3.新建Maven Module service0

在springcloudtest项目中添加maven模块

image

 

image

 

image

 

新建一个Class:Service0,代码如下:

package service0;

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
public class Service0 {
   
    @GetMapping("/service0")
    public String service0(){
        return "service0";
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SpringApplication.run(Service0.class, args);
    }

}

 

在resources目录下新建一个配置文件application.yml(注意yml格式与properties文件不同):

 

spring:
          application:
             name: service0 #冒号后面必须要有空格
       eureka:
           client:
             serviceUrl:
                defaultZone: http://localhost:8080/eureka/ #冒号后面必须要有空格
#  instance:
#    hostname: localhost #冒号后面必须要有空格
#    instance-id: http://localhost:8081 #冒号后面必须要有空格
      server:
             port: 8081 #冒号后面必须要有空格

 

image

 

先运行discovery项目,再运行本项目(同上):

image

 

浏览服务器,http://localhost:8080/,可以看到service0:

 

image

 

4.新建Maven Module service1

操作方法同service0

 

Service1.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
public class Service1 {

    @GetMapping("/service1")
    public String service1(){
        return "service1";
    }
   
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SpringApplication.run(Service1.class, args);
    }

}

 

 

配置文件application.yml,配置信息如下:

 

spring:
          application:
              name: service1 #冒号后面必须要有空格
      eureka:
          client:
             serviceUrl:
                defaultZone: http://localhost:8080/eureka/ #冒号后面必须要有空格
#  instance:
#    hostname: localhost #冒号后面必须要有空格
#    instance-id: http://localhost:8082 #冒号后面必须要有空格
      server:
             port: 8082 #冒号后面必须要有空格

 

 

image

先运行discovery项目,再运行service0和service1(同上)。

 

浏览服务器,http://localhost:8080/,可以看到service0和service1:

image

4.新建Maven Module gateway(网关)

image

 

image

新建一个包cn.demo.gateway:

image

 

新建一个Class:Gatewa,代码如下:

package cn.demo.gateway;

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 Gateway {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SpringApplication.run(Gateway.class, args);
    }

}

 

配置文件application.yml,配置信息如下:


eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8080/eureka/
spring:
  application:
    name: gateway
server:
  port: 8083
zuul:
  routes:
    service0: /service/0/**
    service1: /service/1/**

 

image

 

首先启动Discovery,其次的服务和网关启动顺序随意.通过访问http://localhost:8083/service0/service0/,即可看到,gateway帮我们转发了请求.

image

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值