服务消费者整合 OpenFeign ===> 启动类中要添加 @EnableFeignClients 注解
package com.llq.springcloud;
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableDiscoveryClient // 启用服务发现
@EnableFeignClients
public class MemberNacosConsumerApplication81 {
public static void main(String[] args) {
ApplicationContext ac = SpringApplication.run(MemberNacosConsumerApplication81.class, args);
}
}
架构图:
springcloud
├─ MemberNacosConsumerApplication81.java
├─ config
│ ├─ CustomizationBean.java
│ └─ RibbonRule.java
├─ controller
│ └─ MemberNacosConsumerController.java
└─ service
└─ MemberOpenFeignService.java # member-service-nacos-consumer-81 消费者整合 openFeign
member-service-nacos-consumer-81 消费者整合 openFeign
package com.llq.springcloud.service;
@FeignClient(value = "member-service-nacos-provider") // value:服务名称
public interface MemberOpenFeignService {
/**
* 解读
* 1. 远程调用方式是 get
* 2. 远程调用的 url 是 http://member-service-nacos-provider/member/get/{id}
* 3. member-service-nacos-provider 是 nacos 注册中心服务名
* 4. open-feign 会根据负载均衡算法来决定调用的是 10004 / 10006
* 5. OpenFeign 通过接口方式调用服务
*/
@GetMapping(value = "/member/get/{id}")
public Result<Member> getMembfeById(@PathVariable("id") Long id);
}
服务 10004【服务名字 member-service-nacos-provider:服务名相同的构成集群】
# 注意格式:如果没有缩进的话,SpringBoot项目起不来!!!
server:
port: 10004
spring:
application:
name: member-service-nacos-provider #名称,可以自己指定
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/e_commerce_center_db?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 352420kobe24llq
# 配置 nacos
cloud:
nacos:
discovery:
server-addr: localhost:8848
# 配置 sentinel
# sentinel:
# transport:
# dashboard: localhost:9999
# port: 8888
# 配置暴露所有监控点
#management:
# endpoints:
# web:
# exposure:
# include: '*'
mybatis:
mapperLocations: classpath:mapper/*.xml #后面mapper 文件的位置
type-aliases-package: com.llq.springcloud.entity # 实体类的包路径
调用的方法
@GetMapping("/member/get/{id}")
public Result getMemberById(@PathVariable("id") Long id, HttpServletRequest request) {
Member member = memberService.queryMemberById(id);
// try {
// Thread.sleep(1000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
System.out.println("当前线程id 为:" + Thread.currentThread().getId());
log.info("查询结果= " + member);
if (member != null) {
return Result.success("查询成功 member-service-nacos-provider-10004", member);
} else {
return Result.error("402", "ID= " + id + " 不存在");
}
}
服务 10006【服务名字 member-service-nacos-provider:服务名相同的构成集群】
# 注意格式:如果没有缩进的话,SpringBoot项目起不来!!!
server:
port: 10006
spring:
application:
name: member-service-nacos-provider #名称,可以自己指定
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/e_commerce_center_db?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 352420kobe24llq
# 配置 nacos
cloud:
nacos:
discovery:
server-addr: localhost:8848
# 配置暴露所有监控点
#management:
# endpoints:
# web:
# exposure:
# include: '*'
mybatis:
mapperLocations: classpath:mapper/*.xml #后面mapper 文件的位置
type-aliases-package: com.llq.springcloud.entity # 实体类的包路径
调用方法
@GetMapping(value = "/member/get/{id}")
public Result getMemberById(@PathVariable("id") Long id) {
Member member = memberService.queryMemberById(id);
log.info("查询结果= " + member);
if (member != null) {
return Result.success("查询成功 member-service-nacos-provider-10006", member);
} else {
return Result.error("402", "ID= " + id + " 不存在");
}
}
标签:service,OpenFeign,EnableFeignClients,nacos,member,Result,类中要,provider,id
From: https://www.cnblogs.com/aclq/p/17894131.html