openfeign整合完成
This commit is contained in:
parent
28d1906cf1
commit
fdbe9cbe62
@ -18,6 +18,15 @@
|
|||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
</properties>
|
</properties>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
|
||||||
|
<version>2.2.10.RELEASE</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.cloud</groupId>
|
<groupId>org.springframework.cloud</groupId>
|
||||||
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
|
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
|
||||||
@ -30,11 +39,6 @@
|
|||||||
<groupId>org.mybatis.spring.boot</groupId>
|
<groupId>org.mybatis.spring.boot</groupId>
|
||||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>org.example</groupId>
|
|
||||||
<artifactId>commons</artifactId>
|
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.example</groupId>
|
<groupId>org.example</groupId>
|
||||||
<artifactId>commons</artifactId>
|
<artifactId>commons</artifactId>
|
||||||
|
@ -4,6 +4,7 @@ import org.springframework.boot.SpringApplication;
|
|||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||||
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
|
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
|
||||||
|
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ClassName: BorrowApplication
|
* ClassName: BorrowApplication
|
||||||
@ -13,7 +14,7 @@ import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
|
|||||||
* @Create 2023/8/14 16:25
|
* @Create 2023/8/14 16:25
|
||||||
*/
|
*/
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@EnableEurekaClient
|
@EnableFeignClients
|
||||||
public class BorrowApplication {
|
public class BorrowApplication {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(BorrowApplication.class, args);
|
SpringApplication.run(BorrowApplication.class, args);
|
||||||
|
@ -1,25 +0,0 @@
|
|||||||
package com.test.config;
|
|
||||||
|
|
||||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
|
||||||
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.web.client.RestTemplate;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* ClassName: BeanConfiguration
|
|
||||||
* Package: com.test.config
|
|
||||||
*
|
|
||||||
* @author yovinchen
|
|
||||||
* @Create 2023/8/15 10:08
|
|
||||||
*/
|
|
||||||
@Configuration
|
|
||||||
@LoadBalancerClient(value = "userservice", //指定为 userservice 服务,只要是调用此服务都会使用我们指定的策略
|
|
||||||
configuration = LoadBalancerConfig.class) //指定我们刚刚定义好的配置类
|
|
||||||
public class BeanConfig {
|
|
||||||
@Bean
|
|
||||||
@LoadBalanced
|
|
||||||
RestTemplate template() {
|
|
||||||
return new RestTemplate();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,25 +0,0 @@
|
|||||||
package com.test.config;
|
|
||||||
|
|
||||||
import org.springframework.cloud.client.ServiceInstance;
|
|
||||||
import org.springframework.cloud.loadbalancer.core.RandomLoadBalancer;
|
|
||||||
import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer;
|
|
||||||
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
|
|
||||||
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.core.env.Environment;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* ClassName: LoadBalancerConfig
|
|
||||||
* Package: com.test.config
|
|
||||||
*
|
|
||||||
* @author yovinchen
|
|
||||||
* @Create 2023/8/15 16:46
|
|
||||||
*/
|
|
||||||
public class LoadBalancerConfig {
|
|
||||||
//将官方提供的 RandomLoadBalancer 注册为Bean
|
|
||||||
@Bean
|
|
||||||
public ReactorLoadBalancer<ServiceInstance> randomLoadBalancer(Environment environment, LoadBalancerClientFactory loadBalancerClientFactory){
|
|
||||||
String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
|
|
||||||
return new RandomLoadBalancer(loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), name);
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,19 @@
|
|||||||
|
package com.test.service.client;
|
||||||
|
|
||||||
|
import com.test.entity.Book;
|
||||||
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ClassName: VookClient
|
||||||
|
* Package: com.test.service.client
|
||||||
|
*
|
||||||
|
* @author yovinchen
|
||||||
|
* @Create 2023/8/15 16:56
|
||||||
|
*/
|
||||||
|
@FeignClient("bookservice")
|
||||||
|
public interface BookClient {
|
||||||
|
@RequestMapping("/book/{bid}")
|
||||||
|
Book findBookById(@PathVariable("bid") int bid);
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.test.service.client;
|
||||||
|
|
||||||
|
import com.test.entity.User;
|
||||||
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ClassName: UserClient
|
||||||
|
* Package: com.test.service.client
|
||||||
|
*
|
||||||
|
* @author yovinchen
|
||||||
|
* @Create 2023/8/15 16:54
|
||||||
|
*/
|
||||||
|
@FeignClient("userservice")
|
||||||
|
//声明为userservice服务的HTTP请求客户端
|
||||||
|
public interface UserClient {
|
||||||
|
|
||||||
|
//路径保证和其他微服务提供的一致即可
|
||||||
|
@RequestMapping("/user/{uid}")
|
||||||
|
User getUserById(@PathVariable("uid") int uid); //参数和返回值也保持一致
|
||||||
|
}
|
@ -7,6 +7,8 @@ import com.test.entity.User;
|
|||||||
import com.test.entity.UserBorrowDetail;
|
import com.test.entity.UserBorrowDetail;
|
||||||
import com.test.mapper.BorrowMapper;
|
import com.test.mapper.BorrowMapper;
|
||||||
import com.test.service.BorrowService;
|
import com.test.service.BorrowService;
|
||||||
|
import com.test.service.client.BookClient;
|
||||||
|
import com.test.service.client.UserClient;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
@ -28,16 +30,20 @@ public class BorrowServiceImpl implements BorrowService {
|
|||||||
BorrowMapper mapper;
|
BorrowMapper mapper;
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
RestTemplate template;
|
UserClient userClient;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
BookClient bookClient;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UserBorrowDetail getUserBorrowDetailByUid(int uid) {
|
public UserBorrowDetail getUserBorrowDetailByUid(int uid) {
|
||||||
List<Borrow> borrow = mapper.getBorrowsByUid(uid);
|
List<Borrow> borrow = mapper.getBorrowsByUid(uid);
|
||||||
|
|
||||||
//这里不用再写IP,直接写服务名称userservice
|
User user = userClient.getUserById(uid);
|
||||||
User user = template.getForObject("http://userservice/user/" + uid, User.class);
|
List<Book> bookList = borrow
|
||||||
//这里不用再写IP,直接写服务名称bookservice
|
.stream()
|
||||||
List<Book> bookList = borrow.stream().map(b -> template.getForObject("http://bookservice/book/" + b.getBid(), Book.class)).collect(Collectors.toList());
|
.map(b -> bookClient.findBookById(b.getBid()))
|
||||||
|
.collect(Collectors.toList());
|
||||||
return new UserBorrowDetail(user, bookList);
|
return new UserBorrowDetail(user, bookList);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,6 +25,7 @@ public class UserController {
|
|||||||
//这里以RESTFul风格为例
|
//这里以RESTFul风格为例
|
||||||
@RequestMapping("/user/{uid}")
|
@RequestMapping("/user/{uid}")
|
||||||
public User findUserById(@PathVariable("uid") int uid) {
|
public User findUserById(@PathVariable("uid") int uid) {
|
||||||
|
System.out.println("我被调用了!!!");
|
||||||
return service.getUserById(uid);
|
return service.getUserById(uid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user