引言
在上一节《淘东电商项目(03) - 父类配置》主要描述了「淘东」项目的父类公共内容配置。
代码已提交至Github(版本号:
af6eed34c0068f674312b9de6c8d7b6ef60b2f5b
),有兴趣的同学可以下载来看看: https://github.com/ylw-github/taodong-shop
本文将开始讲解Eureka注册中心的配置以及微服务间的Feign相互调用。
本文目录结构:
l____引言
l____ 1. Eureka注册中心配置
l________1.1 配置maven
l________1.2 application.yml配置
l________1.3 启动类
l____ 2. 微信服务配置
l________2.1 配置maven
l________2.2 application.yml配置
l________2.3 启动类
l____ 3. 会员服务配置
l________3.1 配置maven
l________3.2 application.yml配置
l________3.3 启动类
l____ 4. 会员服务Feign远程调用微信服务
l________4.1 微信服务接口配置
l____________4.1.1 定义实体类
l____________4.1.2 定义接口
l________4.2 会员服务接口配置
l____________4.2.1 定义会员调用微信接口
l________4.3 会员服务Feign调用维信服务
l____________4.3.1 Service父模块添加Feign依赖
l____________4.3.2 微信调用接口实现
l____________4.3.3 会员调用微信接口实现
l____ 5. 测试
l____总结
1.Eureka注册中心配置
模块定位:配置基础设施下(taodong-shop-basics)的注册中心模块(taodong-shop-basics-eureka)。
1.1 配置maven
<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>com.ylw.taodong</groupId>
<artifactId>taodong-shop-basics</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>taodong-shop-basics-eureka</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
1.2 application.yml配置
###服务端口号
server:
port: 8100
###eureka 基本信息配置
eureka:
instance:
###注册到eurekaip地址
hostname: 127.0.0.1
client:
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
###因为自己是为注册中心,不需要自己注册自己
register-with-eureka: false
###因为自己是为注册中心,不需要检索服务
fetch-registry: false
1.3 启动类
@SpringBootApplication
@EnableEurekaServer
public class AppEureka {
public static void main(String[] args) {
SpringApplication.run(AppEureka.class, args);
}
}
2.微信服务配置
模块定位:接口实现层下(taodong-shop-service)的微信服务模块(taodong-shop-service-weixin)。
2.1 配置maven
<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>com.ylw.taodong</groupId>
<artifactId>taodong-shop-service</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>taodong-shop-service-weixin</artifactId>
<dependencies>
<dependency>
<groupId>com.ylw.taodong</groupId>
<artifactId>taodong-shop-service-api-weixin</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
2.2 application.yml配置
###服务启动端口号
server:
port: 8200
###服务名称(服务注册到eureka名称)
spring:
application:
name: taodong-shop-service-weixin
###服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:8100/eureka
2.3 启动类
@SpringBootApplication
@EnableEurekaClient
public class AppWeiXin {
public static void main(String[] args) {
SpringApplication.run(AppWeiXin.class, args);
}
}
3.会员服务配置
模块定位:接口实现层下(taodong-shop-service)的会员服务模块(taodong-shop-service-member)。
3.1 配置maven
<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>com.ylw.taodong</groupId>
<artifactId>taodong-shop-service</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>taodong-shop-service-member</artifactId>
<dependencies>
<dependency>
<groupId>com.ylw.taodong</groupId>
<artifactId>taodong-shop-service-api-member</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
3.2 application.yml配置
###服务启动端口号
server:
port: 8300
###服务名称(服务注册到eureka名称)
spring:
application:
name: taodong-shop-service-member
###服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:8100/eureka
3.3 启动类
@SpringBootApplication
@EnableEurekaClient
public class AppMember {
public static void main(String[] args) {
SpringApplication.run(AppMember.class, args);
}
}
4.会员服务Feign远程调用微信服务
4.1 微信服务接口配置
模块定位:接口层下(taodong-shop-service-api)的微信服务模块(taodong-shop-service-api-weixin)。
4.1.1 定义实体类
package com.ylw.taodong.entity;
import lombok.Data;
@Data
public class AppEntity {
/**
* 应用id
*/
private String appId;
/**
* 应用密钥
*/
private String appSecret;
public AppEntity() {
}
public AppEntity(String appId, String appSecret) {
super();
this.appId = appId;
this.appSecret = appSecret;
}
}
4.1.2 定义接口
模块定位:接口层下(taodong-shop-service-api)的微信服务模块(taodong-shop-service-api-member)。
package com.ylw.taodong.service;
import com.ylw.taodong.entity.AppEntity;
import org.springframework.web.bind.annotation.GetMapping;
public interface AppService {
/**
* 获取app应用信息
*
* @return
*/
@GetMapping("/getApp")
public AppEntity getApp();
}
4.2 会员服务接口配置
4.2.1 定义会员调用微信接口
1.添加微信接口模块依赖:
<dependencies>
<dependency>
<groupId>com.ylw.taodong</groupId>
<artifactId>taodong-shop-service-api-weixin</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
2.调用微信服务接口:
public interface MemberService {
@GetMapping("/memberInvokWeixin")
public AppEntity memberInvokWeixin();
}
4.3 会员服务调用微信服务
模块定位:接口实现层下(taodong-shop-service)的微信服务模块(taodong-shop-service-weixin)和会员服务(taodong-shop-service-member)。
4.3.1 Service父模块添加Feign依赖
接口实现层(taodong-shop-service)添加maven依赖:
<dependencies>
<!-- springcloud feign组件 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
4.3.2 微信被调用接口实现
@RestController
public class AppServiceImpl implements AppService {
@GetMapping("/getApp")
public AppEntity getApp() {
return new AppEntity("1962264482", "ylw666");
}
}
4.3.3 会员调用微信接口实现
1.定义feign接口:
@FeignClient(name = "taodong-shop-service-weixin")
public interface AppServiceFeign extends AppService {
}
2.调用微信接口:
@RestController
public class MemberServiceImpl implements MemberService {
@Autowired
private AppServiceFeign appServiceFeign;
@GetMapping("/memberInvokWeixin")
public AppEntity memberInvokWeixin() {
return appServiceFeign.getApp();
}
}
3.注意:会员启动类AppMember要加上@EnableFeignClients
注解
5.测试
1.运行AppEureka,启动注册中心
2.运行AppMember、AppWeiXin,会员服务和微信服务(同上)
3.浏览器输入http://localhost:8100/,可以看到,微信服务和会员服务都注册登录上了。
4.会员服务调用微信服务,浏览器输入:http://localhost:8300/memberInvokWeixin,可以看到远程调用Feign服务成功。