RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
怎么在springcloud中使用Eureka实现服务治理

怎么在spring cloud中使用Eureka 实现服务治理?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

创新互联专注骨干网络服务器租用10余年,服务更有保障!服务器租用,服务器托管 成都服务器租用,成都服务器托管,骨干网络带宽,享受低延迟,高速访问。灵活、实现低成本的共享或公网数据中心高速带宽的专属高性能服务器。

一、搭建服务注册中心

先列出完整目录结构:

怎么在spring cloud中使用Eureka 实现服务治理

搭建过程如下:

1.创建maven工程:eureka(具体实现略)

2.修改pom文件,引入依赖


 4.0.0
 com.sam
 eureka
 0.0.1-SNAPSHOT

 
  org.springframework.boot
  spring-boot-starter-parent
  1.5.1.RELEASE
 

 
  1.8
 
 
 
  
   
    org.springframework.cloud
    spring-cloud-dependencies
    Camden.SR6
    pom
    import
   
  
 

 
  
     org.springframework.cloud
   spring-cloud-starter-eureka-server
  
 

3.创建启动类

/**
 * 
 * @EnableEurekaServer
 * 用来指定该项目为Eureka的服务注册中心
 */
@EnableEurekaServer
@SpringBootApplication
public class EurekaApp {

 public static void main(String[] args) {
  SpringApplication.run(EurekaApp.class, args);
 }
}

4.配置application.properties文件

#设置tomcat服务端口号
server.port=1111
#设置服务名称
spring.application.name=eureka-service

eureka.instance.hostname=localhost
#注册中心不需要注册自己
eureka.client.register-with-eureka=false
#注册中心不需要去发现服务
eureka.client.fetch-registry=false
#设置服务注册中心的URL
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka

5.启动服务并访问,我们会看到这样的画面:

怎么在spring cloud中使用Eureka 实现服务治理

二、注册服务提供者

先列出完整目录结构:

怎么在spring cloud中使用Eureka 实现服务治理

搭建过程如下:

1.创建maven工程:hello-service(具体实现略)

2.修改pom文件,引入依赖


 4.0.0
 com.sam
 hello-service
 0.0.1-SNAPSHOT
 
  org.springframework.boot
  spring-boot-starter-parent
  1.5.1.RELEASE
 

 
  1.8
 

 
  
   
    org.springframework.cloud
    spring-cloud-dependencies
    Camden.SR6
    pom
    import
   
  
 

 
  
  
   org.springframework.cloud
   spring-cloud-starter-eureka
  
 

3.创建启动类

/***
 * 
 * @EnableDiscoveryClient
 * 让服务使用eureka服务器
 * 实现服务注册和发现
 *
 */
@EnableDiscoveryClient
@SpringBootApplication
public class HelloApp {
 public static void main(String[] args) {
  SpringApplication.run(HelloApp.class, args);
 }
}

4.创建controller

@RestController
public class HelloController {

 Logger logger = LoggerFactory.getLogger(HelloController.class);

 @Autowired
 DiscoveryClient discoveryClient;
 
 @RequestMapping("/hello")
 public String hello() {
  ServiceInstance instance = discoveryClient.getLocalServiceInstance();
  //打印服务的服务id
  logger.info("*********" + instance.getServiceId());
  return "hello,this is hello-service";
 }
}

5.配置application.properties文件

server.port=9090
#设置服务名
spring.application.name=hello-service
#设置服务注册中心的URL,本服务要向该服务注册中心注册自己
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka

6.启动并测试

1.)启动后再hello-service的控制台会有这种字样(xxx代表你的PC名)

复制代码 代码如下:

Registered instance HELLO-SERVICE/xxx:hello-service:9090 with status UP (replication=false)

eureka的控制台会打印出如下字样(xxx代表你的PC名)

复制代码 代码如下:

Registered instance HELLO-SERVICE/xxx:hello-service:9090 with status UP (replication=false)

2.)再次访问localhost:1111,会发现有服务注册到注册中心了

怎么在spring cloud中使用Eureka 实现服务治理

三、服务发现和消费

完整目录结构如下:

怎么在spring cloud中使用Eureka 实现服务治理

搭建过程:

1.创建maven工程(具体实现略)

2.修改pom文件,引入依赖


 4.0.0
 com.sam
 hello-consumer
 0.0.1-SNAPSHOT
 
  org.springframework.boot
  spring-boot-starter-parent
  1.5.1.RELEASE
 

 
  1.8
 

 
  
   
    org.springframework.cloud
    spring-cloud-dependencies
    Camden.SR6
    pom
    import
   
  

 

 
  
  
   org.springframework.cloud
   spring-cloud-starter-eureka
  
  
  
   org.springframework.cloud
   spring-cloud-starter-ribbon
  

 

这里比hello-service服务提供者,多了ribbon的依赖

3.创建启动类

@EnableDiscoveryClient
@SpringBootApplication
public class ConsumerApp {


 //@Bean 应用在方法上,用来将方法返回值设为为bean
 @Bean
 @LoadBalanced //@LoadBalanced实现负载均衡
 public RestTemplate restTemplate() {
  return new RestTemplate();
 }
 
 public static void main(String[] args) {
  SpringApplication.run(ConsumerApp.class, args);
 }
}

这里也要用到@EnableDiscoveryClient, 让服务使用eureka服务器, 实现服务注册和发现 

4.创建controller

@RestController
public class ConsumerController {
 //这里注入的restTemplate就是在com.sam.ConsumerApp中通过@Bean配置的实例
 @Autowired
 RestTemplate restTemplate;
 @RequestMapping("/hello-consumer")
 public String helloConsumer() {
  //调用hello-service服务,注意这里用的是服务名,而不是具体的ip+port
  restTemplate.getForObject("http://hello-service/hello", String.class);
  return "hello consumer finish !!!";
 }
}

5.配置application.properties文件

server.port=9999
spring.application.name=hello-consumer
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka
#这里的配置项目和服务提供者hello-service一样

6.启动,测试1.)启动eureka。为了展示负责均衡的效果,我们的hello-service启动两个服务,启动两个服务的具体步骤如下

怎么在spring cloud中使用Eureka 实现服务治理

怎么在spring cloud中使用Eureka 实现服务治理

怎么在spring cloud中使用Eureka 实现服务治理

怎么在spring cloud中使用Eureka 实现服务治理

以上是hello-service1的启动步骤,端口号为9090;同样方法设置hello-service2,端口号为9091(具体实现略)。

2.)启动hello-consumer

3.)再次访问http://localhost:1111/,会发现有2个hello-service服务(端口号一个是9090,一个是9091),1个hello-consume服务

4.) 多次访问http://localhost:9999/hello-consumer,会发现hello-service1和hello-service2会轮流被调用(已经实现了负责均衡),可以通过两者的控制台打印内容确认(还记得我们在hello-service的controller中有个loggerlogger.info("*********" + instance.getServiceId());吗?对,就是这个打印)

怎么在spring cloud中使用Eureka 实现服务治理

怎么在spring cloud中使用Eureka 实现服务治理

怎么在spring cloud中使用Eureka 实现服务治理

怎么在spring cloud中使用Eureka 实现服务治理

关于怎么在spring cloud中使用Eureka 实现服务治理问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


文章标题:怎么在springcloud中使用Eureka实现服务治理
标题URL:http://scyingshan.cn/article/pocoeo.html