SpringCloud学习之运行第一个Eureka程序

场景

关于Eureka

1.提供了Eureka服务端与客户端。

2.主要用于服务管理。

Eureka架构

构建第一个应用

1.建立服务器端。

2.建立服务提供者。

3.建立服务调用者。

实现

建立服务器端

打开eclipse,新建Maven Project--create a simple project

打开pom.xml,添加依赖

<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>com.badao.cloud</groupId>
  <artifactId>eureka-server</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencyManagement>
  <dependencies>
   <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>Dalston.SR3</version>
    <type>pom</type>
    <scope>import</scope>
   </dependency>
  </dependencies>
 </dependencyManagement>
 
 <dependencies>
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-config</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-eureka-server</artifactId>
  </dependency>
 </dependencies>
</project>

新建启动类

代码:

package com.badao.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class ServerApplication {

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

}

注:

@EnableEurekaServer注解是配置为服务端


在resource下新建配置文件application.yml

修改端口号并使其注册为服务器端。

server:
  port: 8761
eureka:
  client:
    register-with-eureka: false
    fetch-registry: false

运行启动类效果:

建立服务提供者

打开eclipse,新建Maven Project--create a simple project

打开pom.xml

<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>com.bao.cloud</groupId>
  <artifactId>eureka-provide</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencyManagement>
  <dependencies>
   <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>Dalston.SR3</version>
    <type>pom</type>
    <scope>import</scope>
   </dependency>
  </dependencies>
 </dependencyManagement>
 <dependencies>
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-config</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-eureka</artifactId>
  </dependency>
 </dependencies>
</project>

新建启动类

package com.badao.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ProvideApplication {
 public static void main(String[] args) throws Exception {
  SpringApplication.run(ProvideApplication.class, args);
 }


}


新建实体类

package com.badao.cloud;

public class Police {

 private Integer id;
 private String name;
 public Integer getId() {
  return id;
 }
 public void setId(Integer id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }

}


新建Controller

package com.badao.cloud;

import org.apache.tomcat.util.http.fileupload.ParameterParser;
import org.springframework.http.MediaType;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PoliceController {
 
 /**
  * 指定警员id出警
  * @param id
  * @return
  */
 @RequestMapping(value="/call/{id}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
 public Police call(@PathVariable Integer id){
  Police police = new Police();
  police.setId(id);
  police.setName("警员"+id);
  return police;
 }
}


运行启动类,默认8080端口,打开浏览器输入:

http://localhost:8080/call/1

 

将服务提供者注册到服务器端

打开服务提供者的启动类添加注解:

@EnableEurekaClient

完整代码:

package com.badao.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class ProvideApplication {
 public static void main(String[] args) throws Exception {
  SpringApplication.run(ProvideApplication.class, args);
 }


}

在服务提供者项目下新建配置文件application.yml

spring:
  application:
    name: eureka-provide
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

 重新运行服务提供者的启动类和服务端的启动类

查看服务器端,已经成功注册:

新建服务调用者

打开eclipse,新建Maven Project--create a simple project

 

打开项目的pom.xml

<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>com.badao.cloud</groupId>
  <artifactId>eureka-call</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencyManagement>
  <dependencies>
   <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>Dalston.SR3</version>
    <type>pom</type>
    <scope>import</scope>
   </dependency>
  </dependencies>
 </dependencyManagement>
 <dependencies>
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-config</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-eureka</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-ribbon</artifactId>
  </dependency>
 </dependencies>
 
</project>

新建启动类

package com.badao.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class CallApplication {

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

}

新建Controller

package com.badao.cloud;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

@Controller
@Configuration
public class TestController {

 @Bean
 @LoadBalanced
 public RestTemplate getRestTemplate() {
  return new RestTemplate();
 }

 @GetMapping("/router")
 @ResponseBody
 public String router() {
  RestTemplate tpl = getRestTemplate();
  String json = tpl.getForObject("http://eureka-provide/call/1", String.class);
  return json;
 }

}

新建配置文件application.yml

server:
  port: 8081
spring:
  application:
    name: eureka-call
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

打开浏览器输入:

http://localhost:8081/router

源码下载

https://download.csdn.net/download/badao_liumang_qizhi/11206065

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

霸道流氓气质

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值