场景
关于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端口,打开浏览器输入:
将服务提供者注册到服务器端
打开服务提供者的启动类添加注解:
@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/
打开浏览器输入:
源码下载
https://download.csdn.net/download/badao_liumang_qizhi/11206065