本项目会使用之前的Eureka注册中心、会员微服务(两个集群)、新建点名微服务,去调用会员服务中的会员信息。fegin会自动实现负载均衡
启动后看注册中心如下图
项目代码:
链接:https://pan.baidu.com/s/1XUJeXQw1dTWJ9FQ2O7m9Og 密码:mb2w
一、新建springboot2项目工程
项目结构如下
二、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">
<modelVersion>4.0.0</modelVersion>
<groupId>con.joychen</groupId>
<artifactId>fegin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>fegin</name>
<description>fegindianming</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<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-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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>
三、项目配制
spring:
application:
name: dianming-server
cloud:
config:
discovery:
enabled: true
serviceId: dianming-server
server:
port: 8990
eureka:
instance:
hostname: localhost
instance-id: ${spring.application.name}:${spring.cloud.client.hostname}:${server.port}
prefer-ip-address: false
client:
service-url:
defaultZone: http://localhost:7878/eureka/
四、写Fegin接口
package con.joychen.fegin.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@FeignClient(value = "manber-server") //这里是注册在注册中心微服务名称
public interface FeginMenber {
@RequestMapping("/member") //跟据名称调用member接口会自动调用
public List<String> getMenberAll();
}
五、写控制类调用Fegin接口
package con.joychen.fegin.controllers;
import con.joychen.fegin.service.FeginMenber;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class DianMingController {
@Autowired
private FeginMenber feginMenber;
@RequestMapping("/dianmingMenber")
public List<String> getMenber(){
System.out.println("Fegin-convent-manber-server");
return feginMenber.getMenberAll();
}
}
六、启动类配制
package con.joychen.fegin;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
@SpringBootApplication
@EnableEurekaClient //启动注册中心客户端
@EnableFeignClients //启动Fegin
public class FeginApplication {
public static void main(String[] args) {
SpringApplication.run(FeginApplication.class, args);
}
}
七、启动测试 自动实现了负载均衡
请求接口: http://localhost:8990/dianmingMenber
第一次调用测试结果
第二次调用测试结果