一、Eureka服务器
1.建立一个Eureka服务器
首先,建立一个module或者project,需要获得Eureka Server依赖。
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiAzNfRHLGZkRGZkRfJ3bs92YsYTMfVmepNHL90TQjBnWuJGc4dVWsVjMMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnLygjM0QjN0EjMzAjNwkTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
2.application.properties
spring.application.name=eureka-server
#服务注册中心端口号
server.port=8761
#服务注册中心实例的主机名
eureka.instance.hostname=localhost
#是否向服务注册中心注册自己
eureka.client.register-with-eureka=false
#是否检索服务
eureka.client.fetch-registry=false
#服务注册中心的配置内容,指定服务注册中心的位置(下面最后的字符串只能是eureka,不能是别的)
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
2.启动类:
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
3.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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.nealin</groupId>
<artifactId>eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<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>
启动后,可以通过localhost:8761来访问。如下图表示成功
二、Eureka客户-服务提供者
添加一个依赖Eureka Discovery的project或者module。(参考第一张图中)
- application.properties
#自己的名字
spring.application.name=service-producer
#自身端口
server.port=8762
#注册到eureka服务器链接
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
- 启动类需要加上@EnableEurekaClient或者@EnableDiscoveryClient
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class ServiceProducerApplication {
@Value("${server.port}")
String port;
public static void main(String[] args) {
SpringApplication.run(ServiceProducerApplication.class, args);
}
@RequestMapping("/hi")
public String hi(@RequestParam(value = "name", defaultValue = "lbj") String name) {
return "您好 " + name + " ,端口:" + port;
}
}
- 启动后,发现Eureka页面有了这个服务了
三、Eureka客户-服务消费者(ribbon方式)
- application.properties
#自己的名字
spring.application.name=service-consumer
#自身端口
server.port=8764
#注册到eureka服务器链接
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
- 启动类
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class ServiceConsumerApplication {
@Autowired
private RestTemplate restTemplate;
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(ServiceConsumerApplication.class, args);
}
@RequestMapping("hello")
public String hello(@RequestParam(value = "name",defaultValue = "詹姆斯") String name){
return restTemplate.getForObject("http://service-producer/hi?name="+name,String.class);
}
}
http://127.0.0.1:8765/hello
四、Eureka客户-服务消费者(feign方式)
- application.properties
#自己的名字
spring.application.name=service-consumer-feign
#自身端口
server.port=8765
#注册到eureka服务器链接
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
2.启动类
@SpringBootApplication
@RestController
@EnableFeignClients
@EnableDiscoveryClient
public class ServiceConsumerFeignApplication {
@Autowired
IProducer iProducer;
public static void main(String[] args) {
SpringApplication.run(ServiceConsumerFeignApplication.class, args);
}
@RequestMapping(value = "/hello")
public String hello(@RequestParam(value = "name", defaultValue = "ddd") String name) {
return iProducer.hello(name);
}
}
3.对接外部的接口
@FeignClient(value = "service-producer")
public interface IProducer {
@RequestMapping(value = "/hi",method = RequestMethod.GET)
String hello(@RequestParam(value = "name") String name);
}
注意:
1.通过选中Allow parallel run可以运行多个程序,但首先要修改端口号。
2.服务注册中心最后字符串只能是eureka
eureka.client.serviceUrl.defaultZone=http:// e u r e k a . i n s t a n c e . h o s t n a m e : {eureka.instance.hostname}: eureka.instance.hostname:{server.port}/eureka/
3.多启动几个服务提供者,可以观察到负载均衡。
源码:https://github.com/huangbingzhi1/springclouddemo
启动和说明:先启动EurekaServer,ServiceProducer可以变换端口启动好几个。ServiceConsumer是通过ribbon方式进行服务消费的实现。ServiceConsumerFeign是通过feign方式进行服务消费的实现。