SpringCloud实战与原理---快速入门

SpringCloud实战与原理


第一章  快速入门

1. 初始化工程

2. 启动Eureka注册中心

2.1 Eureka注册中心启动代码

2.2 启动Eureka注册中心

3. 注册Eureka客户端

3.1 Eureka客户端代码

3.2 注册Eureka客户端

4. 服务之间的调用

4.1 Feign调用代码

4.2 Feign调用结果

5. 路由网关ZUUL

5.1 路由网关ZUUL代码

5.2 启动路由网关ZUUL


 

第一章  快速入门

本章主要目的:搭建一个可使用的SpringClooud工程,对各模块的功能后续会进行一一分析

1. 初始化工程

用Idea的Spring Initializr新建一个maven工程,pom文件如下

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>eng.lab</groupId>
    <artifactId>springcloudsearch</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springcloudsearch</name>
    <description>Demo project for Spring Boot</description>
    <packaging>pom</packaging>
    <modules>
        <module>eureka-server</module>
    </modules>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR1</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2. 启动Eureka注册中心

2.1 Eureka注册中心启动代码

在idea新建Maven子模块eureka-server

pom文件如下

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>eng.lab</groupId>
        <artifactId>springcloudsearch</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>eng.lab</groupId>
    <artifactId>eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-server</name>
    <description>Demo project for Spring Boot</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

并在启动类中用@EnableEurekaServer启动Eureka注册中心

package eng.lab.eurekaserver;

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

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }

}

在application.yml中配置相关属性

server:
  port: 8888
eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

2.2 启动Eureka注册中心

启动服务成功后,用浏览器打开http://localhost:8888/,展示如下网页则说明Eureka注册中心启动成功。

3. 注册Eureka客户端

3.1 Eureka客户端代码

在idea新建Maven子模块eureka-client-a

pom文件如下

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>eng.lab</groupId>
        <artifactId>springcloudsearch</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>eng.lab</groupId>
    <artifactId>eureka-client-a</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-client-a</name>
    <description>Demo project for Spring Boot</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

</project>

并在启动类中用@EnableEurekaClient启动Eureka客户端

package eng.lab.eurekaclientaa;

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

@EnableEurekaClient
@SpringBootApplication
public class EurekaClientAApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaClientAApplication.class, args);
    }

}

新增测试接口用于调用

package eng.lab.eurekaclientaa.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: Wen-Xueliang
 * @Date: Created in 2020/3/5 22:12
 * @Description:
 */
@RestController
public class TestController {

    @GetMapping("/print")
    public void print() {
        System.out.println("This is A system!");
    }
   

    @GetMapping("/show")
    public String show() {
        return "This is A system!";
    }
}

在application.yml中配置相关属性

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8888/eureka/
server:
  port: 8889
spring:
  application:
    name: eureka-client-a

3.2 注册Eureka客户端

在浏览器中刷新http://localhost:8888/页面,此时出现被注册的服务EUREKA-CLIENT-A

用浏览器打开http://localhost:8889/show,则可直接调用该服务

4. 服务之间的调用

4.1 Feign调用代码

在idea新建Maven子模块eureka-client-b

pom文件如下

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>eng.lab</groupId>
        <artifactId>springcloudsearch</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>eng.lab</groupId>
    <artifactId>eureka-client-b</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-client-b</name>
    <description>Demo project for Spring Boot</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

    </dependencies>
</project>

并在启动类中用@EnableEurekaClient启动Eureka客户端,@EnableFeignClients启动Feign客户端。

package eng.lab.eurekaclientb;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;


@EnableEurekaClient
@EnableFeignClients
@SpringBootApplication
public class EurekaClientBApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaClientBApplication.class, args);
    }

}

新增服务用于调用eureka-client-a

package eng.lab.eurekaclientb.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * @Author: Wen-Xueliang
 * @Date: Created in 2020/3/6 15:00
 * @Description:
 */
@Service
@FeignClient("eureka-client-a")
public interface TestService {

    @GetMapping("/show")
    public String show();
}

新增测试接口用于调用

package eng.lab.eurekaclientb.controller;

import eng.lab.eurekaclientb.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: Wen-Xueliang
 * @Date: Created in 2020/3/6 15:03
 * @Description:
 */
@RestController
public class InvokeController {

    @Autowired
    private TestService testService;

    @GetMapping("/invoke")
    public String invoke() {
        return testService.show();
    }
}

在application.yml中配置相关属性

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8888/eureka/
server:
  port: 8890
spring:
  application:
    name: eureka-client-b

4.2 Feign调用结果

在浏览器中刷新http://localhost:8888/页面,此时出现被新注册的服务EUREKA-CLIENT-B

用浏览器打开http://localhost:8890/invoke,则可调用服务EUREKA-CLIENT-B,令其调用服务EUREKA-CLIENT-A。

5. 路由网关ZUUL

5.1 路由网关ZUUL代码

在idea新建Maven子模块zuul

pom文件如下

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>eng.lab</groupId>
        <artifactId>springcloudsearch</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>eng.lab</groupId>
    <artifactId>zuul</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>zuul</name>
    <description>Demo project for Spring Boot</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>
    </dependencies>
</project>

并在启动类中用@EnableZuulProxy启动Zuul。

package eng.lab.zuul;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@EnableZuulProxy
@SpringBootApplication
public class ZuulApplication {

    public static void main(String[] args) {
        SpringApplication.run(ZuulApplication.class, args);
    }

}

在application.yml中配置相关属性

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8888/eureka/
server:
  port: 8000
spring:
  application:
    name: zuul
zuul:
  routes:
    route-name:
      url: http://localhost:8889/
      path: /a/**

5.2 启动路由网关ZUUL

用浏览器打开http://localhost:8000/a/show,则可直接通过网关调用eureka-client-a服务

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值