一、简介
本篇文章搭建后的项目结构如下,完成了分布式基础设施之eureka,接口层的搭建,最终实现的结果是会员服务和微信服务注册到eureka,并且会员服务可以成功的调用微信服务。后续的文章将会陆续增加功能,逐渐复杂化。
ok,开始实战吧。
二、创建shop-parent项目(公共parent)
pom类型,用做依赖继承
创建完成后删除src文件夹,pom文件添加pom类型packaging。
再添加一些我们需要用到的依赖,最终pom文件为(后续创建module的话会自动在此pom文件加上module相关信息):
<?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.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.liazhan</groupId>
<artifactId>shop-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>shop-parent</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR3</spring-cloud.version>
</properties>
<dependencies>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--lombok bean简洁化-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--springboot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 集成commons工具类 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<!-- json操作 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.30</version>
</dependency>
<!--测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<!--FTP上传/下载/删除/同步-->
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.3</version>
</dependency>
<!--dom4j读写xml-->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
三、在parent项目基础上创建分布式基础设施Module——shop-basics
使用maven类型创建,也是pom类型的
同样创建完删除src文件夹,pom文件添加pom类型packaging。
四、在shop-basics基础上创建Module——shop-basics-cloud-eureka
maven,这个是jar类型的
创建完成后在pom文件添加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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>shop-basics</artifactId>
<groupId>com.liazhan</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>shop-basics-cloud-eureka</artifactId>
<packaging>jar</packaging>
<dependencies>
<!--eureka server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
在resources文件夹下创建文件application.yml
#服务端口号
server:
port: 8100
#eureka信息配置
eureka:
instance:
#eureka地址
hostname: 127.0.0.1
client:
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
#服务端不需要自己注册自己
register-with-eureka: false
#服务端不需要检索服务
fetch-registry: false
在java文件夹下创建包com.liazhan,在包下创建类EurekaApp
package com.liazhan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/** 分布式基础设施之Eureka
* @version V1.0
* @description:
* @author: Liazhan
* @date: 2020/4/7 1:22
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaApp {
public static void main(String[] args) {
SpringApplication.run(EurekaApp.class,args);
}
}
如此便完成了一小步。
接下来我们需要搭建会员服务和微信服务,通过让会员服务调用微信服务来测试服务通信。
五、搭建会员服务和微信服务
模仿上边basics模块的方式搭建,结果如下图
接下来按文章下面的步骤进行修改,最后的项目结构为:
shop-service-api-weixin创建接口和Bean类,如下:
TestEntity
package com.liazhan.weixin.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/** 测试用实体类
* @version V1.0
* @description:
* @author: Liazhan
* @date: 2020/4/7 21:55
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class TestEntity {
/**
* 姓名
*/
String name;
/**
* 地址
*/
String address;
}
这里的Bean类用了Lombok,不了解的可以百度一下,使用这个需要在idea安装Lombok插件,直接在idea搜索插件安装就可以。
WeiXinService
package com.liazhan.weixin.service;
import com.liazhan.weixin.entity.TestEntity;
import org.springframework.web.bind.annotation.GetMapping;
/**微信服务接口
* @version V1.0
* @description:
* @author: Liazhan
* @date: 2020/4/7 21:59
*/
public interface WeiXinService {
/**
* 测试接口
* @return
*/
@GetMapping("/test")
public TestEntity test();
}
Mapping映射统一在接口层写,不在实现类写。实现类只需要写@RestController就行。
shop-service-impl-weixin添加依赖,创建实现类、入口类和配置文件
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">
<parent>
<artifactId>shop-service-impl</artifactId>
<groupId>com.liazhan</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>shop-service-impl-weixin</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<artifactId>shop-service-api-weixin</artifactId>
<groupId>com.liazhan</groupId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
WeiXinServiceImpl
package com.liazhan.weixin.service.impl;
import com.liazhan.weixin.entity.TestEntity;
import com.liazhan.weixin.service.WeiXinService;
import org.springframework.web.bind.annotation.RestController;
/**
* @version V1.0
* @description:
* @author: Liazhan
* @date: 2020/4/7 23:05
*/
@RestController
public class WeiXinServiceImpl implements WeiXinService {
@Override
public TestEntity test() {
return new TestEntity("Liazhan","GuangZhou");
}
}
AppWeiXin
package com.liazhan.weixin;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**微信服务入口类
* @version V1.0
* @description:
* @author: Liazhan
* @date: 2020/4/8 0:14
*/
@SpringBootApplication
public class AppWeiXin {
public static void main(String[] args) {
SpringApplication.run(AppWeiXin.class,args);
}
}
配置文件application.yml
#服务端口号
server:
port: 8200
#eureka信息配置
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8100/eureka
spring:
application:
name: liazhan-weixin
启动shop-service-impl-weixin入口类,访问http://localhost:8200/test
如此,微信服务接口就搭建成功了。
接下来搭建会员服务接口。
shop-service-api-member添加依赖,创建接口
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">
<parent>
<artifactId>shop-service-api</artifactId>
<groupId>com.liazhan</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>shop-service-api-member</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<artifactId>shop-service-api-weixin</artifactId>
<groupId>com.liazhan</groupId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
MemberService
package com.liazhan.member.service;
import com.liazhan.weixin.entity.TestEntity;
import org.springframework.web.bind.annotation.GetMapping;
/** 会员服务接口
* @version V1.0
* @description:
* @author: Liazhan
* @date: 2020/4/7 23:54
*/
public interface MemberService {
/**
* 调用微信服务测试接口
* @return
*/
@GetMapping("/callWeiXin")
public TestEntity callWeiXin();
}
shop-service-impl-member添加依赖,创建feign接口、实现类、入口类、配置文件
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">
<parent>
<artifactId>shop-service-impl</artifactId>
<groupId>com.liazhan</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>shop-service-impl-member</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<artifactId>shop-service-api-member</artifactId>
<groupId>com.liazhan</groupId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
WeiXinServiceFeign
package com.liazhan.member.feign;
import com.liazhan.weixin.service.WeiXinService;
import org.springframework.cloud.openfeign.FeignClient;
/** 微信服务feign调用
* @version V1.0
* @description:
* @author: Liazhan
* @date: 2020/4/8 0:01
*/
@FeignClient(name = "liazhan-weixin")
public interface WeiXinServiceFeign extends WeiXinService {
}
MemberServiceImpl
package com.liazhan.member.service.impl;
import com.liazhan.member.feign.WeiXinServiceFeign;
import com.liazhan.member.service.MemberService;
import com.liazhan.weixin.entity.TestEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**会员服务接口实现类
* @version V1.0
* @description:
* @author: Liazhan
* @date: 2020/4/8 0:08
*/
@RestController
public class MemberServiceImpl implements MemberService {
@Autowired
private WeiXinServiceFeign weiXinServiceFeign;
@Override
public TestEntity callWeiXin() {
return weiXinServiceFeign.test();
}
}
AppMember
package com.liazhan.member;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**会员服务入口类
* @version V1.0
* @description:
* @author: Liazhan
* @date: 2020/4/8 0:13
*/
@SpringBootApplication
@EnableFeignClients
public class AppMember {
public static void main(String[] args) {
SpringApplication.run(AppMember.class,args);
}
}
application.yml
#服务端口号
server:
port: 8300
#eureka信息配置
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8100/eureka
spring:
application:
name: liazhan-member
启动shop-service-impl-member的入口类,访问http://localhost:8300/callWeiXin
说明会员服务调用微信服务成功了。
github项目地址https://github.com/liazhan/shop-project/tree/0ed56a3b18b4988e249218b2b0138945f65f5554