二、SpringCloud的学习之服务提供者和服务消费者的演示(基础)

版权声明:转载请注明出处 https://blog.csdn.net/chenmingxu438521/article/details/90514885

一、背景

1.创建demo-member-service项目(服务提供者),上篇文章已经部署好了Eureka,详细请参考:https://blog.csdn.net/chenmingxu438521/article/details/90513283

二、项目结构

三、服务提供者详细项目讲解

1.pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.2.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>demo-service-member</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo-service-member</name>
	<description>Demo project for Spring Boot</description>

	<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.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		<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>
	</dependencies>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Dalston.RC1</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>
	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>
</project>

2.application.yml

2.1.需要指明spring.application.name,这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name 。 

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8888/eureka/
server:
  port: 8762
spring:
  application:
    name: demo-service-member

3.服务接口

@RestController
public class MemberController {

	@RequestMapping("/getMemberAll")
	public List<String> getMemberAll() {
		List<String> listUser = new ArrayList<String>();
		listUser.add("zhangsan");
		listUser.add("lisi");
		listUser.add("cmx");
		return listUser;
	}
}

4.分布服务

4.1.通过注解@EnableEurekaClient 表明自己是一个eurekaclient。

@SpringBootApplication
@EnableEurekaClient
public class DemoServiceMemberApplication {

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

5.效果演示

注解:你会发现一个服务已经注册在服务中了,服务名为SERVICE-HI ,端口为7862,这时打开 http://localhost:8762/getUserList ,你会在浏览器上看到 :["zhangsan","lisi","cmx"].

四、背景

1.创建demo-service-order项目(服务消费者),上篇文章已经部署好了Eureka,详细请参考:https://blog.csdn.net/chenmingxu438521/article/details/90513283

五、项目结构

六、服务消费者详细项目讲解

1.pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.2.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>demo-service-order</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo-service-order</name>
	<description>Demo project for Spring Boot</description>

	<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.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-ribbon</artifactId>
		</dependency>
		<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>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Dalston.RC1</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>

	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>
</project>

2.application.yml

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8888/eureka/
server:
  port: 8764
spring:
  application:
    name: demo-service-order

3.service

@Service
public class OrderMemberService {
	@Autowired
	private RestTemplate restTemplate;

	public List<String> getOrderUserAll() {
	return restTemplate.getForObject("http://demo-service-member/getMemberAll", List.class);
	}
}

4.controller

@RestController
public class OrderController {
	@Autowired
	private OrderMemberService orderMemberService;

	@RequestMapping("/getOrderUserAll")
	public List<String> getOrderUserAll() {
		System.out.println("订单服务开始调用会员服务");
		return orderMemberService.getOrderUserAll();
	}
}

5.主类(在springcloud的使用中如果使用RestTemplate来进行rpc远程调用的时候 ,在调用会员服务的时候有的会选择使用会员服务端在注册中心注册的名称来进行远程调用 也有的会直接使用域名进行调用,在这个过程中如果使用会员的注册名称的话在RestTemplate那里开启负载均衡:@LoadBalanced如果是使用域名进行调用就不用开启负载均衡 )

@SpringBootApplication
@EnableEurekaClient
public class DemoServiceOrderApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoServiceOrderApplication.class, args);
	}
       //在工程的启动类中,通过@EnableDiscoveryClient向服务中心注册;并且向程序的ioc注入一个bean:             
       //restTemplate;并通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能,rest方式
       //调用需要开启这个
	@Bean
	@LoadBalanced
	RestTemplate restTemplate() {
		return new RestTemplate();
	}
}

6.效果图

浏览器打开效果如下:

七、结束

简单的Eureka的入门,希望能帮助你们,谢谢,如有不足欢迎批评指正。

猜你喜欢

转载自blog.csdn.net/chenmingxu438521/article/details/90514885