文章目录
- SpringCloud之Eureka的使用
- 一、创建Eureka注册中心
- 1.pom文件引入相关依赖
- 2.yml文件配置
- 3.编写启动类并且启动
- 4.启动结果
- 二、创建Eureka提供服务方
- 1.pom文件引入相关依赖
- 2.yml文件配置
- 3.编写SpringBoot启动类
- 4.启动后查看注册中心网页
- 5.编写测试类进行测试
- 1.测试类代码
- 2.结果
- 三、创建Eureka服务消费方
- 1.pom文件引入相关依赖
- 2.yml文件配置
- 3.编写SpringBoot启动类
- 4.启动后查看注册中心网页
- 5.编写消费方代码
- 1.消费方代码
- 2.重新启动进行消费调用
一、创建Eureka注册中心 1.pom文件引入相关依赖
2.yml文件配置4.0.0 com.yun springcloud-euerka-service 1.0-SNAPSHOT springcloud-euerka-service http://www.example.com UTF-8 1.7 1.7 org.springframework.boot spring-boot-starter-parent 1.5.7.RELEASE org.springframework.cloud spring-cloud-starter-netflix-eureka-server junit junit 4.11 test org.springframework.cloud spring-cloud-dependencies Edgware.SR5 pom import
server:
port: 8700
eureka:
client:
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/euerka
register-with-eureka: false #自身,不在向euerka注册
fetch-registry: false #启动时禁用client的注册
instance:
hostname: localhost
spring:
application:
name: eureka-server
3.编写启动类并且启动
package com.yun;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer //当前使用Euerka的 server
public class EuerkaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EuerkaServerApplication.class);
}
}
4.启动结果
二、创建Eureka提供服务方 1.pom文件引入相关依赖
2.yml文件配置**注意此处应该是:spring-cloud-starter-netflix-eureka-client** 4.0.0 com.yun springcloud-euerka-servicesupport 1.0-SNAPSHOT springcloud-euerka-servicesupport http://www.example.com UTF-8 1.7 1.7 org.springframework.boot spring-boot-starter-parent 1.5.7.RELEASE org.springframework.cloud spring-cloud-starter-netflix-eureka-client junit junit 4.11 test org.springframework.cloud spring-cloud-dependencies Edgware.SR5 pom import
server:
port: 8701
eureka:
client:
service-url:
defaultZone: http://${eureka.instance.hostname}:8700/eureka #这个地址为注册中心地址
instance:
hostname: localhost
#当前服务名称
spring:
application:
name: eureka-service
3.编写SpringBoot启动类
package com.yun;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient //代表自己是一个服务提供方,注意这里注解的区别
public class EuerkaServiceApplication {
public static void main(String[] args) {
SpringApplication.run(EuerkaServiceApplication.class);
}
}
4.启动后查看注册中心网页
这里可以看到已经注册成功5.编写测试类进行测试 1.测试类代码
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/hello")
public class HelloController {
@RequestMapping("/world")
public String helloWorld(String s){
System.out.println("传入的值:"+s);
return "传入的值为:"+s;
}
}
2.结果
三、创建Eureka服务消费方
1.pom文件引入相关依赖
2.yml文件配置4.0.0 com.yun springcloud-euerka-serviceconsume 1.0-SNAPSHOT springcloud-euerka-serviceconsume http://www.example.com UTF-8 1.7 1.7 org.springframework.boot spring-boot-starter-parent 1.5.7.RELEASE org.springframework.cloud spring-cloud-starter-netflix-eureka-client junit junit 4.11 test org.springframework.cloud spring-cloud-dependencies Edgware.SR5 pom import
server:
port: 8702 #服务消费方
eureka:
client:
service-url:
defaultZone: http://${eureka.instance.hostname}:8700/eureka
instance:
hostname: localhost
#当前服务名称
spring:
application:
name: eureka-consumer
3.编写SpringBoot启动类
package com.yun;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient //当前使用eureka的server
public class EuerkaConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(EuerkaConsumerApplication.class);
}
}
4.启动后查看注册中心网页
5.编写消费方代码
1.消费方代码
package com.yun;
import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@RequestMapping("/Hello")
public class ConsumerController {
@Autowired
private LoadBalancerClient loadBalancerClient;
@Autowired
private RestTemplate restTemplate; //这里使用Bean注入一下
@RequestMapping("/consumer")
public String helloWorld(String s){
System.out.println("传入的值:"+s);
String forObject3 =restTemplate.getForObject("http://EUREKA-SERVICE/hello/world?s="+s,String.class);
return forObject3;
}
}
package com.yun;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class Beans {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
2.重新启动进行消费调用



