前几天听朋友们议论Eureka 2.0闭源,但由于项目中没用到Springcloud,也就对Eureka一无所知,所以抽空了解了一下Eureka,感觉很不错,而且feign默认集成了Ribbon负载均衡。

一、创建Eureka注册中心

1.创建一个maven项目,在pom.xml添加依赖jar文件

    <dependencies>
		<!--eureka server -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka-server</artifactId>
		</dependency>
	</dependencies>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Dalston.SR1</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

2.创建application.properties文件

spring.application.name=spring-cloud-eureka

eureka.instance.hostname=localhost
server.port=9876
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

3.创建服务启动类

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

4.项目结构如图

5测试

二、创建服务提供者

1.创建一个maven项目,在pom.xml中导入依赖jar

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.4.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
	</dependencies>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Dalston.SR1</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>

2.创建application.properties文件

spring.application.name=spring-cloud-producer
server.port=9999
eureka.client.serviceUrl.defaultZone=http://localhost:9876/eureka/

3.创建UserController接口

@RestController
@RequestMapping("/user")
public class UserController {
	@RequestMapping("/getUserList")
	public ModelMap getUserList() {
		ModelMap modelMap = new ModelMap();
		List<String> userList = new ArrayList<String>();
		userList.add("用户1");
		userList.add("用户2");
		userList.add("用户3");
		modelMap.put("data", userList);
		return modelMap;
	}
}

4.创建启动类

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

5.项目结构如图

6.测试

7.在eureka注册中心上的效果

三、创建服务消费者

1.创建一个maven项目,在pom.xml中导入依赖jar文件

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.4.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-feign</artifactId>
		</dependency>
	</dependencies>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Dalston.SR1</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>

2.创建application.properties文件

spring.application.name=spring-cloud-consumer
server.port=9998
eureka.client.serviceUrl.defaultZone=http://localhost:9876/eureka/

3.创建feign客户端接口

@FeignClient(name = "spring-cloud-producer")
public interface UserService {
	@RequestMapping(value = "/user/getUserList")
	public ModelMap getUserList();
}

4.创建消费者接口

@RestController
@RequestMapping("/order")
public class OrderController {
	@Autowired
	UserService userService;
	@RequestMapping("/getUserList")
	public ModelMap getUserList() {
		return userService.getUserList();
	}
}

5.创建启动类

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class OrderApp {

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

}

6.项目结构如图

7.测试

8.Eureka注册中心上的效果

 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐