【Step3】SpringCloud微服务之创建业务模块及Zuul网关

创建File Service模块

这个模块用来存放图片、文件、视频等资源文件

新建Maven Module项目(不知道怎么创建的请参考上一步Step2

修改maven依赖,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>
	<parent>
		<groupId>cn.lannis.cloud</groupId>
		<artifactId>cloud-parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>wevips-FileService</artifactId>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-rest</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<optional>true</optional>
		</dependency>
	</dependencies>
</project>

修改启动类Application.java

代码如下

package cn.xxx.fileService;

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

@SpringBootApplication
@EnableEurekaClient 
public class FileServiceApplication {
	public static void main(String[] args) {
		SpringApplication.run(FileServiceApplication.class, args);
	}
}

【注意】此处使用@EnableEurekaClient注解,表示自己是一个Client服务

修改application.yml文件

内容如下

server:
  port: 8763 #fileService项目的端口号
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/ #指定服务注册中心的地址,即Eureka项目的地址
spring:
  application:
    name: file-service #自定义当前项目注册到服务中心的名称

创建Controller模拟来请求操作

首先创建Image实体类Image.java

package cn.xxx.fileService.entity;

public class Image {
	private Integer id;
	private String title;
	private String url;

	public Image() {
	}

	public Image(int id, String title, String url) {
		this.id = id;
		this.title = title;
		this.url = url;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

}

创建FileController.java

package cn.xxx.fileService.controller;

import java.util.Arrays;
import java.util.List;

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

import cn.xxx.fileService.entity.Image;

@RestController
@RequestMapping("/")
public class FileController {
	@RequestMapping("/images")
	public List<Image> getImages(){
		List<Image> images = Arrays.asList(
				new Image(1, "Treehouse of Horror V", "https://www.imdb.com/title/tt0096697/mediaviewer/rm3842005760"),
				new Image(2, "The Town", "https://www.imdb.com/title/tt0096697/mediaviewer/rm3698134272"),
				new Image(3, "The Last Traction Hero", "https://www.imdb.com/title/tt0096697/mediaviewer/rm1445594112"));
			return images;
	}
}

启动项目

控制台会打印出端口号,如下图

在这里插入图片描述

同时,在Eureka(http://localhost:8761)中可以看到已经把当前项目注册到了Eureka中,如下图所示:

在这里插入图片描述

项目结构如下

在这里插入图片描述

Gallery-service 模块

模块的作用类似于消费者,(本例中)去消费(调用)file-service(获取数据)

创建Gallery-service模块

过程和Image-service模块一样,只需要修改几个地方

pom文件内容不变,和Image-service的一样

修改

修改启动类代码如下:

package cn.wevips.galleryService;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableEurekaClient 
public class GalleryServiceApplication {
	public static void main(String[] args) {
		SpringApplication.run(GalleryServiceApplication.class, args);
	}
	
	@Configuration
	class RestTemplateConfig {
		//创建一个RestTemplate来调用服务
		@Bean
		@LoadBalanced//在不同的实力接口之间负载均衡
		public RestTemplate restTemplate() {
		    return new RestTemplate();
		}
	}
}

新建HomeController.java

package cn.wevips.galleryService.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import cn.wevips.galleryService.entity.Gallery;

@RestController
@RequestMapping("/")
public class HomeController {
	@Autowired
	private RestTemplate restTemplate;
	
	@Autowired
	private Environment env;
	
	@RequestMapping("/")
	public String home() {
		//当我们运行多个端口不同实例相同的Gallery类时可很方便的看出哪一个实例被调用
		return "Hello from Gallery Service running at port: " + env.getProperty("local.server.port");
	}
  
	@RequestMapping("/{id}")
	public Gallery getGallery(@PathVariable final int id) {
		Gallery gallery = new Gallery();
		gallery.setId(id);
		//获取图片资源
		List<Object> images = restTemplate.getForObject("http://file-service/images/", List.class);
		gallery.setImages(images);
		return gallery;
	}
	//这个方法只有拥有‘admin’权限的用户接入,我们在后续进行 详细说明
	@RequestMapping("/admin")
	public String homeAdmin() {
		return "This is the admin area of Gallery service running at port: " + env.getProperty("local.server.port");
	}
}

Gallery实体类

package cn.wevips.galleryService.entity;

import java.util.List;

public class Gallery {
	private Integer id;
	private List<Object> images;
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public List<Object> getImages() {
		return images;
	}

	public void setImages(List<Object> images) {
		this.images = images;
	}
}

项目结构

项目结构

至此,2个子模块,一个资源模块,一个消费模块都已经创建好了,接下来我们在创建一个Ziuul网关模块,好处。。。。自行查百度先

创建Zuul网关

创建过程和上面一样,只需修改相关文件

修改Maven依赖

因为用到Zuul,我们需要引入相关jar包,pom文件内容如下:

<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>
  <parent>
    <groupId>cn.lannis.cloud</groupId>
    <artifactId>cloud-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>wevips-Zuul</artifactId>
  <dependencies>
	 <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
     </dependency>
     <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-zuul</artifactId>
     </dependency>
     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-devtools</artifactId>
         <optional>true</optional>
     </dependency>
</dependencies>
</project>

修改启动类ZuulApplication.java

package cn.wevips.zuul;

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
@EnableEurekaClient 		
@EnableZuulProxy		// 启用Zuul
public class ZuulApplication {
	public static void main(String[] args) {
		SpringApplication.run(ZuulApplication.class, args);
	}
}

修改application.yml文件

server:
  port: 8762
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
spring:
  application:
    name: zuul-server
zuul: 
  ignored-services: '*'
  routes:
    gallery-service:
      path: /gallery/**
      serviceId: gallery-service

项目结构如

项目结构图

测试项目

依次启动Image-Service,Gallery-Service,Zuul-Service,启动成功后可以在Eureka注册中心看到,如下图:

在这里插入图片描述

浏览器输入localhost:8762/gallery/,出现如下图所示的内容

在这里插入图片描述

浏览器输入 http://localhost:8762/gallery/1,出现如下图所示的内容

在这里插入图片描述

完成

过程稍微多(只是图片多而已),但是总体上来说目前呢还是有点简单的,后面我们会在zuul网关层添加权限校验,及添加其它模块;有什么问题在下方留言哦~

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

疯狂拧螺丝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值