回顾

【Java】Spring Cloud学习笔记01中我们学习了如何在Spring Cloud中创建服务注册中心,下面创建新的服务注册到注册中心。

服务提供者

1、在springcloud工程下新建一个Maven类型的module:

<span class='yzk_title_1425'>【Java】Spring Cloud学习笔记02</span>-Maxchen个人博客

<span class='yzk_title_1425'>【Java】Spring Cloud学习笔记02</span>-Maxchen个人博客

2、接着在pom.xml中添加如下依赖:

<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-netflix-hystrix</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

3、接着在resources目录下创建application.yml并指定该服务的注册中心:

<span class='yzk_title_1425'>【Java】Spring Cloud学习笔记02</span>-Maxchen个人博客

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8762
spring:
  application:
    name: eurekaclient

4、最后创建服务启动类Application并启动应用:

import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;

@SpringCloudApplication
public class Application {

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

可以看到服务已经被注册到注册中心了:

<span class='yzk_title_1425'>【Java】Spring Cloud学习笔记02</span>-Maxchen个人博客

微服务的网关功能

微服务的网关功能也称中转站,通过它提供的接口,可以转发不同的服务。Spring Cloud 提供了这样一个解决方案,那就是 zuul,它的作用就是进行路由转发、异常处理和过滤拦截。下面,我将演示如果使用 zuul 创建一个服务网关。

1、在eurekaclient工程下创建一个HelloController 类,添加内容如下:

@RestController
public class HelloController {

    @Value("${server.port}")
    private int port;

    @RequestMapping("index")
    public String index() {
        return "Hello World!,端口:" + port;
    }
}

之后输入地址http://localhost:8762/index可访问到页面:

<span class='yzk_title_1425'>【Java】Spring Cloud学习笔记02</span>-Maxchen个人博客

2、在springcloud工程下新建一个Maven类型的module取名为eurekagateway:

<span class='yzk_title_1425'>【Java】Spring Cloud学习笔记02</span>-Maxchen个人博客

3、在eurekagateway中添加以下依赖:

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

4、接着创建application.yml文件,将端口号配置为8080,spring.cloud.gateway.discovery.locator.enabled表示是否与服务发现组件(register)进行结合,eureka.client.serviceUrl.defaultZone 的作用是指定注册中心的地址。

server:
  port: 8080
spring:
  application:
    name: eurekagateway
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
logging:
  level:
    org.springframework.cloud.gateway: trace
    org.springframework.http.server.reactive: debug
    org.springframework.web.reactive: debug
    reactor.ipc.netty: debug
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

5、最后创建应用启动类ApplicationGateway启用服务网关微服务:

@SpringCloudApplication
public class ApplicationGateway {

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

最后浏览器输入地址http://localhost:8080/EUREKACLIENT/index能够中转到http://localhost:8762/index当中的内容:

<span class='yzk_title_1425'>【Java】Spring Cloud学习笔记02</span>-Maxchen个人博客<span class='yzk_title_1425'>【Java】Spring Cloud学习笔记02</span>-Maxchen个人博客

 

记录你的想法-丰富你的知识
  • <span class='yzk_title_1668'>【Java】Spring Cloud学习笔记05</span>
    【Java】Spring Cloud学习笔记05
  • <span class='yzk_title_1601'>【Java】Spring Cloud学习笔记04</span>
    【Java】Spring Cloud学习笔记04
  • <span class='yzk_title_1492'>【Java】Spring Cloud学习笔记03</span>
    【Java】Spring Cloud学习笔记03
  • <span class='yzk_title_1416'>【Java】Spring Cloud学习笔记01</span>
    【Java】Spring Cloud学习笔记01
  • <span class='yzk_title_1402'>【Java】Spring boot学习笔记05</span>
    【Java】Spring boot学习笔记05
  • <span class='yzk_title_1394'>【Java】Spring boot学习笔记04</span>
    【Java】Spring boot学习笔记04
发表评论

1 条评论